epd4in2.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # *****************************************************************************
  2. # * | File : epd4in2.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. from PIL import Image
  32. import RPi.GPIO as GPIO
  33. # Display resolution
  34. EPD_WIDTH = 400
  35. EPD_HEIGHT = 300
  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. lut_vcom0 = [
  45. 0x00, 0x17, 0x00, 0x00, 0x00, 0x02,
  46. 0x00, 0x17, 0x17, 0x00, 0x00, 0x02,
  47. 0x00, 0x0A, 0x01, 0x00, 0x00, 0x01,
  48. 0x00, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. ]
  53. lut_ww = [
  54. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  55. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  56. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  57. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. ]
  62. lut_bw = [
  63. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  64. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  65. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  66. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. ]
  71. lut_wb = [
  72. 0x80, 0x17, 0x00, 0x00, 0x00, 0x02,
  73. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  74. 0x80, 0x0A, 0x01, 0x00, 0x00, 0x01,
  75. 0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. ]
  80. lut_bb = [
  81. 0x80, 0x17, 0x00, 0x00, 0x00, 0x02,
  82. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  83. 0x80, 0x0A, 0x01, 0x00, 0x00, 0x01,
  84. 0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. ]
  89. # Hardware reset
  90. def reset(self):
  91. epdconfig.digital_write(self.reset_pin, 1)
  92. epdconfig.delay_ms(200)
  93. epdconfig.digital_write(self.reset_pin, 0)
  94. epdconfig.delay_ms(10)
  95. epdconfig.digital_write(self.reset_pin, 1)
  96. epdconfig.delay_ms(200)
  97. def send_command(self, command):
  98. epdconfig.digital_write(self.dc_pin, 0)
  99. epdconfig.digital_write(self.cs_pin, 0)
  100. epdconfig.spi_writebyte([command])
  101. epdconfig.digital_write(self.cs_pin, 1)
  102. def send_data(self, data):
  103. epdconfig.digital_write(self.dc_pin, 1)
  104. epdconfig.digital_write(self.cs_pin, 0)
  105. epdconfig.spi_writebyte([data])
  106. epdconfig.digital_write(self.cs_pin, 1)
  107. def ReadBusy(self):
  108. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  109. epdconfig.delay_ms(100)
  110. def set_lut(self):
  111. self.send_command(0x20) # vcom
  112. for count in range(0, 44):
  113. self.send_data(self.lut_vcom0[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_command(0x06) # boost soft start
  137. self.send_data(0x17)
  138. self.send_data(0x17)
  139. self.send_data(0x17)
  140. self.send_command(0x04) # POWER_ON
  141. self.ReadBusy()
  142. self.send_command(0x00) # panel setting
  143. self.send_data(0xbf) # KW-BF KWR-AF BWROTP 0f
  144. self.send_data(0x0d)
  145. self.send_command(0x30) # PLL setting
  146. self.send_data(0x3c) # 3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  147. self.send_command(0x61) # resolution setting
  148. self.send_data(0x01)
  149. self.send_data(0x90) # 128
  150. self.send_data(0x01)
  151. self.send_data(0x2c)
  152. self.send_command(0x82) # vcom_DC setting
  153. self.send_data(0x28)
  154. self.send_command(0X50) # VCOM AND DATA INTERVAL SETTING
  155. self.send_data(0x97) # 97white border 77black border VBDF 17|D7 VBDW 97 VBDB 57 VBDF F7 VBDW 77 VBDB 37 VBDR B7
  156. self.set_lut()
  157. # EPD hardware init end
  158. return 0
  159. def getbuffer(self, image):
  160. # logging.debug("bufsiz = ",int(self.width/8) * self.height)
  161. buf = [0xFF] * (int(self.width/8) * self.height)
  162. image_monocolor = image.convert('1')
  163. imwidth, imheight = image_monocolor.size
  164. pixels = image_monocolor.load()
  165. # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  166. if(imwidth == self.width and imheight == self.height):
  167. logging.debug("Horizontal")
  168. for y in range(imheight):
  169. for x in range(imwidth):
  170. # Set the bits for the column of pixels at the current position.
  171. if pixels[x, y] == 0:
  172. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  173. elif(imwidth == self.height and imheight == self.width):
  174. logging.debug("Vertical")
  175. for y in range(imheight):
  176. for x in range(imwidth):
  177. newx = y
  178. newy = self.height - x - 1
  179. if pixels[x, y] == 0:
  180. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  181. return buf
  182. def display(self, image):
  183. self.send_command(0x10)
  184. for i in range(0, int(self.width * self.height / 8)):
  185. self.send_data(0xFF)
  186. self.send_command(0x13)
  187. for i in range(0, int(self.width * self.height / 8)):
  188. self.send_data(image[i])
  189. self.send_command(0x12)
  190. self.ReadBusy()
  191. def Clear(self):
  192. self.send_command(0x10)
  193. for i in range(0, int(self.width * self.height / 8)):
  194. self.send_data(0xFF)
  195. self.send_command(0x13)
  196. for i in range(0, int(self.width * self.height / 8)):
  197. self.send_data(0xFF)
  198. self.send_command(0x12)
  199. self.ReadBusy()
  200. def sleep(self):
  201. self.send_command(0x02) # POWER_OFF
  202. self.ReadBusy()
  203. self.send_command(0x07) # DEEP_SLEEP
  204. self.send_data(0XA5)
  205. epdconfig.module_exit()
  206. ### END OF FILE ###