epd2in7b.py 8.9 KB

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