epd2in13b_V4.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # *****************************************************************************
  2. # * | File : epd2in13b_V4.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2022-04-21
  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. # Display resolution
  32. EPD_WIDTH = 122
  33. EPD_HEIGHT = 250
  34. logger = logging.getLogger(__name__)
  35. class EPD:
  36. def __init__(self):
  37. self.reset_pin = epdconfig.RST_PIN
  38. self.dc_pin = epdconfig.DC_PIN
  39. self.busy_pin = epdconfig.BUSY_PIN
  40. self.cs_pin = epdconfig.CS_PIN
  41. self.width = EPD_WIDTH
  42. self.height = EPD_HEIGHT
  43. # hardware reset
  44. def reset(self):
  45. epdconfig.digital_write(self.reset_pin, 1)
  46. epdconfig.delay_ms(20)
  47. epdconfig.digital_write(self.reset_pin, 0)
  48. epdconfig.delay_ms(2)
  49. epdconfig.digital_write(self.reset_pin, 1)
  50. epdconfig.delay_ms(20)
  51. # send 1 byte command
  52. def send_command(self, command):
  53. epdconfig.digital_write(self.dc_pin, 0)
  54. epdconfig.digital_write(self.cs_pin, 0)
  55. epdconfig.spi_writebyte([command])
  56. epdconfig.digital_write(self.cs_pin, 1)
  57. # send 1 byte data
  58. def send_data(self, data):
  59. epdconfig.digital_write(self.dc_pin, 1)
  60. epdconfig.digital_write(self.cs_pin, 0)
  61. epdconfig.spi_writebyte([data])
  62. epdconfig.digital_write(self.cs_pin, 1)
  63. # send a lot of data
  64. def send_data2(self, data):
  65. epdconfig.digital_write(self.dc_pin, 1)
  66. epdconfig.digital_write(self.cs_pin, 0)
  67. epdconfig.spi_writebyte2(data)
  68. epdconfig.digital_write(self.cs_pin, 1)
  69. # judge e-Paper whether is busy
  70. def busy(self):
  71. logger.debug("e-Paper busy")
  72. while(epdconfig.digital_read(self.busy_pin) != 0):
  73. epdconfig.delay_ms(10)
  74. logger.debug("e-Paper busy release")
  75. # set the display window
  76. def set_windows(self, xstart, ystart, xend, yend):
  77. self.send_command(0x44) # SET_RAM_X_ADDRESS_START_END_POSITION
  78. self.send_data((xstart>>3) & 0xff)
  79. self.send_data((xend>>3) & 0xff)
  80. self.send_command(0x45) # SET_RAM_Y_ADDRESS_START_END_POSITION
  81. self.send_data(ystart & 0xff)
  82. self.send_data((ystart >> 8) & 0xff)
  83. self.send_data(yend & 0xff)
  84. self.send_data((yend >> 8) & 0xff)
  85. # set the display cursor(origin)
  86. def set_cursor(self, xstart, ystart):
  87. self.send_command(0x4E) # SET_RAM_X_ADDRESS_COUNTER
  88. self.send_data(xstart & 0xff)
  89. self.send_command(0x4F) # SET_RAM_Y_ADDRESS_COUNTER
  90. self.send_data(ystart & 0xff)
  91. self.send_data((ystart >> 8) & 0xff)
  92. # initialize
  93. def init(self):
  94. if (epdconfig.module_init() != 0):
  95. return -1
  96. self.reset()
  97. self.busy()
  98. self.send_command(0x12) # SWRESET
  99. self.busy()
  100. self.send_command(0x01) # Driver output control
  101. self.send_data(0xf9)
  102. self.send_data(0x00)
  103. self.send_data(0x00)
  104. self.send_command(0x11) # data entry mode
  105. self.send_data(0x03)
  106. self.set_windows(0, 0, self.width - 1, self.height - 1)
  107. self.set_cursor(0, 0)
  108. self.send_command(0x3C) # BorderWavefrom
  109. self.send_data(0x05)
  110. self.send_command(0x18) # Read built-in temperature sensor
  111. self.send_data(0x80)
  112. self.send_command(0x21) # Display update control
  113. self.send_data(0x80)
  114. self.send_data(0x80)
  115. self.busy()
  116. return 0
  117. # turn on display
  118. def ondisplay(self):
  119. self.send_command(0x20)
  120. self.busy()
  121. # image converted to bytearray
  122. def getbuffer(self, image):
  123. img = image
  124. imwidth, imheight = img.size
  125. if(imwidth == self.width and imheight == self.height):
  126. img = img.convert('1')
  127. elif(imwidth == self.height and imheight == self.width):
  128. # image has correct dimensions, but needs to be rotated
  129. img = img.rotate(90, expand=True).convert('1')
  130. else:
  131. logger.warning("Wrong image dimensions: must be " + str(self.width) + "x" + str(self.height))
  132. # return a blank buffer
  133. return [0x00] * (int(self.width/8) * self.height)
  134. buf = bytearray(img.tobytes('raw'))
  135. return buf
  136. # display image
  137. def display(self, imageblack, imagered):
  138. self.send_command(0x24)
  139. self.send_data2(imageblack)
  140. self.send_command(0x26)
  141. self.send_data2(imagered)
  142. self.ondisplay()
  143. # display white image
  144. def clear(self):
  145. if self.width%8 == 0:
  146. linewidth = int(self.width/8)
  147. else:
  148. linewidth = int(self.width/8) + 1
  149. buf = [0xff] * (int(linewidth * self.height))
  150. self.send_command(0x24)
  151. self.send_data2(buf)
  152. self.send_command(0x26)
  153. self.send_data2(buf)
  154. self.ondisplay()
  155. # Compatible with older version functions
  156. def Clear(self):
  157. self.clear()
  158. # sleep
  159. def sleep(self):
  160. self.send_command(0x10) # DEEP_SLEEP
  161. self.send_data(0x01) # check code
  162. epdconfig.delay_ms(2000)
  163. epdconfig.module_exit()
  164. ### END OF FILE ###