Advertisement
Guest User

Untitled

a guest
Dec 26th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. #!/usr/bin/python
  2. ########################################################################
  3. #
  4. # A Python script for controlling the Adafruit 1.8" TFT LCD module
  5. # from a Raspbery Pi.
  6. #
  7. # Author : Bruce E. Hall, W8BH <bhall66@gmail.com>
  8. # Date : 27 Apr 2013
  9. #
  10. # This module uses the ST7735 controller and SPI data interface
  11. #
  12. #
  13. # For more information, see w8bh.net
  14. #
  15. ########################################################################
  16. import RPi.GPIO as GPIO
  17. import time
  18. #TFT to RPi connections
  19. # PIN TFT RPi
  20. # 1 backlight 3v3
  21. # 2 MISO <none>
  22. # 3 CLK GPIO 24
  23. # 4 MOSI GPIO 23
  24. # 5 CS-TFT GND
  25. # 6 CS-CARD <none>
  26. # 7 D/C GPIO 25
  27. # 8 RESET <none>
  28. # 9 VCC 3V3
  29. # 10 GND GND
  30. SCLK = 24
  31. SDAT = 23
  32. DC = 25
  33. pins = [SCLK,SDAT,DC]
  34. #RGB888 Color constants
  35. BLACK = 0x000000
  36. RED = 0xFF0000
  37. GREEN = 0x00FF00
  38. BLUE = 0x0000FF
  39. WHITE = 0xFFFFFF
  40. COLORSET = [RED,GREEN,BLUE,WHITE]
  41. #ST7735 commands
  42. SWRESET = 0x01 #software reset
  43. SLPOUT = 0x11 #sleep out
  44. DISPON = 0x29 #display on
  45. CASET = 0x2A #column address set
  46. RASET = 0x2B #row address set
  47. RAMWR = 0x2C #RAM write
  48. MADCTL = 0x36 #axis control
  49. COLMOD = 0x3A #color mode########################################################################
  50. #
  51. # Low-level routines
  52. # These routines access GPIO directly
  53. #
  54. def SetPin(pinNumber,value):
  55. #sets the GPIO pin to desired value (1=on,0=off)
  56. GPIO.output(pinNumber,value)
  57. def InitIO():
  58. GPIO.setmode(GPIO.BCM)
  59. GPIO.setwarnings(False)
  60. for pin in pins:
  61. GPIO.setup(pin,GPIO.OUT)
  62. GPIO.output(SCLK,0) #start with clock line low
  63.  
  64.  
  65. ########################################################################
  66. #
  67. # Bit-Banging (software) SPI routines:
  68. #
  69. #
  70. def PulseClock():
  71. #pulses the serial clock line HIGH
  72. SetPin(SCLK,1) #bit clocked on low-high transition
  73. SetPin(SCLK,0) #no delay since python is slow
  74. def WriteByte(value, data=True):
  75. "sends byte to display using software SPI"
  76. mask = 0x80 #start with bit7 (msb)
  77. SetPin(DC,data) #low = command; high = data
  78. for b in range(8): #loop for 8 bits, msb to lsb
  79. SetPin(SDAT,value & mask) #put bit on serial data line
  80. PulseClock() #clock in the bit
  81. mask >>= 1 #go to next bit
  82. def WriteCmd(value):
  83. "Send command byte to display"
  84. WriteByte(value,False) #set D/C line to 0 = command
  85. def WriteWord (value):
  86. "sends a 16-bit word to the display as data"
  87. WriteByte(value >> 8) #write upper 8 bits
  88. WriteByte(value & 0xFF) #write lower 8 bits
  89. def WriteList (byteList):
  90. "Send list of bytes to display, as data"
  91. for byte in byteList: #grab each byte in list
  92. WriteByte(byte) #and send it
  93. def Write888(value,reps=1):
  94. "sends a 24-bit RGB pixel data to display, with optional repeat"
  95. red = value>>16 #red = upper 8 bits
  96. green = (value>>8) & 0xFF #green = middle 8 bits
  97. blue = value & 0xFF #blue = lower 8 bits
  98. RGB = [red,green,blue] #assemble RGB as 3 byte list
  99. for a in range(reps): #send RGB x optional repeat
  100. WriteList(RGB)########################################################################
  101. #
  102. # ST7735 driver routines:
  103. #
  104. #
  105. def InitDisplay():
  106. "Resets & prepares display for active use."
  107. WriteCmd (SWRESET) #software reset, puts display into sleep
  108. time.sleep(0.2) #wait 200mS for controller register init
  109. WriteCmd (SLPOUT) #sleep out
  110. time.sleep(0.2) #wait 200mS for TFT driver circuits
  111. WriteCmd (DISPON) #display on!
  112. def SetAddrWindow(x0,y0,x1,y1):
  113. "sets a rectangular display window into which pixel data is placed"
  114. WriteCmd(CASET) #set column range (x0,x1)
  115. WriteWord(x0)
  116. WriteWord(x1)
  117. WriteCmd(RASET) #set row range (y0,y1)
  118. WriteWord(y0)
  119. WriteWord(y1)
  120. def DrawPixel(x,y,color):
  121. "draws a pixel on the TFT display"
  122. SetAddrWindow(x,y,x,y)
  123. WriteCmd(RAMWR)
  124. Write888(color)
  125. def FillRect(x0,y0,x1,y1,color):
  126. "fills rectangle with given color"
  127. width = x1-x0+1
  128. height = y1-y0+1
  129. SetAddrWindow(x0,y0,x1,y1)
  130. WriteCmd(RAMWR)
  131. Write888(color,width*height)
  132. def FillScreen(color):
  133. "Fills entire screen with given color"
  134. FillRect(0,0,127,159,color)
  135. def ClearScreen():
  136. "Fills entire screen with black"
  137. FillRect(0,0,127,159,BLACK)
  138. ########################################################################
  139. #
  140. # Testing routines:
  141. #
  142. #
  143. def TimeDisplay():
  144. "Measures time required to fill display twice"
  145. startTime=time.time()
  146. print " Now painting screen GREEN"
  147. FillScreen(GREEN)
  148. print " Now clearing screen"
  149. ClearScreen()
  150. elapsedTime=time.time()-startTime
  151. print " Elapsed time %0.1f seconds" % (elapsedTime)########################################################################
  152. #
  153. # Main Program
  154. #
  155. print "Adafruit 1.8 TFT display demo"
  156. InitIO()
  157. InitDisplay()
  158. TimeDisplay()
  159. print "Done."
  160. # END ###############################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement