1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| #!/usr/bin/python
import RPi.GPIO as GPIO
import time
# +================================+
# | TFT to Raspberry connections |
# +----------------+---------------+
# | TFT 1.8" | Raspberry |
# +----------------+---------+-----+
# | PIN | NAME | NAME | PIN |
# +-----+----------+---------+-----+
# | 1 | Vcc | 3V3 | 1 |
# | 2 | GND | GND | 6 |
# | 3 | SCL | GPIO 11 | 23 |
# | 4 | SDA | GPIO 10 | 19 |
# | 5 | RS/DC | GPIO 24 | 18 |
# | 6 | RES | GPIO 25 | 22 |
# | 7 | CS | GPIO 8 | 24 |
# +-----+----------+---------+-----+
# | 8 | MISO | | |
# | 9 | SCLK | | |
# | 10 | MOSI | | |
# | 11 | CS | | |
# +=====+==========+=========+=====+
SCLK = 11
SDAT = 10
DC = 24
pins = [SCLK,SDAT,DC]
# RGB888 Color constants
BLACK = 0x000000
RED = 0xFF0000
GREEN = 0x00FF00
BLUE = 0x0000FF
WHITE = 0xFFFFFF
COLORSET = [RED,GREEN,BLUE,WHITE]
# ST7735 commands
SWRESET = 0x01 # software reset
SLPOUT = 0x11 # sleep out
DISPON = 0x29 # display on
CASET = 0x2A # column address set
RASET = 0x2B # row address set
RAMWR = 0x2C # RAM write
MADCTL = 0x36 # axis control
COLMOD = 0x3A # color mode
########################################################################
#
# Low-level routines
# These routines access GPIO directly
#
########################################################################
def SetPin(pinNumber,value):
#sets the GPIO pin to desired value (1=on,0=off)
GPIO.output(pinNumber,value)
def InitIO():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for pin in pins:
GPIO.setup(pin,GPIO.OUT)
GPIO.output(SCLK,0) #start with clock line low
########################################################################
#
# Bit-Banging (software) SPI routines:
#
########################################################################
def PulseClock():
#pulses the serial clock line HIGH
SetPin(SCLK,1) #bit clocked on low-high transition
SetPin(SCLK,0) #no delay since python is slow
def WriteByte(value, data=True):
"sends byte to display using software SPI"
mask = 0x80 #start with bit7 (msb)
SetPin(DC,data) #low = command; high = data
for b in range(8): #loop for 8 bits, msb to lsb
SetPin(SDAT,value & mask) #put bit on serial data line
PulseClock() #clock in the bit
mask >>= 1 #go to next bit
def WriteCmd(value):
"Send command byte to display"
WriteByte(value,False) #set D/C line to 0 = command
def WriteWord (value):
"sends a 16-bit word to the display as data"
WriteByte(value >> 8) #write upper 8 bits
WriteByte(value & 0xFF) #write lower 8 bits
def WriteList (byteList):
"Send list of bytes to display, as data"
for byte in byteList: #grab each byte in list
WriteByte(byte) #and send it
def Write888(value,reps=1):
"sends a 24-bit RGB pixel data to display, with optional repeat"
red = value>>16 # red = upper 8 bits
green = (value>>8) & 0xFF # green = middle 8 bits
blue = value & 0xFF # blue = lower 8 bits
RGB = [red,green,blue] # assemble RGB as 3 byte list
for a in range(reps): # send RGB x optional repeat
WriteList(RGB)
########################################################################
#
# ST7735 driver routines:
#
########################################################################
def InitDisplay():
"Resets & prepares display for active use."
WriteCmd (SWRESET) #software reset, puts display into sleep
time.sleep(0.2) #wait 200mS for controller register init
WriteCmd (SLPOUT) #sleep out
time.sleep(0.2) #wait 200mS for TFT driver circuits
WriteCmd (DISPON) #display on!
def SetAddrWindow(x0,y0,x1,y1):
"sets a rectangular display window into which pixel data is placed"
WriteCmd(CASET) #set column range (x0,x1)
WriteWord(x0)
WriteWord(x1)
WriteCmd(RASET) #set row range (y0,y1)
WriteWord(y0)
WriteWord(y1)
def DrawPixel(x,y,color):
"draws a pixel on the TFT display"
SetAddrWindow(x,y,x,y)
WriteCmd(RAMWR)
Write888(color)
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)
########################################################################
#
# Testing routines:
#
########################################################################
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)
########################################################################
#
# Main Program
#
########################################################################
print "Adafruit 1.8 TFT display demo"
print "-----------------------------"
InitIO()
InitDisplay()
TimeDisplay()
print "Done."
# END ############################################################### |
Partager