Week 9: Programming Applications

Happy Thanksgiving! This week's "take home" project was to program an application for a board we had already made - the application is meant to make it easier for a user to EITHER a)read the input from a sensor board or b)modify the output on an output board. I have very little programming experience (just some basic familiarity with Python) so this week was very challenging for me, but regardless of the amount of success I had (or didn't have...) I learned a lot about programming that is readily applicable to this class, which I have documented here for future use. It should also be noted that there are a plethora of tutorials and forums online that are really useful, and I also wanted to thank all my classmates who responded to my e-mails over the Thanksgiving break to help me figure things out.

My goal was to create an application that would allow the user to enter a string of text, and that this would be sent to the LED array board I built last week so that the LED array spelled out the string one character at a time. My workflow was as follows:

  1. User enters phrase (in Python app).
  2. Phrase is sent as string to serial port (in Python).
  3. LED array board reads serial port (in C).
  4. Each character received is read and matched to a C array of the HEX codes of the characters and their corresponding flash sequences on the LED board.
  5. The LED array is driven to display each character in LED flashing.

I found programming the Python app pretty easy - I got a test version of my input application to work. It takes the user's input through an Entry widget (from the Tkinter module - more on that later) and then a button will print out what the user wrote on the Python IDLE GUI. It was more difficult for me to program the C code because I have little experience in that language. I did manage to cobble together the code that will read the serial port, and I also added the code that runs the LEDs. I then created a 1-D array that contains every letter and its matching flash LED pattern.

Below are pictures of the Python portion of my app.

Programming Tips in Python and C

I wanted to compile all the code I learned in the course of this project to help out anybody else who was trying a similar thing. The individual pieces I learned are listed below:

Using Python to open and write to a serial port

We've seen parts of this code before in Prof. Gershenfeld's applications, but here's an explanation regardless. The lines would read as follows:

import serial
if (len(sys.argv) != 2):
print "command line: LED_array_input.py serial_port"
port = sys.argv[1]

ser = serial.Serial(port,9600)

import serial

This imports the serial library to Python.

if (len(sys.argv) != 2):
print "command line: LED_array_input.py serial_port"

This asks the user to enter the name of the program and the serial port being used in the command line when opening the program, because the program needs to know the correct serial port that it should send data to.

port = sys.argv[1]

This sets the port as being the second item in the string entered in the command line (the indexing starts at 0, which is why [1] refers to the second item in the string).

ser = serial.Serial(port,9600)

This sets the serial port as described in the command line, and with a baudrate of 9600.

Using the Entry widget from Tkinter in Python

The entry widget can be used to take in a single line of text to be used in a program. Its a widget contained within the Tkinter library, which is a standard library loaded with any Python package.

You need to import Tkinter first:
from Tkinter import *

Then in order to create my entry widget I used the following lines of code:
master = Tk()
master.title("Spell It On The LED Array")
e = Entry(master)


These create the line that accepts text; I also created a button that would print the "text" variable in the IDLE GUI as well as send the text to the serial port.

Using Arrays in C

An array is sort of like a dictionary of values that you can index and look up when you need them. You can create 1 dimensional or 2 dimensional arrays (up to the nth degree, but no one ever needs more than 2) to reference values. To create a 2-D array:

int arrayname[m][n]

This creates an array of integers with m spaces across, n spaces down. (Recognize that if you set m = 100, the indices run from 0 - 99.)

arrayname[m][n] = SOMETHING

This sets the value of a particular element within the array.

Using Structures in C

Structures are a little different from arrays, in that they are more like databases where you can have many fields describing a single entry. I didn't end up using them here, but they can be useful if, for example, you need to record information about employees and you want to assign a single employee an ID number, age, salary, and department.

Things Still To Do

I haven't completed the C side of the code, because I still don't completely understand how to take the received byte of information and identify it as a specific character that I can then lookup in my array to power my LED board. I know I need to use the getchar() function, and somehow reference it to the *rxbyte variable...

At any rate, here is both my Python code and my C code as they currently stand:

C Code

Python Code

This is a test code in Python that runs just the Entry widget and the button, if you want to play with how that code works.

And thank you again to everyone who helped me out over the Thanksgiving weekend!