GxEPD2_750c.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_750c.h"
  12. #include "WaveTables.h"
  13. GxEPD2_750c::GxEPD2_750c(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_750c::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_750c::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_750c::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_750c::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_750c::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_750c::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_750c::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. }
  144. _writeCommand(0x92); // partial out
  145. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  146. }
  147. void GxEPD2_750c::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)
  148. {
  149. if (data1)
  150. {
  151. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  152. int16_t wb = (w + 1) / 2; // width bytes, bitmaps are padded
  153. x -= x % 2; // byte boundary
  154. w = wb * 2; // byte boundary
  155. int16_t x1 = x < 0 ? 0 : x; // limit
  156. int16_t y1 = y < 0 ? 0 : y; // limit
  157. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  158. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  159. int16_t dx = x1 - x;
  160. int16_t dy = y1 - y;
  161. w1 -= dx;
  162. h1 -= dy;
  163. if ((w1 <= 0) || (h1 <= 0)) return;
  164. _Init_Part();
  165. _writeCommand(0x91); // partial in
  166. _setPartialRamArea(x1, y1, w1, h1);
  167. _writeCommand(0x10);
  168. for (int16_t i = 0; i < h1; i++)
  169. {
  170. for (int16_t j = 0; j < w1 / 2; j++)
  171. {
  172. uint8_t data;
  173. // use wb, h of bitmap for index!
  174. int16_t idx = mirror_y ? j + dx / 2 + ((h - 1 - (i + dy))) * wb : j + dx / 2 + (i + dy) * wb;
  175. if (pgm)
  176. {
  177. #if defined(__AVR) || defined(ESP8266) || defined(ESP32)
  178. data = pgm_read_byte(&data1[idx]);
  179. #else
  180. data = data1[idx];
  181. #endif
  182. }
  183. else
  184. {
  185. data = data1[idx];
  186. }
  187. if (invert) data = ~data;
  188. _writeData(data);
  189. }
  190. }
  191. _writeCommand(0x92); // partial out
  192. delay(1); // yield() to avoid WDT on ESP8266 and ESP32
  193. }
  194. }
  195. void GxEPD2_750c::drawImage(const uint8_t bitmap[], int16_t x, int16_t y, int16_t w, int16_t h, bool invert, bool mirror_y, bool pgm)
  196. {
  197. writeImage(bitmap, x, y, w, h, invert, mirror_y, pgm);
  198. refresh(x, y, w, h);
  199. }
  200. void GxEPD2_750c::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)
  201. {
  202. writeImage(black, color, x, y, w, h, invert, mirror_y, pgm);
  203. refresh(x, y, w, h);
  204. }
  205. void GxEPD2_750c::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)
  206. {
  207. writeNative(data1, data2, x, y, w, h, invert, mirror_y, pgm);
  208. refresh(x, y, w, h);
  209. }
  210. void GxEPD2_750c::refresh(bool partial_update_mode)
  211. {
  212. if (partial_update_mode) refresh(0, 0, WIDTH, HEIGHT);
  213. else _Update_Full();
  214. }
  215. void GxEPD2_750c::refresh(int16_t x, int16_t y, int16_t w, int16_t h)
  216. {
  217. x -= x % 8; // byte boundary
  218. w -= x % 8; // byte boundary
  219. int16_t x1 = x < 0 ? 0 : x; // limit
  220. int16_t y1 = y < 0 ? 0 : y; // limit
  221. int16_t w1 = x + w < int16_t(WIDTH) ? w : int16_t(WIDTH) - x; // limit
  222. int16_t h1 = y + h < int16_t(HEIGHT) ? h : int16_t(HEIGHT) - y; // limit
  223. w1 -= x1 - x;
  224. h1 -= y1 - y;
  225. _Init_Part();
  226. _setPartialRamArea(x1, y1, w1, h1);
  227. _Update_Part();
  228. }
  229. void GxEPD2_750c::powerOff()
  230. {
  231. _PowerOff();
  232. }
  233. void GxEPD2_750c::_send8pixel(uint8_t black_data, uint8_t color_data)
  234. {
  235. for (uint8_t j = 0; j < 8; j++)
  236. {
  237. uint8_t t = 0x00; // black
  238. if (black_data & 0x80); // keep black
  239. else if (color_data & 0x80) t = 0x04; //color
  240. else t = 0x03; // white
  241. t <<= 4;
  242. black_data <<= 1;
  243. color_data <<= 1;
  244. j++;
  245. if (black_data & 0x80); // keep black
  246. else if (color_data & 0x80) t |= 0x04; //color
  247. else t |= 0x03; // white
  248. black_data <<= 1;
  249. color_data <<= 1;
  250. _writeData(t);
  251. }
  252. }
  253. void GxEPD2_750c::_setPartialRamArea(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  254. {
  255. uint16_t xe = (x + w - 1) | 0x0007; // byte boundary inclusive (last byte)
  256. uint16_t ye = y + h - 1;
  257. x &= 0xFFF8; // byte boundary
  258. xe |= 0x0007; // byte boundary
  259. _writeCommand(0x90); // partial window
  260. _writeData(x / 256);
  261. _writeData(x % 256);
  262. _writeData(xe / 256);
  263. _writeData(xe % 256);
  264. _writeData(y / 256);
  265. _writeData(y % 256);
  266. _writeData(ye / 256);
  267. _writeData(ye % 256);
  268. //_writeData(0x01); // distortion on full right half
  269. _writeData(0x00); // distortion on right half
  270. }
  271. void GxEPD2_750c::_PowerOn()
  272. {
  273. if (!_power_is_on)
  274. {
  275. _writeCommand(0x04);
  276. _waitWhileBusy("_PowerOn", power_on_time);
  277. }
  278. _power_is_on = true;
  279. }
  280. void GxEPD2_750c::_PowerOff()
  281. {
  282. _writeCommand(0x02); // power off
  283. _waitWhileBusy("_PowerOff", power_off_time);
  284. _power_is_on = false;
  285. }
  286. void GxEPD2_750c::_InitDisplay()
  287. {
  288. // reset required for wakeup
  289. if (!_power_is_on && (_rst >= 0))
  290. {
  291. digitalWrite(_rst, 0);
  292. delay(10);
  293. digitalWrite(_rst, 1);
  294. delay(10);
  295. }
  296. /**********************************release flash sleep**********************************/
  297. _writeCommand(0X65); //FLASH CONTROL
  298. _writeData(0x01);
  299. _writeCommand(0xAB);
  300. _writeCommand(0X65); //FLASH CONTROL
  301. _writeData(0x00);
  302. /**********************************release flash sleep**********************************/
  303. _writeCommand(0x01);
  304. _writeData (0x37); //POWER SETTING
  305. _writeData (0x00);
  306. //_writeCommand(0x04); //POWER ON
  307. //_waitWhileBusy("PowerOn", power_on_time);
  308. _writeCommand(0X00); //PANNEL SETTING
  309. _writeData(0xCF);
  310. _writeData(0x08);
  311. _writeCommand(0x06); //boost
  312. _writeData (0xc7);
  313. _writeData (0xcc);
  314. _writeData (0x28);
  315. _writeCommand(0x30); //PLL setting
  316. _writeData (0x3c);
  317. _writeCommand(0X41); //TEMPERATURE SETTING
  318. _writeData(0x00);
  319. _writeCommand(0X50); //VCOM AND DATA INTERVAL SETTING
  320. _writeData(0x77);
  321. _writeCommand(0X60); //TCON SETTING
  322. _writeData(0x22);
  323. _writeCommand(0x61); //tres 640*384
  324. _writeData (0x02); //source 640
  325. _writeData (0x80);
  326. _writeData (0x01); //gate 384
  327. _writeData (0x80);
  328. _writeCommand(0X82); //VDCS SETTING
  329. _writeData(0x1E); //decide by LUT file
  330. _writeCommand(0xe5); //FLASH MODE
  331. _writeData(0x03);
  332. }
  333. void GxEPD2_750c::_Init_Full()
  334. {
  335. _InitDisplay();
  336. _PowerOn();
  337. }
  338. void GxEPD2_750c::_Init_Part()
  339. {
  340. _InitDisplay();
  341. _PowerOn();
  342. }
  343. void GxEPD2_750c::_Update_Full()
  344. {
  345. _writeCommand(0x12); //display refresh
  346. _waitWhileBusy("_Update_Full", full_refresh_time);
  347. }
  348. void GxEPD2_750c::_Update_Part()
  349. {
  350. _writeCommand(0x12); //display refresh
  351. _waitWhileBusy("_Update_Part", partial_refresh_time);
  352. }