epd4in01f.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. # *****************************************************************************
  4. # * | File : epd4in01f.py
  5. # * | Author : Waveshare team
  6. # * | Function : Electronic paper driver
  7. # * | Info :
  8. # *----------------
  9. # * | This version: V1.1
  10. # * | Date : 2022-08-10
  11. # # | Info : python demo
  12. # -----------------------------------------------------------------------------
  13. # Permission is hereby granted, free of charge, to any person obtaining a copy
  14. # of this software and associated documnetation files (the "Software"), to deal
  15. # in the Software without restriction, including without limitation the rights
  16. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. # copies of the Software, and to permit persons to whom the Software is
  18. # furished to do so, subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included in
  21. # all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. # THE SOFTWARE.
  30. #
  31. import logging
  32. from . import epdconfig
  33. # Display resolution
  34. EPD_WIDTH = 640
  35. EPD_HEIGHT = 400
  36. logger = logging.getLogger(__name__)
  37. class EPD:
  38. def __init__(self):
  39. self.reset_pin = epdconfig.RST_PIN
  40. self.dc_pin = epdconfig.DC_PIN
  41. self.busy_pin = epdconfig.BUSY_PIN
  42. self.cs_pin = epdconfig.CS_PIN
  43. self.width = EPD_WIDTH
  44. self.height = EPD_HEIGHT
  45. self.BLACK = 0x000000 # 0000 BGR
  46. self.WHITE = 0xffffff # 0001
  47. self.GREEN = 0x00ff00 # 0010
  48. self.BLUE = 0xff0000 # 0011
  49. self.RED = 0x0000ff # 0100
  50. self.YELLOW = 0x00ffff # 0101
  51. self.ORANGE = 0x0080ff # 0110
  52. # Hardware reset
  53. def reset(self):
  54. epdconfig.digital_write(self.reset_pin, 1)
  55. epdconfig.delay_ms(200)
  56. epdconfig.digital_write(self.reset_pin, 0)
  57. epdconfig.delay_ms(1)
  58. epdconfig.digital_write(self.reset_pin, 1)
  59. epdconfig.delay_ms(200)
  60. def send_command(self, command):
  61. epdconfig.digital_write(self.dc_pin, 0)
  62. epdconfig.digital_write(self.cs_pin, 0)
  63. epdconfig.spi_writebyte([command])
  64. epdconfig.digital_write(self.cs_pin, 1)
  65. def send_data(self, data):
  66. epdconfig.digital_write(self.dc_pin, 1)
  67. epdconfig.digital_write(self.cs_pin, 0)
  68. epdconfig.spi_writebyte([data])
  69. epdconfig.digital_write(self.cs_pin, 1)
  70. # send a lot of data
  71. def send_data2(self, data):
  72. epdconfig.digital_write(self.dc_pin, 1)
  73. epdconfig.digital_write(self.cs_pin, 0)
  74. epdconfig.spi_writebyte2(data)
  75. epdconfig.digital_write(self.cs_pin, 1)
  76. def ReadBusyHigh(self):
  77. logger.debug("e-Paper busy")
  78. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  79. epdconfig.delay_ms(10)
  80. logger.debug("e-Paper busy release")
  81. def ReadBusyLow(self):
  82. logger.debug("e-Paper busy")
  83. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  84. epdconfig.delay_ms(10)
  85. logger.debug("e-Paper busy release")
  86. def init(self):
  87. if (epdconfig.module_init() != 0):
  88. return -1
  89. # EPD hardware init start
  90. self.reset()
  91. self.ReadBusyHigh()
  92. self.send_command(0x00)
  93. self.send_data(0x2f)
  94. self.send_data(0x00)
  95. self.send_command(0x01)
  96. self.send_data(0x37)
  97. self.send_data(0x00)
  98. self.send_data(0x05)
  99. self.send_data(0x05)
  100. self.send_command(0x03)
  101. self.send_data(0x00)
  102. self.send_command(0x06)
  103. self.send_data(0xC7)
  104. self.send_data(0xC7)
  105. self.send_data(0x1D)
  106. self.send_command(0x41)
  107. self.send_data(0x00)
  108. self.send_command(0x50)
  109. self.send_data(0x37)
  110. self.send_command(0x60)
  111. self.send_data(0x22)
  112. self.send_command(0x61)
  113. self.send_data(0x02)
  114. self.send_data(0x80)
  115. self.send_data(0x01)
  116. self.send_data(0x90)
  117. self.send_command(0xE3)
  118. self.send_data(0xAA)
  119. # EPD hardware init end
  120. return 0
  121. def getbuffer(self, image):
  122. buf = [0x00] * int(self.width * self.height / 2)
  123. image_monocolor = image.convert('RGB')#Picture mode conversion
  124. imwidth, imheight = image_monocolor.size
  125. pixels = image_monocolor.load()
  126. logger.debug('imwidth = %d imheight = %d ',imwidth, imheight)
  127. if(imwidth == self.width and imheight == self.height):
  128. for y in range(imheight):
  129. for x in range(imwidth):
  130. # Set the bits for the column of pixels at the current position.
  131. Add = int((x + y * self.width) / 2)
  132. Color = 0;
  133. if (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  134. Color = 0
  135. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 255):
  136. Color = 1
  137. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  138. Color = 2
  139. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 255):
  140. Color = 3
  141. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  142. Color = 4
  143. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  144. Color = 5
  145. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 128 and pixels[x, y][2] == 0):
  146. Color = 6
  147. data_t = buf[Add]&(~(0xF0 >> ((x % 2)*4)))
  148. buf[Add] = data_t | ((Color << 4) >> ((x % 2)*4));
  149. elif(imwidth == self.height and imheight == self.width):
  150. for y in range(imheight):
  151. for x in range(imwidth):
  152. newx = y
  153. newy = self.height - x - 1
  154. Add = int((newx + newy*self.width) / 2)
  155. Color = 0;
  156. if (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  157. Color = 0
  158. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 255):
  159. Color = 1
  160. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  161. Color = 2
  162. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 255):
  163. Color = 3
  164. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  165. Color = 4
  166. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  167. Color = 5
  168. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 128 and pixels[x, y][2] == 0):
  169. Color = 6
  170. data_t = buf[Add]&(~(0xF0 >> ((newx % 2)*4)))
  171. buf[Add] = data_t | ((Color << 4) >> ((newx % 2)*4));
  172. return buf
  173. def display(self,image):
  174. self.send_command(0x61)#Set Resolution setting
  175. self.send_data(0x02)
  176. self.send_data(0x80)
  177. self.send_data(0x01)
  178. self.send_data(0x90)
  179. self.send_command(0x10)
  180. self.send_data2(image)
  181. self.send_command(0x04)#0x04
  182. self.ReadBusyHigh()
  183. self.send_command(0x12)#0x12
  184. self.ReadBusyHigh()
  185. self.send_command(0x02) #0x02
  186. self.ReadBusyLow()
  187. # epdconfig.delay_ms(500)
  188. def Clear(self):
  189. self.send_command(0x61)#Set Resolution setting
  190. self.send_data(0x02)
  191. self.send_data(0x80)
  192. self.send_data(0x01)
  193. self.send_data(0x90)
  194. self.send_command(0x10)
  195. self.send_data2([0x11] * int(EPD_HEIGHT) * int(EPD_WIDTH/2))
  196. #BLACK 0x00 /// 0000
  197. #WHITE 0x11 /// 0001
  198. #GREEN 0x22 /// 0010
  199. #BLUE 0x33 /// 0011
  200. #RED 0x44 /// 0100
  201. #YELLOW 0x55 /// 0101
  202. #ORANGE 0x66 /// 0110
  203. #CLEAN 0x77 /// 0111 unavailable Afterimage
  204. self.send_command(0x04)#0x04
  205. self.ReadBusyHigh()
  206. self.send_command(0x12)#0x12
  207. self.ReadBusyHigh()
  208. self.send_command(0x02) #0x02
  209. self.ReadBusyLow()
  210. # epdconfig.delay_ms(500)
  211. def sleep(self):
  212. # epdconfig.delay_ms(500)
  213. self.send_command(0x07) # DEEP_SLEEP
  214. self.send_data(0XA5)
  215. epdconfig.delay_ms(2000)
  216. epdconfig.module_exit()