epd2in7.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # *****************************************************************************
  2. # * | File : epd2in7.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V4.0
  8. # * | Date : 2019-06-20
  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 = 176
  33. EPD_HEIGHT = 264
  34. class EPD:
  35. def __init__(self):
  36. self.reset_pin = epdconfig.RST_PIN
  37. self.dc_pin = epdconfig.DC_PIN
  38. self.busy_pin = epdconfig.BUSY_PIN
  39. self.cs_pin = epdconfig.CS_PIN
  40. self.width = EPD_WIDTH
  41. self.height = EPD_HEIGHT
  42. lut_vcom_dc = [0x00, 0x00,
  43. 0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
  44. 0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
  45. 0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
  46. 0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  50. ]
  51. lut_ww = [
  52. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  53. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  54. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  55. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. ]
  60. lut_bw = [
  61. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  62. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  63. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  64. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. ]
  69. lut_bb = [
  70. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  71. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  72. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  73. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. ]
  78. lut_wb = [
  79. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  80. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  81. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  82. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. ]
  87. # Hardware reset
  88. def reset(self):
  89. epdconfig.digital_write(self.reset_pin, 1)
  90. epdconfig.delay_ms(200)
  91. epdconfig.digital_write(self.reset_pin, 0)
  92. epdconfig.delay_ms(10)
  93. epdconfig.digital_write(self.reset_pin, 1)
  94. epdconfig.delay_ms(200)
  95. def send_command(self, command):
  96. epdconfig.digital_write(self.dc_pin, 0)
  97. epdconfig.digital_write(self.cs_pin, 0)
  98. epdconfig.spi_writebyte([command])
  99. epdconfig.digital_write(self.cs_pin, 1)
  100. def send_data(self, data):
  101. epdconfig.digital_write(self.dc_pin, 1)
  102. epdconfig.digital_write(self.cs_pin, 0)
  103. epdconfig.spi_writebyte([data])
  104. epdconfig.digital_write(self.cs_pin, 1)
  105. def ReadBusy(self):
  106. logging.debug("e-Paper busy")
  107. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  108. epdconfig.delay_ms(200)
  109. logging.debug("e-Paper busy release")
  110. def set_lut(self):
  111. self.send_command(0x20) # vcom
  112. for count in range(0, 44):
  113. self.send_data(self.lut_vcom_dc[count])
  114. self.send_command(0x21) # ww --
  115. for count in range(0, 42):
  116. self.send_data(self.lut_ww[count])
  117. self.send_command(0x22) # bw r
  118. for count in range(0, 42):
  119. self.send_data(self.lut_bw[count])
  120. self.send_command(0x23) # wb w
  121. for count in range(0, 42):
  122. self.send_data(self.lut_bb[count])
  123. self.send_command(0x24) # bb b
  124. for count in range(0, 42):
  125. self.send_data(self.lut_wb[count])
  126. def init(self):
  127. if (epdconfig.module_init() != 0):
  128. return -1
  129. # EPD hardware init start
  130. self.reset()
  131. self.send_command(0x01) # POWER_SETTING
  132. self.send_data(0x03) # VDS_EN, VDG_EN
  133. self.send_data(0x00) # VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  134. self.send_data(0x2b) # VDH
  135. self.send_data(0x2b) # VDL
  136. self.send_data(0x09) # VDHR
  137. self.send_command(0x06) # BOOSTER_SOFT_START
  138. self.send_data(0x07)
  139. self.send_data(0x07)
  140. self.send_data(0x17)
  141. # Power optimization
  142. self.send_command(0xF8)
  143. self.send_data(0x60)
  144. self.send_data(0xA5)
  145. # Power optimization
  146. self.send_command(0xF8)
  147. self.send_data(0x89)
  148. self.send_data(0xA5)
  149. # Power optimization
  150. self.send_command(0xF8)
  151. self.send_data(0x90)
  152. self.send_data(0x00)
  153. # Power optimization
  154. self.send_command(0xF8)
  155. self.send_data(0x93)
  156. self.send_data(0x2A)
  157. # Power optimization
  158. self.send_command(0xF8)
  159. self.send_data(0xA0)
  160. self.send_data(0xA5)
  161. # Power optimization
  162. self.send_command(0xF8)
  163. self.send_data(0xA1)
  164. self.send_data(0x00)
  165. # Power optimization
  166. self.send_command(0xF8)
  167. self.send_data(0x73)
  168. self.send_data(0x41)
  169. self.send_command(0x16) # PARTIAL_DISPLAY_REFRESH
  170. self.send_data(0x00)
  171. self.send_command(0x04) # POWER_ON
  172. self.ReadBusy()
  173. self.send_command(0x00) # PANEL_SETTING
  174. self.send_data(0xAF) # KW-BF KWR-AF BWROTP 0f
  175. self.send_command(0x30) # PLL_CONTROL
  176. self.send_data(0x3A) # 3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  177. self.send_command(0x82) # VCM_DC_SETTING_REGISTER
  178. self.send_data(0x12)
  179. self.set_lut()
  180. return 0
  181. def getbuffer(self, image):
  182. # logging.debug("bufsiz = ",int(self.width/8) * self.height)
  183. buf = [0xFF] * (int(self.width/8) * self.height)
  184. image_monocolor = image.convert('1')
  185. imwidth, imheight = image_monocolor.size
  186. pixels = image_monocolor.load()
  187. # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  188. if(imwidth == self.width and imheight == self.height):
  189. logging.debug("Vertical")
  190. for y in range(imheight):
  191. for x in range(imwidth):
  192. # Set the bits for the column of pixels at the current position.
  193. if pixels[x, y] == 0:
  194. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  195. elif(imwidth == self.height and imheight == self.width):
  196. logging.debug("Horizontal")
  197. for y in range(imheight):
  198. for x in range(imwidth):
  199. newx = y
  200. newy = self.height - x - 1
  201. if pixels[x, y] == 0:
  202. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  203. return buf
  204. def display(self, image):
  205. self.send_command(0x10)
  206. for i in range(0, int(self.width * self.height / 8)):
  207. self.send_data(0xFF)
  208. self.send_command(0x13)
  209. for i in range(0, int(self.width * self.height / 8)):
  210. self.send_data(image[i])
  211. self.send_command(0x12)
  212. self.ReadBusy()
  213. def Clear(self, color):
  214. self.send_command(0x10)
  215. for i in range(0, int(self.width * self.height / 8)):
  216. self.send_data(0xFF)
  217. self.send_command(0x13)
  218. for i in range(0, int(self.width * self.height / 8)):
  219. self.send_data(0xFF)
  220. self.send_command(0x12)
  221. self.ReadBusy()
  222. def sleep(self):
  223. self.send_command(0X50)
  224. self.send_data(0xf7)
  225. self.send_command(0X02)
  226. self.send_command(0X07)
  227. self.send_data(0xA5)
  228. epdconfig.module_exit()
  229. ### END OF FILE ###