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

 

 

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!

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!")

Nokia 5110 LCD on raspberry Pi

I'm just taking baby steps here, trying to duplicate the work of others. I compiled a C program from binerry.de that talks to the 5110 from the GPIO's on the Pi.

These Nokia graphical screens are cheap, about $10 at adafruit and eBay, and they're used enough that there's starter sets of C and python out there.

Eventual goal is to have a display powered by python. Idea is to output current song from mpd, along with other interface info, for a wifi radio.

Photo

Sent from my iPhone

PiTX Raspberry Pi Power Controller

P35

Jason on his blog (http://www.boeeerb.co.uk/pitx-an-atx-style-solution-for-the-pi/) described an innovative approach to controlling the power on a raspberry Pi.

I've already corrupted the file system on my Pi by just pulling power, so this was a great idea!

Here's my first prototype based on Jason's work. This version uses a DC 2.1 barrel connector to a 5V 1A wall wart, a DPDT 5V NTE R40 relay, some tactile buttons from adafruit.com all soldered together on an adafruit perma-proto board.

The large tactile buttons are color-coded green for go/start, red for shutdown/stop. The shutdown button is hooked into GPIO 18 on the Pi, where a running python script checks for signal. On high, the script executes a shutdown.

Getting CMedia USB Sound to Work with MPD on the Raspberry Pi

This assumes that you have a working installation of mpd and mpc and you can get sound out of the analog port. Using 2012-09-18_wheezy.img, the analog sound still has loud pops when you change streaming stations in mpd, plus noticeable hiss in the background of quiet passages.

To get the CMedia USB Sound dongle (looks like this one available at Newegg) working, you need to install alsamixer (if not already installed) via the command:

sudo apt-get install alsa-utils

Once that is installed, you need to modify the appropriate section of /etc/modprobe.d/alsa-base.conf so it looks like this:

# Keep snd-usb-audio from beeing loaded as first soundcard
#options snd-usb-audio index=-2
options snd-usb-audio index=0

Setting the index to 0 allows the USB Sound dongle to be loaded first. Index=-2 prevents that.

Then, in /etc/mpd.conf, you want to change the sound section like this:

# An example of an ALSA output:
#
audio_output {
    type           "alsa"
    name         "My ALSA Device"
    device        "hw:0,0"    # optional
    format         "44100:16:2"    # optional
#  mixer_device    "default"    # optional
    mixer_control    "Speaker"    # added 2012-10-04 for USB Sound dongle
    mixer_index    "0"        # optional
}

"Speaker" is how alsa-mixer refers to the device (alsamixer partial screen show below):

 Card: C-Media USB Audio Device                       F1:  Help               │
 Chip: USB Mixer                                      F2:  System information │
 View: F3:[Playback] F4: Capture  F5: All             F6:  Select sound card  │
 Item: Speaker [dB gain: -26.69, -26.69]              Esc: Exit              

If you have problems, examine the output of mpd from /var/log/mpd/mpd.log by tail. When I was troubleshooting this, the log was helpful. Here are some sample messages which pointed me in the right direction:

Oct 14 19:46 : avahi: Service 'Music Player' successfully established.
Oct 14 19:46 : mixer: Failed to read mixer for 'My ALSA Device': no such mixer control: PCM

I tried using PCM because that is the associated with the first installed sound card. Making the changes shown for alsa-base.conf to allow the USB Sound card to start first changes the alsamixer output to Item: Speaker from Item:PCM

LCD and Pandora on raspberry pi

P32

Went to town on the raspberry pi this week while I stayed with my friend John in Eureka.

The picture shows my new Pi in a laser cut case I made at TechShop SF last week. On top is a TextStar tiny LCD showing one of 4 screens, this one the wireless USB adapter's IP address. See the post at jeremyblythe.blogspot.co.uk, "Raspberry Pi with TextStar Serial LCD Display" on how to set this up with python.

I used adafruit's nice Occidentalis v0.2 release for the main software, based on Raspbian Wheezy Debian. This has a lot of good packages already built in, including ssh. I just used my Mac Terminal program to set the Pi up over Ethernet initially.

To get pandora going, I used pianobar. The install is simple:
sudo apt-get install pianobar
Set up the config file for automatic login and you're good to go.

To get streaming radio on the Pi, I installed mpd, the Music Player Daemon. This is an easy install also:
sudo apt-get install mpd mpc
See the article at www.t3node.com/blog/streaming-audio-with-mpd-and-icecast2-on-raspberry-pi for setup.

Getting the Pi to work with the popular mpd iPhone client, MPoD, was a bit of a challenge. MPoD could see the Pi via avahi/bonjour, but not connect to it. John found out you have to have bind_to_address for "local host" commented out for MPoD to be able to connect to it.