epd2in7b_V2.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. 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. # Hardware reset
  43. def reset(self):
  44. epdconfig.digital_write(self.reset_pin, 1)
  45. epdconfig.delay_ms(200)
  46. epdconfig.digital_write(self.reset_pin, 0)
  47. epdconfig.delay_ms(2)
  48. epdconfig.digital_write(self.reset_pin, 1)
  49. epdconfig.delay_ms(200)
  50. # Send Command
  51. def send_command(self, command):
  52. epdconfig.digital_write(self.dc_pin, 0)
  53. epdconfig.digital_write(self.cs_pin, 0)
  54. epdconfig.spi_writebyte([command])
  55. epdconfig.digital_write(self.cs_pin, 1)
  56. # Send Data
  57. def send_data(self, data):
  58. epdconfig.digital_write(self.dc_pin, 1)
  59. epdconfig.digital_write(self.cs_pin, 0)
  60. epdconfig.spi_writebyte([data])
  61. epdconfig.digital_write(self.cs_pin, 1)
  62. # Read Busy
  63. def ReadBusy(self):
  64. logging.debug("e-Paper busy")
  65. while(epdconfig.digital_read(self.busy_pin) == 1): # 0: idle, 1: busy
  66. epdconfig.delay_ms(10)
  67. logging.debug("e-Paper busy release")
  68. # Setting the display window
  69. def SetWindows(self, Xstart, Ystart, Xend, Yend):
  70. self.send_command(0x44)
  71. self.send_data((Xstart >> 3) & 0xff)
  72. self.send_data((Xend >> 3) & 0xff)
  73. self.send_command(0x45)
  74. self.send_data(Ystart & 0xff)
  75. self.send_data((Ystart >> 8) & 0xff)
  76. self.send_data(Yend & 0xff)
  77. self.send_data((Yend >> 8) & 0xff)
  78. # Set Cursor
  79. def SetCursor(self, Xstart, Ystart):
  80. self.send_command(0x4E)
  81. self.send_data(Xstart & 0xff)
  82. self.send_command(0x4F)
  83. self.send_data(Ystart & 0xff)
  84. self.send_data((Ystart >> 8) & 0xff)
  85. # Initialize the e-Paper register
  86. def init(self):
  87. if (epdconfig.module_init() != 0):
  88. return -1
  89. self.reset()
  90. self.ReadBusy()
  91. self.send_command(0x12)
  92. self.ReadBusy()
  93. self.send_command(0x00)
  94. self.send_data(0x27)
  95. self.send_data(0x01)
  96. self.send_data(0x00)
  97. self.send_command(0x11)
  98. self.send_data(0x03)
  99. self.SetWindows(0, 0, self.width-1, self.height-1)
  100. self.SetCursor(0, 0)
  101. return 0
  102. def getbuffer(self, image):
  103. # logging.debug("bufsiz = ",int(self.width/8) * self.height)
  104. buf = [0xFF] * (int(self.width/8) * self.height)
  105. image_monocolor = image.convert('1')
  106. imwidth, imheight = image_monocolor.size
  107. pixels = image_monocolor.load()
  108. # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  109. if(imwidth == self.width and imheight == self.height):
  110. logging.debug("Vertical")
  111. for y in range(imheight):
  112. for x in range(imwidth):
  113. # Set the bits for the column of pixels at the current position.
  114. if pixels[x, y] == 0:
  115. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  116. elif(imwidth == self.height and imheight == self.width):
  117. logging.debug("Horizontal")
  118. for y in range(imheight):
  119. for x in range(imwidth):
  120. newx = y
  121. newy = self.height - x - 1
  122. if pixels[x, y] == 0:
  123. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  124. return buf
  125. # Sends the image buffer in RAM to e-Paper and displays
  126. def display(self, imageblack, imagered):
  127. Width = self.width / 8
  128. Height = self.height
  129. self.send_command(0x24)
  130. for i in range(0, int(Width * Height)):
  131. self.send_data(imageblack[i])
  132. self.send_command(0x26)
  133. for i in range(0, int(Width * Height)):
  134. self.send_data(~imagered[i])
  135. self.TurnOnDisplay()
  136. # Clear the screen
  137. def Clear(self):
  138. self.send_command(0x24)
  139. for i in range(0, int(self.width * self.height / 8)):
  140. self.send_data(0xff)
  141. self.send_command(0x26)
  142. for i in range(0, int(self.width * self.height / 8)):
  143. self.send_data(0x00)
  144. self.TurnOnDisplay()
  145. # Turn on display
  146. def TurnOnDisplay(self):
  147. self.send_command(0x20)
  148. self.ReadBusy()
  149. # Enter sleep mode
  150. def sleep(self):
  151. self.send_command(0x10)
  152. self.send_data(0x01)
  153. def Dev_exit(self):
  154. epdconfig.module_exit()
  155. ### END OF FILE ###