2
0

epd4in2.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. # *****************************************************************************
  2. # * | File : epd4in2.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 = 400
  35. EPD_HEIGHT = 300
  36. GRAY1 = 0xff #white
  37. GRAY2 = 0xC0
  38. GRAY3 = 0x80 #gray
  39. GRAY4 = 0x00 #Blackest
  40. logger = logging.getLogger(__name__)
  41. class EPD:
  42. def __init__(self):
  43. self.reset_pin = epdconfig.RST_PIN
  44. self.dc_pin = epdconfig.DC_PIN
  45. self.busy_pin = epdconfig.BUSY_PIN
  46. self.cs_pin = epdconfig.CS_PIN
  47. self.width = EPD_WIDTH
  48. self.height = EPD_HEIGHT
  49. self.GRAY1 = GRAY1 #white
  50. self.GRAY2 = GRAY2
  51. self.GRAY3 = GRAY3 #gray
  52. self.GRAY4 = GRAY4 #Blackest
  53. lut_vcom0 = [
  54. 0x00, 0x17, 0x00, 0x00, 0x00, 0x02,
  55. 0x00, 0x17, 0x17, 0x00, 0x00, 0x02,
  56. 0x00, 0x0A, 0x01, 0x00, 0x00, 0x01,
  57. 0x00, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. ]
  62. lut_ww = [
  63. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  64. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  65. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  66. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. ]
  71. lut_bw = [
  72. 0x40, 0x17, 0x00, 0x00, 0x00, 0x02,
  73. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  74. 0x40, 0x0A, 0x01, 0x00, 0x00, 0x01,
  75. 0xA0, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. ]
  80. lut_wb = [
  81. 0x80, 0x17, 0x00, 0x00, 0x00, 0x02,
  82. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  83. 0x80, 0x0A, 0x01, 0x00, 0x00, 0x01,
  84. 0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. ]
  89. lut_bb = [
  90. 0x80, 0x17, 0x00, 0x00, 0x00, 0x02,
  91. 0x90, 0x17, 0x17, 0x00, 0x00, 0x02,
  92. 0x80, 0x0A, 0x01, 0x00, 0x00, 0x01,
  93. 0x50, 0x0E, 0x0E, 0x00, 0x00, 0x02,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. ]
  98. #******************************partial screen update LUT*********************************/
  99. EPD_4IN2_Partial_lut_vcom1 =[
  100. 0x00 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  101. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  102. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  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, ]
  108. EPD_4IN2_Partial_lut_ww1 =[
  109. 0x00 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  110. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  111. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  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. EPD_4IN2_Partial_lut_bw1 =[
  117. 0x80 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  118. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  119. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  120. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  121. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  122. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  123. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00, ]
  124. EPD_4IN2_Partial_lut_wb1 =[
  125. 0x40 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  126. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  127. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  128. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  129. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  130. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  131. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00, ]
  132. EPD_4IN2_Partial_lut_bb1 =[
  133. 0x00 ,0x19 ,0x01 ,0x00 ,0x00 ,0x01,
  134. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  135. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  136. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  137. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  138. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  139. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00, ]
  140. #******************************gray*********************************/
  141. #0~3 gray
  142. EPD_4IN2_4Gray_lut_vcom =[
  143. 0x00 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  144. 0x60 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  145. 0x00 ,0x14 ,0x00 ,0x00 ,0x00 ,0x01,
  146. 0x00 ,0x13 ,0x0A ,0x01 ,0x00 ,0x01,
  147. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  148. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  149. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
  150. ]
  151. #R21
  152. EPD_4IN2_4Gray_lut_ww =[
  153. 0x40 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  154. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  155. 0x10 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  156. 0xA0 ,0x13 ,0x01 ,0x00 ,0x00 ,0x01,
  157. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  158. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  159. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  160. ]
  161. #R22H r
  162. EPD_4IN2_4Gray_lut_bw =[
  163. 0x40 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  164. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  165. 0x00 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  166. 0x99 ,0x0C ,0x01 ,0x03 ,0x04 ,0x01,
  167. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  168. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  169. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  170. ]
  171. #R23H w
  172. EPD_4IN2_4Gray_lut_wb =[
  173. 0x40 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  174. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  175. 0x00 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  176. 0x99 ,0x0B ,0x04 ,0x04 ,0x01 ,0x01,
  177. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  178. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  179. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  180. ]
  181. #R24H b
  182. EPD_4IN2_4Gray_lut_bb =[
  183. 0x80 ,0x0A ,0x00 ,0x00 ,0x00 ,0x01,
  184. 0x90 ,0x14 ,0x14 ,0x00 ,0x00 ,0x01,
  185. 0x20 ,0x14 ,0x0A ,0x00 ,0x00 ,0x01,
  186. 0x50 ,0x13 ,0x01 ,0x00 ,0x00 ,0x01,
  187. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  188. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  189. 0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00,
  190. ]
  191. # Hardware reset
  192. def reset(self):
  193. epdconfig.digital_write(self.reset_pin, 1)
  194. epdconfig.delay_ms(20)
  195. epdconfig.digital_write(self.reset_pin, 0)
  196. epdconfig.delay_ms(5)
  197. epdconfig.digital_write(self.reset_pin, 1)
  198. epdconfig.delay_ms(20)
  199. epdconfig.digital_write(self.reset_pin, 0)
  200. epdconfig.delay_ms(5)
  201. epdconfig.digital_write(self.reset_pin, 1)
  202. epdconfig.delay_ms(20)
  203. epdconfig.digital_write(self.reset_pin, 0)
  204. epdconfig.delay_ms(5)
  205. epdconfig.digital_write(self.reset_pin, 1)
  206. epdconfig.delay_ms(20)
  207. def send_command(self, command):
  208. epdconfig.digital_write(self.dc_pin, 0)
  209. epdconfig.digital_write(self.cs_pin, 0)
  210. epdconfig.spi_writebyte([command])
  211. epdconfig.digital_write(self.cs_pin, 1)
  212. def send_data(self, data):
  213. epdconfig.digital_write(self.dc_pin, 1)
  214. epdconfig.digital_write(self.cs_pin, 0)
  215. epdconfig.spi_writebyte([data])
  216. epdconfig.digital_write(self.cs_pin, 1)
  217. def ReadBusy(self):
  218. self.send_command(0x71)
  219. while(epdconfig.digital_read(self.busy_pin) == 0): # 0: idle, 1: busy
  220. self.send_command(0x71)
  221. epdconfig.delay_ms(100)
  222. def set_lut(self):
  223. self.send_command(0x20) # vcom
  224. for count in range(0, 44):
  225. self.send_data(self.lut_vcom0[count])
  226. self.send_command(0x21) # ww --
  227. for count in range(0, 42):
  228. self.send_data(self.lut_ww[count])
  229. self.send_command(0x22) # bw r
  230. for count in range(0, 42):
  231. self.send_data(self.lut_bw[count])
  232. self.send_command(0x23) # wb w
  233. for count in range(0, 42):
  234. self.send_data(self.lut_bb[count])
  235. self.send_command(0x24) # bb b
  236. for count in range(0, 42):
  237. self.send_data(self.lut_wb[count])
  238. def Partial_SetLut(self):
  239. self.send_command(0x20);
  240. for count in range(0, 44):
  241. self.send_data(self.EPD_4IN2_Partial_lut_vcom1[count])
  242. self.send_command(0x21);
  243. for count in range(0, 42):
  244. self.send_data(self.EPD_4IN2_Partial_lut_ww1[count])
  245. self.send_command(0x22);
  246. for count in range(0, 42):
  247. self.send_data(self.EPD_4IN2_Partial_lut_bw1[count])
  248. self.send_command(0x23);
  249. for count in range(0, 42):
  250. self.send_data(self.EPD_4IN2_Partial_lut_wb1[count])
  251. self.send_command(0x24);
  252. for count in range(0, 42):
  253. self.send_data(self.EPD_4IN2_Partial_lut_bb1[count])
  254. def Gray_SetLut(self):
  255. self.send_command(0x20) #vcom
  256. for count in range(0, 42):
  257. self.send_data(self.EPD_4IN2_4Gray_lut_vcom[count])
  258. self.send_command(0x21) #red not use
  259. for count in range(0, 42):
  260. self.send_data(self.EPD_4IN2_4Gray_lut_ww[count])
  261. self.send_command(0x22) #bw r
  262. for count in range(0, 42):
  263. self.send_data(self.EPD_4IN2_4Gray_lut_bw[count])
  264. self.send_command(0x23) #wb w
  265. for count in range(0, 42):
  266. self.send_data(self.EPD_4IN2_4Gray_lut_wb[count])
  267. self.send_command(0x24) #bb b
  268. for count in range(0, 42):
  269. self.send_data(self.EPD_4IN2_4Gray_lut_bb[count])
  270. self.send_command(0x25) #vcom
  271. for count in range(0, 42):
  272. self.send_data(self.EPD_4IN2_4Gray_lut_ww[count])
  273. def init(self):
  274. if (epdconfig.module_init() != 0):
  275. return -1
  276. # EPD hardware init start
  277. self.reset()
  278. self.send_command(0x01) # POWER SETTING
  279. self.send_data(0x03) # VDS_EN, VDG_EN
  280. self.send_data(0x00) # VCOM_HV, VGHL_LV[1], VGHL_LV[0]
  281. self.send_data(0x2b) # VDH
  282. self.send_data(0x2b) # VDL
  283. self.send_command(0x06) # boost soft start
  284. self.send_data(0x17)
  285. self.send_data(0x17)
  286. self.send_data(0x17)
  287. self.send_command(0x04) # POWER_ON
  288. self.ReadBusy()
  289. self.send_command(0x00) # panel setting
  290. self.send_data(0xbf) # KW-BF KWR-AF BWROTP 0f
  291. self.send_data(0x0d)
  292. self.send_command(0x30) # PLL setting
  293. self.send_data(0x3c) # 3A 100HZ 29 150Hz 39 200HZ 31 171HZ
  294. self.send_command(0x61) # resolution setting
  295. self.send_data(0x01)
  296. self.send_data(0x90) # 128
  297. self.send_data(0x01)
  298. self.send_data(0x2c)
  299. self.send_command(0x82) # vcom_DC setting
  300. self.send_data(0x28)
  301. self.send_command(0X50) # VCOM AND DATA INTERVAL SETTING
  302. self.send_data(0x97) # 97white border 77black border VBDF 17|D7 VBDW 97 VBDB 57 VBDF F7 VBDW 77 VBDB 37 VBDR B7
  303. self.set_lut()
  304. # EPD hardware init end
  305. return 0
  306. def Init_4Gray(self):
  307. if (epdconfig.module_init() != 0):
  308. return -1
  309. # EPD hardware init start
  310. self.reset()
  311. self.send_command(0x01) #POWER SETTING
  312. self.send_data (0x03)
  313. self.send_data (0x00) #VGH=20V,VGL=-20V
  314. self.send_data (0x2b) #VDH=15V
  315. self.send_data (0x2b) #VDL=-15V
  316. self.send_data (0x13)
  317. self.send_command(0x06) #booster soft start
  318. self.send_data (0x17) #A
  319. self.send_data (0x17) #B
  320. self.send_data (0x17) #C
  321. self.send_command(0x04)
  322. self.ReadBusy()
  323. self.send_command(0x00) #panel setting
  324. self.send_data(0x3f) #KW-3f KWR-2F BWROTP 0f BWOTP 1f
  325. self.send_command(0x30) #PLL setting
  326. self.send_data (0x3c) #100hz
  327. self.send_command(0x61) #resolution setting
  328. self.send_data (0x01) #400
  329. self.send_data (0x90)
  330. self.send_data (0x01) #300
  331. self.send_data (0x2c)
  332. self.send_command(0x82) #vcom_DC setting
  333. self.send_data (0x12)
  334. self.send_command(0X50) #VCOM AND DATA INTERVAL SETTING
  335. self.send_data(0x97)
  336. def getbuffer(self, image):
  337. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  338. buf = [0xFF] * (int(self.width/8) * self.height)
  339. image_monocolor = image.convert('1')
  340. imwidth, imheight = image_monocolor.size
  341. pixels = image_monocolor.load()
  342. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  343. if(imwidth == self.width and imheight == self.height):
  344. logger.debug("Horizontal")
  345. for y in range(imheight):
  346. for x in range(imwidth):
  347. # Set the bits for the column of pixels at the current position.
  348. if pixels[x, y] == 0:
  349. buf[int((x + y * self.width) / 8)] &= ~(0x80 >> (x % 8))
  350. elif(imwidth == self.height and imheight == self.width):
  351. logger.debug("Vertical")
  352. for y in range(imheight):
  353. for x in range(imwidth):
  354. newx = y
  355. newy = self.height - x - 1
  356. if pixels[x, y] == 0:
  357. buf[int((newx + newy*self.width) / 8)] &= ~(0x80 >> (y % 8))
  358. return buf
  359. def getbuffer_4Gray(self, image):
  360. # logger.debug("bufsiz = ",int(self.width/8) * self.height)
  361. buf = [0xFF] * (int(self.width / 4) * self.height)
  362. image_monocolor = image.convert('L')
  363. imwidth, imheight = image_monocolor.size
  364. pixels = image_monocolor.load()
  365. i=0
  366. # logger.debug("imwidth = %d, imheight = %d",imwidth,imheight)
  367. if(imwidth == self.width and imheight == self.height):
  368. logger.debug("Vertical")
  369. for y in range(imheight):
  370. for x in range(imwidth):
  371. # Set the bits for the column of pixels at the current position.
  372. if(pixels[x, y] == 0xC0):
  373. pixels[x, y] = 0x80
  374. elif (pixels[x, y] == 0x80):
  375. pixels[x, y] = 0x40
  376. i= i+1
  377. if(i%4 == 0):
  378. 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)
  379. elif(imwidth == self.height and imheight == self.width):
  380. logger.debug("Horizontal")
  381. for x in range(imwidth):
  382. for y in range(imheight):
  383. newx = y
  384. newy = x
  385. if(pixels[x, y] == 0xC0):
  386. pixels[x, y] = 0x80
  387. elif (pixels[x, y] == 0x80):
  388. pixels[x, y] = 0x40
  389. i= i+1
  390. if(i%4 == 0):
  391. 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)
  392. return buf
  393. def display(self, image):
  394. self.send_command(0x92);
  395. self.set_lut();
  396. self.send_command(0x10)
  397. for i in range(0, int(self.width * self.height / 8)):
  398. self.send_data(0xFF)
  399. self.send_command(0x13)
  400. for i in range(0, int(self.width * self.height / 8)):
  401. self.send_data(image[i])
  402. self.send_command(0x12)
  403. self.ReadBusy()
  404. def EPD_4IN2_PartialDisplay(self, X_start, Y_start, X_end, Y_end, Image):
  405. # EPD_WIDTH = 400
  406. # EPD_HEIGHT = 300
  407. if(EPD_WIDTH % 8 != 0):
  408. Width = int(EPD_WIDTH / 8) + 1;
  409. else:
  410. Width = int(EPD_WIDTH / 8);
  411. Height = EPD_HEIGHT;
  412. if(X_start % 8 != 0):
  413. X_start = int(X_start/8)*8+8
  414. if(X_end % 8 != 0):
  415. X_end = int(X_end/8)*8+8
  416. self.Partial_SetLut();
  417. self.send_command(0x91); #This command makes the display enter partial mode
  418. self.send_command(0x90); #resolution setting
  419. self.send_data (int(X_start/256));
  420. self.send_data (int(X_start%256)); #x-start
  421. self.send_data (int(X_end /256));
  422. self.send_data (int(X_end %256)-1); #x-end
  423. self.send_data (int(Y_start/256));
  424. self.send_data (int(Y_start%256)); #y-start
  425. self.send_data (int(Y_end/256));
  426. self.send_data (int(Y_end%256)-1); #y-end
  427. self.send_data (0x28);
  428. self.send_command(0x10); #writes Old data to SRAM for programming
  429. for j in range(0, int(Y_end - Y_start)):
  430. for i in range(0, int(X_end/8) - int(X_start/8)):
  431. self.send_data(Image[(Y_start + j)*Width + int(X_start/8) + i]);
  432. self.send_command(0x13); #writes New data to SRAM.
  433. for j in range(0, int(Y_end - Y_start)):
  434. for i in range(0, int(X_end/8) - int(X_start/8)):
  435. self.send_data(~Image[(Y_start + j)*Width + int(X_start/8) + i]);
  436. self.send_command(0x12); #DISPLAY REFRESH
  437. epdconfig.delay_ms(200) #The delay here is necessary, 200uS at least!!!
  438. self.ReadBusy()
  439. def display_4Gray(self, image):
  440. self.send_command(0x92);
  441. self.set_lut();
  442. self.send_command(0x10)
  443. for i in range(0, int(EPD_WIDTH * EPD_HEIGHT / 8)): # EPD_WIDTH * EPD_HEIGHT / 4
  444. temp3=0
  445. for j in range(0, 2):
  446. temp1 = image[i*2+j]
  447. for k in range(0, 2):
  448. temp2 = temp1&0xC0
  449. if(temp2 == 0xC0):
  450. temp3 |= 0x01#white
  451. elif(temp2 == 0x00):
  452. temp3 |= 0x00 #black
  453. elif(temp2 == 0x80):
  454. temp3 |= 0x01 #gray1
  455. else: #0x40
  456. temp3 |= 0x00 #gray2
  457. temp3 <<= 1
  458. temp1 <<= 2
  459. temp2 = temp1&0xC0
  460. if(temp2 == 0xC0): #white
  461. temp3 |= 0x01
  462. elif(temp2 == 0x00): #black
  463. temp3 |= 0x00
  464. elif(temp2 == 0x80):
  465. temp3 |= 0x01 #gray1
  466. else : #0x40
  467. temp3 |= 0x00 #gray2
  468. if(j!=1 or k!=1):
  469. temp3 <<= 1
  470. temp1 <<= 2
  471. self.send_data(temp3)
  472. self.send_command(0x13)
  473. for i in range(0, int(EPD_WIDTH * EPD_HEIGHT / 8)): #5808*4 46464
  474. temp3=0
  475. for j in range(0, 2):
  476. temp1 = image[i*2+j]
  477. for k in range(0, 2):
  478. temp2 = temp1&0xC0
  479. if(temp2 == 0xC0):
  480. temp3 |= 0x01#white
  481. elif(temp2 == 0x00):
  482. temp3 |= 0x00 #black
  483. elif(temp2 == 0x80):
  484. temp3 |= 0x00 #gray1
  485. else: #0x40
  486. temp3 |= 0x01 #gray2
  487. temp3 <<= 1
  488. temp1 <<= 2
  489. temp2 = temp1&0xC0
  490. if(temp2 == 0xC0): #white
  491. temp3 |= 0x01
  492. elif(temp2 == 0x00): #black
  493. temp3 |= 0x00
  494. elif(temp2 == 0x80):
  495. temp3 |= 0x00 #gray1
  496. else: #0x40
  497. temp3 |= 0x01 #gray2
  498. if(j!=1 or k!=1):
  499. temp3 <<= 1
  500. temp1 <<= 2
  501. self.send_data(temp3)
  502. self.Gray_SetLut()
  503. self.send_command(0x12)
  504. epdconfig.delay_ms(200)
  505. self.ReadBusy()
  506. # pass
  507. def Clear(self):
  508. self.send_command(0x10)
  509. for i in range(0, int(self.width * self.height / 8)):
  510. self.send_data(0xFF)
  511. self.send_command(0x13)
  512. for i in range(0, int(self.width * self.height / 8)):
  513. self.send_data(0xFF)
  514. self.send_command(0x12)
  515. self.ReadBusy()
  516. def sleep(self):
  517. self.send_command(0x02) # POWER_OFF
  518. self.ReadBusy()
  519. self.send_command(0x07) # DEEP_SLEEP
  520. self.send_data(0XA5)
  521. epdconfig.delay_ms(2000)
  522. epdconfig.module_exit()
  523. ### END OF FILE ###