epd2in13g.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # *****************************************************************************
  2. # * | File : epd2in13g.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2023-05-29
  9. # # | Info : python demo
  10. # -----------------------------------------------------------------------------
  11. # ******************************************************************************/
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. import logging
  31. from . import epdconfig
  32. import PIL
  33. from PIL import Image
  34. import io
  35. # Display resolution
  36. EPD_WIDTH = 122
  37. EPD_HEIGHT = 250
  38. logger = logging.getLogger(__name__)
  39. class EPD:
  40. def __init__(self):
  41. self.reset_pin = epdconfig.RST_PIN
  42. self.dc_pin = epdconfig.DC_PIN
  43. self.busy_pin = epdconfig.BUSY_PIN
  44. self.cs_pin = epdconfig.CS_PIN
  45. self.width = EPD_WIDTH
  46. self.height = EPD_HEIGHT
  47. self.BLACK = 0x000000 # 00 BGR
  48. self.WHITE = 0xffffff # 01
  49. self.YELLOW = 0x00ffff # 10
  50. self.RED = 0x0000ff # 11
  51. self.Gate_BITS = EPD_HEIGHT
  52. if self.width < 128:
  53. self.Source_BITS = 128
  54. else:
  55. self.Source_BITS = self.width
  56. # Hardware reset
  57. def reset(self):
  58. epdconfig.digital_write(self.reset_pin, 1)
  59. epdconfig.delay_ms(200)
  60. epdconfig.digital_write(self.reset_pin, 0) # module reset
  61. epdconfig.delay_ms(2)
  62. epdconfig.digital_write(self.reset_pin, 1)
  63. epdconfig.delay_ms(200)
  64. def send_command(self, command):
  65. epdconfig.digital_write(self.dc_pin, 0)
  66. epdconfig.digital_write(self.cs_pin, 0)
  67. epdconfig.spi_writebyte([command])
  68. epdconfig.digital_write(self.cs_pin, 1)
  69. def send_data(self, data):
  70. epdconfig.digital_write(self.dc_pin, 1)
  71. epdconfig.digital_write(self.cs_pin, 0)
  72. epdconfig.spi_writebyte([data])
  73. epdconfig.digital_write(self.cs_pin, 1)
  74. def ReadBusy(self):
  75. logger.debug("e-Paper busy H")
  76. epdconfig.delay_ms(100)
  77. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  78. epdconfig.delay_ms(5)
  79. logger.debug("e-Paper busy release")
  80. def SetWindow(self):
  81. self.send_command(0x61) # SET_RAM_X_ADDRESS_START_END_POSITION
  82. # x point must be the multiple of 8 or the last 3 bits will be ignored
  83. self.send_data(int(self.Source_BITS/256))
  84. self.send_data(self.Source_BITS%256)
  85. self.send_data(int(self.Gate_BITS/256))
  86. self.send_data(self.Gate_BITS%256)
  87. def TurnOnDisplay(self):
  88. self.send_command(0x12) # DISPLAY_REFRESH
  89. self.send_data(0X00)
  90. self.ReadBusy()
  91. def init(self):
  92. if (epdconfig.module_init() != 0):
  93. return -1
  94. # EPD hardware init start
  95. self.reset()
  96. self.ReadBusy()
  97. self.send_command(0x4D)
  98. self.send_data(0x78)
  99. self.send_command(0x00)
  100. self.send_data(0x0F)
  101. self.send_data(0x29)
  102. self.send_command(0x01)
  103. self.send_data(0x07)
  104. self.send_data(0x00)
  105. self.send_command(0x03)
  106. self.send_data(0x10)
  107. self.send_data(0x54)
  108. self.send_data(0x44)
  109. self.send_command(0x06)
  110. self.send_data(0x05)
  111. self.send_data(0x00)
  112. self.send_data(0x3F)
  113. self.send_data(0x0A)
  114. self.send_data(0x25)
  115. self.send_data(0x12)
  116. self.send_data(0x1A)
  117. self.send_command(0x50)
  118. self.send_data(0x37)
  119. self.send_command(0x60)
  120. self.send_data(0x02)
  121. self.send_data(0x02)
  122. self.SetWindow()
  123. self.send_command(0xE7)
  124. self.send_data(0x1C)
  125. self.send_command(0xE3)
  126. self.send_data(0x22)
  127. self.send_command(0xB4)
  128. self.send_data(0xD0)
  129. self.send_command(0xB5)
  130. self.send_data(0x03)
  131. self.send_command(0xE9)
  132. self.send_data(0x01)
  133. self.send_command(0x30)
  134. self.send_data(0x08)
  135. self.send_command(0x04)
  136. self.ReadBusy()
  137. return 0
  138. def getbuffer(self, image):
  139. # Create a pallette with the 4 colors supported by the panel
  140. pal_image = Image.new("P", (1,1))
  141. pal_image.putpalette( (0,0,0, 255,255,255, 255,255,0, 255,0,0) + (0,0,0)*252)
  142. # Check if we need to rotate the image
  143. imwidth, imheight = image.size
  144. if(imwidth == self.width and imheight == self.height):
  145. image_temp = image
  146. elif(imwidth == self.height and imheight == self.width):
  147. image_temp = image.rotate(90, expand=True)
  148. else:
  149. logger.warning("Invalid image dimensions: %d x %d, expected %d x %d" % (imwidth, imheight, self.width, self.height))
  150. # Convert the soruce image to the 4 colors, dithering if needed
  151. image_4color = image_temp.convert("RGB").quantize(palette=pal_image)
  152. buf_4color = bytearray(image_4color.tobytes('raw'))
  153. # into a single byte to transfer to the panel
  154. if self.width % 4 == 0 :
  155. Width = self.width // 4
  156. else :
  157. Width = self.width // 4 + 1
  158. Height = self.height
  159. buf = [0x00] * int(Width * Height)
  160. idx = 0
  161. for j in range(0, Height):
  162. for i in range(0, Width):
  163. if i == Width -1:
  164. buf[i + j * Width] = (buf_4color[idx] << 6) + (buf_4color[idx+1] << 4)
  165. idx = idx + 2
  166. else:
  167. buf[i + j * Width] = (buf_4color[idx] << 6) + (buf_4color[idx+1] << 4) + (buf_4color[idx+2] << 2) + buf_4color[idx+3]
  168. idx = idx + 4
  169. return buf
  170. def display(self, image):
  171. if self.width % 4 == 0 :
  172. Width = self.width // 4
  173. else :
  174. Width = self.width // 4 + 1
  175. Height = self.height
  176. self.send_command(0x10)
  177. for j in range(0, Height):
  178. for i in range(0, self.Source_BITS//4):
  179. if i < 31 :
  180. self.send_data(image[i + j * Width])
  181. else :
  182. self.send_data(0x00)
  183. self.TurnOnDisplay()
  184. def Clear(self, color=0x55):
  185. Width = self.Source_BITS//4
  186. Height = self.height
  187. self.send_command(0x10)
  188. for j in range(0, Height):
  189. for i in range(0, Width):
  190. self.send_data(color)
  191. self.TurnOnDisplay()
  192. def sleep(self):
  193. self.send_command(0x02) # POWER_OFF
  194. self.ReadBusy()
  195. epdconfig.delay_ms(100)
  196. self.send_command(0x07) # DEEP_SLEEP
  197. self.send_data(0XA5)
  198. epdconfig.delay_ms(2000)
  199. epdconfig.module_exit()
  200. ### END OF FILE ###