GxEPD2_583c.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Display Library for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: these e-papers require 3.3V supply AND data lines!
  3. //
  4. // based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
  5. //
  6. // Author: Jean-Marc Zingg
  7. //
  8. // Version: see library.properties
  9. //
  10. // Library: https://github.com/ZinggJM/GxEPD2
  11. #include "GxEPD2_583c.h"
  12. #include "WaveTables.h"
  13. GxEPD2_583c::GxEPD2_583c(int8_t cs, int8_t dc, int8_t rst, int8_t busy) : GxEPD2_EPD(cs, dc, rst, busy, LOW, 40000000)
  14. {
  15. _initial = true;
  16. _power_is_on = false;
  17. }
  18. void GxEPD2_583c::init(uint32_t serial_diag_bitrate)
  19. {
  20. GxEPD2_EPD::init(serial_diag_bitrate);
  21. _initial = true;
  22. _power_is_on = false;
  23. }
  24. void GxEPD2_583c::clearScreen(uint8_t value)
  25. {
  26. if (value == 0xFF) value = 0x33; // white value for this controller
  27. _Init_Part();
  28. _writeCommand(0x91); // partial in
  29. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  30. _writeCommand(0x10);
  31. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  32. {
  33. _writeData(value);
  34. }
  35. _Update_Part();
  36. _writeCommand(0x92); // partial out
  37. }
  38. void GxEPD2_583c::clearScreen(uint8_t black_value, uint8_t color_value)
  39. {
  40. _Init_Part();
  41. _writeCommand(0x91); // partial in
  42. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  43. _writeCommand(0x10);
  44. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  45. {
  46. _send8pixel(~black_value, ~color_value);
  47. }
  48. _Update_Part();
  49. _writeCommand(0x92); // partial out
  50. }
  51. void GxEPD2_583c::writeScreenBuffer(uint8_t value)
  52. {
  53. if (value == 0xFF) value = 0x33; // white value for this controller
  54. _Init_Part();
  55. _writeCommand(0x91); // partial in
  56. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  57. _writeCommand(0x10);
  58. for (uint32_t i = 0; i < uint32_t(WIDTH) * uint32_t(HEIGHT) / 2; i++)
  59. {
  60. _writeData(value);
  61. }
  62. _writeCommand(0x92); // partial out
  63. }
  64. void GxEPD2_583c::writeScreenBuffer(uint8_t black_value, uint8_t color_value)
  65. {
  66. _Init_Part();
  67. _writeCommand(0x91); // partial in
  68. _setPartialRamArea(0, 0, WIDTH, HEIGHT);
  69. _writeCommand(0x10);
  70. for (uint32_t i = 0; uint32_t(WIDTH) * uint32_t(HEIGHT) / 8; i++)
  71. {
  72. _send8pixel(~black_value, ~color_value);
  73. }
  74. _writeCommand(0x92); // partial out
  75. }
  76. void GxEPD2_583c::writeImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  77. {
  78. writeImage(bitmap, NULL, x, y, w, h, invert, mirror_y, pgm);
  79. }
  80. void GxEPD2_583c::writeImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  81. {
  82. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  83. int16_t wb = (w + 7) / 8; // width bytes, bitmaps are padded
  84. x -= x % 8; // byte boundary
  85. w = wb * 8; // byte boundary
  86. int16_t x1 = x < 0 ? 0 : x; // limit
  87. int16_t y1 = y < 0 ? 0 : y; // limit
  88. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  89. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  90. int16_t dx = x1 - x;
  91. int16_t dy = y1 - y;
  92. w1 -= dx;
  93. h1 -= dy;
  94. if ((w1 <= 0) || (h1 <= 0)) return;
  95. _Init_Part();
  96. _writeCommand(0x91); // partial in
  97. _setPartialRamArea(x1, y1, w1, h1);
  98. _writeCommand(0x10);
  99. for (int16_t i = 0; i < h1; i++)
  100. {
  101. for (int16_t j = 0; j < w1 / 8; j++)
  102. {
  103. uint8_t black_data = 0xFF;
  104. uint8_t color_data = 0xFF;
  105. if (black)
  106. {
  107. // use wb, h of bitmap for index!
  108. int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  109. if (pgm)
  110. {
  111. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  112. black_data = pgm_read_byte(&black[idx]);
  113. #else
  114. black_data = black[idx];
  115. #endif
  116. }
  117. else
  118. {
  119. black_data = black[idx];
  120. }
  121. if (invert) black_data = ~black_data;
  122. }
  123. if (color)
  124. {
  125. // use wb, h of bitmap for index!
  126. int16_t idx = mirror_y ? j + dx / 8 + ((h - 1 - (i + dy))) * wb : j + dx / 8 + (i + dy) * wb;
  127. if (pgm)
  128. {
  129. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  130. color_data = pgm_read_byte(&color[idx]);
  131. #else
  132. color_data = color[idx];
  133. #endif
  134. }
  135. else
  136. {
  137. color_data = color[idx];
  138. }
  139. if (invert) color_data = ~color_data;
  140. }
  141. _send8pixel(~black_data, ~color_data);
  142. }
  143. #if defined(ESP8266)
  144. yield();
  145. #endif
  146. }
  147. _writeCommand(0x92); // partial out
  148. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  149. }
  150. void GxEPD2_583c::writeNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  151. {
  152. if (data1)
  153. {
  154. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  155. int16_t wb = (w + 1) / 2; // width bytes, bitmaps are padded
  156. x -= x % 2; // byte boundary
  157. w = wb * 2; // byte boundary
  158. int16_t x1 = x < 0 ? 0 : x; // limit
  159. int16_t y1 = y < 0 ? 0 : y; // limit
  160. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  161. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  162. int16_t dx = x1 - x;
  163. int16_t dy = y1 - y;
  164. w1 -= dx;
  165. h1 -= dy;
  166. if ((w1 <= 0) || (h1 <= 0)) return;
  167. _Init_Part();
  168. _writeCommand(0x91); // partial in
  169. _setPartialRamArea(x1, y1, w1, h1);
  170. _writeCommand(0x10);
  171. for (int16_t i = 0; i < h1; i++)
  172. {
  173. for (int16_t j = 0; j < w1 / 2; j++)
  174. {
  175. uint8_t data;
  176. // use wb, h of bitmap for index!
  177. int16_t idx = mirror_y ? j + dx / 2 + ((h - 1 - (i + dy))) * wb : j + dx / 2 + (i + dy) * wb;
  178. if (pgm)
  179. {
  180. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  181. data = pgm_read_byte(&data1[idx]);
  182. #else
  183. data = data1[idx];
  184. #endif
  185. }
  186. else
  187. {
  188. data = data1[idx];
  189. }
  190. if (invert) data = ~data;
  191. _writeData(data);
  192. }
  193. }
  194. _writeCommand(0x92); // partial out
  195. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  196. }
  197. }
  198. void GxEPD2_583c::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  199. {
  200. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  201. refresh(x, y, w, h);
  202. }
  203. void GxEPD2_583c::drawImage(const uint8_t* black, const uint8_t* color, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  204. {
  205. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  206. refresh(x, y, w, h);
  207. }
  208. void GxEPD2_583c::drawNative(const uint8_t* data1, const uint8_t* data2, int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  209. {
  210. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  211. refresh(x, y, w, h);
  212. }
  213. void GxEPD2_583c::refresh(bool partial_update_mode)
  214. {
  215. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  216. else _Update_Full();
  217. }
  218. void GxEPD2_583c::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  219. {
  220. x -= x % 8; // byte boundary
  221. w -= x % 8; // byte boundary
  222. int16_t x1 = x < 0 ? 0 : x; // limit
  223. int16_t y1 = y < 0 ? 0 : y; // limit
  224. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  225. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  226. w1 -= x1 - x;
  227. h1 -= y1 - y;
  228. _Init_Part();
  229. _setPartialRamArea(x1, y1, w1, h1);
  230. _Update_Part();
  231. }
  232. void GxEPD2_583c::powerOff()
  233. {
  234. _PowerOff();
  235. }
  236. void GxEPD2_583c::_send8pixel(uint8_t black_data, uint8_t color_data)
  237. {
  238. for (uint8_t j = 0; j < 8; j++)
  239. {
  240. uint8_t t = 0x00; // black
  241. if (black_data & 0x80); // keep black
  242. else if (color_data & 0x80) t = 0x04; //color
  243. else t = 0x03; // white
  244. t <<= 4;
  245. black_data <<= 1;
  246. color_data <<= 1;
  247. j++;
  248. if (black_data & 0x80); // keep black
  249. else if (color_data & 0x80) t |= 0x04; //color
  250. else t |= 0x03; // white
  251. black_data <<= 1;
  252. color_data <<= 1;
  253. _writeData(t);
  254. }
  255. }
  256. void GxEPD2_583c::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  257. {
  258. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  259. uint16_t ye = y + h - 1;
  260. x &= 0xFFF8; // byte boundary
  261. xe |= 0x0007; // byte boundary
  262. _writeCommand(0x90); // partial window
  263. _writeData(x / 256);
  264. _writeData(x % 256);
  265. _writeData(xe / 256);
  266. _writeData(xe % 256);
  267. _writeData(y / 256);
  268. _writeData(y % 256);
  269. _writeData(ye / 256);
  270. _writeData(ye % 256);
  271. //_writeData(0x01); // distortion on full right half
  272. _writeData(0x00); // distortion on right half
  273. }
  274. void GxEPD2_583c::_PowerOn()
  275. {
  276. if (!_power_is_on)
  277. {
  278. _writeCommand(0x04);
  279. _waitWhileBusy("_PowerOn", power_on_time);
  280. }
  281. _power_is_on = true;
  282. }
  283. void GxEPD2_583c::_PowerOff()
  284. {
  285. _writeCommand(0x02); // power off
  286. _waitWhileBusy("_PowerOff", power_off_time);
  287. _power_is_on = false;
  288. }
  289. void GxEPD2_583c::_InitDisplay()
  290. {
  291. // reset required for wakeup
  292. if (!_power_is_on && (_rst >= 0))
  293. {
  294. digitalWrite(_rst, 0);
  295. delay(10);
  296. digitalWrite(_rst, 1);
  297. delay(10);
  298. }
  299. _writeCommand(0x01);
  300. _writeData (0x37); //POWER SETTING
  301. _writeData (0x00);
  302. _writeCommand(0X00); //PANNEL SETTING
  303. _writeData(0xCF);
  304. _writeData(0x08);
  305. _writeCommand(0x06); //boost
  306. _writeData (0xc7);
  307. _writeData (0xcc);
  308. _writeData (0x28);
  309. _writeCommand(0x30); //PLL setting
  310. //_writeData (0x3a); //PLL: 0-15��:0x3C, 15+:0x3A
  311. _writeData (0x3c); //PLL: 0-15��:0x3C, 15+:0x3A
  312. _writeCommand(0X41); //TEMPERATURE SETTING
  313. _writeData(0x00);
  314. _writeCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  315. _writeData(0x77);
  316. _writeCommand(0X60); //TCON SETTING
  317. _writeData(0x22);
  318. _writeCommand(0x61); //600*448
  319. _writeData (0x02); //source 600
  320. _writeData (0x58);
  321. _writeData (0x01); //gate 448
  322. _writeData (0xc0);
  323. _writeCommand(0X82); //VCOM VOLTAGE SETTING
  324. _writeData(0x28); //all temperature range
  325. _writeCommand(0xe5); //FLASH MODE
  326. _writeData(0x03);
  327. }
  328. void GxEPD2_583c::_Init_Full()
  329. {
  330. _InitDisplay();
  331. _PowerOn();
  332. }
  333. void GxEPD2_583c::_Init_Part()
  334. {
  335. _InitDisplay();
  336. _PowerOn();
  337. }
  338. void GxEPD2_583c::_Update_Full()
  339. {
  340. _writeCommand(0x12); //display refresh
  341. _waitWhileBusy("_Update_Full", full_refresh_time);
  342. }
  343. void GxEPD2_583c::_Update_Part()
  344. {
  345. _writeCommand(0x12); //display refresh
  346. _waitWhileBusy("_Update_Part", partial_refresh_time);
  347. }