epd2in9b_V3.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # *****************************************************************************
  2. # * | File : epd2in9b_V3.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.1
  8. # * | Date : 2020-12-03
  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 = 128
  33. EPD_HEIGHT = 296
  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. def send_command(self, command):
  51. epdconfig.digital_write(self.dc_pin, 0)
  52. epdconfig.digital_write(self.cs_pin, 0)
  53. epdconfig.spi_writebyte([command])
  54. epdconfig.digital_write(self.cs_pin, 1)
  55. def send_data(self, data):
  56. epdconfig.digital_write(self.dc_pin, 1)
  57. epdconfig.digital_write(self.cs_pin, 0)
  58. epdconfig.spi_writebyte([data])
  59. epdconfig.digital_write(self.cs_pin, 1)
  60. def ReadBusy(self):
  61. logging.debug("e-Paper busy")
  62. self.send_command(0X71)
  63. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  64. self.send_command(0X71)
  65. epdconfig.delay_ms(200)
  66. logging.debug("e-Paper busy release")
  67. def init(self):
  68. if (epdconfig.module_init() != 0):
  69. return -1
  70. # EPD hardware init start
  71. self.reset()
  72. self.send_command(0x04)
  73. self.ReadBusy()#waiting for the electronic paper IC to release the idle signal
  74. self.send_command(0x00) #panel setting
  75. self.send_data(0x0f) #LUT from OTP,128x296
  76. self.send_data(0x89) #Temperature sensor, boost and other related timing settings
  77. self.send_command(0x61) #resolution setting
  78. self.send_data (0x80)
  79. self.send_data (0x01)
  80. self.send_data (0x28)
  81. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  82. self.send_data(0x77) #WBmode:VBDF 17|D7 VBDW 97 VBDB 57
  83. # WBRmode:VBDF F7 VBDW 77 VBDB 37 VBDR B7
  84. return 0
  85. def getbuffer(self, image):
  86. # logging.debug("bufsiz = ",int(self.width/8) * self.height)
  87. buf = [0xFF] * (int(self.width/8) * self.height)
  88. image_monocolor = image.convert('1')
  89. imwidth, imheight = image_monocolor.size
  90. pixels = image_monocolor.load()
  91. # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  92. if(imwidth == self.width and imheight == self.height):
  93. logging.debug("Vertical")
  94. for y in range(imheight):
  95. for x in range(imwidth):
  96. # Set the bits for the column of pixels at the current position.
  97. if pixels[x, y] == 0:
  98. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  99. elif(imwidth == self.height and imheight == self.width):
  100. logging.debug("Horizontal")
  101. for y in range(imheight):
  102. for x in range(imwidth):
  103. newx = y
  104. newy = self.height - x - 1
  105. if pixels[x, y] == 0:
  106. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  107. return buf
  108. def display(self, blackimage, ryimage): # ryimage: red or yellow image
  109. if (blackimage != None):
  110. self.send_command(0X10)
  111. for i in range(0, int(self.width * self.height / 8)):
  112. self.send_data(blackimage[i])
  113. if (ryimage != None):
  114. self.send_command(0X13)
  115. for i in range(0, int(self.width * self.height / 8)):
  116. self.send_data(ryimage[i])
  117. self.send_command(0x12)
  118. epdconfig.delay_ms(200)
  119. self.ReadBusy()
  120. def Clear(self):
  121. self.send_command(0X10)
  122. for i in range(0, int(self.width * self.height / 8)):
  123. self.send_data(0xff)
  124. self.send_command(0X13)
  125. for i in range(0, int(self.width * self.height / 8)):
  126. self.send_data(0xff)
  127. self.send_command(0x12)
  128. epdconfig.delay_ms(200)
  129. self.ReadBusy()
  130. def sleep(self):
  131. self.send_command(0X02) # power off
  132. self.ReadBusy()
  133. self.send_command(0X07) # deep sleep
  134. self.send_data(0xA5)
  135. def Dev_exit(self):
  136. epdconfig.module_exit()
  137. ### END OF FILE ###