gdisp_IS31FL3731C.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "gfx.h"
  15. #if GFX_USE_GDISP
  16. #define GDISP_DRIVER_VMT GDISPVMT_IS31FL3731C_ERGODOX
  17. #include "drivers/gdisp/IS31FL3731C/gdisp_lld_config.h"
  18. #include "src/gdisp/gdisp_driver.h"
  19. #include "board_IS31FL3731C.h"
  20. // Can't include led_tables from here
  21. extern const uint8_t CIE1931_CURVE[];
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. #ifndef GDISP_SCREEN_HEIGHT
  26. #define GDISP_SCREEN_HEIGHT 9
  27. #endif
  28. #ifndef GDISP_SCREEN_WIDTH
  29. #define GDISP_SCREEN_WIDTH 16
  30. #endif
  31. #ifndef GDISP_INITIAL_CONTRAST
  32. #define GDISP_INITIAL_CONTRAST 0
  33. #endif
  34. #ifndef GDISP_INITIAL_BACKLIGHT
  35. #define GDISP_INITIAL_BACKLIGHT 100
  36. #endif
  37. #define GDISP_FLG_NEEDFLUSH (GDISP_FLG_DRIVER<<0)
  38. #define IS31_ADDR_DEFAULT 0x74
  39. #define IS31_REG_CONFIG 0x00
  40. // bits in reg
  41. #define IS31_REG_CONFIG_PICTUREMODE 0x00
  42. #define IS31_REG_CONFIG_AUTOPLAYMODE 0x08
  43. #define IS31_REG_CONFIG_AUDIOPLAYMODE 0x18
  44. // D2:D0 bits are starting frame for autoplay mode
  45. #define IS31_REG_PICTDISP 0x01 // D2:D0 frame select for picture mode
  46. #define IS31_REG_AUTOPLAYCTRL1 0x02
  47. // D6:D4 number of loops (000=infty)
  48. // D2:D0 number of frames to be used
  49. #define IS31_REG_AUTOPLAYCTRL2 0x03 // D5:D0 delay time (*11ms)
  50. #define IS31_REG_DISPLAYOPT 0x05
  51. #define IS31_REG_DISPLAYOPT_INTENSITY_SAME 0x20 // same intensity for all frames
  52. #define IS31_REG_DISPLAYOPT_BLINK_ENABLE 0x8
  53. // D2:D0 bits blink period time (*0.27s)
  54. #define IS31_REG_AUDIOSYNC 0x06
  55. #define IS31_REG_AUDIOSYNC_ENABLE 0x1
  56. #define IS31_REG_FRAMESTATE 0x07
  57. #define IS31_REG_BREATHCTRL1 0x08
  58. // D6:D4 fade out time (26ms*2^i)
  59. // D2:D0 fade in time (26ms*2^i)
  60. #define IS31_REG_BREATHCTRL2 0x09
  61. #define IS31_REG_BREATHCTRL2_ENABLE 0x10
  62. // D2:D0 extinguish time (3.5ms*2^i)
  63. #define IS31_REG_SHUTDOWN 0x0A
  64. #define IS31_REG_SHUTDOWN_OFF 0x0
  65. #define IS31_REG_SHUTDOWN_ON 0x1
  66. #define IS31_REG_AGCCTRL 0x0B
  67. #define IS31_REG_ADCRATE 0x0C
  68. #define IS31_COMMANDREGISTER 0xFD
  69. #define IS31_FUNCTIONREG 0x0B // helpfully called 'page nine'
  70. #define IS31_FUNCTIONREG_SIZE 0xD
  71. #define IS31_FRAME_SIZE 0xB4
  72. #define IS31_PWM_REG 0x24
  73. #define IS31_PWM_SIZE 0x90
  74. #define IS31_LED_MASK_SIZE 0x12
  75. #define IS31_SCREEN_WIDTH 16
  76. #define IS31
  77. /*===========================================================================*/
  78. /* Driver local functions. */
  79. /*===========================================================================*/
  80. typedef struct{
  81. uint8_t write_buffer_offset;
  82. uint8_t write_buffer[IS31_FRAME_SIZE];
  83. uint8_t frame_buffer[GDISP_SCREEN_HEIGHT * GDISP_SCREEN_WIDTH];
  84. uint8_t page;
  85. }__attribute__((__packed__)) PrivData;
  86. // Some common routines and macros
  87. #define PRIV(g) ((PrivData*)g->priv)
  88. /*===========================================================================*/
  89. /* Driver exported functions. */
  90. /*===========================================================================*/
  91. static GFXINLINE void write_page(GDisplay* g, uint8_t page) {
  92. uint8_t tx[2] __attribute__((aligned(2)));
  93. tx[0] = IS31_COMMANDREGISTER;
  94. tx[1] = page;
  95. write_data(g, tx, 2);
  96. }
  97. static GFXINLINE void write_register(GDisplay* g, uint8_t page, uint8_t reg, uint8_t data) {
  98. uint8_t tx[2] __attribute__((aligned(2)));
  99. tx[0] = reg;
  100. tx[1] = data;
  101. write_page(g, page);
  102. write_data(g, tx, 2);
  103. }
  104. static GFXINLINE void write_ram(GDisplay *g, uint8_t page, uint16_t offset, uint16_t length) {
  105. PRIV(g)->write_buffer_offset = offset;
  106. write_page(g, page);
  107. write_data(g, (uint8_t*)PRIV(g), length + 1);
  108. }
  109. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  110. // The private area is the display surface.
  111. g->priv = gfxAlloc(sizeof(PrivData));
  112. __builtin_memset(PRIV(g), 0, sizeof(PrivData));
  113. PRIV(g)->page = 0;
  114. // Initialise the board interface
  115. init_board(g);
  116. gfxSleepMilliseconds(10);
  117. // zero function page, all registers (assuming full_page is all zeroes)
  118. write_ram(g, IS31_FUNCTIONREG, 0, IS31_FUNCTIONREG_SIZE);
  119. set_hardware_shutdown(g, false);
  120. gfxSleepMilliseconds(10);
  121. // software shutdown
  122. write_register(g, IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
  123. gfxSleepMilliseconds(10);
  124. // zero function page, all registers
  125. write_ram(g, IS31_FUNCTIONREG, 0, IS31_FUNCTIONREG_SIZE);
  126. gfxSleepMilliseconds(10);
  127. // zero all LED registers on all 8 pages, and enable the mask
  128. __builtin_memcpy(PRIV(g)->write_buffer, get_led_mask(g), IS31_LED_MASK_SIZE);
  129. for(uint8_t i=0; i<8; i++) {
  130. write_ram(g, i, 0, IS31_FRAME_SIZE);
  131. gfxSleepMilliseconds(1);
  132. }
  133. // software shutdown disable (i.e. turn stuff on)
  134. write_register(g, IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  135. gfxSleepMilliseconds(10);
  136. // Finish Init
  137. post_init_board(g);
  138. /* Initialise the GDISP structure */
  139. g->g.Width = GDISP_SCREEN_WIDTH;
  140. g->g.Height = GDISP_SCREEN_HEIGHT;
  141. g->g.Orientation = GDISP_ROTATE_0;
  142. g->g.Powermode = powerOn;
  143. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  144. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  145. return TRUE;
  146. }
  147. #if GDISP_HARDWARE_FLUSH
  148. LLDSPEC void gdisp_lld_flush(GDisplay *g) {
  149. // Don't flush if we don't need it.
  150. if (!(g->flags & GDISP_FLG_NEEDFLUSH))
  151. return;
  152. PRIV(g)->page++;
  153. PRIV(g)->page %= 2;
  154. // TODO: some smarter algorithm for this
  155. // We should run only one physical page at a time
  156. // This way we don't need to send so much data, and
  157. // we could use slightly less memory
  158. uint8_t* src = PRIV(g)->frame_buffer;
  159. for (int y=0;y<GDISP_SCREEN_HEIGHT;y++) {
  160. for (int x=0;x<GDISP_SCREEN_WIDTH;x++) {
  161. PRIV(g)->write_buffer[get_led_address(g, x, y)]=CIE1931_CURVE[*src];
  162. ++src;
  163. }
  164. }
  165. write_ram(g, PRIV(g)->page, IS31_PWM_REG, IS31_PWM_SIZE);
  166. gfxSleepMilliseconds(1);
  167. write_register(g, IS31_FUNCTIONREG, IS31_REG_PICTDISP, PRIV(g)->page);
  168. g->flags &= ~GDISP_FLG_NEEDFLUSH;
  169. }
  170. #endif
  171. #if GDISP_HARDWARE_DRAWPIXEL
  172. LLDSPEC void gdisp_lld_draw_pixel(GDisplay *g) {
  173. coord_t x, y;
  174. switch(g->g.Orientation) {
  175. default:
  176. case GDISP_ROTATE_0:
  177. x = g->p.x;
  178. y = g->p.y;
  179. break;
  180. case GDISP_ROTATE_180:
  181. x = GDISP_SCREEN_WIDTH-1 - g->p.x;
  182. y = g->p.y;
  183. break;
  184. }
  185. PRIV(g)->frame_buffer[y * GDISP_SCREEN_WIDTH + x] = gdispColor2Native(g->p.color);
  186. g->flags |= GDISP_FLG_NEEDFLUSH;
  187. }
  188. #endif
  189. #if GDISP_HARDWARE_PIXELREAD
  190. LLDSPEC color_t gdisp_lld_get_pixel_color(GDisplay *g) {
  191. coord_t x, y;
  192. switch(g->g.Orientation) {
  193. default:
  194. case GDISP_ROTATE_0:
  195. x = g->p.x;
  196. y = g->p.y;
  197. break;
  198. case GDISP_ROTATE_180:
  199. x = GDISP_SCREEN_WIDTH-1 - g->p.x;
  200. y = g->p.y;
  201. break;
  202. }
  203. return gdispNative2Color(PRIV(g)->frame_buffer[y * GDISP_SCREEN_WIDTH + x]);
  204. }
  205. #endif
  206. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  207. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  208. switch(g->p.x) {
  209. case GDISP_CONTROL_POWER:
  210. if (g->g.Powermode == (powermode_t)g->p.ptr)
  211. return;
  212. switch((powermode_t)g->p.ptr) {
  213. case powerOff:
  214. case powerSleep:
  215. case powerDeepSleep:
  216. write_register(g, IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_OFF);
  217. break;
  218. case powerOn:
  219. write_register(g, IS31_FUNCTIONREG, IS31_REG_SHUTDOWN, IS31_REG_SHUTDOWN_ON);
  220. break;
  221. default:
  222. return;
  223. }
  224. g->g.Powermode = (powermode_t)g->p.ptr;
  225. return;
  226. case GDISP_CONTROL_ORIENTATION:
  227. if (g->g.Orientation == (orientation_t)g->p.ptr)
  228. return;
  229. switch((orientation_t)g->p.ptr) {
  230. /* Rotation is handled by the drawing routines */
  231. case GDISP_ROTATE_0:
  232. case GDISP_ROTATE_180:
  233. g->g.Height = GDISP_SCREEN_HEIGHT;
  234. g->g.Width = GDISP_SCREEN_WIDTH;
  235. break;
  236. case GDISP_ROTATE_90:
  237. case GDISP_ROTATE_270:
  238. g->g.Height = GDISP_SCREEN_WIDTH;
  239. g->g.Width = GDISP_SCREEN_HEIGHT;
  240. break;
  241. default:
  242. return;
  243. }
  244. g->g.Orientation = (orientation_t)g->p.ptr;
  245. return;
  246. case GDISP_CONTROL_CONTRAST:
  247. return;
  248. }
  249. }
  250. #endif // GDISP_NEED_CONTROL
  251. #endif // GFX_USE_GDISP