Python parallel port control

 

Looking for a really easy way to control your project from a computer? If you have a parallel port which isn’t used you’re in luck. Python has a module that makes it easy to toggle the pins on the parallel port

First install the pyParallel module. It’s in the Ubuntu repositories:

sudo apt-get install python-parallel

To use the module just import it, instantiate an object, then write or read from that object.

import parallel
parPort = parallel.Parallel()
parPort.setData(0x01)

Now, this threw a permission error for me. But a bit of searching led me to find that you need to remove the lp module and insert the ppdev module:

sudo rmmod lp
sudo modprobe ppdev

This module will load again next time you reboot. Consider blacklisting it if you are using automated Python scripts that need parallel port access.

That’s it! Don’t you love Python? Of course there are some additional functions availalbe for this module so check the documentation to see what else can be done.

Git Rebase

It was pointed out to me that I had a typo in my code for the SPI shift register tutorial. I had the definition wrong for the data pin used by SPI (I guess AVR is not too particular about setting MOSI as an output for master SPI mode). Since it is supposed to be a clear example I needed to fix it. But the git repository that I used has a tag to some earlier code as the simple example, then more complex code at the latest revision. I wanted to make the change throughout and for this I decided to use the Rebase command. Here’s the steps I took:

  • Check out a new clone of the repository
  • Do a hard reset to the offending commit
  • Fix the problem and commit it
  • Hard reset back to the most recent version
  • Use the Rebase command to bring that change forward in time
  • Pull from the remote repository (you’ll be told to do this if you try to push first)
  • Push back to the remote repository

Fixing tags

  • You CANNOT move a tag on the remote repository. This is a security feature.
  • Delete the old tag and make a new one at the proper commit.
Take a look at the actual commands after the break.

My first Chumby app

I managed to get my hands on an Insignia Infocast 8 used at a bargain price on eBay. It’s an 8″ touchscreen device that is a clone of the Chumby. They’re designed to be hackable, so you can SSH into it as a feature. The apps are written in Flash, and I managed to get my first one running yesterday. Of course I’m using Linux tools only, but found that Haxe and Geany do a great job. I’ll roll the code and the steps I took into the post after the jump.

Continue reading

Terminal Life

Terminal-life

After completing my first iteration of Conway’s Game of Life I was unhappy with the performance. After a bit of testing I discovered that the real bottleneck wasn’t in my algorithm, but in the overhead associated with the pygame module that was taking care of the graphics.

 

I just completed a branch of the code that does away with the pygame module. Instead, it runs in a bash shell. The program uses tput to read in lines and columns available in the terminal, and also to move the cursor back to the top left before each new generation is drawn. Of course the limitation here is that it only works on Linux. I was able to maintain the look by using a Unicode character which draws a box. That’s a screenshot of the code running.

Conway’s Game of Life

I finally got around to programming Conway’s Game of Life. I’ve long wanted to give this a try but just today decided to take some time to myself and actually do it. I chose Python, a language I’ve worked with quite a bit but one I’ve never used for GUI programming. I spent the majority of my time trying to figure out how to display the Life grid, and decided to use a package called pygame, which I enjoy quite a bit!

About the code:

The game itself was actually pretty easy to code. I decided to make a multidimensional array as a lookup table. The first dimension is indexed by whether the current cell is dead (0) or alive (1). The second dimension is indexed by the sum of the living cells around the test cell. The return value is status of the test cell after the rules are applied for the next generation. The rest is just iterating through the various buffer arrays and then writing to the display.

The number of cells, cell size, gap between cells, delay between generations, and percent of live cells atĀ genesisĀ are all configurable. The game checks for stagnation at the end of evolution and will change the window title to show how many it took to reach equilibrium.

The pygame package turned out to be very easy to work with. I has an event handler that takes care of the delay time between generations. Take a look at it if you are ever working on a game!

I’d love to hear your thoughts about my code. Check it out and then leave a comment or send me a tweet!

Source Code Repository

Follow Me:

@szczys