epd2in66.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # *****************************************************************************
  2. # * | File : epd2in66.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.1
  8. # * | Date : 2022-08-9
  9. # # | Info : python demo
  10. # -----------------------------------------------------------------------------
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of the Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. import logging
  30. from . import epdconfig
  31. # Display resolution
  32. EPD_WIDTH = 152
  33. EPD_HEIGHT = 296
  34. logger = logging.getLogger(__name__)
  35. class EPD:
  36. def __init__(self):
  37. self.reset_pin = epdconfig.RST_PIN
  38. self.dc_pin = epdconfig.DC_PIN
  39. self.busy_pin = epdconfig.BUSY_PIN
  40. self.cs_pin = epdconfig.CS_PIN
  41. self.width = EPD_WIDTH
  42. self.height = EPD_HEIGHT
  43. WF_PARTIAL = [
  44. 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45. 0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
  46. 0x00,0x00,0x00,0x00,0x40,0x40,0x00,0x00,0x00,0x00,
  47. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,
  48. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50. 0x0A,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x00,
  51. 0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
  52. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  54. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  55. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  56. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  57. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  58. 0x00,0x00,0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x22,
  59. 0x00,0x00,0x00,0x22,0x17,0x41,0xB0,0x32,0x36,
  60. ]
  61. # Hardware reset
  62. def reset(self):
  63. epdconfig.digital_write(self.reset_pin, 1)
  64. epdconfig.delay_ms(200)
  65. epdconfig.digital_write(self.reset_pin, 0)
  66. epdconfig.delay_ms(2)
  67. epdconfig.digital_write(self.reset_pin, 1)
  68. epdconfig.delay_ms(200)
  69. def send_command(self, command):
  70. epdconfig.digital_write(self.dc_pin, 0)
  71. epdconfig.digital_write(self.cs_pin, 0)
  72. epdconfig.spi_writebyte([command])
  73. epdconfig.digital_write(self.cs_pin, 1)
  74. def send_data(self, data):
  75. epdconfig.digital_write(self.dc_pin, 1)
  76. epdconfig.digital_write(self.cs_pin, 0)
  77. epdconfig.spi_writebyte([data])
  78. epdconfig.digital_write(self.cs_pin, 1)
  79. # send a lot of data
  80. def send_data2(self, data):
  81. epdconfig.digital_write(self.dc_pin, 1)
  82. epdconfig.digital_write(self.cs_pin, 0)
  83. epdconfig.spi_writebyte2(data)
  84. epdconfig.digital_write(self.cs_pin, 1)
  85. def ReadBusy(self):
  86. logger.debug("e-Paper busy")
  87. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  88. epdconfig.delay_ms(200)
  89. logger.debug("e-Paper busy release")
  90. def init(self, mode):
  91. if (epdconfig.module_init() != 0):
  92. return -1
  93. # EPD hardware init start
  94. self.reset()
  95. self.send_command(0x12)
  96. epdconfig.delay_ms(300)
  97. self.ReadBusy()
  98. self.send_command(0x11) # setting gaet number
  99. self.send_data(0x03)
  100. self.send_command(0x44) # set gate voltage
  101. self.send_data(0x01)
  102. self.send_data(0x13)
  103. self.send_command(0x45) # set source voltage
  104. self.send_data(0x0)
  105. self.send_data(0x0)
  106. self.send_data(0x28)
  107. self.send_data(0x01)
  108. if(mode == 0): #full
  109. self.send_command(0x3C)
  110. self.send_data(0x01)
  111. elif(mode == 1): #partial
  112. self.load_lut(self.WF_PARTIAL)
  113. self.send_command(0x37) # set display option, these setting turn on previous function
  114. self.send_data(0x00)
  115. self.send_data(0x00)
  116. self.send_data(0x00)
  117. self.send_data(0x00)
  118. self.send_data(0x00)
  119. self.send_data(0x40)
  120. self.send_data(0x00)
  121. self.send_data(0x00)
  122. self.send_data(0x00)
  123. self.send_data(0x00)
  124. self.send_command(0x3C)
  125. self.send_data(0x80)
  126. self.send_command(0x22)
  127. self.send_data(0xcf)
  128. self.send_command(0x20)
  129. self.ReadBusy()
  130. else:
  131. logger.debug("There is no such mode")
  132. return 0
  133. def load_lut(self, lut):
  134. self.send_command(0x32)
  135. # for i in range(0, 153):
  136. # self.send_data(lut[i])
  137. self.send_data2(lut)
  138. def turnon_display(self):
  139. self.send_command(0x20)
  140. self.ReadBusy()
  141. def getbuffer(self, image):
  142. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  143. buf = [0xFF] * (int(self.width/8) * self.height)
  144. image_monocolor = image.convert('1')
  145. imwidth, imheight = image_monocolor.size
  146. pixels = image_monocolor.load()
  147. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  148. if(imwidth == self.width and imheight == self.height):
  149. logger.debug("Vertical")
  150. for y in range(imheight):
  151. for x in range(imwidth):
  152. # Set the bits for the column of pixels at the current position.
  153. if pixels[x, y] == 0:
  154. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  155. elif(imwidth == self.height and imheight == self.width):
  156. logger.debug("Horizontal")
  157. for y in range(imheight):
  158. for x in range(imwidth):
  159. newx = y
  160. newy = self.height - x - 1
  161. if pixels[x, y] == 0:
  162. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  163. return buf
  164. def display(self, image):
  165. if (image == None):
  166. return
  167. self.send_command(0x4E)
  168. self.send_data(0x01)
  169. self.send_command(0x4F)
  170. self.send_data(0x27)
  171. self.send_data(0x01)
  172. self.send_command(0x24)
  173. self.send_data2(image)
  174. self.turnon_display()
  175. def Clear(self):
  176. self.send_command(0x4E)
  177. self.send_data(0x01)
  178. self.send_command(0x4F)
  179. self.send_data(0x27)
  180. self.send_data(0x01)
  181. if self.width%8 == 0:
  182. linewidth = int(self.width/8)
  183. else:
  184. linewidth = int(self.width/8) + 1
  185. buf = [0xff] * int(self.height * linewidth)
  186. self.send_command(0x24)
  187. self.send_data2(buf)
  188. self.send_command(0x26)
  189. self.send_data2(buf)
  190. self.turnon_display()
  191. def sleep(self):
  192. self.send_command(0X10) # DEEP_SLEEP_MODE
  193. self.send_data(0x01)
  194. epdconfig.delay_ms(2000)
  195. epdconfig.module_exit()
  196. ### END OF FILE ###