The new Raspberry Pi B Model B+ is here - Pick one up in the store and check our detailed guide!

SPI-VFD on python - can't set cursor on Samsung VFD

by sys_spud on Fri Feb 15, 2013 6:23 pm

I've been working on translating the arduino library SPI-VFD for the Samsung 20T202DA2JA display to python. I want to use this on the raspberry pi.

I have hardware spidev modules/libraries installed and they appear to work. I can turn the display on/off, set brightness and print text to the screen

However, I'm having trouble setting the cursor to the second line on the display. This would be the following code in the arduino SPI-VFD library:
Code: Select all | TOGGLE FULL SIZE
row_offsets = [0x00, 0x40, 0x14, 0x54]
    if row > _numlines:
       row = _numlines-1        # count rows starting with 0

    command(VFD_SETDDRAMADDR | (col + row_offsets[row]) )


I have printed the value being sent to the command function, it appears correct. I have spent quite a bit of time already trying to troubleshoot this, but right now I'm stumped. Hoping someone out there has at least some direction for me to pursue. I'm very new to python but trying to learn as much as I can. I have about a dozen arduino projects under my belt.

Here's the full code for if it helps:
Thanks for any help you can give me.
Code: Select all | TOGGLE FULL SIZE
import time
import spidev


# SPI connection
SCE  = 10 # gpio pin 24 = wiringpi no. 10 (CE0 BCM 8)
SCLK = 14 # gpio pin 23 = wiringpi no. 14 (SCLK BCM 11)
DIN  = 12 # gpio pin 19 = wiringpi no. 12 (MOSI BCM 10)

# data
COLS = 20
ROWS = 2

# commands
VFD_CLEARDISPLAY = 0x01
VFD_RETURNHOME = 0x02
VFD_ENTRYMODESET = 0x04
VFD_DISPLAYCONTROL = 0x08
VFD_CURSORSHIFT = 0x10
VFD_FUNCTIONSET = 0x20
VFD_SETCGRAMADDR = 0x40
VFD_SETDDRAMADDR = 0x80

# flags for display entry mode
VFD_ENTRYRIGHT = 0x00
VFD_ENTRYLEFT = 0x02
VFD_ENTRYSHIFTINCREMENT = 0x01
VFD_ENTRYSHIFTDECREMENT = 0x00

# flags for display on/off control
VFD_DISPLAYON = 0x04
#VFD_DISPLAYOFF = 0x00
VFD_CURSORON = 0x02
VFD_CURSOROFF = 0x00
VFD_BLINKON = 0x01
VFD_BLINKOFF = 0x00

# flags for display/cursor shift
VFD_DISPLAYMOVE = 0x08
VFD_CURSORMOVE = 0x00
VFD_MOVERIGHT = 0x04
VFD_MOVELEFT = 0x00

# flags for function set
VFD_8BITMODE = 0x10
VFD_4BITMODE = 0x00
VFD_2LINE = 0x08
VFD_1LINE = 0x00
VFD_BRIGHTNESS25 = 0x03
VFD_BRIGHTNESS50 = 0x02
VFD_BRIGHTNESS75 = 0x01
VFD_BRIGHTNESS100 = 0x00

VFD_5x10DOTS = 0x04
VFD_5x8DOTS = 0x00

VFD_SPICOMMAND = 0xF8
VFD_SPIDATA = 0xFA


def init():
    _displayfunction = VFD_8BITMODE
    begin(COLS,ROWS,_displayfunction, VFD_BRIGHTNESS25)

def begin(cols, lines, _displayfunction, brightness):
    if lines > 1:
       _displayfunction |= VFD_2LINE

    setBrightness(_displayfunction, brightness)

    _numlines = lines
    _currline = 0
   
    # Initialize to default text direction (for romance languages#include "SPI_VFD.h"
    _displaymode = VFD_ENTRYLEFT | VFD_ENTRYSHIFTDECREMENT
    # set the entry mode
    command(VFD_ENTRYMODESET | _displaymode)
 
    command(VFD_DISPLAYCONTROL | VFD_DISPLAYON)
   
    # go to address 0
    command(VFD_SETDDRAMADDR) 
   
    clear()
    home()
   
def display(_displaycontrol):
    _displaycontrol |= VFD_DISPLAYON
    command(VFD_DISPLAYCONTROL | _displaycontrol) 
 
def clear():
    command(VFD_CLEARDISPLAY)
    time.sleep(4)

def home():
    command(VFD_RETURNHOME)
    time.sleep(2)

def setBrightness(_displayfunction, brightness):
    #set the brightness (only if a valid value is passed
    if brightness <= VFD_BRIGHTNESS25:
        _displayfunction &= ~VFD_BRIGHTNESS25
        _displayfunction |= brightness

    command(VFD_FUNCTIONSET | _displayfunction)

def setCursor(col, row):
    _numlines = 2
    row_offsets = [0x00, 0x40, 0x14, 0x54]
    if row > _numlines:
       row = _numlines-1        # count rows starting with 0
    print ("Sending cursor data :")
    command(VFD_SETDDRAMADDR | (col + row_offsets[row]) )

def noDisplay(vfdoff):
    command(VFD_DISPLAYCONTROL | vfdoff)


def text(string):
    #   display_char(ord(char))
    l = [VFD_SPIDATA]
    for char in string:
       l.append(ord(char))
    spi.writebytes(list(l))
   
       

def command(_setting):
    print _setting
    spi.writebytes([VFD_SPICOMMAND, _setting])
 
 

# initalize SPI
spi = spidev.SpiDev()

spi.open(0,0)
spi.max_speed_hz=5000000

print("<==== Mainlline Starts ====>")
init()

print("<==== Print Text ====>")
thistext = "Hello, World!"
text(thistext)

time.sleep(5)
#clear()
#home()

setCursor(1,1)
thistext = "See me Again!"
text(thistext)

time.sleep(5)
#clear()
#home()
print("<==== End of Program ====>")
sys_spud
 
Posts: 12
Joined: Sun Aug 31, 2008 8:34 pm

<SOLVED> SPI-VFD on python - can't set cursor on Samsung VFD

by sys_spud on Thu Feb 21, 2013 11:14 pm

LMGTFM (Let Me Google That For Me!)

From a lot of guess-work and research, I found a reference at mitchtech (http://mitchtech.net/category/tutorials/raspberry-pi/ that mentioned setting SPI to 3Wire mode. This was in the context of mode options, which reminded me of a discussion on stackoverflow (http://stackoverflow.com/questions/13415670/raspberry-pi-and-spi-interface-python-how-to-transfer-data) detailing python binding options to spidev.

Tried this code and it seemed to do the trick:

Code: Select all | TOGGLE FULL SIZE
# initalize SPI
spi = spidev.SpiDev()

spi.open(0,0)
spi.max_speed_hz=5000000
# set spi mode to 3WIRE
spi.mode = 3               #<==== this was the key change!
sys_spud
 
Posts: 12
Joined: Sun Aug 31, 2008 8:34 pm