epd2in13.py 7.8 KB

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