6 #include "lets_split.h"
7 #include "common/glcdfont.c"
8 #ifdef ADAFRUIT_BLE_ENABLE
9 #include "adafruit_ble.h"
15 #include "pincontrol.h"
17 //assign the right code to your layers
25 #define _MOUSECURSOR 256
28 // Set this to 1 to help diagnose early startup problems
29 // when testing power-on with ble. Turn it off otherwise,
30 // as the latency of printing most of the debug info messes
31 // with the matrix scan, causing keys to drop.
32 #define DEBUG_TO_SCREEN 0
34 // Controls the SSD1306 128x32 OLED display via i2c
36 #define i2cAddress 0x3C
38 #define DisplayHeight 32
39 #define DisplayWidth 128
44 #define MatrixRows (DisplayHeight / FontHeight)
45 #define MatrixCols (DisplayWidth / FontWidth)
47 struct CharacterMatrix {
48 uint8_t display[MatrixRows][MatrixCols];
53 static struct CharacterMatrix display;
54 //static uint16_t last_battery_update;
55 //static uint32_t vbat;
56 //#define BatteryUpdateInterval 10000 /* milliseconds */
57 #define ScreenOffInterval 300000 /* milliseconds */
59 static uint8_t displaying;
61 static uint16_t last_flush;
68 DisplayAllOnResume = 0xA4,
73 SetDisplayOffset = 0xD3,
76 SetDisplayClockDiv = 0xD5,
94 ActivateScroll = 0x2f,
95 DeActivateScroll = 0x2e,
96 SetVerticalScrollArea = 0xa3,
97 RightHorizontalScroll = 0x26,
98 LeftHorizontalScroll = 0x27,
99 VerticalAndRightHorizontalScroll = 0x29,
100 VerticalAndLeftHorizontalScroll = 0x2a,
104 // Write command sequence.
105 // Returns true on success.
106 static inline bool _send_cmd1(uint8_t cmd) {
109 if (i2c_start_write(i2cAddress)) {
110 xprintf("failed to start write to %d\n", i2cAddress);
114 if (i2c_master_write(0x0 /* command byte follows */)) {
115 print("failed to write control byte\n");
120 if (i2c_master_write(cmd)) {
121 xprintf("failed to write command %d\n", cmd);
130 // Write 2-byte command sequence.
131 // Returns true on success
132 static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
133 if (!_send_cmd1(cmd)) {
136 return _send_cmd1(opr);
139 // Write 3-byte command sequence.
140 // Returns true on success
141 static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
142 if (!_send_cmd1(cmd)) {
145 if (!_send_cmd1(opr1)) {
148 return _send_cmd1(opr2);
151 #define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
152 #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
153 #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
155 static void matrix_clear(struct CharacterMatrix *matrix);
157 static void clear_display(void) {
158 matrix_clear(&display);
160 // Clear all of the display bits (there can be random noise
161 // in the RAM on startup)
162 send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
163 send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
165 if (i2c_start_write(i2cAddress)) {
168 if (i2c_master_write(0x40)) {
172 for (uint8_t row = 0; row < MatrixRows; ++row) {
173 for (uint8_t col = 0; col < DisplayWidth; ++col) {
178 display.dirty = false;
186 static int8_t capture_sendchar(uint8_t c) {
188 iota_gfx_write_char(c);
197 bool iota_gfx_init(void) {
198 bool success = false;
200 send_cmd1(DisplayOff);
201 send_cmd2(SetDisplayClockDiv, 0x80);
202 send_cmd2(SetMultiPlex, DisplayHeight - 1);
204 send_cmd2(SetDisplayOffset, 0);
207 send_cmd1(SetStartLine | 0x0);
208 send_cmd2(SetChargePump, 0x14 /* Enable */);
209 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
211 /// Flips the display orientation 0 degrees
212 send_cmd1(SegRemap | 0x1);
213 send_cmd1(ComScanDec);
215 // the following Flip the display orientation 180 degrees
217 send_cmd1(ComScanInc);
219 send_cmd2(SetComPins, 0x2);
220 send_cmd2(SetContrast, 0x8f);
221 send_cmd2(SetPreCharge, 0xf1);
222 send_cmd2(SetVComDetect, 0x40);
223 send_cmd1(DisplayAllOnResume);
224 send_cmd1(NormalDisplay);
225 send_cmd1(DeActivateScroll);
226 send_cmd1(DisplayOn);
228 send_cmd2(SetContrast, 0); // Dim
237 print_set_sendchar(capture_sendchar);
244 bool iota_gfx_off(void) {
245 bool success = false;
247 send_cmd1(DisplayOff);
254 bool iota_gfx_on(void) {
255 bool success = false;
257 send_cmd1(DisplayOn);
264 static void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
268 if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
269 // We went off the end; scroll the display upwards by one line
270 memmove(&matrix->display[0], &matrix->display[1],
271 MatrixCols * (MatrixRows - 1));
272 matrix->cursor = &matrix->display[MatrixRows - 1][0];
273 memset(matrix->cursor, ' ', MatrixCols);
277 static void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
278 matrix->dirty = true;
281 // Clear to end of line from the cursor and then move to the
282 // start of the next line
283 uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
285 while (cursor_col++ < MatrixCols) {
286 matrix_write_char_inner(matrix, ' ');
291 matrix_write_char_inner(matrix, c);
294 void iota_gfx_write_char(uint8_t c) {
295 matrix_write_char(&display, c);
298 static void matrix_write(struct CharacterMatrix *matrix, const char *data) {
299 const char *end = data + strlen(data);
301 matrix_write_char(matrix, *data);
306 void iota_gfx_write(const char *data) {
307 matrix_write(&display, data);
310 static void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
312 uint8_t c = pgm_read_byte(data);
316 matrix_write_char(matrix, c);
321 void iota_gfx_write_P(const char *data) {
322 matrix_write_P(&display, data);
325 static void matrix_clear(struct CharacterMatrix *matrix) {
326 memset(matrix->display, ' ', sizeof(matrix->display));
327 matrix->cursor = &matrix->display[0][0];
328 matrix->dirty = true;
331 void iota_gfx_clear_screen(void) {
332 matrix_clear(&display);
335 static void matrix_render(struct CharacterMatrix *matrix) {
336 last_flush = timer_read();
342 // Move to the home position
343 send_cmd3(PageAddr, 0, MatrixRows - 1);
344 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
346 if (i2c_start_write(i2cAddress)) {
349 if (i2c_master_write(0x40)) {
354 for (uint8_t row = 0; row < MatrixRows; ++row) {
355 for (uint8_t col = 0; col < MatrixCols; ++col) {
356 const uint8_t *glyph = font + (matrix->display[row][col] * (FontWidth - 1));
358 for (uint8_t glyphCol = 0; glyphCol < FontWidth - 1; ++glyphCol) {
359 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
360 i2c_master_write(colBits);
363 // 1 column of space between chars (it's not included in the glyph)
368 matrix->dirty = false;
377 void iota_gfx_flush(void) {
378 matrix_render(&display);
381 static void matrix_update(struct CharacterMatrix *dest,
382 const struct CharacterMatrix *source) {
383 if (memcmp(dest->display, source->display, sizeof(dest->display))) {
384 memcpy(dest->display, source->display, sizeof(dest->display));
389 static void render_status_info(void) {
396 struct CharacterMatrix matrix;
398 matrix_clear(&matrix);
399 matrix_write_P(&matrix, PSTR("USB: "));
401 switch (USB_DeviceState) {
402 case DEVICE_STATE_Unattached:
403 matrix_write_P(&matrix, PSTR("Unattached"));
405 case DEVICE_STATE_Suspended:
406 matrix_write_P(&matrix, PSTR("Suspended"));
408 case DEVICE_STATE_Configured:
409 matrix_write_P(&matrix, PSTR("Connected"));
411 case DEVICE_STATE_Powered:
412 matrix_write_P(&matrix, PSTR("Powered"));
414 case DEVICE_STATE_Default:
415 matrix_write_P(&matrix, PSTR("Default"));
417 case DEVICE_STATE_Addressed:
418 matrix_write_P(&matrix, PSTR("Addressed"));
421 matrix_write_P(&matrix, PSTR("Invalid"));
425 // 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
428 snprintf(buf,sizeof(buf), "Undef-%ld", layer_state);
429 matrix_write_P(&matrix, PSTR("\n\nLayer: "));
430 switch (layer_state) {
432 matrix_write_P(&matrix, PSTR("Default"));
435 matrix_write_P(&matrix, PSTR("Raise"));
438 matrix_write_P(&matrix, PSTR("Lower"));
441 matrix_write_P(&matrix, PSTR("ADJUST"));
444 matrix_write(&matrix, buf);
447 // Host Keyboard LED Status
449 snprintf(led, sizeof(led), "\n%s %s %s",
450 (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? "NUMLOCK" : " ",
451 (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? "CAPS" : " ",
452 (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) ? "SCLK" : " ");
453 matrix_write(&matrix, led);
454 matrix_update(&display, &matrix);
457 void iota_gfx_task(void) {
458 render_status_info();
464 if (timer_elapsed(last_flush) > ScreenOffInterval) {