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