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