epd2in7b_V2.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # *****************************************************************************
  2. # * | File : epd2in7b_V2.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.0
  8. # * | Date : 2020-10-22
  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 = 176
  33. EPD_HEIGHT = 264
  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(200)
  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(200)
  51. # Send 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 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. # Read Busy
  64. def ReadBusy(self):
  65. logger.debug("e-Paper busy")
  66. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  67. epdconfig.delay_ms(10)
  68. logger.debug("e-Paper busy release")
  69. # Setting the display window
  70. def SetWindows(self, Xstart, Ystart, Xend, Yend):
  71. self.send_command(0x44)
  72. self.send_data((Xstart >> 3) & 0xff)
  73. self.send_data((Xend >> 3) & 0xff)
  74. self.send_command(0x45)
  75. self.send_data(Ystart & 0xff)
  76. self.send_data((Ystart >> 8) & 0xff)
  77. self.send_data(Yend & 0xff)
  78. self.send_data((Yend >> 8) & 0xff)
  79. # Set Cursor
  80. def SetCursor(self, Xstart, Ystart):
  81. self.send_command(0x4E)
  82. self.send_data(Xstart & 0xff)
  83. self.send_command(0x4F)
  84. self.send_data(Ystart & 0xff)
  85. self.send_data((Ystart >> 8) & 0xff)
  86. # Initialize the e-Paper register
  87. def init(self):
  88. if (epdconfig.module_init() != 0):
  89. return -1
  90. self.reset()
  91. self.ReadBusy()
  92. self.send_command(0x12)
  93. self.ReadBusy()
  94. self.send_command(0x00)
  95. self.send_data(0x27)
  96. self.send_data(0x01)
  97. self.send_data(0x00)
  98. self.send_command(0x11)
  99. self.send_data(0x03)
  100. self.SetWindows(0, 0, self.width-1, self.height-1)
  101. self.SetCursor(0, 0)
  102. return 0
  103. def getbuffer(self, image):
  104. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  105. buf = [0xFF] * (int(self.width/8) * self.height)
  106. image_monocolor = image.convert('1')
  107. imwidth, imheight = image_monocolor.size
  108. pixels = image_monocolor.load()
  109. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  110. if(imwidth == self.width and imheight == self.height):
  111. logger.debug("Vertical")
  112. for y in range(imheight):
  113. for x in range(imwidth):
  114. # Set the bits for the column of pixels at the current position.
  115. if pixels[x, y] == 0:
  116. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  117. elif(imwidth == self.height and imheight == self.width):
  118. logger.debug("Horizontal")
  119. for y in range(imheight):
  120. for x in range(imwidth):
  121. newx = y
  122. newy = self.height - x - 1
  123. if pixels[x, y] == 0:
  124. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  125. return buf
  126. # Sends the image buffer in RAM to e-Paper and displays
  127. def display(self, imageblack, imagered):
  128. Width = self.width / 8
  129. Height = self.height
  130. self.send_command(0x24)
  131. for i in range(0, int(Width * Height)):
  132. self.send_data(imageblack[i])
  133. self.send_command(0x26)
  134. for i in range(0, int(Width * Height)):
  135. self.send_data(~imagered[i])
  136. self.TurnOnDisplay()
  137. # Clear the screen
  138. def Clear(self):
  139. self.send_command(0x24)
  140. for i in range(0, int(self.width * self.height / 8)):
  141. self.send_data(0xff)
  142. self.send_command(0x26)
  143. for i in range(0, int(self.width * self.height / 8)):
  144. self.send_data(0x00)
  145. self.TurnOnDisplay()
  146. # Turn on display
  147. def TurnOnDisplay(self):
  148. self.send_command(0x20)
  149. self.ReadBusy()
  150. # Enter sleep mode
  151. def sleep(self):
  152. self.send_command(0x10)
  153. self.send_data(0x01)
  154. epdconfig.delay_ms(2000)
  155. epdconfig.module_exit()
  156. ### END OF FILE ###