15. Interface and applications programming

"write an application that interfaces with an input &/or output device"
page contents:
python
c language
learning points

Tutorials

To get into programming again (I used to program a little when I was a kid...in BASIC...), I watched these tutorials about Python (they can also be found on Youtube).

Python

Using the tutorials, I started playing around with Python. Here is a one Python sketch (Python1.py) that I wrote:

    print "Hello world"
    print "multiple", 3 * 2
    print "bigger?", 2 > 1
    cars = 10
    print cars
    print "there are", cars, "cars"
    passengers = 4
    print "each car fits", passengers, "passengers"
    print "which equals to", cars * passengers, "people"

    my_name = 'bob'
    my_age = 98
    my_city = 'ducktown'

    print "you can call me %s" % my_name
    print "I am %d years old (almost)" % my_age

    x = "there are %d type of people" % 10
    binary = "binary"
    do_not = "don't"
    y = "Those who understand %s and those who %s" % (binary, do_not)

    print "I said: %r" % x
    print "and also: %r" % y

    hilarious = False
    joke_eval = "is that joke funny or what? %r"

    print joke_eval % hilarious

    # things that are being imported by sketch2.py:
    x = "frank in sketch1" 

    def ask_name():
    y = raw_input("name: ")
    return y

    def tell_name():
    print "it's not hubba"
I wrote the code so that it loads data from another file. Being able to address data from other sources makes programming very flexible and powerful. This is python2.py:
    # importing functions from another file (sketch1 is assumed to be .py)
    # using * after import imports the whole file


    from sketch1 import ask_name, tell_name, x

    # defining a variable in another file
    # x = "frank" 
    print x


    # defining this in sketch1.py:

    # def ask_name():
    #   y = raw_input("name: ")
    #   return y

    print x
    x = ask_name()
    print x

    tell_name()
Which gives this kind of output in the Terminal:
    Totoro:python Casper$ python sketch2.py
    Hello world
    multiple 6
    bigger? True
    10
    there are 10 cars
    each car fits 4 passengers
    which equals to 40 people
    you can call me bob
    I am 98 years old (almost)
    I said: 'there are 10 type of people'
    and also: "Those who understand binary and those who don't"
    is that joke funny or what? False
    frank in sketch1
    frank in sketch1
    name: aha
    aha
    it's not hubba

C language

C was developed in the early 1970s for Unix (c on wikipedia). It evolved into one of the most extensively used programming languages. Many modern popular programming languages are also largely based on C. A great reference for learning C is "A Little C Primer".
Using Sublime Text and the Terminal I wrote this program (to experiment with pointers, for instance):

      #include <stdlib.h>
      #include <stdio.h>
      #include <string.h>

      struct inventory 
      {
      char *thing;
      int amount;
      int price;
      };

      int mult (int x, int y);

      int main () {
      struct inventory chairs;

      chairs.thing = "rietveld";
      // chairs.amount = 5;
      // chairs.price = 54;

      printf ("Please tell me how many chairs there are: ");
      scanf ("%d", &chairs.amount);
      printf ("thank you. Now please enter their price: ");
      scanf ("%d", &chairs.price);

      printf("there is this thing called %c\n", *chairs.thing);
      printf ("of which there are %d\n", chairs.amount);
      printf ("and they cost each %d\n", chairs.price);
      printf ("total value comes to: %d\n", mult(chairs.amount, chairs.price));

      char *aString = "Hi, how are you?\n";
      printf ("%s", aString);

      exit(EXIT_SUCCESS);
      }

      int mult (int x, int y) 
      {
      return x * y;
      }

In the Terminal, compile a .c file with the command gcc sketch9.c -o sketch9 (where 'sketch9' should be replaced by your filename). Running open sketch9 in Terminal gives:

      Please tell me how many chairs there are: 12
      thank you. Now please enter their price: 40
      there is this thing called r
      of which there are 12
      and they cost each 40
      total value comes to: 480
      Hi, how are you?

Visualising serial data

note:This is code and documentation about work I did for my first final project, a camera for audio that records 15-30 second audioclips when it's triggered. At some point, I built a board with a microphone, like this one.The hello.mic.45.c code can be used to read data from the microphone and send it to the serial port. I wrote a python script to capture serial dat, write it to a file and format the input in hex values:

    import sys
    import serial

    # open serial port
    port = sys.argv[1]
    ser = serial.Serial(port,115200)

    my_file = open("fofodata.txt", "w")

    while (1):
    valueSER = ord(ser.read())
    # writeValue = valueSER
    my_file.write(str(valueSER) + "\n")
    # my_file.write(hex(valueSER).lstrip("0x") + "\n")
    # print str(valueSER)

    my_file.close()
This produces a .txt file with 2-digit HEX values in rows.

top of page