driver_SSD13x6.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * (c) 2004,2006 Richard Titmuss for SlimProtoLib
  3. * (c) Philippe G. 2019, philippe_44@outlook.com
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <stdint.h>
  22. #include <arpa/inet.h>
  23. #include "esp_log.h"
  24. #include "display.h"
  25. #include "globdefs.h"
  26. #include "ssd13x6.h"
  27. #include "ssd13x6_draw.h"
  28. #include "ssd13x6_font.h"
  29. #include "ssd13x6_default_if.h"
  30. #define I2C_ADDRESS 0x3C
  31. static const char *TAG = "display";
  32. #define max(a,b) (((a) > (b)) ? (a) : (b))
  33. // handlers
  34. static bool init(char *config, char *welcome);
  35. static void clear(void);
  36. static bool set_font(int num, enum display_font_e font, int space);
  37. static void text(enum display_font_e font, enum display_pos_e pos, int attribute, char *text, ...);
  38. static bool line(int num, int x, int attribute, char *text);
  39. static int stretch(int num, char *string, int max);
  40. static void draw_cbr(u8_t *data, int height);
  41. static void draw(int x1, int y1, int x2, int y2, bool by_column, u8_t *data);
  42. static void brightness(u8_t level);
  43. static void on(bool state);
  44. static void update(void);
  45. // display structure for others to use
  46. struct display_s SSD13x6_display = { 0, 0,
  47. init, clear, set_font, on, brightness,
  48. text, line, stretch, update, draw, draw_cbr, NULL };
  49. // SSD13x6 specific function
  50. static struct SSD13x6_Device Display;
  51. static SSD13x6_AddressMode AddressMode = AddressMode_Invalid;
  52. static const unsigned char BitReverseTable256[] =
  53. {
  54. 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
  55. 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
  56. 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
  57. 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
  58. 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
  59. 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
  60. 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
  61. 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
  62. 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
  63. 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
  64. 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
  65. 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
  66. 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
  67. 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
  68. 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
  69. 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
  70. };
  71. #define MAX_LINES 8
  72. static struct {
  73. int y, space;
  74. const struct SSD13x6_FontDef *font;
  75. } lines[MAX_LINES];
  76. /****************************************************************************************
  77. *
  78. */
  79. static bool init(char *config, char *welcome) {
  80. bool res = true;
  81. int width = -1, height = -1, model = SSD1306;
  82. char *p;
  83. ESP_LOGI(TAG, "Initializing display with config: %s",config);
  84. // no time for smart parsing - this is for tinkerers
  85. if ((p = strcasestr(config, "width")) != NULL) width = atoi(strchr(p, '=') + 1);
  86. if ((p = strcasestr(config, "height")) != NULL) height = atoi(strchr(p, '=') + 1);
  87. if (strcasestr(config, "ssd1326")) model = SSD1326;
  88. else if (strcasestr(config, "sh1106")) model = SH1106;
  89. if (width == -1 || height == -1) {
  90. ESP_LOGW(TAG, "No display configured %s [%d x %d]", config, width, height);
  91. return false;
  92. }
  93. if (strstr(config, "I2C") && i2c_system_port != -1) {
  94. int address = I2C_ADDRESS;
  95. if ((p = strcasestr(config, "address")) != NULL) address = atoi(strchr(p, '=') + 1);
  96. SSD13x6_I2CMasterInitDefault( i2c_system_port, -1, -1 ) ;
  97. SSD13x6_I2CMasterAttachDisplayDefault( &Display, model, width, height, address, -1 );
  98. SSD13x6_SetHFlip( &Display, strcasestr(config, "HFlip") ? true : false);
  99. SSD13x6_SetVFlip( &Display, strcasestr(config, "VFlip") ? true : false);
  100. SSD13x6_SetFont( &Display, &Font_droid_sans_fallback_15x17 );
  101. SSD13x6_display.width = width;
  102. SSD13x6_display.height = height;
  103. text(DISPLAY_FONT_MEDIUM, DISPLAY_CENTERED, DISPLAY_CLEAR | DISPLAY_UPDATE, welcome);
  104. ESP_LOGI(TAG, "Display is I2C on port %u", address);
  105. } else if (strstr(config, "SPI") && spi_system_host != -1) {
  106. int CS_pin = -1;
  107. if ((p = strcasestr(config, "CS")) != NULL) CS_pin = atoi(strchr(p, '=') + 1);
  108. SSD13x6_SPIMasterInitDefault( spi_system_host, spi_system_dc_gpio );
  109. SSD13x6_SPIMasterAttachDisplayDefault( &Display, model, width, height, CS_pin, -1 );
  110. SSD13x6_SetFont( &Display, &Font_droid_sans_fallback_15x17 );
  111. SSD13x6_display.width = width;
  112. SSD13x6_display.height = height;
  113. text(DISPLAY_FONT_MEDIUM, DISPLAY_CENTERED, DISPLAY_CLEAR | DISPLAY_UPDATE, welcome);
  114. ESP_LOGI(TAG, "Display is SPI host %u with cs:%d", spi_system_host, CS_pin);
  115. } else {
  116. res = false;
  117. ESP_LOGW(TAG, "Unknown display type or no serial interface configured");
  118. }
  119. return res;
  120. }
  121. /****************************************************************************************
  122. *
  123. */
  124. static void clear(void) {
  125. SSD13x6_Clear( &Display, SSD_COLOR_BLACK );
  126. SSD13x6_Update( &Display );
  127. }
  128. /****************************************************************************************
  129. * Set fonts for each line in text mode
  130. */
  131. static bool set_font(int num, enum display_font_e font, int space) {
  132. if (--num >= MAX_LINES) return false;
  133. switch(font) {
  134. case DISPLAY_FONT_LINE_1:
  135. lines[num].font = &Font_line_1;
  136. break;
  137. case DISPLAY_FONT_LINE_2:
  138. lines[num].font = &Font_line_2;
  139. break;
  140. case DISPLAY_FONT_SMALL:
  141. lines[num].font = &Font_droid_sans_fallback_11x13;
  142. break;
  143. case DISPLAY_FONT_MEDIUM:
  144. case DISPLAY_FONT_DEFAULT:
  145. default:
  146. lines[num].font = &Font_droid_sans_fallback_15x17;
  147. break;
  148. case DISPLAY_FONT_LARGE:
  149. lines[num].font = &Font_droid_sans_fallback_24x28;
  150. break;
  151. case DISPLAY_FONT_SEGMENT:
  152. if (Display.Height == 32) lines[num].font = &Font_Tarable7Seg_16x32;
  153. else lines[num].font = &Font_Tarable7Seg_32x64;
  154. break;
  155. }
  156. // re-calculate lines absolute position
  157. lines[num].space = space;
  158. lines[0].y = lines[0].space;
  159. for (int i = 1; i <= num; i++) lines[i].y = lines[i-1].y + lines[i-1].font->Height + lines[i].space;
  160. ESP_LOGI(TAG, "Adding line %u at %d (height:%u)", num + 1, lines[num].y, lines[num].font->Height);
  161. if (lines[num].y + lines[num].font->Height > Display.Height) {
  162. ESP_LOGW(TAG, "line does not fit display");
  163. return false;
  164. }
  165. return true;
  166. }
  167. /****************************************************************************************
  168. *
  169. */
  170. static bool line(int num, int x, int attribute, char *text) {
  171. int width;
  172. // counting 1..n
  173. num--;
  174. // always horizontal mode for text display
  175. if (AddressMode != AddressMode_Horizontal) {
  176. AddressMode = AddressMode_Horizontal;
  177. SSD13x6_SetDisplayAddressMode( &Display, AddressMode );
  178. }
  179. SSD13x6_SetFont( &Display, lines[num].font );
  180. if (attribute & DISPLAY_MONOSPACE) SSD13x6_FontForceMonospace( &Display, true );
  181. width = SSD13x6_FontMeasureString( &Display, text );
  182. // adjusting position, erase only EoL for rigth-justified
  183. if (x == DISPLAY_RIGHT) x = Display.Width - width - 1;
  184. else if (x == DISPLAY_CENTER) x = (Display.Width - width) / 2;
  185. // erase if requested
  186. if (attribute & DISPLAY_CLEAR) {
  187. int y_min = max(0, lines[num].y), y_max = max(0, lines[num].y + lines[num].font->Height);
  188. for (int c = (attribute & DISPLAY_ONLY_EOL) ? x : 0; c < Display.Width; c++)
  189. for (int y = y_min; y < y_max; y++)
  190. SSD13x6_DrawPixelFast( &Display, c, y, SSD_COLOR_BLACK );
  191. }
  192. SSD13x6_FontDrawString( &Display, x, lines[num].y, text, SSD_COLOR_WHITE );
  193. ESP_LOGD(TAG, "displaying %s line %u (x:%d, attr:%u)", text, num+1, x, attribute);
  194. // update whole display if requested
  195. if (attribute & DISPLAY_UPDATE) SSD13x6_Update( &Display );
  196. return width + x < Display.Width;
  197. }
  198. /****************************************************************************************
  199. * Try to align string for better scrolling visual. there is probably much better to do
  200. */
  201. static int stretch(int num, char *string, int max) {
  202. char space[] = " ";
  203. int len = strlen(string), extra = 0, boundary;
  204. num--;
  205. // we might already fit
  206. SSD13x6_SetFont( &Display, lines[num].font );
  207. if (SSD13x6_FontMeasureString( &Display, string ) <= Display.Width) return 0;
  208. // add some space for better visual
  209. strncat(string, space, max-len);
  210. string[max] = '\0';
  211. len = strlen(string);
  212. // mark the end of the extended string
  213. boundary = SSD13x6_FontMeasureString( &Display, string );
  214. // add a full display width
  215. while (len < max && SSD13x6_FontMeasureString( &Display, string ) - boundary < Display.Width) {
  216. string[len++] = string[extra++];
  217. string[len] = '\0';
  218. }
  219. return boundary;
  220. }
  221. /****************************************************************************************
  222. *
  223. */
  224. static void text(enum display_font_e font, enum display_pos_e pos, int attribute, char *text, ...) {
  225. va_list args;
  226. va_start(args, text);
  227. TextAnchor Anchor = TextAnchor_Center;
  228. if (attribute & DISPLAY_CLEAR) SSD13x6_Clear( &Display, SSD_COLOR_BLACK );
  229. if (!text) return;
  230. switch(font) {
  231. case DISPLAY_FONT_LINE_1:
  232. SSD13x6_SetFont( &Display, &Font_line_1 );
  233. break;
  234. case DISPLAY_FONT_LINE_2:
  235. SSD13x6_SetFont( &Display, &Font_line_2 );
  236. break;
  237. case DISPLAY_FONT_SMALL:
  238. SSD13x6_SetFont( &Display, &Font_droid_sans_fallback_11x13 );
  239. break;
  240. case DISPLAY_FONT_MEDIUM:
  241. case DISPLAY_FONT_DEFAULT:
  242. default:
  243. SSD13x6_SetFont( &Display, &Font_droid_sans_fallback_15x17 );
  244. break;
  245. case DISPLAY_FONT_LARGE:
  246. SSD13x6_SetFont( &Display, &Font_droid_sans_fallback_24x28 );
  247. break;
  248. case DISPLAY_FONT_SEGMENT:
  249. if (Display.Height == 32) SSD13x6_SetFont( &Display, &Font_Tarable7Seg_16x32 );
  250. else SSD13x6_SetFont( &Display, &Font_Tarable7Seg_32x64 );
  251. break;
  252. }
  253. switch(pos) {
  254. case DISPLAY_TOP_LEFT:
  255. default:
  256. Anchor = TextAnchor_NorthWest;
  257. break;
  258. case DISPLAY_MIDDLE_LEFT:
  259. Anchor = TextAnchor_West;
  260. break;
  261. case DISPLAY_BOTTOM_LEFT:
  262. Anchor = TextAnchor_SouthWest;
  263. break;
  264. case DISPLAY_CENTERED:
  265. Anchor = TextAnchor_Center;
  266. break;
  267. }
  268. ESP_LOGD(TAG, "SSDD13x6 displaying %s at %u with attribute %u", text, Anchor, attribute);
  269. if (AddressMode != AddressMode_Horizontal) {
  270. AddressMode = AddressMode_Horizontal;
  271. SSD13x6_SetDisplayAddressMode( &Display, AddressMode );
  272. }
  273. SSD13x6_FontDrawAnchoredString( &Display, Anchor, text, SSD_COLOR_WHITE );
  274. if (attribute & DISPLAY_UPDATE) SSD13x6_Update( &Display );
  275. va_end(args);
  276. }
  277. /****************************************************************************************
  278. * Process graphic display data from column-oriented bytes, MSbit first
  279. */
  280. static void draw_cbr(u8_t *data, int height) {
  281. #ifndef FULL_REFRESH
  282. // force addressing mode by rows
  283. if (AddressMode != AddressMode_Horizontal) {
  284. AddressMode = AddressMode_Horizontal;
  285. SSD13x6_SetDisplayAddressMode( &Display, AddressMode );
  286. }
  287. // try to minimize I2C traffic which is very slow
  288. int rows = (height ? height : Display.Height) / 8;
  289. for (int r = 0; r < rows; r++) {
  290. uint8_t first = 0, last;
  291. uint8_t *optr = Display.Framebuffer + r*Display.Width, *iptr = data + r;
  292. // row/col swap, frame buffer comparison and bit-reversing
  293. for (int c = 0; c < Display.Width; c++) {
  294. u8_t byte = BitReverseTable256[*iptr];
  295. if (byte != *optr) {
  296. if (!first) first = c + 1;
  297. last = c ;
  298. }
  299. *optr++ = byte;
  300. iptr += rows;
  301. }
  302. // now update the display by "byte rows"
  303. if (first--) {
  304. SSD13x6_SetColumnAddress( &Display, first, last );
  305. SSD13x6_SetPageAddress( &Display, r, r);
  306. SSD13x6_WriteRawData( &Display, Display.Framebuffer + r*Display.Width + first, last - first + 1);
  307. }
  308. }
  309. #else
  310. if (!height) height = Display->Height;
  311. SSD13x6_SetPageAddress( &Display, 0, height / 8 - 1);
  312. // force addressing mode by columns (if we can)
  313. if (SSD13x6_GetCaps( &Display ) & CAPS_ADDRESS_VERTICAL) {
  314. // just copy data in frame buffer with bit-reverse
  315. for (int c = 0; c < Display.Width; c++)
  316. for (int r = 0; r < height / 8; r++)
  317. Display.Framebuffer[c*Display.Height/8 + r] = BitReverseTable256[data[c*height/8 +r]];
  318. if (AddressMode != AddressMode_Vertical) {
  319. AddressMode = AddressMode_Vertical;
  320. SSD13x6_SetDisplayAddressMode( &Display, AddressMode );
  321. }
  322. } else {
  323. // need to do rwo/col swap and bit-reverse
  324. int rows = (height ? height : Display.Height) / 8;
  325. for (int r = 0; r < rows; r++) {
  326. uint8_t *optr = Display.Framebuffer + r*Display.Width, *iptr = data + r;
  327. for (int c = 0; c < Display.Width; c++) {
  328. *optr++ = BitReverseTable256[*iptr];;
  329. iptr += rows;
  330. }
  331. }
  332. ESP_LOGW(TAG, "Can't set addressing mode to vertical, swapping");
  333. }
  334. SSD13x6_WriteRawData(&Display, Display.Framebuffer, Display.Width * Display.Height/8);
  335. #endif
  336. }
  337. /****************************************************************************************
  338. * Process graphic display data MSBit first
  339. * WARNING: this has not been tested yet
  340. */
  341. static void draw(int x1, int y1, int x2, int y2, bool by_column, u8_t *data) {
  342. if (y1 % 8 || y2 % 8) {
  343. ESP_LOGW(TAG, "must write rows on byte boundaries (%u,%u) to (%u,%u)", x1, y1, x2, y2);
  344. return;
  345. }
  346. // default end point to display size
  347. if (x2 == -1) x2 = Display.Width - 1;
  348. if (y2 == -1) y2 = Display.Height - 1;
  349. // set addressing mode to match data
  350. if (by_column) {
  351. if (AddressMode != AddressMode_Vertical) {
  352. AddressMode = AddressMode_Vertical;
  353. SSD13x6_SetDisplayAddressMode( &Display, AddressMode );
  354. }
  355. // copy the window and do row/col exchange
  356. for (int r = y1/8; r <= y2/8; r++) {
  357. uint8_t *optr = Display.Framebuffer + r*Display.Width + x1, *iptr = data + r;
  358. for (int c = x1; c <= x2; c++) {
  359. *optr++ = *iptr;
  360. iptr += (y2-y1)/8 + 1;
  361. }
  362. }
  363. } else {
  364. // just copy the window inside the frame buffer
  365. for (int r = y1/8; r <= y2/8; r++) {
  366. uint8_t *optr = Display.Framebuffer + r*Display.Width + x1, *iptr = data + r*(x2-x1+1);
  367. for (int c = x1; c <= x2; c++) *optr++ = *iptr++;
  368. }
  369. }
  370. SSD13x6_SetColumnAddress( &Display, x1, x2);
  371. SSD13x6_SetPageAddress( &Display, y1/8, y2/8);
  372. SSD13x6_WriteRawData( &Display, data, (x2-x1 + 1) * ((y2-y1)/8 + 1));
  373. }
  374. /****************************************************************************************
  375. * Brightness
  376. */
  377. static void brightness(u8_t level) {
  378. SSD13x6_DisplayOn( &Display );
  379. SSD13x6_SetContrast( &Display, (uint8_t) level);
  380. }
  381. /****************************************************************************************
  382. * Display On/Off
  383. */
  384. static void on(bool state) {
  385. if (state) SSD13x6_DisplayOn( &Display );
  386. else SSD13x6_DisplayOff( &Display );
  387. }
  388. /****************************************************************************************
  389. * Update
  390. */
  391. static void update(void) {
  392. SSD13x6_Update( &Display );
  393. }