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