epd2in13d.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # *****************************************************************************
  2. # * | File : epd2in13d.py
  3. # * | Author : Waveshare team
  4. # * | Function : Electronic paper driver
  5. # * | Info :
  6. # *----------------
  7. # * | This version: V4.0
  8. # * | Date : 2019-06-20
  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. from PIL import Image
  32. import RPi.GPIO as GPIO
  33. # Display resolution
  34. EPD_WIDTH = 104
  35. EPD_HEIGHT = 212
  36. logger = logging.getLogger(__name__)
  37. class EPD:
  38. def __init__(self):
  39. self.reset_pin = epdconfig.RST_PIN
  40. self.dc_pin = epdconfig.DC_PIN
  41. self.busy_pin = epdconfig.BUSY_PIN
  42. self.cs_pin = epdconfig.CS_PIN
  43. self.width = EPD_WIDTH
  44. self.height = EPD_HEIGHT
  45. lut_vcomDC = [
  46. 0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
  47. 0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
  48. 0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
  49. 0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x00, 0x00,
  54. ]
  55. lut_ww = [
  56. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  57. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  58. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  59. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. ]
  64. lut_bw = [
  65. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  66. 0x90, 0x0F, 0x0F, 0x00, 0x00, 0x03,
  67. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  68. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. ]
  73. lut_wb = [
  74. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  75. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  76. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  77. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. ]
  82. lut_bb = [
  83. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  84. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  85. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  86. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90. ]
  91. lut_vcom1 = [
  92. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x00, 0x00,
  100. ]
  101. lut_ww1 = [
  102. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. ]
  110. lut_bw1 = [
  111. 0x80, 0x19, 0x01, 0x00, 0x00, 0x01,
  112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. ]
  119. lut_wb1 = [
  120. 0x40, 0x19, 0x01, 0x00, 0x00, 0x01,
  121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  122. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. ]
  128. lut_bb1 = [
  129. 0x00, 0x19, 0x01, 0x00, 0x00, 0x01,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  136. ]
  137. # Hardware reset
  138. def reset(self):
  139. epdconfig.digital_write(self.reset_pin, 1)
  140. epdconfig.delay_ms(200)
  141. epdconfig.digital_write(self.reset_pin, 0)
  142. epdconfig.delay_ms(5)
  143. epdconfig.digital_write(self.reset_pin, 1)
  144. epdconfig.delay_ms(200)
  145. def send_command(self, command):
  146. epdconfig.digital_write(self.dc_pin, 0)
  147. epdconfig.digital_write(self.cs_pin, 0)
  148. epdconfig.spi_writebyte([command])
  149. epdconfig.digital_write(self.cs_pin, 1)
  150. def send_data(self, data):
  151. epdconfig.digital_write(self.dc_pin, 1)
  152. epdconfig.digital_write(self.cs_pin, 0)
  153. epdconfig.spi_writebyte([data])
  154. epdconfig.digital_write(self.cs_pin, 1)
  155. def ReadBusy(self):
  156. logger.debug("e-Paper busy")
  157. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  158. self.send_command(0x71)
  159. epdconfig.delay_ms(100)
  160. logger.debug("e-Paper busy release")
  161. def TurnOnDisplay(self):
  162. self.send_command(0x12)
  163. epdconfig.delay_ms(100)
  164. self.ReadBusy()
  165. def init(self):
  166. if (epdconfig.module_init() != 0):
  167. return -1
  168. # EPD hardware init start
  169. self.reset()
  170. self.send_command(0x01) # POWER SETTING
  171. self.send_data(0x03)
  172. self.send_data(0x00)
  173. self.send_data(0x2b)
  174. self.send_data(0x2b)
  175. self.send_data(0x03)
  176. self.send_command(0x06) # boost soft start
  177. self.send_data(0x17) # A
  178. self.send_data(0x17) # B
  179. self.send_data(0x17) # C
  180. self.send_command(0x04)
  181. self.ReadBusy()
  182. self.send_command(0x00) # panel setting
  183. self.send_data(0xbf) # LUT from OTP,128x296
  184. self.send_data(0x0d) # VCOM to 0V fast
  185. self.send_command(0x30) # PLL setting
  186. self.send_data(0x3a) # 3a 100HZ 29 150Hz 39 200HZ 31 171HZ
  187. self.send_command(0x61) # resolution setting
  188. self.send_data(self.width)
  189. self.send_data((self.height >> 8) & 0xff)
  190. self.send_data(self.height& 0xff)
  191. self.send_command(0x82) # vcom_DC setting
  192. self.send_data(0x28)
  193. return 0
  194. def SetFullReg(self):
  195. self.send_command(0x82)
  196. self.send_data(0x00)
  197. self.send_command(0X50)
  198. self.send_data(0x97)
  199. self.send_command(0x20) # vcom
  200. for count in range(0, 44):
  201. self.send_data(self.lut_vcomDC[count])
  202. self.send_command(0x21) # ww --
  203. for count in range(0, 42):
  204. self.send_data(self.lut_ww[count])
  205. self.send_command(0x22) # bw r
  206. for count in range(0, 42):
  207. self.send_data(self.lut_bw[count])
  208. self.send_command(0x23) # wb w
  209. for count in range(0, 42):
  210. self.send_data(self.lut_wb[count])
  211. self.send_command(0x24) # bb b
  212. for count in range(0, 42):
  213. self.send_data(self.lut_bb[count])
  214. def SetPartReg(self):
  215. self.send_command(0x82)
  216. self.send_data(0x03)
  217. self.send_command(0X50)
  218. self.send_data(0x47)
  219. self.send_command(0x20) # vcom
  220. for count in range(0, 44):
  221. self.send_data(self.lut_vcom1[count])
  222. self.send_command(0x21) # ww --
  223. for count in range(0, 42):
  224. self.send_data(self.lut_ww1[count])
  225. self.send_command(0x22) # bw r
  226. for count in range(0, 42):
  227. self.send_data(self.lut_bw1[count])
  228. self.send_command(0x23) # wb w
  229. for count in range(0, 42):
  230. self.send_data(self.lut_wb1[count])
  231. self.send_command(0x24) # bb b
  232. for count in range(0, 42):
  233. self.send_data(self.lut_bb1[count])
  234. def getbuffer(self, image):
  235. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  236. buf = [0xFF] * (int(self.width/8) * self.height)
  237. image_monocolor = image.convert('1')
  238. imwidth, imheight = image_monocolor.size
  239. pixels = image_monocolor.load()
  240. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  241. if(imwidth == self.width and imheight == self.height):
  242. logger.debug("Vertical")
  243. for y in range(imheight):
  244. for x in range(imwidth):
  245. # Set the bits for the column of pixels at the current position.
  246. if pixels[x, y] == 0:
  247. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  248. elif(imwidth == self.height and imheight == self.width):
  249. logger.debug("Horizontal")
  250. for y in range(imheight):
  251. for x in range(imwidth):
  252. newx = y
  253. newy = self.height - x - 1
  254. if pixels[x, y] == 0:
  255. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  256. return buf
  257. def display(self, image):
  258. if (Image == None):
  259. return
  260. self.send_command(0x10)
  261. for i in range(0, int(self.width * self.height / 8)):
  262. self.send_data(0x00)
  263. epdconfig.delay_ms(10)
  264. self.send_command(0x13)
  265. for i in range(0, int(self.width * self.height / 8)):
  266. self.send_data(image[i])
  267. epdconfig.delay_ms(10)
  268. self.SetFullReg()
  269. self.TurnOnDisplay()
  270. def DisplayPartial(self, image):
  271. if (Image == None):
  272. return
  273. self.send_command(0x91)
  274. self.send_command(0x90)
  275. self.send_data(0)
  276. self.send_data(self.width - 1)
  277. self.send_data(0)
  278. self.send_data(0)
  279. self.send_data(int(self.height / 256))
  280. self.send_data(self.height % 256 - 1)
  281. self.send_data(0x28)
  282. self.send_command(0x10)
  283. for i in range(0, int(self.width * self.height / 8)):
  284. self.send_data(image[i])
  285. epdconfig.delay_ms(10)
  286. self.send_command(0x13)
  287. for i in range(0, int(self.width * self.height / 8)):
  288. self.send_data(~image[i])
  289. epdconfig.delay_ms(10)
  290. self.SetPartReg()
  291. self.TurnOnDisplay()
  292. def Clear(self, color):
  293. self.send_command(0x10)
  294. for i in range(0, int(self.width * self.height / 8)):
  295. self.send_data(0x00)
  296. epdconfig.delay_ms(10)
  297. self.send_command(0x13)
  298. for i in range(0, int(self.width * self.height / 8)):
  299. self.send_data(0xFF)
  300. epdconfig.delay_ms(10)
  301. self.SetFullReg()
  302. self.TurnOnDisplay()
  303. def sleep(self):
  304. self.send_command(0X50)
  305. self.send_data(0xf7)
  306. self.send_command(0X02) # power off
  307. self.send_command(0X07) # deep sleep
  308. self.send_data(0xA5)
  309. epdconfig.delay_ms(2000)
  310. epdconfig.module_exit()
  311. ### END OF FILE ###