epd4in2.py 23 KB

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