epd4in2b_V2.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # *****************************************************************************
  2. # * | File : epd4in2bc.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V4.1
  8. # * | Date : 2022-08-10
  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 = 400
  33. EPD_HEIGHT = 300
  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. self.flag = 0
  44. if (epdconfig.module_init(cleanup=True) != 0):
  45. return -1
  46. # Hardware reset
  47. def reset(self):
  48. epdconfig.digital_write(self.reset_pin, 1)
  49. epdconfig.delay_ms(200)
  50. epdconfig.digital_write(self.reset_pin, 0)
  51. epdconfig.delay_ms(5)
  52. epdconfig.digital_write(self.reset_pin, 1)
  53. epdconfig.delay_ms(200)
  54. def send_command(self, command):
  55. epdconfig.digital_write(self.dc_pin, 0)
  56. epdconfig.digital_write(self.cs_pin, 0)
  57. epdconfig.DEV_SPI_write(command)
  58. epdconfig.digital_write(self.cs_pin, 1)
  59. def send_data(self, data):
  60. epdconfig.digital_write(self.dc_pin, 1)
  61. epdconfig.digital_write(self.cs_pin, 0)
  62. epdconfig.DEV_SPI_write(data)
  63. epdconfig.digital_write(self.cs_pin, 1)
  64. # send a lot of data
  65. def send_data2(self, data):
  66. epdconfig.digital_write(self.dc_pin, 1)
  67. epdconfig.digital_write(self.cs_pin, 0)
  68. epdconfig.spi_writebyte2(data)
  69. epdconfig.digital_write(self.cs_pin, 1)
  70. def ReadBusy(self):
  71. logger.debug("e-Paper busy")
  72. if(self.flag == 1):
  73. while(epdconfig.digital_read(self.busy_pin) == 1):
  74. epdconfig.delay_ms(100)
  75. else:
  76. while(epdconfig.digital_read(self.busy_pin) == 0):
  77. epdconfig.delay_ms(100)
  78. logger.debug("e-Paper busy release")
  79. def TurnOnDisplay(self):
  80. if(self.flag == 1):
  81. self.send_command(0x22)
  82. self.send_data(0xF7)
  83. self.send_command(0x20)
  84. self.ReadBusy()
  85. else:
  86. self.send_command(0x12)
  87. epdconfig.delay_ms(100)
  88. self.ReadBusy()
  89. def init(self):
  90. i = 0x00
  91. self.reset()
  92. self.send_command(0x2F)
  93. epdconfig.delay_ms(100)
  94. epdconfig.digital_write(self.dc_pin, 1)
  95. epdconfig.digital_write(self.cs_pin, 0)
  96. i = epdconfig.DEV_SPI_read()
  97. epdconfig.digital_write(self.cs_pin, 1)
  98. # print(i)
  99. if(i == 0x01):
  100. self.flag = 1
  101. self.ReadBusy()
  102. self.send_command(0x12)
  103. self.ReadBusy()
  104. self.send_command(0x3C)
  105. self.send_data(0x05)
  106. self.send_command(0x18)
  107. self.send_data(0x80)
  108. self.send_command(0x11)
  109. self.send_data(0x03)
  110. self.send_command(0x44)
  111. self.send_data(0x00)
  112. self.send_data(self.width//8-1)
  113. self.send_command(0x45)
  114. self.send_data(0x00)
  115. self.send_data(0x00)
  116. self.send_data((self.height-1)%256)
  117. self.send_data((self.height-1)//256)
  118. self.send_command(0x4E)
  119. self.send_data(0x00)
  120. self.send_command(0x4F)
  121. self.send_data(0x00)
  122. self.send_data(0x00)
  123. self.ReadBusy()
  124. else:
  125. self.flag = 0
  126. self.send_command(0x04) # POWER_ON
  127. self.ReadBusy()
  128. self.send_command(0x00) # panel setting
  129. self.send_data(0x0f)
  130. return 0
  131. def getbuffer(self, image):
  132. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  133. buf = [0xFF] * (int(self.width/8) * self.height)
  134. image_monocolor = image.convert('1')
  135. imwidth, imheight = image_monocolor.size
  136. pixels = image_monocolor.load()
  137. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  138. if(imwidth == self.width and imheight == self.height):
  139. logger.debug("Horizontal")
  140. for y in range(imheight):
  141. for x in range(imwidth):
  142. # Set the bits for the column of pixels at the current position.
  143. if pixels[x, y] == 0:
  144. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  145. elif(imwidth == self.height and imheight == self.width):
  146. logger.debug("Vertical")
  147. for y in range(imheight):
  148. for x in range(imwidth):
  149. newx = y
  150. newy = self.height - x - 1
  151. if pixels[x, y] == 0:
  152. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  153. return buf
  154. def display(self, imageblack, imagered):
  155. high = self.height
  156. if( self.width % 8 == 0) :
  157. wide = self.width // 8
  158. else :
  159. wide = self.width // 8 + 1
  160. if(self.flag == 1):
  161. self.send_command(0x24)
  162. for j in range(0, high):
  163. for i in range(0, wide):
  164. self.send_data(imageblack[i + j * wide])
  165. self.send_command(0x26)
  166. for j in range(0, high):
  167. for i in range(0, wide):
  168. self.send_data(~imagered[i + j * wide])
  169. else:
  170. self.send_command(0x10)
  171. for j in range(0, high):
  172. for i in range(0, wide):
  173. self.send_data(imageblack[i + j * wide])
  174. self.send_command(0x13)
  175. for j in range(0, high):
  176. for i in range(0, wide):
  177. self.send_data(imagered[i + j * wide])
  178. self.TurnOnDisplay()
  179. def Clear(self):
  180. high = self.height
  181. if( self.width % 8 == 0) :
  182. wide = self.width // 8
  183. else :
  184. wide = self.width // 8 + 1
  185. if(self.flag == 1):
  186. self.send_command(0x24)
  187. for j in range(0, high):
  188. for i in range(0, wide):
  189. self.send_data(0xff)
  190. self.send_command(0x26)
  191. for j in range(0, high):
  192. for i in range(0, wide):
  193. self.send_data(0x00)
  194. else:
  195. self.send_command(0x10)
  196. for j in range(0, high):
  197. for i in range(0, wide):
  198. self.send_data(0xff)
  199. self.send_command(0x13)
  200. for j in range(0, high):
  201. for i in range(0, wide):
  202. self.send_data(0xff)
  203. self.TurnOnDisplay()
  204. def sleep(self):
  205. if(self.flag == 1):
  206. self.send_command(0X10)
  207. self.send_data(0x03)
  208. else:
  209. self.send_command(0X50)
  210. self.send_data(0xf7)
  211. self.send_command(0X02)
  212. self.ReadBusy()
  213. self.send_command(0X07)
  214. self.send_data(0xA5)
  215. epdconfig.delay_ms(2000)
  216. epdconfig.module_exit()
  217. ### END OF FILE ###