LCD_1in44.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # -*- coding:UTF-8 -*-
  2. ##
  3. # | file : LCD_1IN44.py
  4. # | version : V2.0
  5. # | date : 2018-07-16
  6. # | function : On the ST7735S chip driver and clear screen, drawing lines, drawing, writing
  7. # and other functions to achieve
  8. #
  9. # Permission is hereby granted, free of charge, to any person obtaining a copy
  10. # of this software and associated documnetation files (the "Software"), to deal
  11. # in the Software without restriction, including without limitation the rights
  12. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. # copies of the Software, and to permit persons to whom the Software is
  14. # furished to do so, subject to the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be included in
  17. # all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. # THE SOFTWARE.
  26. #
  27. import LCD_Config
  28. import RPi.GPIO as GPIO
  29. import time
  30. import numpy as np
  31. LCD_1IN44 = 1
  32. LCD_1IN8 = 0
  33. if LCD_1IN44 == 1:
  34. LCD_WIDTH = 128 #LCD width
  35. LCD_HEIGHT = 128 #LCD height
  36. LCD_X = 2
  37. LCD_Y = 1
  38. if LCD_1IN8 == 1:
  39. LCD_WIDTH = 160
  40. LCD_HEIGHT = 128
  41. LCD_X = 1
  42. LCD_Y = 2
  43. LCD_X_MAXPIXEL = 132 #LCD width maximum memory
  44. LCD_Y_MAXPIXEL = 162 #LCD height maximum memory
  45. #scanning method
  46. L2R_U2D = 1
  47. L2R_D2U = 2
  48. R2L_U2D = 3
  49. R2L_D2U = 4
  50. U2D_L2R = 5
  51. U2D_R2L = 6
  52. D2U_L2R = 7
  53. D2U_R2L = 8
  54. SCAN_DIR_DFT = U2D_R2L
  55. class LCD:
  56. def __init__(self):
  57. self.width = LCD_WIDTH
  58. self.height = LCD_HEIGHT
  59. self.LCD_Scan_Dir = SCAN_DIR_DFT
  60. self.LCD_X_Adjust = LCD_X
  61. self.LCD_Y_Adjust = LCD_Y
  62. """ Hardware reset """
  63. def LCD_Reset(self):
  64. GPIO.output(LCD_Config.LCD_RST_PIN, GPIO.HIGH)
  65. LCD_Config.Driver_Delay_ms(100)
  66. GPIO.output(LCD_Config.LCD_RST_PIN, GPIO.LOW)
  67. LCD_Config.Driver_Delay_ms(100)
  68. GPIO.output(LCD_Config.LCD_RST_PIN, GPIO.HIGH)
  69. LCD_Config.Driver_Delay_ms(100)
  70. """ Write register address and data """
  71. def LCD_WriteReg(self, Reg):
  72. GPIO.output(LCD_Config.LCD_DC_PIN, GPIO.LOW)
  73. LCD_Config.SPI_Write_Byte([Reg])
  74. def LCD_WriteData_8bit(self, Data):
  75. GPIO.output(LCD_Config.LCD_DC_PIN, GPIO.HIGH)
  76. LCD_Config.SPI_Write_Byte([Data])
  77. def LCD_WriteData_NLen16Bit(self, Data, DataLen):
  78. GPIO.output(LCD_Config.LCD_DC_PIN, GPIO.HIGH)
  79. for i in range(0, DataLen):
  80. LCD_Config.SPI_Write_Byte([Data >> 8])
  81. LCD_Config.SPI_Write_Byte([Data & 0xff])
  82. """ Common register initialization """
  83. def LCD_InitReg(self):
  84. #ST7735R Frame Rate
  85. self.LCD_WriteReg(0xB1)
  86. self.LCD_WriteData_8bit(0x01)
  87. self.LCD_WriteData_8bit(0x2C)
  88. self.LCD_WriteData_8bit(0x2D)
  89. self.LCD_WriteReg(0xB2)
  90. self.LCD_WriteData_8bit(0x01)
  91. self.LCD_WriteData_8bit(0x2C)
  92. self.LCD_WriteData_8bit(0x2D)
  93. self.LCD_WriteReg(0xB3)
  94. self.LCD_WriteData_8bit(0x01)
  95. self.LCD_WriteData_8bit(0x2C)
  96. self.LCD_WriteData_8bit(0x2D)
  97. self.LCD_WriteData_8bit(0x01)
  98. self.LCD_WriteData_8bit(0x2C)
  99. self.LCD_WriteData_8bit(0x2D)
  100. #Column inversion
  101. self.LCD_WriteReg(0xB4)
  102. self.LCD_WriteData_8bit(0x07)
  103. #ST7735R Power Sequence
  104. self.LCD_WriteReg(0xC0)
  105. self.LCD_WriteData_8bit(0xA2)
  106. self.LCD_WriteData_8bit(0x02)
  107. self.LCD_WriteData_8bit(0x84)
  108. self.LCD_WriteReg(0xC1)
  109. self.LCD_WriteData_8bit(0xC5)
  110. self.LCD_WriteReg(0xC2)
  111. self.LCD_WriteData_8bit(0x0A)
  112. self.LCD_WriteData_8bit(0x00)
  113. self.LCD_WriteReg(0xC3)
  114. self.LCD_WriteData_8bit(0x8A)
  115. self.LCD_WriteData_8bit(0x2A)
  116. self.LCD_WriteReg(0xC4)
  117. self.LCD_WriteData_8bit(0x8A)
  118. self.LCD_WriteData_8bit(0xEE)
  119. self.LCD_WriteReg(0xC5)#VCOM
  120. self.LCD_WriteData_8bit(0x0E)
  121. #ST7735R Gamma Sequence
  122. self.LCD_WriteReg(0xe0)
  123. self.LCD_WriteData_8bit(0x0f)
  124. self.LCD_WriteData_8bit(0x1a)
  125. self.LCD_WriteData_8bit(0x0f)
  126. self.LCD_WriteData_8bit(0x18)
  127. self.LCD_WriteData_8bit(0x2f)
  128. self.LCD_WriteData_8bit(0x28)
  129. self.LCD_WriteData_8bit(0x20)
  130. self.LCD_WriteData_8bit(0x22)
  131. self.LCD_WriteData_8bit(0x1f)
  132. self.LCD_WriteData_8bit(0x1b)
  133. self.LCD_WriteData_8bit(0x23)
  134. self.LCD_WriteData_8bit(0x37)
  135. self.LCD_WriteData_8bit(0x00)
  136. self.LCD_WriteData_8bit(0x07)
  137. self.LCD_WriteData_8bit(0x02)
  138. self.LCD_WriteData_8bit(0x10)
  139. self.LCD_WriteReg(0xe1)
  140. self.LCD_WriteData_8bit(0x0f)
  141. self.LCD_WriteData_8bit(0x1b)
  142. self.LCD_WriteData_8bit(0x0f)
  143. self.LCD_WriteData_8bit(0x17)
  144. self.LCD_WriteData_8bit(0x33)
  145. self.LCD_WriteData_8bit(0x2c)
  146. self.LCD_WriteData_8bit(0x29)
  147. self.LCD_WriteData_8bit(0x2e)
  148. self.LCD_WriteData_8bit(0x30)
  149. self.LCD_WriteData_8bit(0x30)
  150. self.LCD_WriteData_8bit(0x39)
  151. self.LCD_WriteData_8bit(0x3f)
  152. self.LCD_WriteData_8bit(0x00)
  153. self.LCD_WriteData_8bit(0x07)
  154. self.LCD_WriteData_8bit(0x03)
  155. self.LCD_WriteData_8bit(0x10)
  156. #Enable test command
  157. self.LCD_WriteReg(0xF0)
  158. self.LCD_WriteData_8bit(0x01)
  159. #Disable ram power save mode
  160. self.LCD_WriteReg(0xF6)
  161. self.LCD_WriteData_8bit(0x00)
  162. #65k mode
  163. self.LCD_WriteReg(0x3A)
  164. self.LCD_WriteData_8bit(0x05)
  165. #********************************************************************************
  166. #function: Set the display scan and color transfer modes
  167. #parameter:
  168. # Scan_dir : Scan direction
  169. # Colorchose : RGB or GBR color format
  170. #********************************************************************************
  171. def LCD_SetGramScanWay(self, Scan_dir):
  172. #Get the screen scan direction
  173. self.LCD_Scan_Dir = Scan_dir
  174. #Get GRAM and LCD width and height
  175. if (Scan_dir == L2R_U2D) or (Scan_dir == L2R_D2U) or (Scan_dir == R2L_U2D) or (Scan_dir == R2L_D2U) :
  176. self.width = LCD_HEIGHT
  177. self.height = LCD_WIDTH
  178. if Scan_dir == L2R_U2D:
  179. MemoryAccessReg_Data = 0X00 | 0x00
  180. elif Scan_dir == L2R_D2U:
  181. MemoryAccessReg_Data = 0X00 | 0x80
  182. elif Scan_dir == R2L_U2D:
  183. MemoryAccessReg_Data = 0x40 | 0x00
  184. else: #R2L_D2U:
  185. MemoryAccessReg_Data = 0x40 | 0x80
  186. else:
  187. self.width = LCD_WIDTH
  188. self.height = LCD_HEIGHT
  189. if Scan_dir == U2D_L2R:
  190. MemoryAccessReg_Data = 0X00 | 0x00 | 0x20
  191. elif Scan_dir == U2D_R2L:
  192. MemoryAccessReg_Data = 0X00 | 0x40 | 0x20
  193. elif Scan_dir == D2U_L2R:
  194. MemoryAccessReg_Data = 0x80 | 0x00 | 0x20
  195. else: #R2L_D2U
  196. MemoryAccessReg_Data = 0x40 | 0x80 | 0x20
  197. #please set (MemoryAccessReg_Data & 0x10) != 1
  198. if (MemoryAccessReg_Data & 0x10) != 1:
  199. self.LCD_X_Adjust = LCD_Y
  200. self.LCD_Y_Adjust = LCD_X
  201. else:
  202. self.LCD_X_Adjust = LCD_X
  203. self.LCD_Y_Adjust = LCD_Y
  204. # Set the read / write scan direction of the frame memory
  205. self.LCD_WriteReg(0x36) #MX, MY, RGB mode
  206. if LCD_1IN44 == 1:
  207. self.LCD_WriteData_8bit( MemoryAccessReg_Data | 0x08) #0x08 set RGB
  208. else:
  209. self.LCD_WriteData_8bit( MemoryAccessReg_Data & 0xf7) #RGB color filter panel
  210. #/********************************************************************************
  211. #function:
  212. # initialization
  213. #********************************************************************************/
  214. def LCD_Init(self, Lcd_ScanDir):
  215. if (LCD_Config.GPIO_Init() != 0):
  216. return -1
  217. #Turn on the backlight
  218. GPIO.output(LCD_Config.LCD_BL_PIN,GPIO.HIGH)
  219. #Hardware reset
  220. self.LCD_Reset()
  221. #Set the initialization register
  222. self.LCD_InitReg()
  223. #Set the display scan and color transfer modes
  224. self.LCD_SetGramScanWay(Lcd_ScanDir)
  225. LCD_Config.Driver_Delay_ms(200)
  226. #sleep out
  227. self.LCD_WriteReg(0x11)
  228. LCD_Config.Driver_Delay_ms(120)
  229. #Turn on the LCD display
  230. self.LCD_WriteReg(0x29)
  231. def LCD_Backlight(self, on):
  232. if on==True:
  233. GPIO.output(LCD_Config.LCD_BL_PIN,GPIO.HIGH)
  234. #GPIO.output(LCD_Config.LCD_DC_PIN,GPIO.HIGH)
  235. else:
  236. GPIO.output(LCD_Config.LCD_BL_PIN,GPIO.LOW)
  237. #GPIO.output(LCD_Config.LCD_DC_PIN,GPIO.LOW)
  238. #/********************************************************************************
  239. #function: Sets the start position and size of the display area
  240. #parameter:
  241. # Xstart : X direction Start coordinates
  242. # Ystart : Y direction Start coordinates
  243. # Xend : X direction end coordinates
  244. # Yend : Y direction end coordinates
  245. #********************************************************************************/
  246. def LCD_SetWindows(self, Xstart, Ystart, Xend, Yend):
  247. #set the X coordinates
  248. self.LCD_WriteReg(0x2A)
  249. self.LCD_WriteData_8bit(0x00)
  250. self.LCD_WriteData_8bit((Xstart & 0xff) + self.LCD_X_Adjust)
  251. self.LCD_WriteData_8bit(0x00)
  252. self.LCD_WriteData_8bit(((Xend - 1) & 0xff) + self.LCD_X_Adjust)
  253. #set the Y coordinates
  254. self.LCD_WriteReg (0x2B)
  255. self.LCD_WriteData_8bit(0x00)
  256. self.LCD_WriteData_8bit((Ystart & 0xff) + self.LCD_Y_Adjust)
  257. self.LCD_WriteData_8bit(0x00)
  258. self.LCD_WriteData_8bit(((Yend - 1) & 0xff )+ self.LCD_Y_Adjust)
  259. self.LCD_WriteReg(0x2C)
  260. def LCD_Clear(self):
  261. #hello
  262. _buffer = [0xff]*(self.width * self.height * 2)
  263. self.LCD_SetWindows(0, 0, self.width, self.height)
  264. GPIO.output(LCD_Config.LCD_DC_PIN, GPIO.HIGH)
  265. for i in range(0,len(_buffer),4096):
  266. LCD_Config.SPI_Write_Byte(_buffer[i:i+4096])
  267. def LCD_ShowImage(self,Image,Xstart,Ystart):
  268. if (Image == None):
  269. return
  270. imwidth, imheight = Image.size
  271. if imwidth != self.width or imheight != self.height:
  272. raise ValueError('Image must be same dimensions as display \
  273. ({0}x{1}).' .format(self.width, self.height))
  274. img = np.asarray(Image)
  275. pix = np.zeros((self.width,self.height,2), dtype = np.uint8)
  276. pix[...,[0]] = np.add(np.bitwise_and(img[...,[0]],0xF8),np.right_shift(img[...,[1]],5))
  277. pix[...,[1]] = np.add(np.bitwise_and(np.left_shift(img[...,[1]],3),0xE0),np.right_shift(img[...,[2]],3))
  278. pix = pix.flatten().tolist()
  279. self.LCD_SetWindows(0, 0, self.width , self.height)
  280. GPIO.output(LCD_Config.LCD_DC_PIN, GPIO.HIGH)
  281. for i in range(0,len(pix),4096):
  282. LCD_Config.SPI_Write_Byte(pix[i:i+4096])