2

I've been trying to my tft screen work with the Pi using a tutorial here.

The python script doesn't seem to work, I get this error every time I try to run it:

Traceback (most recent call last):
  File "tft.py", line 49, in <module>
    red = value>>16
NameError: name 'value' is not defined

The script is copy-pasted from the PDF and needed lots of massaging since copying to nano causes all sort of broken lines (comment lines broken up). Here's the code I'm trying to use:

#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import spidev

BLACK   = 0x000000
RED     = 0xFF0000
GREEN   = 0x00FF00
BLUE    = 0x0000FF
WHITE   = 0xFFFFFF
COLORSET = [RED,GREEN,BLUE,WHITE]

SWRESET = 0x01
SLPOUT  = 0x11
DISPON  = 0x29
CASET   = 0x2A
RASET   = 0x2B
RAMWR   = 0x2C
MADCTL  = 0x36
COLMOD  = 0x3A

def SetPin(pinNumber,value):
    GPIO.output(pinNumber,value)
def InitIO():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(DC,GPIO.OUT)

def WriteByte(value, data=True):
       SetPin(DC,data)
       spi.writebytes([value])
def WriteCmd(value):
       "Send command byte to display"
       WriteByte(value,False)

"sends a 16-bit word to the display as data"
def WriteWord (value):
       WriteByte(value >> 8)
       WriteByte(value & 0xFF)
def WriteList (byteList):
       "Send list of bytes to display, as data"
       for byte in byteList:
              WriteByte(byte)

def Write888(value,width,count):
       "sends a 24-bit RGB pixel data to display, with optional repeat"
red = value>>16                                             #the offending line
green = (value>>8) & 0xFF
blue = value & 0xFF
RGB  = [red,green,blue]
SetPin(DC,1)
for a in range(count):
       spi.writebytes(RGB*width)

def InitDisplay():
       "Resets & prepares display for active use."
WriteCmd  (SWRESET)
time.sleep(0.2)
WriteCmd  (SLPOUT)
time.sleep(0.2)
WriteCmd  (DISPON)


def SetAddrWindow(x0,y0,x1,y1):
       "sets a rectangular display window into which pixel data is placed"
       WriteCmd(CASET)
       WriteWord(x0)
       WriteWord(x1)
       WriteCmd(RASET)
       WriteWord(y0)
       WriteWord(y1)
def FillRect(x0,y0,x1,y1,color):
       "fills rectangle with given color"
       width = x1-x0+1
       height = y1-y0+1
       SetAddrWindow(x0,y0,x1,y1)
       WriteCmd(RAMWR)
       Write888(color,width,height)

def FillScreen(color):
       "Fills entire screen with given color"
       FillRect(0,0,127,159,color)
def ClearScreen():
       "Fills entire screen with black"
       FillRect(0,0,127,159,BLACK)

def TimeDisplay():
       "Measures time required to fill display twice"
       startTime=time.time()
       print "  Now painting screen GREEN"
       FillScreen(GREEN)
       print "  Now clearing screen"
       ClearScreen()
       elapsedTime=time.time()-startTime
       print "  Elapsed time %0.1f seconds" % (elapsedTime)

print "Adafruit 1.8 TFT display demo with hardware SPI"
spi = spidev.SpiDev()
spi.open(0,0)
spi.mode = 0
InitIO()
InitDisplay()
TimeDisplay()
spi.close()
print "Done."

What am I doing wrong?

  • The answer should be simple. You haven't created the variable value, that's why the error says NameError: name 'value' is not defined. However, that's not true, as value is an argument for the function that contains the offending line. Can you comment out that line (red = value>>16) and try running it again, and see if you get a similar error again, but from the following line, green = (value>>8) & 0xFF? If so, please post the error as an edit on your post. – RPiAwesomeness Jun 11 '14 at 17:13
  • Yes, now the it complains about the green line. What shall I do? – alkopop79 Jun 12 '14 at 16:06
  • I take it that HeatfanJohn's answer solved it for you. Glad to hear that you got it resolved :D – RPiAwesomeness Jun 12 '14 at 16:57
  • Don't know. I've burnt my finger before and I'm never sure when to mark what and how. I'm surely grateful for HeatfanJohn if that's what you're suggesting. – alkopop79 Jun 13 '14 at 17:21
  • If you're saying you aren't sure if you did the right thing by marking the answer, I think you're just fine. If the answer resolved your issue, marking as answer is just a courtesy, showing you appreciate the time and effort someone put in, as well as making it clear to any readers that this answer is the fix. I personally upvote any answers that are well done and required some effort to put together and/or helped me. – RPiAwesomeness Jun 13 '14 at 17:25
1

Perhaps your formatting in your question was wrong, but the code you show doesn't have correct indentation for the lines starting with red = value>>16. Python uses indentation to denote blocks. Without the indentation the lines are not a part of Write888.

The code below shows the correct indentation.

def Write888(value,width,count):
    "sends a 24-bit RGB pixel data to display, with optional repeat"
    red = value>>16                                             #the offending line
    green = (value>>8) & 0xFF
    blue = value & 0xFF
    RGB  = [red,green,blue]
    SetPin(DC,1)
    for a in range(count):
        spi.writebytes(RGB*width)

For your second issue `

You need to add this part of the program from the tutorial that is missing from your copy. Obviously the comments (lines starting with "#" aren't required, but the remaining lines define some constants including DC).

#TFT to RPi connections 
# PIN TFT RPi 
# 1 backlight 3v3 
# 2 MISO <none> 
# 3 CLK GPIO 24 
# 4 MOSI GPIO 23 
# 5 CS-TFT GND 
# 6 CS-CARD <none> 
# 7 D/C GPIO 25 
# 8 RESET <none> 
# 9 VCC 3V3 
# 10 GND GND 

SCLK = 24 
SDAT = 23 
DC = 25 
pins = [SCLK,SDAT,DC] 
  • I don't think so. I still get an error then: File "tft.py", line 48 red = value>>16 #the offending line ^ IndentationError: unexpected indent – alkopop79 Jun 12 '14 at 16:04
  • Can you post your updated program listing? – HeatfanJohn Jun 12 '14 at 16:20
  • Sorry, can you rephrase that? You mean post the same code? Where can I post the script? – alkopop79 Jun 12 '14 at 16:22
  • Post the code exactly as you have it formatted now. We need to check that you don't have any extra spaces in any lines – HeatfanJohn Jun 12 '14 at 16:25
  • Thank you for you kind help! Wasn't sure where to post it, does this help? codepad.org/cUJtt6ff – alkopop79 Jun 12 '14 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.