Feb 24, 2013

Python: Call a function from outside a Class (in another Class)

Warning! I'm a noob to python and the following may not be the best way to solve the problem. It may even contain errors!

I wanted to create a python class that would contain most of the functions I needed to interface a Samsung VFD to a raspberry pi. The VFD connects to the pi using SPI. To make things a little cleaner, I wanted to initialize SPI from within my main class, called SPI_VFD in the code shown below. I also needed to call the functions spi.writebytes and spi.xfer2 (from the module spidev) from within my SPI_VFD class.

In initial testing, here's what worked for me:

import spidev

class thisspi:
    spi = spidev.SpiDev()

class SPI_VFD:
     def __init__(self, callspi):
         self.myspi = callspi
         self.setspi
     def setspi():
         self.myspi.spi.open(0,0)
         self.myspi.spi.mode=3

#instantiate spi connection
s = thisspi()

# initialize SPI_VFD class, passing the spi instance
vfd = SPI_VFD(s)

 

 

Feb 22, 2013

SPI for Samsung VFD on the raspberry Pi

TL:DR version - use spi.mode = 3
For an Internet radio project, I wanted to display "Now Playing" information on a beautiful Samsung VFD sourced from adafruit.com. Adafruit has provided a nice arduino library for this display which I used as a starter set to translate into/build a python SPI-VFD class for the raspberry Pi.

I'm a complete noob at python but I tackled the translation of C code to python without too much trouble. I got things going in fairly short order after some google searches, was able to print "Hello, World!" However, the VFD was behaving a bit oddly on a few commands, the cursor. Well, you have to have a cursor to get to the second line, right?


Initially, I found a very useful thread on the raspberry Pi forum from poster bgreat on setting up a Nokia LCD to work with the Pi. That didn't fit my use case because it used more connections for SPI than the Samsung's three wires. It did give me a model of how to code for SPI on the Pi in general, however. The posting thread is also good for getting background on how to install/activate hardware SPI on the Pi.

But I hit a wall for a couple of weeks on fixing the cursor. I did a lot of searches based on guesses as to what the problem was. TTL voltage? Tried a 74HCT244 TTL up converter, same cursor result.  Under-current flakiness? Used a 2A power supply and separate, powered hub. Same. Some kind of SPI under-run, tried sending extra dummy bytes, still flakey. Think, think, think!

In my googling, I tripped over a posting reference to 3-WIRE mode in SPI. Hmn... The Samsung VFD uses only three SPI connections. Look at the data sheet, there's just a quick, passing reference to "three wire serial interface" and nothing else said.  OK, let's experiment!

I saw a test program on mitchtech.net that included source code to set SPI mode with a variety of parameters. Those parameters triggered a memory of an answer on stackoverflow.com about some Py object bindings. OK. So first, not knowing what I'm doing, I stupidly try the command:
spi.mode = 1
thinkng the mode is either on/off. The python interpreter accepts that, but I still get the same flakey results. Go back and look at the SPI test program again. There's a case statement for mode  that clearly says "3  #3-wire mode". Doh! Missed that.

Try:
spi.mode = 3

Success!