epd1in54c.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # /*****************************************************************************
  2. # * | File : epd1in54c.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V4.0
  8. # * | Date : 2019-06-18
  9. # * | Info : python2 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 epdconfig
  30. # Display resolution
  31. EPD_WIDTH = 152
  32. EPD_HEIGHT = 152
  33. class EPD:
  34. def __init__(self):
  35. self.reset_pin = epdconfig.RST_PIN
  36. self.dc_pin = epdconfig.DC_PIN
  37. self.busy_pin = epdconfig.BUSY_PIN
  38. self.cs_pin = epdconfig.CS_PIN
  39. self.width = EPD_WIDTH
  40. self.height = EPD_HEIGHT
  41. # Hardware reset
  42. def reset(self):
  43. epdconfig.digital_write(self.reset_pin, 1)
  44. epdconfig.delay_ms(10)
  45. epdconfig.digital_write(self.reset_pin, 0)
  46. epdconfig.delay_ms(1)
  47. epdconfig.digital_write(self.reset_pin, 1)
  48. epdconfig.delay_ms(10)
  49. def send_command(self, command):
  50. epdconfig.digital_write(self.dc_pin, 0)
  51. epdconfig.digital_write(self.cs_pin, 0)
  52. epdconfig.spi_writebyte([command])
  53. epdconfig.digital_write(self.cs_pin, 1)
  54. def send_data(self, data):
  55. epdconfig.digital_write(self.dc_pin, 1)
  56. epdconfig.digital_write(self.cs_pin, 0)
  57. epdconfig.spi_writebyte([data])
  58. epdconfig.digital_write(self.cs_pin, 1)
  59. def ReadBusy(self):
  60. print("e-Paper busy")
  61. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  62. epdconfig.delay_ms(200)
  63. print("e-Paper busy release")
  64. def init(self):
  65. if (epdconfig.module_init() != 0):
  66. return -1
  67. # EPD hardware init start
  68. self.reset()
  69. self.send_command(0x06) # boost soft start
  70. self.send_data(0x17)
  71. self.send_data(0x17)
  72. self.send_data(0x17)
  73. self.send_command(0x04) # power on
  74. self.ReadBusy()
  75. self.send_command(0x00) # panel setting
  76. self.send_data(0x0f) # LUT from OTP,160x296
  77. self.send_data(0x0d) # VCOM to 0V fast
  78. self.send_command(0x61) # resolution setting
  79. self.send_data(0x98)
  80. self.send_data(0x00)
  81. self.send_data(0x98)
  82. self.send_command(0x50)
  83. self.send_data(0x77)
  84. def getbuffer(self, image):
  85. buf = [0xFF] * ((self.width/8) * self.height)
  86. image_monocolor = image.convert('1')
  87. imwidth, imheight = image_monocolor.size
  88. pixels = image_monocolor.load()
  89. if(imwidth == self.width and imheight == self.height):
  90. print("Horizontal")
  91. for y in range(imheight):
  92. for x in range(imwidth):
  93. # Set the bits for the column of pixels at the current position.
  94. if pixels[x, y] == 0:
  95. buf[(x + y * self.width) / 8] &= ~(0x80 >> (x % 8))
  96. elif(imwidth == self.height and imheight == self.width):
  97. print("Vertical")
  98. for y in range(imheight):
  99. for x in range(imwidth):
  100. newx = y
  101. newy = self.height - x - 1
  102. if pixels[x, y] == 0:
  103. buf[(newx + newy*self.width) / 8] &= ~(0x80 >> (y % 8))
  104. return buf
  105. def display(self, blackimage, yellowimage):
  106. self.send_command(0x10)
  107. print("blackimage")
  108. for i in range(0, self.width * self.height / 8):
  109. self.send_data(blackimage[i])
  110. self.send_command(0x13)
  111. print("yellowimage")
  112. for i in range(0, self.width * self.height / 8):
  113. self.send_data(yellowimage[i])
  114. self.send_command(0x12)
  115. self.ReadBusy()
  116. def Clear(self):
  117. self.send_command(0x10)
  118. for i in range(0, self.width * self.height / 8):
  119. self.send_data(0xFF)
  120. self.send_command(0x13)
  121. for i in range(0, self.width * self.height / 8):
  122. self.send_data(0xFF)
  123. self.send_command(0x12)
  124. self.ReadBusy()
  125. # after this, call epd.init() to awaken the module
  126. def sleep(self):
  127. self.send_command(0X02) # power off
  128. self.ReadBusy()
  129. self.send_command(0X07) # deep sleep
  130. self.send_data(0xA5)
  131. epdconfig.module_exit()
  132. ### END OF FILE ###