epd2in7.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. # *****************************************************************************
  2. # * | File : epd2in7.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. # Display resolution
  32. EPD_WIDTH = 176
  33. EPD_HEIGHT = 264
  34. GRAY1 = 0xff #white
  35. GRAY2 = 0xC0
  36. GRAY3 = 0x80 #gray
  37. GRAY4 = 0x00 #Blackest
  38. class EPD:
  39. def __init__(self):
  40. self.reset_pin = epdconfig.RST_PIN
  41. self.dc_pin = epdconfig.DC_PIN
  42. self.busy_pin = epdconfig.BUSY_PIN
  43. self.cs_pin = epdconfig.CS_PIN
  44. self.width = EPD_WIDTH
  45. self.height = EPD_HEIGHT
  46. self.GRAY1 = GRAY1 #white
  47. self.GRAY2 = GRAY2
  48. self.GRAY3 = GRAY3 #gray
  49. self.GRAY4 = GRAY4 #Blackest
  50. lut_vcom_dc = [0x00, 0x00,
  51. 0x00, 0x08, 0x00, 0x00, 0x00, 0x02,
  52. 0x60, 0x28, 0x28, 0x00, 0x00, 0x01,
  53. 0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
  54. 0x00, 0x12, 0x12, 0x00, 0x00, 0x01,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  58. ]
  59. lut_ww = [
  60. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  61. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  62. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  63. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. ]
  68. lut_bw = [
  69. 0x40, 0x08, 0x00, 0x00, 0x00, 0x02,
  70. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  71. 0x40, 0x14, 0x00, 0x00, 0x00, 0x01,
  72. 0xA0, 0x12, 0x12, 0x00, 0x00, 0x01,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. ]
  77. lut_bb = [
  78. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  79. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  80. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  81. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  82. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. ]
  86. lut_wb = [
  87. 0x80, 0x08, 0x00, 0x00, 0x00, 0x02,
  88. 0x90, 0x28, 0x28, 0x00, 0x00, 0x01,
  89. 0x80, 0x14, 0x00, 0x00, 0x00, 0x01,
  90. 0x50, 0x12, 0x12, 0x00, 0x00, 0x01,
  91. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94. ]
  95. ###################full screen update LUT######################
  96. #0~3 gray
  97. gray_lut_vcom = [
  98. 0x00, 0x00,
  99. 0x00, 0x0A, 0x00, 0x00, 0x00, 0x01,
  100. 0x60, 0x14, 0x14, 0x00, 0x00, 0x01,
  101. 0x00, 0x14, 0x00, 0x00, 0x00, 0x01,
  102. 0x00, 0x13, 0x0A, 0x01, 0x00, 0x01,
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. ]
  107. #R21
  108. gray_lut_ww =[
  109. 0x40, 0x0A, 0x00, 0x00, 0x00, 0x01,
  110. 0x90, 0x14, 0x14, 0x00, 0x00, 0x01,
  111. 0x10, 0x14, 0x0A, 0x00, 0x00, 0x01,
  112. 0xA0, 0x13, 0x01, 0x00, 0x00, 0x01,
  113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. ]
  117. #R22H r
  118. gray_lut_bw =[
  119. 0x40, 0x0A, 0x00, 0x00, 0x00, 0x01,
  120. 0x90, 0x14, 0x14, 0x00, 0x00, 0x01,
  121. 0x00, 0x14, 0x0A, 0x00, 0x00, 0x01,
  122. 0x99, 0x0C, 0x01, 0x03, 0x04, 0x01,
  123. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  126. ]
  127. #R23H w
  128. gray_lut_wb =[
  129. 0x40, 0x0A, 0x00, 0x00, 0x00, 0x01,
  130. 0x90, 0x14, 0x14, 0x00, 0x00, 0x01,
  131. 0x00, 0x14, 0x0A, 0x00, 0x00, 0x01,
  132. 0x99, 0x0B, 0x04, 0x04, 0x01, 0x01,
  133. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  136. ]
  137. #R24H b
  138. gray_lut_bb =[
  139. 0x80, 0x0A, 0x00, 0x00, 0x00, 0x01,
  140. 0x90, 0x14, 0x14, 0x00, 0x00, 0x01,
  141. 0x20, 0x14, 0x0A, 0x00, 0x00, 0x01,
  142. 0x50, 0x13, 0x01, 0x00, 0x00, 0x01,
  143. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146. ]
  147. # Hardware reset
  148. def reset(self):
  149. epdconfig.digital_write(self.reset_pin, 1)
  150. epdconfig.delay_ms(200)
  151. epdconfig.digital_write(self.reset_pin, 0)
  152. epdconfig.delay_ms(5)
  153. epdconfig.digital_write(self.reset_pin, 1)
  154. epdconfig.delay_ms(200)
  155. def send_command(self, command):
  156. epdconfig.digital_write(self.dc_pin, 0)
  157. epdconfig.digital_write(self.cs_pin, 0)
  158. epdconfig.spi_writebyte([command])
  159. epdconfig.digital_write(self.cs_pin, 1)
  160. def send_data(self, data):
  161. epdconfig.digital_write(self.dc_pin, 1)
  162. epdconfig.digital_write(self.cs_pin, 0)
  163. epdconfig.spi_writebyte([data])
  164. epdconfig.digital_write(self.cs_pin, 1)
  165. def ReadBusy(self):
  166. logging.debug("e-Paper busy")
  167. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  168. epdconfig.delay_ms(200)
  169. logging.debug("e-Paper busy release")
  170. def set_lut(self):
  171. self.send_command(0x20) # vcom
  172. for count in range(0, 44):
  173. self.send_data(self.lut_vcom_dc[count])
  174. self.send_command(0x21) # ww --
  175. for count in range(0, 42):
  176. self.send_data(self.lut_ww[count])
  177. self.send_command(0x22) # bw r
  178. for count in range(0, 42):
  179. self.send_data(self.lut_bw[count])
  180. self.send_command(0x23) # wb w
  181. for count in range(0, 42):
  182. self.send_data(self.lut_bb[count])
  183. self.send_command(0x24) # bb b
  184. for count in range(0, 42):
  185. self.send_data(self.lut_wb[count])
  186. def gray_SetLut(self):
  187. self.send_command(0x20)
  188. for count in range(0, 44): #vcom
  189. self.send_data(self.gray_lut_vcom[count])
  190. self.send_command(0x21) #red not use
  191. for count in range(0, 42):
  192. self.send_data(self.gray_lut_ww[count])
  193. self.send_command(0x22) #bw r
  194. for count in range(0, 42):
  195. self.send_data(self.gray_lut_bw[count])
  196. self.send_command(0x23) #wb w
  197. for count in range(0, 42):
  198. self.send_data(self.gray_lut_wb[count])
  199. self.send_command(0x24) #bb b
  200. for count in range(0, 42):
  201. self.send_data(self.gray_lut_bb[count])
  202. self.send_command(0x25) #vcom
  203. for count in range(0, 42):
  204. self.send_data(self.gray_lut_ww[count])
  205. def init(self):
  206. if (epdconfig.module_init() != 0):
  207. return -1
  208. # EPD hardware init start
  209. self.reset()
  210. self.send_command(0x01) # POWER_SETTING
  211. self.send_data(0x03) # VDS_EN, VDG_EN
  212. self.send_data(0x00) # VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  213. self.send_data(0x2b) # VDH
  214. self.send_data(0x2b) # VDL
  215. self.send_data(0x09) # VDHR
  216. self.send_command(0x06) # BOOSTER_SOFT_START
  217. self.send_data(0x07)
  218. self.send_data(0x07)
  219. self.send_data(0x17)
  220. # Power optimization
  221. self.send_command(0xF8)
  222. self.send_data(0x60)
  223. self.send_data(0xA5)
  224. # Power optimization
  225. self.send_command(0xF8)
  226. self.send_data(0x89)
  227. self.send_data(0xA5)
  228. # Power optimization
  229. self.send_command(0xF8)
  230. self.send_data(0x90)
  231. self.send_data(0x00)
  232. # Power optimization
  233. self.send_command(0xF8)
  234. self.send_data(0x93)
  235. self.send_data(0x2A)
  236. # Power optimization
  237. self.send_command(0xF8)
  238. self.send_data(0xA0)
  239. self.send_data(0xA5)
  240. # Power optimization
  241. self.send_command(0xF8)
  242. self.send_data(0xA1)
  243. self.send_data(0x00)
  244. # Power optimization
  245. self.send_command(0xF8)
  246. self.send_data(0x73)
  247. self.send_data(0x41)
  248. self.send_command(0x16) # PARTIAL_DISPLAY_REFRESH
  249. self.send_data(0x00)
  250. self.send_command(0x04) # POWER_ON
  251. self.ReadBusy()
  252. self.send_command(0x00) # PANEL_SETTING
  253. self.send_data(0xAF) # KW-BF KWR-AF BWROTP 0f
  254. self.send_command(0x30) # PLL_CONTROL
  255. self.send_data(0x3A) # 3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  256. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  257. self.send_data(0x57)
  258. self.send_command(0x82) # VCM_DC_SETTING_REGISTER
  259. self.send_data(0x12)
  260. self.set_lut()
  261. return 0
  262. def Init_4Gray(self):
  263. if (epdconfig.module_init() != 0):
  264. return -1
  265. self.reset()
  266. self.send_command(0x01) #POWER SETTING
  267. self.send_data (0x03)
  268. self.send_data (0x00)
  269. self.send_data (0x2b)
  270. self.send_data (0x2b)
  271. self.send_command(0x06) #booster soft start
  272. self.send_data (0x07) #A
  273. self.send_data (0x07) #B
  274. self.send_data (0x17) #C
  275. self.send_command(0xF8) #boost??
  276. self.send_data (0x60)
  277. self.send_data (0xA5)
  278. self.send_command(0xF8) #boost??
  279. self.send_data (0x89)
  280. self.send_data (0xA5)
  281. self.send_command(0xF8) #boost??
  282. self.send_data (0x90)
  283. self.send_data (0x00)
  284. self.send_command(0xF8) #boost??
  285. self.send_data (0x93)
  286. self.send_data (0x2A)
  287. self.send_command(0xF8) #boost??
  288. self.send_data (0xa0)
  289. self.send_data (0xa5)
  290. self.send_command(0xF8) #boost??
  291. self.send_data (0xa1)
  292. self.send_data (0x00)
  293. self.send_command(0xF8) #boost??
  294. self.send_data (0x73)
  295. self.send_data (0x41)
  296. self.send_command(0x16)
  297. self.send_data(0x00)
  298. self.send_command(0x04)
  299. self.ReadBusy()
  300. self.send_command(0x00) #panel setting
  301. self.send_data(0xbf) #KW-BF KWR-AF BWROTP 0f
  302. self.send_command(0x30) #PLL setting
  303. self.send_data (0x90) #100hz
  304. self.send_command(0x61) #resolution setting
  305. self.send_data (0x00) #176
  306. self.send_data (0xb0)
  307. self.send_data (0x01) #264
  308. self.send_data (0x08)
  309. self.send_command(0x82) #vcom_DC setting
  310. self.send_data (0x12)
  311. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  312. self.send_data(0x57)
  313. def getbuffer(self, image):
  314. # logging.debug("bufsiz = ",int(self.width/8) * self.height)
  315. buf = [0xFF] * (int(self.width/8) * self.height)
  316. image_monocolor = image.convert('1')
  317. imwidth, imheight = image_monocolor.size
  318. pixels = image_monocolor.load()
  319. # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  320. if(imwidth == self.width and imheight == self.height):
  321. logging.debug("Vertical")
  322. for y in range(imheight):
  323. for x in range(imwidth):
  324. # Set the bits for the column of pixels at the current position.
  325. if pixels[x, y] == 0:
  326. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  327. elif(imwidth == self.height and imheight == self.width):
  328. logging.debug("Horizontal")
  329. for y in range(imheight):
  330. for x in range(imwidth):
  331. newx = y
  332. newy = self.height - x - 1
  333. if pixels[x, y] == 0:
  334. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  335. return buf
  336. def getbuffer_4Gray(self, image):
  337. # logging.debug("bufsiz = ",int(self.width/8) * self.height)
  338. buf = [0xFF] * (int(self.width / 4) * self.height)
  339. image_monocolor = image.convert('L')
  340. imwidth, imheight = image_monocolor.size
  341. pixels = image_monocolor.load()
  342. i=0
  343. # logging.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  344. if(imwidth == self.width and imheight == self.height):
  345. logging.debug("Vertical")
  346. for y in range(imheight):
  347. for x in range(imwidth):
  348. # Set the bits for the column of pixels at the current position.
  349. if(pixels[x, y] == 0xC0):
  350. pixels[x, y] = 0x80
  351. elif (pixels[x, y] == 0x80):
  352. pixels[x, y] = 0x40
  353. i= i+1
  354. if(i%4 == 0):
  355. buf[int((x + (y * self.width))/4)] = ((pixels[x-3, y]&0xc0) | (pixels[x-2, y]&0xc0)>>2 | (pixels[x-1, y]&0xc0)>>4 | (pixels[x, y]&0xc0)>>6)
  356. elif(imwidth == self.height and imheight == self.width):
  357. logging.debug("Horizontal")
  358. for x in range(imwidth):
  359. for y in range(imheight):
  360. newx = y
  361. newy = x
  362. if(pixels[x, y] == 0xC0):
  363. pixels[x, y] = 0x80
  364. elif (pixels[x, y] == 0x80):
  365. pixels[x, y] = 0x40
  366. i= i+1
  367. if(i%4 == 0):
  368. buf[int((newx + (newy * self.width))/4)] = ((pixels[x, y-3]&0xc0) | (pixels[x, y-2]&0xc0)>>2 | (pixels[x, y-1]&0xc0)>>4 | (pixels[x, y]&0xc0)>>6)
  369. return buf
  370. def display(self, image):
  371. self.send_command(0x10)
  372. for i in range(0, int(self.width * self.height / 8)):
  373. self.send_data(0xFF)
  374. self.send_command(0x13)
  375. for i in range(0, int(self.width * self.height / 8)):
  376. self.send_data(image[i])
  377. self.send_command(0x12)
  378. self.ReadBusy()
  379. def display_4Gray(self, image):
  380. self.send_command(0x10)
  381. for i in range(0, 5808): #5808*4 46464
  382. temp3=0
  383. for j in range(0, 2):
  384. temp1 = image[i*2+j]
  385. for k in range(0, 2):
  386. temp2 = temp1&0xC0
  387. if(temp2 == 0xC0):
  388. temp3 |= 0x01#white
  389. elif(temp2 == 0x00):
  390. temp3 |= 0x00 #black
  391. elif(temp2 == 0x80):
  392. temp3 |= 0x01 #gray1
  393. else: #0x40
  394. temp3 |= 0x00 #gray2
  395. temp3 <<= 1
  396. temp1 <<= 2
  397. temp2 = temp1&0xC0
  398. if(temp2 == 0xC0): #white
  399. temp3 |= 0x01
  400. elif(temp2 == 0x00): #black
  401. temp3 |= 0x00
  402. elif(temp2 == 0x80):
  403. temp3 |= 0x01 #gray1
  404. else : #0x40
  405. temp3 |= 0x00 #gray2
  406. if(j!=1 or k!=1):
  407. temp3 <<= 1
  408. temp1 <<= 2
  409. self.send_data(temp3)
  410. self.send_command(0x13)
  411. for i in range(0, 5808): #5808*4 46464
  412. temp3=0
  413. for j in range(0, 2):
  414. temp1 = image[i*2+j]
  415. for k in range(0, 2):
  416. temp2 = temp1&0xC0
  417. if(temp2 == 0xC0):
  418. temp3 |= 0x01#white
  419. elif(temp2 == 0x00):
  420. temp3 |= 0x00 #black
  421. elif(temp2 == 0x80):
  422. temp3 |= 0x00 #gray1
  423. else: #0x40
  424. temp3 |= 0x01 #gray2
  425. temp3 <<= 1
  426. temp1 <<= 2
  427. temp2 = temp1&0xC0
  428. if(temp2 == 0xC0): #white
  429. temp3 |= 0x01
  430. elif(temp2 == 0x00): #black
  431. temp3 |= 0x00
  432. elif(temp2 == 0x80):
  433. temp3 |= 0x00 #gray1
  434. else: #0x40
  435. temp3 |= 0x01 #gray2
  436. if(j!=1 or k!=1):
  437. temp3 <<= 1
  438. temp1 <<= 2
  439. self.send_data(temp3)
  440. self.gray_SetLut()
  441. self.send_command(0x12)
  442. epdconfig.delay_ms(200)
  443. self.ReadBusy()
  444. # pass
  445. def Clear(self, color):
  446. self.send_command(0x10)
  447. for i in range(0, int(self.width * self.height / 8)):
  448. self.send_data(0xFF)
  449. self.send_command(0x13)
  450. for i in range(0, int(self.width * self.height / 8)):
  451. self.send_data(0xFF)
  452. self.send_command(0x12)
  453. self.ReadBusy()
  454. def sleep(self):
  455. self.send_command(0X50)
  456. self.send_data(0xf7)
  457. self.send_command(0X02)
  458. self.send_command(0X07)
  459. self.send_data(0xA5)
  460. def Dev_exit(self):
  461. epdconfig.module_exit()
  462. ### END OF FILE ###