#!/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."