epd5in65f.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. # *****************************************************************************
  4. # * | File : epd5in65f.py
  5. # * | Author : Waveshare team
  6. # * | Function : Electronic paper driver
  7. # * | Info :
  8. # *----------------
  9. # * | This version: V1.0
  10. # * | Date : 2020-03-02
  11. # # | Info : python demo
  12. # -----------------------------------------------------------------------------
  13. # Permission is hereby granted, free of charge, to any person obtaining a copy
  14. # of this software and associated documnetation files (the "Software"), to deal
  15. # in the Software without restriction, including without limitation the rights
  16. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. # copies of the Software, and to permit persons to whom the Software is
  18. # furished to do so, subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included in
  21. # all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. # THE SOFTWARE.
  30. #
  31. import logging
  32. from . import epdconfig
  33. # Display resolution
  34. EPD_WIDTH = 600
  35. EPD_HEIGHT = 448
  36. class EPD:
  37. def __init__(self):
  38. self.reset_pin = epdconfig.RST_PIN
  39. self.dc_pin = epdconfig.DC_PIN
  40. self.busy_pin = epdconfig.BUSY_PIN
  41. self.cs_pin = epdconfig.CS_PIN
  42. self.width = EPD_WIDTH
  43. self.height = EPD_HEIGHT
  44. self.BLACK = 0x000000 # 0000 BGR
  45. self.WHITE = 0xffffff # 0001
  46. self.GREEN = 0x00ff00 # 0010
  47. self.BLUE = 0xff0000 # 0011
  48. self.RED = 0x0000ff # 0100
  49. self.YELLOW = 0x00ffff # 0101
  50. self.ORANGE = 0x0080ff # 0110
  51. # Hardware reset
  52. def reset(self):
  53. epdconfig.digital_write(self.reset_pin, 1)
  54. epdconfig.delay_ms(600)
  55. epdconfig.digital_write(self.reset_pin, 0)
  56. epdconfig.delay_ms(2)
  57. epdconfig.digital_write(self.reset_pin, 1)
  58. epdconfig.delay_ms(200)
  59. def send_command(self, command):
  60. epdconfig.digital_write(self.dc_pin, 0)
  61. epdconfig.digital_write(self.cs_pin, 0)
  62. epdconfig.spi_writebyte([command])
  63. epdconfig.digital_write(self.cs_pin, 1)
  64. def send_data(self, data):
  65. epdconfig.digital_write(self.dc_pin, 1)
  66. epdconfig.digital_write(self.cs_pin, 0)
  67. epdconfig.spi_writebyte([data])
  68. epdconfig.digital_write(self.cs_pin, 1)
  69. def ReadBusyHigh(self):
  70. logging.debug("e-Paper busy")
  71. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  72. epdconfig.delay_ms(100)
  73. logging.debug("e-Paper busy release")
  74. def ReadBusyLow(self):
  75. logging.debug("e-Paper busy")
  76. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  77. epdconfig.delay_ms(100)
  78. logging.debug("e-Paper busy release")
  79. def init(self):
  80. if (epdconfig.module_init() != 0):
  81. return -1
  82. # EPD hardware init start
  83. self.reset()
  84. self.ReadBusyHigh()
  85. self.send_command(0x00)
  86. self.send_data(0xEF)
  87. self.send_data(0x08)
  88. self.send_command(0x01)
  89. self.send_data(0x37)
  90. self.send_data(0x00)
  91. self.send_data(0x23)
  92. self.send_data(0x23)
  93. self.send_command(0x03)
  94. self.send_data(0x00)
  95. self.send_command(0x06)
  96. self.send_data(0xC7)
  97. self.send_data(0xC7)
  98. self.send_data(0x1D)
  99. self.send_command(0x30)
  100. self.send_data(0x3c)
  101. self.send_command(0x40)
  102. self.send_data(0x00)
  103. self.send_command(0x50)
  104. self.send_data(0x37)
  105. self.send_command(0x60)
  106. self.send_data(0x22)
  107. self.send_command(0x61)
  108. self.send_data(0x02)
  109. self.send_data(0x58)
  110. self.send_data(0x01)
  111. self.send_data(0xC0)
  112. self.send_command(0xE3)
  113. self.send_data(0xAA)
  114. epdconfig.delay_ms(100)
  115. self.send_command(0x50)
  116. self.send_data(0x37)
  117. # EPD hardware init end
  118. return 0
  119. def getbuffer(self, image):
  120. buf = [0x00] * int(self.width * self.height / 2)
  121. image_monocolor = image.convert('RGB')#Picture mode conversion
  122. imwidth, imheight = image_monocolor.size
  123. pixels = image_monocolor.load()
  124. logging.debug('imwidth = %d imheight = %d ',imwidth, imheight)
  125. if(imwidth == self.width and imheight == self.height):
  126. for y in range(imheight):
  127. for x in range(imwidth):
  128. # Set the bits for the column of pixels at the current position.
  129. Add = int((x + y * self.width) / 2)
  130. Color = 0;
  131. if (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  132. Color = 0
  133. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 255):
  134. Color = 1
  135. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  136. Color = 2
  137. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 255):
  138. Color = 3
  139. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  140. Color = 4
  141. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  142. Color = 5
  143. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 128 and pixels[x, y][2] == 0):
  144. Color = 6
  145. data_t = buf[Add]&(~(0xF0 >> ((x % 2)*4)))
  146. buf[Add] = data_t | ((Color << 4) >> ((x % 2)*4));
  147. elif(imwidth == self.height and imheight == self.width):
  148. for y in range(imheight):
  149. for x in range(imwidth):
  150. newx = y
  151. newy = self.height - x - 1
  152. Add = int((newx + newy*self.width) / 2)
  153. Color = 0;
  154. if (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  155. Color = 0
  156. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 255):
  157. Color = 1
  158. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  159. Color = 2
  160. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 255):
  161. Color = 3
  162. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  163. Color = 4
  164. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  165. Color = 5
  166. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 128 and pixels[x, y][2] == 0):
  167. Color = 6
  168. data_t = buf[Add]&(~(0xF0 >> ((newx % 2)*4)))
  169. buf[Add] = data_t | ((Color << 4) >> ((newx % 2)*4));
  170. return buf
  171. def display(self,image):
  172. self.send_command(0x61)#Set Resolution setting
  173. self.send_data(0x02)
  174. self.send_data(0x58)
  175. self.send_data(0x01)
  176. self.send_data(0xC0)
  177. self.send_command(0x10)
  178. for i in range(0, int(EPD_HEIGHT)):
  179. for j in range(0, int(EPD_WIDTH/2)):
  180. self.send_data((image[j+(int(EPD_WIDTH/2)*i)]))
  181. self.send_command(0x04)#0x04
  182. self.ReadBusyHigh()
  183. self.send_command(0x12)#0x12
  184. self.ReadBusyHigh()
  185. self.send_command(0x02) #0x02
  186. self.ReadBusyLow()
  187. epdconfig.delay_ms(500)
  188. def Clear(self):
  189. self.send_command(0x61)#Set Resolution setting
  190. self.send_data(0x02)
  191. self.send_data(0x58)
  192. self.send_data(0x01)
  193. self.send_data(0xC0)
  194. self.send_command(0x10)
  195. for i in range(0, int(EPD_HEIGHT)):
  196. for j in range(0, int(EPD_WIDTH/2)):
  197. self.send_data(0x11)
  198. #BLACK 0x00 /// 0000
  199. #WHITE 0x11 /// 0001
  200. #GREEN 0x22 /// 0010
  201. #BLUE 0x33 /// 0011
  202. #RED 0x44 /// 0100
  203. #YELLOW 0x55 /// 0101
  204. #ORANGE 0x66 /// 0110
  205. #CLEAN 0x77 /// 0111 unavailable Afterimage
  206. self.send_command(0x04)#0x04
  207. self.ReadBusyHigh()
  208. self.send_command(0x12)#0x12
  209. self.ReadBusyHigh()
  210. self.send_command(0x02) #0x02
  211. self.ReadBusyLow()
  212. epdconfig.delay_ms(500)
  213. def sleep(self):
  214. epdconfig.delay_ms(500)
  215. self.send_command(0x07) # DEEP_SLEEP
  216. self.send_data(0XA5)
  217. epdconfig.digital_write(self.reset_pin, 0)
  218. epdconfig.delay_ms(2000)
  219. epdconfig.module_exit()