epd2in13.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # *****************************************************************************
  2. # * | File : epd2in13.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V4.0
  8. # * | Date : 2019-06-20
  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. import numpy as np
  32. # Display resolution
  33. EPD_WIDTH = 122
  34. EPD_HEIGHT = 250
  35. logger = logging.getLogger(__name__)
  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. lut_full_update = [
  45. 0x22, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x11,
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47. 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E, 0x1E,
  48. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  49. ]
  50. lut_partial_update = [
  51. 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x0F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  55. ]
  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)
  61. epdconfig.delay_ms(5)
  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. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  76. epdconfig.delay_ms(100)
  77. def TurnOnDisplay(self):
  78. self.send_command(0x22) # DISPLAY_UPDATE_CONTROL_2
  79. self.send_data(0xC4)
  80. self.send_command(0x20) # MASTER_ACTIVATION
  81. self.send_command(0xFF) # TERMINATE_FRAME_READ_WRITE
  82. logger.debug("e-Paper busy")
  83. self.ReadBusy()
  84. logger.debug("e-Paper busy release")
  85. def init(self, lut):
  86. if (epdconfig.module_init() != 0):
  87. return -1
  88. # EPD hardware init start
  89. self.reset()
  90. self.send_command(0x01) # DRIVER_OUTPUT_CONTROL
  91. self.send_data((EPD_HEIGHT - 1) & 0xFF)
  92. self.send_data(((EPD_HEIGHT - 1) >> 8) & 0xFF)
  93. self.send_data(0x00) # GD = 0 SM = 0 TB = 0
  94. self.send_command(0x0C) # BOOSTER_SOFT_START_CONTROL
  95. self.send_data(0xD7)
  96. self.send_data(0xD6)
  97. self.send_data(0x9D)
  98. self.send_command(0x2C) # WRITE_VCOM_REGISTER
  99. self.send_data(0xA8) # VCOM 7C
  100. self.send_command(0x3A) # SET_DUMMY_LINE_PERIOD
  101. self.send_data(0x1A) # 4 dummy lines per gate
  102. self.send_command(0x3B) # SET_GATE_TIME
  103. self.send_data(0x08) # 2us per line
  104. self.send_command(0X3C) # BORDER_WAVEFORM_CONTROL
  105. self.send_data(0x03)
  106. self.send_command(0X11) # DATA_ENTRY_MODE_SETTING
  107. self.send_data(0x03) # X increment; Y increment
  108. # WRITE_LUT_REGISTER
  109. self.send_command(0x32)
  110. for count in range(30):
  111. self.send_data(lut[count])
  112. return 0
  113. ##
  114. # @brief: specify the memory area for data R/W
  115. ##
  116. def SetWindows(self, x_start, y_start, x_end, y_end):
  117. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  118. self.send_data((x_start >> 3) & 0xFF)
  119. self.send_data((x_end >> 3) & 0xFF)
  120. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  121. self.send_data(y_start & 0xFF)
  122. self.send_data((y_start >> 8) & 0xFF)
  123. self.send_data(y_end & 0xFF)
  124. self.send_data((y_end >> 8) & 0xFF)
  125. ##
  126. # @brief: specify the start point for data R/W
  127. ##
  128. def SetCursor(self, x, y):
  129. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  130. # x point must be the multiple of 8 or the last 3 bits will be ignored
  131. self.send_data((x >> 3) & 0xFF)
  132. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  133. self.send_data(y & 0xFF)
  134. self.send_data((y >> 8) & 0xFF)
  135. self.ReadBusy()
  136. def getbuffer(self, image):
  137. if self.width%8 == 0:
  138. linewidth = int(self.width/8)
  139. else:
  140. linewidth = int(self.width/8) + 1
  141. buf = [0xFF] * (linewidth * self.height)
  142. image_monocolor = image.convert('1')
  143. imwidth, imheight = image_monocolor.size
  144. pixels = image_monocolor.load()
  145. if(imwidth == self.width and imheight == self.height):
  146. logger.debug("Vertical")
  147. for y in range(imheight):
  148. for x in range(imwidth):
  149. if pixels[x, y] == 0:
  150. # x = imwidth - x
  151. buf[int(x / 8) + y * linewidth] &= ~(0x80 >> (x % 8))
  152. elif(imwidth == self.height and imheight == self.width):
  153. logger.debug("Horizontal")
  154. for y in range(imheight):
  155. for x in range(imwidth):
  156. newx = y
  157. newy = self.height - x - 1
  158. if pixels[x, y] == 0:
  159. # newy = imwidth - newy - 1
  160. buf[int(newx / 8) + newy*linewidth] &= ~(0x80 >> (y % 8))
  161. return buf
  162. def display(self, image):
  163. if self.width%8 == 0:
  164. linewidth = int(self.width/8)
  165. else:
  166. linewidth = int(self.width/8) + 1
  167. self.SetWindows(0, 0, self.width, self.height);
  168. for j in range(0, self.height):
  169. self.SetCursor(0, j);
  170. self.send_command(0x24);
  171. for i in range(0, linewidth):
  172. self.send_data(image[i + j * linewidth])
  173. self.TurnOnDisplay()
  174. def Clear(self, color):
  175. if self.width%8 == 0:
  176. linewidth = int(self.width/8)
  177. else:
  178. linewidth = int(self.width/8) + 1
  179. self.SetWindows(0, 0, self.width, self.height);
  180. for j in range(0, self.height):
  181. self.SetCursor(0, j);
  182. self.send_command(0x24);
  183. for i in range(0, linewidth):
  184. self.send_data(color)
  185. self.TurnOnDisplay()
  186. def sleep(self):
  187. self.send_command(0x10) #enter deep sleep
  188. self.send_data(0x01)
  189. epdconfig.delay_ms(100)
  190. epdconfig.delay_ms(2000)
  191. epdconfig.module_exit()
  192. ### END OF FILE ###