epd5in83_V2.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # *****************************************************************************
  2. # * | File : epd5in83_V2.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V1.1
  8. # * | Date : 2022-08-10
  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 = 648
  33. EPD_HEIGHT = 480
  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. 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. def send_data(self, data):
  57. epdconfig.digital_write(self.dc_pin, 1)
  58. epdconfig.digital_write(self.cs_pin, 0)
  59. epdconfig.spi_writebyte([data])
  60. epdconfig.digital_write(self.cs_pin, 1)
  61. # send a lot of data
  62. def send_data2(self, data):
  63. epdconfig.digital_write(self.dc_pin, 1)
  64. epdconfig.digital_write(self.cs_pin, 0)
  65. epdconfig.spi_writebyte2(data)
  66. epdconfig.digital_write(self.cs_pin, 1)
  67. def ReadBusy(self):
  68. logger.debug("e-Paper busy")
  69. while(epdconfig.digital_read(self.busy_pin) == 0):
  70. epdconfig.delay_ms(20)
  71. logger.debug("e-Paper busy release")
  72. def TurnOnDisplay(self):
  73. self.send_command(0x12); #POWER ON
  74. epdconfig.delay_ms(100)
  75. self.ReadBusy();
  76. def init(self):
  77. if (epdconfig.module_init() != 0):
  78. return -1
  79. # EPD hardware init start
  80. self.reset()
  81. self.send_command(0x01) #POWER SETTING
  82. self.send_data (0x07)
  83. self.send_data (0x07) #VGH=20V,VGL=-20V
  84. self.send_data (0x3f) #VDH=15V
  85. self.send_data (0x3f) #VDL=-15V
  86. self.send_command(0x04) #POWER ON
  87. epdconfig.delay_ms(100)
  88. self.ReadBusy() #waiting for the electronic paper IC to release the idle signal
  89. self.send_command(0X00) #PANNEL SETTING
  90. self.send_data(0x1F) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  91. self.send_command(0x61) #tres
  92. self.send_data (0x02) #source 648
  93. self.send_data (0x88)
  94. self.send_data (0x01) #gate 480
  95. self.send_data (0xE0)
  96. self.send_command(0X15)
  97. self.send_data(0x00)
  98. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  99. self.send_data(0x10)
  100. self.send_data(0x07)
  101. self.send_command(0X60) #TCON SETTING
  102. self.send_data(0x22)
  103. # EPD hardware init end
  104. return 0
  105. def getbuffer(self, image):
  106. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  107. buf = [0xFF] * (int(self.width/8) * self.height)
  108. image_monocolor = image.convert('1')
  109. imwidth, imheight = image_monocolor.size
  110. pixels = image_monocolor.load()
  111. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  112. if(imwidth == self.width and imheight == self.height):
  113. logger.debug("Vertical")
  114. for y in range(imheight):
  115. for x in range(imwidth):
  116. # Set the bits for the column of pixels at the current position.
  117. if pixels[x, y] == 0:
  118. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  119. elif(imwidth == self.height and imheight == self.width):
  120. logger.debug("Horizontal")
  121. for y in range(imheight):
  122. for x in range(imwidth):
  123. newx = y
  124. newy = self.height - x - 1
  125. if pixels[x, y] == 0:
  126. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  127. return buf
  128. def display(self, image):
  129. buf = [0x00] * int(self.width * self.height / 8)
  130. for i in range(0, int(self.width * self.height / 8)):
  131. buf[i] = ~image[i]
  132. self.send_command(0x10)
  133. self.send_data2([0x00] * int(self.width * self.height / 8))
  134. self.send_command(0x13)
  135. self.send_data2(buf)
  136. self.TurnOnDisplay()
  137. def Clear(self):
  138. self.send_command(0x10)
  139. self.send_data2([0x00] * int(self.width * self.height / 8))
  140. self.send_command(0x13)
  141. self.send_data2([0x00] * int(self.width * self.height / 8))
  142. self.TurnOnDisplay()
  143. def sleep(self):
  144. self.send_command(0x02) # POWER_OFF
  145. self.ReadBusy()
  146. self.send_command(0x07) # DEEP_SLEEP
  147. self.send_data(0XA5)
  148. epdconfig.delay_ms(2000)
  149. epdconfig.module_exit()
  150. ### END OF FILE ###