epd4in01f.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.0
  10. # * | Date : 2020-11-06
  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. def ReadBusyHigh(self):
  71. logger.debug("e-Paper busy")
  72. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  73. epdconfig.delay_ms(10)
  74. logger.debug("e-Paper busy release")
  75. def ReadBusyLow(self):
  76. logger.debug("e-Paper busy")
  77. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  78. epdconfig.delay_ms(10)
  79. logger.debug("e-Paper busy release")
  80. def init(self):
  81. if (epdconfig.module_init() != 0):
  82. return -1
  83. # EPD hardware init start
  84. self.reset()
  85. self.ReadBusyHigh()
  86. self.send_command(0x00);
  87. self.send_data(0x2f);
  88. self.send_data(0x00);
  89. self.send_command(0x01);
  90. self.send_data(0x37);
  91. self.send_data(0x00);
  92. self.send_data(0x05);
  93. self.send_data(0x05);
  94. self.send_command(0x03);
  95. self.send_data(0x00);
  96. self.send_command(0x06);
  97. self.send_data(0xC7);
  98. self.send_data(0xC7);
  99. self.send_data(0x1D);
  100. self.send_command(0x41);
  101. self.send_data(0x00);
  102. self.send_command(0x50);
  103. self.send_data(0x37);
  104. self.send_command(0x60);
  105. self.send_data(0x22);
  106. self.send_command(0x61);
  107. self.send_data(0x02);
  108. self.send_data(0x80);
  109. self.send_data(0x01);
  110. self.send_data(0x90);
  111. self.send_command(0xE3);
  112. self.send_data(0xAA);
  113. # EPD hardware init end
  114. return 0
  115. def getbuffer(self, image):
  116. buf = [0x00] * int(self.width * self.height / 2)
  117. image_monocolor = image.convert('RGB')#Picture mode conversion
  118. imwidth, imheight = image_monocolor.size
  119. pixels = image_monocolor.load()
  120. logger.debug('imwidth = %d imheight = %d ',imwidth, imheight)
  121. if(imwidth == self.width and imheight == self.height):
  122. for y in range(imheight):
  123. for x in range(imwidth):
  124. # Set the bits for the column of pixels at the current position.
  125. Add = int((x + y * self.width) / 2)
  126. Color = 0;
  127. if (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  128. Color = 0
  129. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 255):
  130. Color = 1
  131. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  132. Color = 2
  133. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 255):
  134. Color = 3
  135. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  136. Color = 4
  137. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  138. Color = 5
  139. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 128 and pixels[x, y][2] == 0):
  140. Color = 6
  141. data_t = buf[Add]&(~(0xF0 >> ((x % 2)*4)))
  142. buf[Add] = data_t | ((Color << 4) >> ((x % 2)*4));
  143. elif(imwidth == self.height and imheight == self.width):
  144. for y in range(imheight):
  145. for x in range(imwidth):
  146. newx = y
  147. newy = self.height - x - 1
  148. Add = int((newx + newy*self.width) / 2)
  149. Color = 0;
  150. if (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  151. Color = 0
  152. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 255):
  153. Color = 1
  154. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  155. Color = 2
  156. elif (pixels[x, y][0] == 0 and pixels[x, y][1] == 0 and pixels[x, y][2] == 255):
  157. Color = 3
  158. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 0 and pixels[x, y][2] == 0):
  159. Color = 4
  160. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 255 and pixels[x, y][2] == 0):
  161. Color = 5
  162. elif (pixels[x, y][0] == 255 and pixels[x, y][1] == 128 and pixels[x, y][2] == 0):
  163. Color = 6
  164. data_t = buf[Add]&(~(0xF0 >> ((newx % 2)*4)))
  165. buf[Add] = data_t | ((Color << 4) >> ((newx % 2)*4));
  166. return buf
  167. def display(self,image):
  168. self.send_command(0x61)#Set Resolution setting
  169. self.send_data(0x02)
  170. self.send_data(0x80)
  171. self.send_data(0x01)
  172. self.send_data(0x90)
  173. self.send_command(0x10)
  174. for i in range(0, int(EPD_HEIGHT)):
  175. for j in range(0, int(EPD_WIDTH/2)):
  176. self.send_data((image[j+(int(EPD_WIDTH/2)*i)]))
  177. self.send_command(0x04)#0x04
  178. self.ReadBusyHigh()
  179. self.send_command(0x12)#0x12
  180. self.ReadBusyHigh()
  181. self.send_command(0x02) #0x02
  182. self.ReadBusyLow()
  183. # epdconfig.delay_ms(500)
  184. def Clear(self):
  185. self.send_command(0x61)#Set Resolution setting
  186. self.send_data(0x02)
  187. self.send_data(0x80)
  188. self.send_data(0x01)
  189. self.send_data(0x90)
  190. self.send_command(0x10)
  191. for i in range(0, int(EPD_HEIGHT)):
  192. for j in range(0, int(EPD_WIDTH/2)):
  193. self.send_data(0x11)
  194. #BLACK 0x00 /// 0000
  195. #WHITE 0x11 /// 0001
  196. #GREEN 0x22 /// 0010
  197. #BLUE 0x33 /// 0011
  198. #RED 0x44 /// 0100
  199. #YELLOW 0x55 /// 0101
  200. #ORANGE 0x66 /// 0110
  201. #CLEAN 0x77 /// 0111 unavailable Afterimage
  202. self.send_command(0x04)#0x04
  203. self.ReadBusyHigh()
  204. self.send_command(0x12)#0x12
  205. self.ReadBusyHigh()
  206. self.send_command(0x02) #0x02
  207. self.ReadBusyLow()
  208. # epdconfig.delay_ms(500)
  209. def sleep(self):
  210. # epdconfig.delay_ms(500)
  211. self.send_command(0x07) # DEEP_SLEEP
  212. self.send_data(0XA5)
  213. epdconfig.delay_ms(2000)
  214. epdconfig.module_exit()