]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/crkbd/ssd1306.c
Keyboard: Update logo and commonize it (#4151)
[qmk_firmware.git] / keyboards / crkbd / ssd1306.c
1 #ifdef SSD1306OLED
2
3 #include "ssd1306.h"
4 #include "i2c.h"
5 #include <string.h>
6 #include "print.h"
7 #ifdef ADAFRUIT_BLE_ENABLE
8 #include "adafruit_ble.h"
9 #endif
10 #ifdef PROTOCOL_LUFA
11 #include "lufa.h"
12 #endif
13 #include "sendchar.h"
14 #include "timer.h"
15
16 static const unsigned char font[] PROGMEM;
17
18 // Set this to 1 to help diagnose early startup problems
19 // when testing power-on with ble.  Turn it off otherwise,
20 // as the latency of printing most of the debug info messes
21 // with the matrix scan, causing keys to drop.
22 #define DEBUG_TO_SCREEN 0
23
24 //static uint16_t last_battery_update;
25 //static uint32_t vbat;
26 //#define BatteryUpdateInterval 10000 /* milliseconds */
27 #define ScreenOffInterval 300000 /* milliseconds */
28 #if DEBUG_TO_SCREEN
29 static uint8_t displaying;
30 #endif
31 static uint16_t last_flush;
32
33 // Write command sequence.
34 // Returns true on success.
35 static inline bool _send_cmd1(uint8_t cmd) {
36   bool res = false;
37
38   if (i2c_start_write(SSD1306_ADDRESS)) {
39     xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
40     goto done;
41   }
42
43   if (i2c_master_write(0x0 /* command byte follows */)) {
44     print("failed to write control byte\n");
45
46     goto done;
47   }
48
49   if (i2c_master_write(cmd)) {
50     xprintf("failed to write command %d\n", cmd);
51     goto done;
52   }
53   res = true;
54 done:
55   i2c_master_stop();
56   return res;
57 }
58
59 // Write 2-byte command sequence.
60 // Returns true on success
61 static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
62   if (!_send_cmd1(cmd)) {
63     return false;
64   }
65   return _send_cmd1(opr);
66 }
67
68 // Write 3-byte command sequence.
69 // Returns true on success
70 static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
71   if (!_send_cmd1(cmd)) {
72     return false;
73   }
74   if (!_send_cmd1(opr1)) {
75     return false;
76   }
77   return _send_cmd1(opr2);
78 }
79
80 #define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
81 #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
82 #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
83
84 static void clear_display(void) {
85   matrix_clear(&display);
86
87   // Clear all of the display bits (there can be random noise
88   // in the RAM on startup)
89   send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
90   send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
91
92   if (i2c_start_write(SSD1306_ADDRESS)) {
93     goto done;
94   }
95   if (i2c_master_write(0x40)) {
96     // Data mode
97     goto done;
98   }
99   for (uint8_t row = 0; row < MatrixRows; ++row) {
100     for (uint8_t col = 0; col < DisplayWidth; ++col) {
101       i2c_master_write(0);
102     }
103   }
104
105   display.dirty = false;
106
107 done:
108   i2c_master_stop();
109 }
110
111 #if DEBUG_TO_SCREEN
112 #undef sendchar
113 static int8_t capture_sendchar(uint8_t c) {
114   sendchar(c);
115   iota_gfx_write_char(c);
116
117   if (!displaying) {
118     iota_gfx_flush();
119   }
120   return 0;
121 }
122 #endif
123
124 bool iota_gfx_init(bool rotate) {
125   bool success = false;
126
127   i2c_master_init();
128   send_cmd1(DisplayOff);
129   send_cmd2(SetDisplayClockDiv, 0x80);
130   send_cmd2(SetMultiPlex, DisplayHeight - 1);
131
132   send_cmd2(SetDisplayOffset, 0);
133
134
135   send_cmd1(SetStartLine | 0x0);
136   send_cmd2(SetChargePump, 0x14 /* Enable */);
137   send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
138
139   if(rotate){
140     // the following Flip the display orientation 180 degrees
141     send_cmd1(SegRemap);
142     send_cmd1(ComScanInc);
143   }else{
144     // Flips the display orientation 0 degrees
145     send_cmd1(SegRemap | 0x1);
146     send_cmd1(ComScanDec);
147   }
148
149   send_cmd2(SetComPins, 0x2);
150   send_cmd2(SetContrast, 0x8f);
151   send_cmd2(SetPreCharge, 0xf1);
152   send_cmd2(SetVComDetect, 0x40);
153   send_cmd1(DisplayAllOnResume);
154   send_cmd1(NormalDisplay);
155   send_cmd1(DeActivateScroll);
156   send_cmd1(DisplayOn);
157
158   send_cmd2(SetContrast, 0); // Dim
159
160   clear_display();
161
162   success = true;
163
164   iota_gfx_flush();
165
166 #if DEBUG_TO_SCREEN
167   print_set_sendchar(capture_sendchar);
168 #endif
169
170 done:
171   return success;
172 }
173
174 bool iota_gfx_off(void) {
175   bool success = false;
176
177   send_cmd1(DisplayOff);
178   success = true;
179
180 done:
181   return success;
182 }
183
184 bool iota_gfx_on(void) {
185   bool success = false;
186
187   send_cmd1(DisplayOn);
188   success = true;
189
190 done:
191   return success;
192 }
193
194 void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
195   *matrix->cursor = c;
196   ++matrix->cursor;
197
198   if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
199     // We went off the end; scroll the display upwards by one line
200     memmove(&matrix->display[0], &matrix->display[1],
201             MatrixCols * (MatrixRows - 1));
202     matrix->cursor = &matrix->display[MatrixRows - 1][0];
203     memset(matrix->cursor, ' ', MatrixCols);
204   }
205 }
206
207 void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
208   matrix->dirty = true;
209
210   if (c == '\n') {
211     // Clear to end of line from the cursor and then move to the
212     // start of the next line
213     uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
214
215     while (cursor_col++ < MatrixCols) {
216       matrix_write_char_inner(matrix, ' ');
217     }
218     return;
219   }
220
221   matrix_write_char_inner(matrix, c);
222 }
223
224 void iota_gfx_write_char(uint8_t c) {
225   matrix_write_char(&display, c);
226 }
227
228 void matrix_write(struct CharacterMatrix *matrix, const char *data) {
229   const char *end = data + strlen(data);
230   while (data < end) {
231     matrix_write_char(matrix, *data);
232     ++data;
233   }
234 }
235
236 void matrix_write_ln(struct CharacterMatrix *matrix, const char *data) {
237   char data_ln[strlen(data)+2];
238   snprintf(data_ln, sizeof(data_ln), "%s\n", data);
239   matrix_write(matrix, data_ln);
240 }
241
242 void iota_gfx_write(const char *data) {
243   matrix_write(&display, data);
244 }
245
246 void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
247   while (true) {
248     uint8_t c = pgm_read_byte(data);
249     if (c == 0) {
250       return;
251     }
252     matrix_write_char(matrix, c);
253     ++data;
254   }
255 }
256
257 void iota_gfx_write_P(const char *data) {
258   matrix_write_P(&display, data);
259 }
260
261 void matrix_clear(struct CharacterMatrix *matrix) {
262   memset(matrix->display, ' ', sizeof(matrix->display));
263   matrix->cursor = &matrix->display[0][0];
264   matrix->dirty = true;
265 }
266
267 void iota_gfx_clear_screen(void) {
268   matrix_clear(&display);
269 }
270
271 void matrix_render(struct CharacterMatrix *matrix) {
272   last_flush = timer_read();
273   iota_gfx_on();
274 #if DEBUG_TO_SCREEN
275   ++displaying;
276 #endif
277
278   // Move to the home position
279   send_cmd3(PageAddr, 0, MatrixRows - 1);
280   send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
281
282   if (i2c_start_write(SSD1306_ADDRESS)) {
283     goto done;
284   }
285   if (i2c_master_write(0x40)) {
286     // Data mode
287     goto done;
288   }
289
290   for (uint8_t row = 0; row < MatrixRows; ++row) {
291     for (uint8_t col = 0; col < MatrixCols; ++col) {
292       const uint8_t *glyph = font + (matrix->display[row][col] * FontWidth);
293
294       for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
295         uint8_t colBits = pgm_read_byte(glyph + glyphCol);
296         i2c_master_write(colBits);
297       }
298
299       // 1 column of space between chars (it's not included in the glyph)
300       //i2c_master_write(0);
301     }
302   }
303
304   matrix->dirty = false;
305
306 done:
307   i2c_master_stop();
308 #if DEBUG_TO_SCREEN
309   --displaying;
310 #endif
311 }
312
313 void iota_gfx_flush(void) {
314   matrix_render(&display);
315 }
316
317 __attribute__ ((weak))
318 void iota_gfx_task_user(void) {
319 }
320
321 void iota_gfx_task(void) {
322   iota_gfx_task_user();
323
324   if (display.dirty) {
325     iota_gfx_flush();
326   }
327
328   if (timer_elapsed(last_flush) > ScreenOffInterval) {
329     iota_gfx_off();
330   }
331 }
332 #endif