epd4in01f.py 9.2 KB

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