thisoldgeek's posterous http://thisoldgeek.posterous.com Most recent posts at thisoldgeek's posterous posterous.com Sun, 24 Feb 2013 19:38:00 -0800 Python: Call a function from outside a Class (in another Class) http://thisoldgeek.posterous.com/python-call-a-function-from-outside-a-class-i http://thisoldgeek.posterous.com/python-call-a-function-from-outside-a-class-i

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):
         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)

 

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/901724/bob_profile.JPG http://posterous.com/users/1kLnYQjaRXeV bob murphy thisoldgeek bob murphy
Fri, 22 Feb 2013 15:41:00 -0800 SPI for Samsung VFD on the raspberry Pi http://thisoldgeek.posterous.com/spi-for-samsung-vfd-on-the-raspberry-pi http://thisoldgeek.posterous.com/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!

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/901724/bob_profile.JPG http://posterous.com/users/1kLnYQjaRXeV bob murphy thisoldgeek bob murphy
Sun, 10 Feb 2013 14:34:00 -0800 Raspberry Pi SPI to Samsung VFD http://thisoldgeek.posterous.com/raspberry-pi-spi-to-samsung-vfd http://thisoldgeek.posterous.com/raspberry-pi-spi-to-samsung-vfd

P405

Did a crude translation of the adafruit SPI_VFD arduino library to python on the raspberry Pi, with the target being the Samsung 20T202DA2JA vacuum fluorescent display.

I'll do a longer write-up when I have time. You need to install spidev as a prerequisite.

The key to getting output is sending a series of integers in a call as below:

def text(string):
     L = [VFD_DATA]
     for char in string:
          L.append(ord(char))
     spi.writebytes( list(L) )

text("Hello, World!")

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/901724/bob_profile.JPG http://posterous.com/users/1kLnYQjaRXeV bob murphy thisoldgeek bob murphy