]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/lets_split/ssd1306.c
3c7816bb3277858d7758bfb4f920d5fb66c62cd4
[qmk_firmware.git] / keyboards / lets_split / ssd1306.c
1 #include "config.h"
2 #include "i2c.h"
3 #include <stdbool.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include "print.h"
7 #include "lets_split.h"
8 #include "common/glcdfont.c"
9 #ifdef ADAFRUIT_BLE_ENABLE
10 #include "adafruit_ble.h"
11 #endif
12 #ifdef PROTOCOL_LUFA
13 #include "lufa.h"
14 #endif
15 #include "sendchar.h"
16 #include "pincontrol.h"
17
18 //assign the right code to your layers
19 #define _BASE 0
20 #define _LOWER 8
21 #define _RAISE 16
22 #define _FNLAYER 64
23 #define _NUMLAY 128
24 #define _NLOWER 136
25 #define _NFNLAYER 192
26 #define _MOUSECURSOR 256
27 #define _ADJUST 65560
28
29 // Set this to 1 to help diagnose early startup problems
30 // when testing power-on with ble.  Turn it off otherwise,
31 // as the latency of printing most of the debug info messes
32 // with the matrix scan, causing keys to drop.
33 #define DEBUG_TO_SCREEN 0
34
35 // Controls the SSD1306 128x32 OLED display via i2c
36
37 #define i2cAddress 0x3C
38
39 #define DisplayHeight 32
40 #define DisplayWidth 128
41
42 #define FontHeight 8
43 #define FontWidth 6
44
45 #define MatrixRows (DisplayHeight / FontHeight)
46 #define MatrixCols (DisplayWidth / FontWidth)
47
48 struct CharacterMatrix {
49   uint8_t display[MatrixRows][MatrixCols];
50   uint8_t *cursor;
51   bool dirty;
52 };
53
54 static struct CharacterMatrix display;
55 //static uint16_t last_battery_update;
56 //static uint32_t vbat;
57 //#define BatteryUpdateInterval 10000 /* milliseconds */
58 #define ScreenOffInterval 300000 /* milliseconds */
59 #if DEBUG_TO_SCREEN
60 static uint8_t displaying;
61 #endif
62 static uint16_t last_flush;
63
64 enum ssd1306_cmds {
65   DisplayOff = 0xAE,
66   DisplayOn = 0xAF,
67
68   SetContrast = 0x81,
69   DisplayAllOnResume = 0xA4,
70
71   DisplayAllOn = 0xA5,
72   NormalDisplay = 0xA6,
73   InvertDisplay = 0xA7,
74   SetDisplayOffset = 0xD3,
75   SetComPins = 0xda,
76   SetVComDetect = 0xdb,
77   SetDisplayClockDiv = 0xD5,
78   SetPreCharge = 0xd9,
79   SetMultiPlex = 0xa8,
80   SetLowColumn = 0x00,
81   SetHighColumn = 0x10,
82   SetStartLine = 0x40,
83
84   SetMemoryMode = 0x20,
85   ColumnAddr = 0x21,
86   PageAddr = 0x22,
87
88   ComScanInc = 0xc0,
89   ComScanDec = 0xc8,
90   SegRemap = 0xa0,
91   SetChargePump = 0x8d,
92   ExternalVcc = 0x01,
93   SwitchCapVcc = 0x02,
94
95   ActivateScroll = 0x2f,
96   DeActivateScroll = 0x2e,
97   SetVerticalScrollArea = 0xa3,
98   RightHorizontalScroll = 0x26,
99   LeftHorizontalScroll = 0x27,
100   VerticalAndRightHorizontalScroll = 0x29,
101   VerticalAndLeftHorizontalScroll = 0x2a,
102 };
103
104
105 // Write command sequence.
106 // Returns true on success.
107 static inline bool _send_cmd1(uint8_t cmd) {
108   bool res = false;
109
110   if (i2c_start_write(i2cAddress)) {
111     xprintf("failed to start write to %d\n", i2cAddress);
112     goto done;
113   }
114
115   if (i2c_master_write(0x0 /* command byte follows */)) {
116     print("failed to write control byte\n");
117
118     goto done;
119   }
120
121   if (i2c_master_write(cmd)) {
122     xprintf("failed to write command %d\n", cmd);
123     goto done;
124   }
125   res = true;
126 done:
127   i2c_master_stop();
128   return res;
129 }
130
131 // Write 2-byte command sequence.
132 // Returns true on success
133 static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
134   if (!_send_cmd1(cmd)) {
135     return false;
136   }
137   return _send_cmd1(opr);
138 }
139
140 // Write 3-byte command sequence.
141 // Returns true on success
142 static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
143   if (!_send_cmd1(cmd)) {
144     return false;
145   }
146   if (!_send_cmd1(opr1)) {
147     return false;
148   }
149   return _send_cmd1(opr2);
150 }
151
152 #define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
153 #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
154 #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
155
156 static void matrix_clear(struct CharacterMatrix *matrix);
157
158 static void clear_display(void) {
159   matrix_clear(&display);
160
161   // Clear all of the display bits (there can be random noise
162   // in the RAM on startup)
163   send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
164   send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
165
166   if (i2c_start_write(i2cAddress)) {
167     goto done;
168   }
169   if (i2c_master_write(0x40)) {
170     // Data mode
171     goto done;
172   }
173   for (uint8_t row = 0; row < MatrixRows; ++row) {
174     for (uint8_t col = 0; col < DisplayWidth; ++col) {
175       i2c_master_write(0);
176     }
177   }
178
179   display.dirty = false;
180
181 done:
182   i2c_master_stop();
183 }
184
185 #if DEBUG_TO_SCREEN
186 #undef sendchar
187 static int8_t capture_sendchar(uint8_t c) {
188   sendchar(c);
189   iota_gfx_write_char(c);
190
191   if (!displaying) {
192     iota_gfx_flush();
193   }
194   return 0;
195 }
196 #endif
197
198 bool iota_gfx_init(void) {
199   bool success = false;
200
201   send_cmd1(DisplayOff);
202   send_cmd2(SetDisplayClockDiv, 0x80);
203   send_cmd2(SetMultiPlex, DisplayHeight - 1);
204
205   send_cmd2(SetDisplayOffset, 0);
206
207
208   send_cmd1(SetStartLine | 0x0);
209   send_cmd2(SetChargePump, 0x14 /* Enable */);
210   send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
211
212 /// Flips the display orientation 0 degrees
213   send_cmd1(SegRemap | 0x1);
214   send_cmd1(ComScanDec);
215 /*
216 // the following Flip the display orientation 180 degrees
217   send_cmd1(SegRemap);
218   send_cmd1(ComScanInc);
219 // end flip */
220   send_cmd2(SetComPins, 0x2);
221   send_cmd2(SetContrast, 0x8f);
222   send_cmd2(SetPreCharge, 0xf1);
223   send_cmd2(SetVComDetect, 0x40);
224   send_cmd1(DisplayAllOnResume);
225   send_cmd1(NormalDisplay);
226   send_cmd1(DeActivateScroll);
227   send_cmd1(DisplayOn);
228
229   send_cmd2(SetContrast, 0); // Dim
230
231   clear_display();
232
233   success = true;
234
235   iota_gfx_flush();
236
237 #if DEBUG_TO_SCREEN
238   print_set_sendchar(capture_sendchar);
239 #endif
240
241 done:
242   return success;
243 }
244
245 bool iota_gfx_off(void) {
246   bool success = false;
247
248   send_cmd1(DisplayOff);
249   success = true;
250
251 done:
252   return success;
253
254
255 bool iota_gfx_on(void) {
256   bool success = false;
257
258   send_cmd1(DisplayOn);
259   success = true;
260
261 done:
262   return success;
263 }
264
265 static void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
266   *matrix->cursor = c;
267   ++matrix->cursor;
268
269   if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
270     // We went off the end; scroll the display upwards by one line
271     memmove(&matrix->display[0], &matrix->display[1],
272             MatrixCols * (MatrixRows - 1));
273     matrix->cursor = &matrix->display[MatrixRows - 1][0];
274     memset(matrix->cursor, ' ', MatrixCols);
275   }
276 }
277
278 static void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
279   matrix->dirty = true;
280
281   if (c == '\n') {
282     // Clear to end of line from the cursor and then move to the
283     // start of the next line
284     uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
285
286     while (cursor_col++ < MatrixCols) {
287       matrix_write_char_inner(matrix, ' ');
288     }
289     return;
290   }
291
292   matrix_write_char_inner(matrix, c);
293 }
294
295 void iota_gfx_write_char(uint8_t c) {
296   matrix_write_char(&display, c);
297 }
298
299 static void matrix_write(struct CharacterMatrix *matrix, const char *data) {
300   const char *end = data + strlen(data);
301   while (data < end) {
302     matrix_write_char(matrix, *data);
303     ++data;
304   }
305 }
306
307 void iota_gfx_write(const char *data) {
308   matrix_write(&display, data);
309 }
310
311 static void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
312   while (true) {
313     uint8_t c = pgm_read_byte(data);
314     if (c == 0) {
315       return;
316     }
317     matrix_write_char(matrix, c);
318     ++data;
319   }
320 }
321
322 void iota_gfx_write_P(const char *data) {
323   matrix_write_P(&display, data);
324 }
325
326 static void matrix_clear(struct CharacterMatrix *matrix) {
327   memset(matrix->display, ' ', sizeof(matrix->display));
328   matrix->cursor = &matrix->display[0][0];
329   matrix->dirty = true;
330 }
331
332 void iota_gfx_clear_screen(void) {
333   matrix_clear(&display);
334 }
335
336 static void matrix_render(struct CharacterMatrix *matrix) {
337   last_flush = timer_read();
338   iota_gfx_on();
339 #if DEBUG_TO_SCREEN
340   ++displaying;
341 #endif
342
343   // Move to the home position
344   send_cmd3(PageAddr, 0, MatrixRows - 1);
345   send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
346
347   if (i2c_start_write(i2cAddress)) {
348     goto done;
349   }
350   if (i2c_master_write(0x40)) {
351     // Data mode
352     goto done;
353   }
354
355   for (uint8_t row = 0; row < MatrixRows; ++row) {
356     for (uint8_t col = 0; col < MatrixCols; ++col) {
357       const uint8_t *glyph = font + (matrix->display[row][col] * (FontWidth - 1));
358
359       for (uint8_t glyphCol = 0; glyphCol < FontWidth - 1; ++glyphCol) {
360         uint8_t colBits = pgm_read_byte(glyph + glyphCol);
361         i2c_master_write(colBits);
362       }
363
364       // 1 column of space between chars (it's not included in the glyph)
365       i2c_master_write(0);
366     }
367   }
368
369   matrix->dirty = false;
370
371 done:
372   i2c_master_stop();
373 #if DEBUG_TO_SCREEN
374   --displaying;
375 #endif
376 }
377
378 void iota_gfx_flush(void) {
379   matrix_render(&display);
380 }
381
382 static void matrix_update(struct CharacterMatrix *dest,
383                           const struct CharacterMatrix *source) {
384   if (memcmp(dest->display, source->display, sizeof(dest->display))) {
385     memcpy(dest->display, source->display, sizeof(dest->display));
386     dest->dirty = true;
387   }
388 }
389
390 static void render_status_info(void) {
391 #if DEBUG_TO_SCREEN
392   if (debug_enable) {
393     return;
394   }
395 #endif
396
397   struct CharacterMatrix matrix;
398
399   matrix_clear(&matrix);
400   matrix_write_P(&matrix, PSTR("USB: "));
401 #ifdef PROTOCOL_LUFA
402   switch (USB_DeviceState) {
403     case DEVICE_STATE_Unattached:
404       matrix_write_P(&matrix, PSTR("Unattached"));
405       break;
406     case DEVICE_STATE_Suspended:
407       matrix_write_P(&matrix, PSTR("Suspended"));
408       break;
409     case DEVICE_STATE_Configured:
410       matrix_write_P(&matrix, PSTR("Connected"));
411       break;
412     case DEVICE_STATE_Powered:
413       matrix_write_P(&matrix, PSTR("Powered"));
414       break;
415     case DEVICE_STATE_Default:
416       matrix_write_P(&matrix, PSTR("Default"));
417       break;
418     case DEVICE_STATE_Addressed:
419       matrix_write_P(&matrix, PSTR("Addressed"));
420       break;
421     default:
422       matrix_write_P(&matrix, PSTR("Invalid"));
423   }
424 #endif
425
426 // Define layers here, Have not worked out how to have text displayed for each layer. Copy down the number you see and add a case for it below
427
428   char buf[40];
429   snprintf(buf,sizeof(buf), "Undef-%ld", layer_state);
430   matrix_write_P(&matrix, PSTR("\n\nLayer: "));
431     switch (layer_state) {
432         case _BASE:
433            matrix_write_P(&matrix, PSTR("Default"));
434            break;
435         case _RAISE:
436            matrix_write_P(&matrix, PSTR("Raise"));
437            break;
438         case _LOWER:
439            matrix_write_P(&matrix, PSTR("Lower"));
440            break;
441         case _ADJUST:
442            matrix_write_P(&matrix, PSTR("ADJUST"));
443            break;
444         default:
445            matrix_write(&matrix, buf);
446  }
447   
448   // Host Keyboard LED Status
449   char led[40];
450     snprintf(led, sizeof(led), "\n%s  %s  %s",
451             (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? "NUMLOCK" : "       ",
452             (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? "CAPS" : "    ",
453             (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) ? "SCLK" : "    ");
454   matrix_write(&matrix, led);
455   matrix_update(&display, &matrix);
456 }
457
458 void iota_gfx_task(void) {
459   render_status_info();
460
461   if (display.dirty) {
462     iota_gfx_flush();
463   }
464
465   if (timer_elapsed(last_flush) > ScreenOffInterval) {
466     iota_gfx_off();
467   }
468 }