µGFX library fork

gdisp_lld_SSD1331.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file, you can obtain one at:
  4. *
  5. * http://ugfx.org/license.html
  6. */
  7. #include "gfx.h"
  8. #if GFX_USE_GDISP
  9. #if defined(GDISP_SCREEN_HEIGHT) || defined(GDISP_SCREEN_HEIGHT)
  10. #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
  11. #warning "GDISP: This low level driver does not support setting a screen size. It is being ignored."
  12. #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
  13. COMPILER_WARNING("GDISP: This low level driver does not support setting a screen size. It is being ignored.")
  14. #endif
  15. #undef GDISP_SCREEN_WIDTH
  16. #undef GDISP_SCREEN_HEIGHT
  17. #endif
  18. #define GDISP_DRIVER_VMT GDISPVMT_SSD1331
  19. #include "gdisp_lld_config.h"
  20. #include "../../../src/gdisp/gdisp_driver.h"
  21. #include "board_SSD1331.h"
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. #ifndef GDISP_SCREEN_HEIGHT
  26. #define GDISP_SCREEN_HEIGHT 64
  27. #endif
  28. #ifndef GDISP_SCREEN_WIDTH
  29. #define GDISP_SCREEN_WIDTH 96
  30. #endif
  31. #ifndef GDISP_INITIAL_CONTRAST
  32. #define GDISP_INITIAL_CONTRAST 100
  33. #endif
  34. #ifndef GDISP_INITIAL_BACKLIGHT
  35. #define GDISP_INITIAL_BACKLIGHT 32
  36. #endif
  37. #include "SSD1331.h"
  38. /*===========================================================================*/
  39. /* Driver local functions. */
  40. /*===========================================================================*/
  41. // Some common routines and macros
  42. #define write_reg(g, reg, data) { write_cmd(g, reg); write_data(g, data); }
  43. /*===========================================================================*/
  44. /* Driver exported functions. */
  45. /*===========================================================================*/
  46. static const uint8_t initdata[] = {
  47. SSD1331_DISPLAY_OFF,
  48. SSD1331_START_LINE, 0x00,
  49. SSD1331_COM_OFFSET, 0x00,
  50. SSD1331_PIXELS_NORMAL,
  51. SSD1331_MULTIPLEX, 0x3F,
  52. SSD1331_RESET, SSD1331_RESET_OFF,
  53. SSD1331_POWER, SSD1331_POWER_ON,
  54. SSD1331_PHASE_PERIOD, 0x31,
  55. SSD1331_CLOCKS, 0xF0,
  56. SSD1331_PRECHARGE_A, 0x64,
  57. SSD1331_PRECHARGE_B, 0x78,
  58. SSD1331_PRECHARGE_C, 0x64,
  59. SSD1331_PRECHARGE_VOLTAGE, 0x3A,
  60. SSD1331_DESELECT_VOLTAGE, 0x3E,
  61. SSD1331_CONTRAST_A, 0x91,
  62. SSD1331_CONTRAST_B, 0x50,
  63. SSD1331_CONTRAST_C, 0x7D,
  64. SSD1331_BRIGHTNESS, (GDISP_INITIAL_BACKLIGHT*10)/63,
  65. #if GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB565
  66. SSD1331_MODE, SSD1331_MODE_16_BIT|SSD1331_MODE_COM_SPLIT,
  67. #elif GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_BGR565
  68. SSD1331_MODE, SSD1331_MODE_16_BIT|SSD1331_MODE_COM_SPLIT|SSD1331_MODE_BGR,
  69. #elif GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB332
  70. SSD1331_MODE, SSD1331_MODE_8_BIT|SSD1331_MODE_COM_SPLIT,
  71. #elif GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_BGR332
  72. SSD1331_MODE, SSD1331_MODE_8_BIT|SSD1331_MODE_COM_SPLIT|SSD1331_MODE_BGR,
  73. #else
  74. #error "SSD1331: Invalid color format"
  75. #endif
  76. SSD1331_DRAW_MODE, SSD1331_DRAW_FILLRECT,
  77. };
  78. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  79. unsigned i;
  80. // No private area for this controller
  81. g->priv = 0;
  82. // Initialise the board interface
  83. init_board(g);
  84. // Hardware reset
  85. setpin_reset(g, TRUE);
  86. gfxSleepMilliseconds(20);
  87. setpin_reset(g, FALSE);
  88. gfxSleepMilliseconds(20);
  89. // Get the bus for the following initialisation commands
  90. acquire_bus(g);
  91. for(i=0;i<sizeof(initdata);i++)
  92. write_cmd(g, initdata[i]);
  93. write_cmd(g, SSD1331_DISPLAY_ON);
  94. // Finish Init
  95. post_init_board(g);
  96. // Release the bus
  97. release_bus(g);
  98. /* Initialise the GDISP structure */
  99. g->g.Width = GDISP_SCREEN_WIDTH;
  100. g->g.Height = GDISP_SCREEN_HEIGHT;
  101. g->g.Orientation = GDISP_ROTATE_0;
  102. g->g.Powermode = powerOn;
  103. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  104. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  105. return TRUE;
  106. }
  107. #if GDISP_HARDWARE_STREAM_WRITE
  108. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  109. acquire_bus(g);
  110. write_cmd(g, SSD1331_COLUMN_ADDRESS);
  111. write_cmd(g, g->p.x);
  112. write_cmd(g, g->p.x + g->p.cx - 1);
  113. write_cmd(g, SSD1331_ROW_ADDRESS);
  114. write_cmd(g, g->p.y);
  115. write_cmd(g, g->p.y + g->p.cy - 1);
  116. }
  117. #if GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB565 || GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_BGR565
  118. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  119. LLDCOLOR_TYPE c;
  120. c = gdispColor2Native(g->p.color);
  121. write_data(g, c >> 8);
  122. write_data(g, c);
  123. }
  124. #else
  125. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  126. write_data(g, gdispColor2Native(g->p.color));
  127. }
  128. #endif
  129. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  130. release_bus(g);
  131. }
  132. #endif
  133. #if GDISP_HARDWARE_FILLS
  134. LLDSPEC void gdisp_lld_fill_area(GDisplay *g) {
  135. uint8_t a6, b6, c6;
  136. acquire_bus(g);
  137. if (g->p.color == Black) {
  138. // Use clear window command
  139. write_cmd(g, SSD1331_DRAW_CLEAR);
  140. write_cmd(g, g->p.x);
  141. write_cmd(g, g->p.y);
  142. write_cmd(g, g->p.x + g->p.cx - 1);
  143. write_cmd(g, g->p.y + g->p.cy - 1);
  144. release_bus(g);
  145. return;
  146. }
  147. #if GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB565 || GDISP_LLD_PIXELFORMAT == GDISP_PIXELFORMAT_RGB332
  148. c6 = RED_OF(g->p.color) >> 2;
  149. #if COLOR_BITS_R < 5
  150. if (c6 & 0x20) c6 |= (1<<(6-COLOR_BITS_R))-1;
  151. #endif
  152. b6 = GREEN_OF(g->p.color) >> 2;
  153. #if COLOR_BITS_G < 6
  154. if (b6 & 0x20) b6 |= (1<<(6-COLOR_BITS_G))-1;
  155. #endif
  156. a6 = BLUE_OF(g->p.color) >> 2;
  157. #if COLOR_BITS_B < 5
  158. if (a6 & 0x20) a6 |= (1<<(6-COLOR_BITS_B))-1;
  159. #endif
  160. #else
  161. c6 = BLUE_OF(g->p.color) >> 2;
  162. #if COLOR_BITS_B < 5
  163. if (c6 & 0x20) c6 |= (1<<(6-COLOR_BITS_B))-1;
  164. #endif
  165. b6 = GREEN_OF(g->p.color) >> 2;
  166. #if COLOR_BITS_G < 6
  167. if (b6 & 0x20) b6 |= (1<<(6-COLOR_BITS_G))-1;
  168. #endif
  169. a6 = RED_OF(g->p.color) >> 2;
  170. #if COLOR_BITS_R < 5
  171. if (a6 & 0x20) a6 |= (1<<(6-COLOR_BITS_R))-1;
  172. #endif
  173. #endif
  174. if (g->p.cx == 1 || g->p.cy == 1) {
  175. // Use Line command
  176. write_cmd(g, SSD1331_DRAW_LINE);
  177. write_cmd(g, g->p.x);
  178. write_cmd(g, g->p.y);
  179. write_cmd(g, g->p.x + g->p.cx - 1);
  180. write_cmd(g, g->p.y + g->p.cy - 1);
  181. } else {
  182. // Use Rectangle command
  183. write_cmd(g, SSD1331_DRAW_RECT);
  184. write_cmd(g, g->p.x);
  185. write_cmd(g, g->p.y);
  186. write_cmd(g, g->p.x + g->p.cx - 1);
  187. write_cmd(g, g->p.y + g->p.cy - 1);
  188. //outline
  189. write_cmd(g, c6);
  190. write_cmd(g, b6);
  191. write_cmd(g, a6);
  192. //fill
  193. }
  194. write_cmd(g, c6);
  195. write_cmd(g, b6);
  196. write_cmd(g, a6);
  197. release_bus(g);
  198. }
  199. #endif
  200. #if GDISP_NEED_SCROLL && GDISP_HARDWARE_SCROLL
  201. LLDSPEC void gdisp_lld_vertical_scroll(GDisplay *g) {
  202. acquire_bus(g);
  203. write_cmd(g, SSD1331_DRAW_COPY);
  204. write_cmd(g, g->p.x);
  205. write_cmd(g, g->p.y);
  206. write_cmd(g, g->p.x + g->p.cx - 1);
  207. write_cmd(g, g->p.y + g->p.cy - 1);
  208. write_cmd(g, g->p.x);
  209. write_cmd(g, g->p.y - lines);
  210. release_bus(g);
  211. }
  212. #endif
  213. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  214. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  215. switch(g->p.x) {
  216. case GDISP_CONTROL_POWER:
  217. if (g->g.Powermode == (powermode_t)g->p.ptr)
  218. return;
  219. switch((powermode_t)g->p.ptr) {
  220. case powerOff:
  221. case powerSleep:
  222. case powerDeepSleep:
  223. acquire_bus(g);
  224. write_cmd(g, SSD1331_DISPLAY_OFF);
  225. release_bus(g);
  226. break;
  227. case powerOn:
  228. acquire_bus(g);
  229. write_cmd(g, SSD1331_DISPLAY_ON);
  230. release_bus(g);
  231. break;
  232. default:
  233. return;
  234. }
  235. g->g.Powermode = (powermode_t)g->p.ptr;
  236. return;
  237. /*
  238. case GDISP_CONTROL_ORIENTATION:
  239. if (g->g.Orientation == (orientation_t)g->p.ptr)
  240. return;
  241. switch((orientation_t)g->p.ptr) {
  242. case GDISP_ROTATE_0:
  243. acquire_bus(g);
  244. //TODO
  245. release_bus(g);
  246. g->g.Height = GDISP_SCREEN_HEIGHT;
  247. g->g.Width = GDISP_SCREEN_WIDTH;
  248. break;
  249. case GDISP_ROTATE_90:
  250. acquire_bus(g);
  251. //TODO
  252. release_bus(g);
  253. g->g.Height = GDISP_SCREEN_WIDTH;
  254. g->g.Width = GDISP_SCREEN_HEIGHT;
  255. break;
  256. case GDISP_ROTATE_180:
  257. acquire_bus(g);
  258. //TODO
  259. release_bus(g);
  260. g->g.Height = GDISP_SCREEN_HEIGHT;
  261. g->g.Width = GDISP_SCREEN_WIDTH;
  262. break;
  263. case GDISP_ROTATE_270:
  264. acquire_bus(g);
  265. //TODO
  266. release_bus(g);
  267. g->g.Height = GDISP_SCREEN_WIDTH;
  268. g->g.Width = GDISP_SCREEN_HEIGHT;
  269. break;
  270. default:
  271. return;
  272. }
  273. g->g.Orientation = (orientation_t)g->p.ptr;
  274. return;
  275. */
  276. case GDISP_CONTROL_BACKLIGHT:
  277. if ((unsigned)g->p.ptr > 100)
  278. g->p.ptr = (void *)100;
  279. acquire_bus(g);
  280. write_cmd(g, SSD1331_BRIGHTNESS);
  281. write_cmd(g, ((unsigned)g->p.ptr*10)/63);
  282. release_bus(g);
  283. g->g.Backlight = (unsigned)g->p.ptr;
  284. return;
  285. //case GDISP_CONTROL_CONTRAST:
  286. default:
  287. return;
  288. }
  289. }
  290. #endif
  291. #endif /* GFX_USE_GDISP */