7 #include "lets_split.h"
8 #include "common/glcdfont.c"
9 #ifdef ADAFRUIT_BLE_ENABLE
10 #include "adafruit_ble.h"
16 #include "pincontrol.h"
18 //assign the right code to your layers
26 #define _MOUSECURSOR 256
29 // Set this to 1 to help diagnose early startup problems
30 // when testing power-on with ble. Turn it off otherwise,
31 // as the latency of printing most of the debug info messes
32 // with the matrix scan, causing keys to drop.
33 #define DEBUG_TO_SCREEN 0
35 // Controls the SSD1306 128x32 OLED display via i2c
37 #define i2cAddress 0x3C
39 #define DisplayHeight 32
40 #define DisplayWidth 128
45 #define MatrixRows (DisplayHeight / FontHeight)
46 #define MatrixCols (DisplayWidth / FontWidth)
48 struct CharacterMatrix {
49 uint8_t display[MatrixRows][MatrixCols];
54 static struct CharacterMatrix display;
55 //static uint16_t last_battery_update;
56 //static uint32_t vbat;
57 //#define BatteryUpdateInterval 10000 /* milliseconds */
58 #define ScreenOffInterval 300000 /* milliseconds */
60 static uint8_t displaying;
62 static uint16_t last_flush;
69 DisplayAllOnResume = 0xA4,
74 SetDisplayOffset = 0xD3,
77 SetDisplayClockDiv = 0xD5,
95 ActivateScroll = 0x2f,
96 DeActivateScroll = 0x2e,
97 SetVerticalScrollArea = 0xa3,
98 RightHorizontalScroll = 0x26,
99 LeftHorizontalScroll = 0x27,
100 VerticalAndRightHorizontalScroll = 0x29,
101 VerticalAndLeftHorizontalScroll = 0x2a,
105 // Write command sequence.
106 // Returns true on success.
107 static inline bool _send_cmd1(uint8_t cmd) {
110 if (i2c_start_write(i2cAddress)) {
111 xprintf("failed to start write to %d\n", i2cAddress);
115 if (i2c_master_write(0x0 /* command byte follows */)) {
116 print("failed to write control byte\n");
121 if (i2c_master_write(cmd)) {
122 xprintf("failed to write command %d\n", cmd);
131 // Write 2-byte command sequence.
132 // Returns true on success
133 static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
134 if (!_send_cmd1(cmd)) {
137 return _send_cmd1(opr);
140 // Write 3-byte command sequence.
141 // Returns true on success
142 static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
143 if (!_send_cmd1(cmd)) {
146 if (!_send_cmd1(opr1)) {
149 return _send_cmd1(opr2);
152 #define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
153 #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
154 #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
156 static void matrix_clear(struct CharacterMatrix *matrix);
158 static void clear_display(void) {
159 matrix_clear(&display);
161 // Clear all of the display bits (there can be random noise
162 // in the RAM on startup)
163 send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
164 send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
166 if (i2c_start_write(i2cAddress)) {
169 if (i2c_master_write(0x40)) {
173 for (uint8_t row = 0; row < MatrixRows; ++row) {
174 for (uint8_t col = 0; col < DisplayWidth; ++col) {
179 display.dirty = false;
187 static int8_t capture_sendchar(uint8_t c) {
189 iota_gfx_write_char(c);
198 bool iota_gfx_init(void) {
199 bool success = false;
201 send_cmd1(DisplayOff);
202 send_cmd2(SetDisplayClockDiv, 0x80);
203 send_cmd2(SetMultiPlex, DisplayHeight - 1);
205 send_cmd2(SetDisplayOffset, 0);
208 send_cmd1(SetStartLine | 0x0);
209 send_cmd2(SetChargePump, 0x14 /* Enable */);
210 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
212 /* Flips the display orientation 0 degrees
213 send_cmd1(SegRemap | 0x1);
214 send_cmd1(ComScanDec);
216 // the following Flip the display orientation 180 degrees */
218 send_cmd1(ComScanInc);
220 send_cmd2(SetComPins, 0x2);
221 send_cmd2(SetContrast, 0x8f);
222 send_cmd2(SetPreCharge, 0xf1);
223 send_cmd2(SetVComDetect, 0x40);
224 send_cmd1(DisplayAllOnResume);
225 send_cmd1(NormalDisplay);
226 send_cmd1(DeActivateScroll);
227 send_cmd1(DisplayOn);
229 send_cmd2(SetContrast, 0); // Dim
238 print_set_sendchar(capture_sendchar);
245 bool iota_gfx_off(void) {
246 bool success = false;
248 send_cmd1(DisplayOff);
255 bool iota_gfx_on(void) {
256 bool success = false;
258 send_cmd1(DisplayOn);
265 static void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
269 if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
270 // We went off the end; scroll the display upwards by one line
271 memmove(&matrix->display[0], &matrix->display[1],
272 MatrixCols * (MatrixRows - 1));
273 matrix->cursor = &matrix->display[MatrixRows - 1][0];
274 memset(matrix->cursor, ' ', MatrixCols);
278 static void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
279 matrix->dirty = true;
282 // Clear to end of line from the cursor and then move to the
283 // start of the next line
284 uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
286 while (cursor_col++ < MatrixCols) {
287 matrix_write_char_inner(matrix, ' ');
292 matrix_write_char_inner(matrix, c);
295 void iota_gfx_write_char(uint8_t c) {
296 matrix_write_char(&display, c);
299 static void matrix_write(struct CharacterMatrix *matrix, const char *data) {
300 const char *end = data + strlen(data);
302 matrix_write_char(matrix, *data);
307 void iota_gfx_write(const char *data) {
308 matrix_write(&display, data);
311 static void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
313 uint8_t c = pgm_read_byte(data);
317 matrix_write_char(matrix, c);
322 void iota_gfx_write_P(const char *data) {
323 matrix_write_P(&display, data);
326 static void matrix_clear(struct CharacterMatrix *matrix) {
327 memset(matrix->display, ' ', sizeof(matrix->display));
328 matrix->cursor = &matrix->display[0][0];
329 matrix->dirty = true;
332 void iota_gfx_clear_screen(void) {
333 matrix_clear(&display);
336 static void matrix_render(struct CharacterMatrix *matrix) {
337 last_flush = timer_read();
343 // Move to the home position
344 send_cmd3(PageAddr, 0, MatrixRows - 1);
345 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
347 if (i2c_start_write(i2cAddress)) {
350 if (i2c_master_write(0x40)) {
355 for (uint8_t row = 0; row < MatrixRows; ++row) {
356 for (uint8_t col = 0; col < MatrixCols; ++col) {
357 const uint8_t *glyph = font + (matrix->display[row][col] * (FontWidth - 1));
359 for (uint8_t glyphCol = 0; glyphCol < FontWidth - 1; ++glyphCol) {
360 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
361 i2c_master_write(colBits);
364 // 1 column of space between chars (it's not included in the glyph)
369 matrix->dirty = false;
378 void iota_gfx_flush(void) {
379 matrix_render(&display);
382 //#include "LUFA/Drivers/Peripheral/ADC.h"
384 /* Returns the battery voltage; returns the number of millivolts
385 static uint32_t read_battery_voltage(void) {
386 if (last_battery_update == 0 ||
387 timer_elapsed(last_battery_update) > BatteryUpdateInterval) {
388 ADC_Init(ADC_SINGLE_CONVERSION | ADC_PRESCALE_32);
389 ADC_SetupChannel(12);
390 vbat = 2 * 3.3 * ADC_GetChannelReading(ADC_REFERENCE_AVCC | ADC_CHANNEL12);
392 last_battery_update = timer_read();
397 static void matrix_update(struct CharacterMatrix *dest,
398 const struct CharacterMatrix *source) {
399 if (memcmp(dest->display, source->display, sizeof(dest->display))) {
400 memcpy(dest->display, source->display, sizeof(dest->display));
405 static void render_status_info(void) {
412 struct CharacterMatrix matrix;
414 matrix_clear(&matrix);
415 matrix_write_P(&matrix, PSTR("USB: "));
417 switch (USB_DeviceState) {
418 case DEVICE_STATE_Unattached:
419 matrix_write_P(&matrix, PSTR("Unattached"));
421 case DEVICE_STATE_Suspended:
422 matrix_write_P(&matrix, PSTR("Suspended"));
424 case DEVICE_STATE_Configured:
425 matrix_write_P(&matrix, PSTR("Connected"));
427 case DEVICE_STATE_Powered:
428 matrix_write_P(&matrix, PSTR("Powered"));
430 case DEVICE_STATE_Default:
431 matrix_write_P(&matrix, PSTR("Default"));
433 case DEVICE_STATE_Addressed:
434 matrix_write_P(&matrix, PSTR("Addressed"));
437 matrix_write_P(&matrix, PSTR("Invalid"));
441 // matrix_write_P(&matrix, (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? PSTR("\n NUMLOCK") : PSTR("\n"));
442 // matrix_write_P(&matrix, PSTR("\nBLE: "));
443 //#ifdef ADAFRUIT_BLE_ENABLE
444 // matrix_write_P(&matrix, adafruit_ble_is_connected() ? PSTR("Connected")
445 // : PSTR("Not Connected"));
449 // snprintf(buf, sizeof(buf), "VBat: %4lumV\nLayer: 0x%04lx %s",
450 // read_battery_voltage(), layer_state,
451 // (host_keyboard_leds() & USB_LED_CAPS_LOCK) ? "CAPS" : "");
452 // matrix_write(&matrix, buf);
455 snprintf(buf,sizeof(buf), "Undef-%ld", layer_state);
456 matrix_write_P(&matrix, PSTR("\n\nLayer: "));
457 switch (layer_state) {
459 matrix_write_P(&matrix, PSTR("Default"));
462 matrix_write_P(&matrix, PSTR("Raise"));
465 matrix_write_P(&matrix, PSTR("Lower"));
468 matrix_write_P(&matrix, PSTR("Mouse/Macro"));
471 matrix_write_P(&matrix, PSTR("Symbols/Fn"));
474 matrix_write_P(&matrix, PSTR("Numbers"));
477 matrix_write_P(&matrix, PSTR("Num + Lower"));
480 matrix_write_P(&matrix, PSTR("Num + FN"));
483 matrix_write_P(&matrix, PSTR("ADJUST"));
486 matrix_write(&matrix, buf);
490 snprintf(led, sizeof(led), "\n%s %s %s",
491 (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? "NUMLOCK" : " ",
492 (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? "CAPS" : " ",
493 (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) ? "SCLK" : " ");
494 matrix_write(&matrix, led);
495 matrix_update(&display, &matrix);
498 void iota_gfx_task(void) {
499 render_status_info();
505 if (timer_elapsed(last_flush) > ScreenOffInterval) {