epd2in13.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # //*****************************************************************************
  2. # * | File : epd2in13.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V3.1
  8. # * | Date : 2019-03-20
  9. # * | Info : python3 demo
  10. # * fix: TurnOnDisplay()
  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 epdconfig
  31. from PIL import Image
  32. import RPi.GPIO as GPIO
  33. # import numpy as np
  34. # Display resolution
  35. EPD_WIDTH = 122
  36. EPD_HEIGHT = 250
  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.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, GPIO.HIGH)
  59. epdconfig.delay_ms(200)
  60. epdconfig.digital_write(self.reset_pin, GPIO.LOW) # module reset
  61. epdconfig.delay_ms(200)
  62. epdconfig.digital_write(self.reset_pin, GPIO.HIGH)
  63. epdconfig.delay_ms(200)
  64. def send_command(self, command):
  65. epdconfig.digital_write(self.dc_pin, GPIO.LOW)
  66. epdconfig.spi_writebyte([command])
  67. def send_data(self, data):
  68. epdconfig.digital_write(self.dc_pin, GPIO.HIGH)
  69. epdconfig.spi_writebyte([data])
  70. def wait_until_idle(self):
  71. print("busy")
  72. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  73. epdconfig.delay_ms(100)
  74. print("free busy")
  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. self.wait_until_idle()
  81. def init(self, lut):
  82. if (epdconfig.module_init() != 0):
  83. return -1
  84. # EPD hardware init start
  85. self.reset()
  86. self.send_command(0x01) # DRIVER_OUTPUT_CONTROL
  87. self.send_data((EPD_HEIGHT - 1) & 0xFF)
  88. self.send_data(((EPD_HEIGHT - 1) >> 8) & 0xFF)
  89. self.send_data(0x00) # GD = 0 SM = 0 TB = 0
  90. self.send_command(0x0C) # BOOSTER_SOFT_START_CONTROL
  91. self.send_data(0xD7)
  92. self.send_data(0xD6)
  93. self.send_data(0x9D)
  94. self.send_command(0x2C) # WRITE_VCOM_REGISTER
  95. self.send_data(0xA8) # VCOM 7C
  96. self.send_command(0x3A) # SET_DUMMY_LINE_PERIOD
  97. self.send_data(0x1A) # 4 dummy lines per gate
  98. self.send_command(0x3B) # SET_GATE_TIME
  99. self.send_data(0x08) # 2us per line
  100. self.send_command(0X3C) # BORDER_WAVEFORM_CONTROL
  101. self.send_data(0x03)
  102. self.send_command(0X11) # DATA_ENTRY_MODE_SETTING
  103. self.send_data(0x03) # X increment; Y increment
  104. # WRITE_LUT_REGISTER
  105. self.send_command(0x32)
  106. for count in range(30):
  107. self.send_data(lut[count])
  108. return 0
  109. ##
  110. # @brief: specify the memory area for data R//W
  111. ##
  112. def SetWindows(self, x_start, y_start, x_end, y_end):
  113. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  114. self.send_data((x_start >> 3) & 0xFF)
  115. self.send_data((x_end >> 3) & 0xFF)
  116. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  117. self.send_data(y_start & 0xFF)
  118. self.send_data((y_start >> 8) & 0xFF)
  119. self.send_data(y_end & 0xFF)
  120. self.send_data((y_end >> 8) & 0xFF)
  121. ##
  122. # @brief: specify the start point for data R//W
  123. ##
  124. def SetCursor(self, x, y):
  125. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  126. # x point must be the multiple of 8 or the last 3 bits will be ignored
  127. self.send_data((x >> 3) & 0xFF)
  128. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  129. self.send_data(y & 0xFF)
  130. self.send_data((y >> 8) & 0xFF)
  131. self.wait_until_idle()
  132. def getbuffer(self, image):
  133. if self.width%8 == 0:
  134. linewidth = self.width//8
  135. else:
  136. linewidth = self.width//8 + 1
  137. buf = [0xFF] * (linewidth * self.height)
  138. image_monocolor = image.convert('1')
  139. imwidth, imheight = image_monocolor.size
  140. pixels = image_monocolor.load()
  141. if(imwidth == self.width and imheight == self.height):
  142. print("Vertical")
  143. for y in range(imheight):
  144. for x in range(imwidth):
  145. if pixels[x, y] == 0:
  146. # x = imwidth - x
  147. buf[x // 8 + y * linewidth] &= ~(0x80 >> (x % 8))
  148. elif(imwidth == self.height and imheight == self.width):
  149. print("Horizontal")
  150. for y in range(imheight):
  151. for x in range(imwidth):
  152. newx = y
  153. newy = self.height - x - 1
  154. if pixels[x, y] == 0:
  155. # newy = imwidth - newy - 1
  156. buf[newx // 8 + newy*linewidth] &= ~(0x80 >> (y % 8))
  157. return buf
  158. def display(self, image):
  159. if self.width%8 == 0:
  160. linewidth = self.width//8
  161. else:
  162. linewidth = self.width//8 + 1
  163. self.SetWindows(0, 0, EPD_WIDTH, EPD_HEIGHT);
  164. for j in range(0, self.height):
  165. self.SetCursor(0, j);
  166. self.send_command(0x24);
  167. for i in range(0, linewidth):
  168. self.send_data(image[i + j * linewidth])
  169. self.TurnOnDisplay()
  170. def Clear(self, color):
  171. if self.width%8 == 0:
  172. linewidth = self.width//8
  173. else:
  174. linewidth = self.width//8 + 1
  175. self.SetWindows(0, 0, EPD_WIDTH, EPD_HEIGHT);
  176. for j in range(0, self.height):
  177. self.SetCursor(0, j);
  178. self.send_command(0x24);
  179. for i in range(0, linewidth):
  180. self.send_data(color)
  181. self.TurnOnDisplay()
  182. def sleep(self):
  183. self.send_command(0x10) #enter deep sleep
  184. # self.send_data(0x01)
  185. epdconfig.delay_ms(100)
  186. ### END OF FILE ###