9 #include "common/glcdfont.c"
11 #include <helixfont.h>
13 #ifdef ADAFRUIT_BLE_ENABLE
14 #include "adafruit_ble.h"
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
28 //static uint16_t last_battery_update;
29 //static uint32_t vbat;
30 //#define BatteryUpdateInterval 10000 /* milliseconds */
32 // 'last_flush' is declared as uint16_t,
33 // so this must be less than 65535
34 #define ScreenOffInterval 60000 /* milliseconds */
36 static uint8_t displaying;
38 static uint16_t last_flush;
40 static bool force_dirty = true;
42 // Write command sequence.
43 // Returns true on success.
44 static inline bool _send_cmd1(uint8_t cmd) {
47 if (i2c_start_write(SSD1306_ADDRESS)) {
48 xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
52 if (i2c_master_write(0x0 /* command byte follows */)) {
53 print("failed to write control byte\n");
58 if (i2c_master_write(cmd)) {
59 xprintf("failed to write command %d\n", cmd);
68 // Write 2-byte command sequence.
69 // Returns true on success
70 static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
71 if (!_send_cmd1(cmd)) {
74 return _send_cmd1(opr);
77 // Write 3-byte command sequence.
78 // Returns true on success
79 static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
80 if (!_send_cmd1(cmd)) {
83 if (!_send_cmd1(opr1)) {
86 return _send_cmd1(opr2);
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;}
93 static void clear_display(void) {
94 matrix_clear(&display);
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);
101 if (i2c_start_write(SSD1306_ADDRESS)) {
104 if (i2c_master_write(0x40)) {
108 for (uint8_t row = 0; row < MatrixRows; ++row) {
109 for (uint8_t col = 0; col < DisplayWidth; ++col) {
114 display.dirty = false;
122 static int8_t capture_sendchar(uint8_t c) {
124 iota_gfx_write_char(c);
133 bool iota_gfx_init(bool rotate) {
134 bool success = false;
137 send_cmd1(DisplayOff);
138 send_cmd2(SetDisplayClockDiv, 0x80);
139 send_cmd2(SetMultiPlex, DisplayHeight - 1);
141 send_cmd2(SetDisplayOffset, 0);
144 send_cmd1(SetStartLine | 0x0);
145 send_cmd2(SetChargePump, 0x14 /* Enable */);
146 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
149 // the following Flip the display orientation 180 degrees
151 send_cmd1(ComScanInc);
153 // Flips the display orientation 0 degrees
154 send_cmd1(SegRemap | 0x1);
155 send_cmd1(ComScanDec);
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);
167 send_cmd2(SetContrast, 0); // Dim
176 print_set_sendchar(capture_sendchar);
183 bool iota_gfx_off(void) {
184 bool success = false;
186 send_cmd1(DisplayOff);
193 bool iota_gfx_on(void) {
194 bool success = false;
196 send_cmd1(DisplayOn);
203 void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
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);
216 void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
217 matrix->dirty = true;
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;
224 while (cursor_col++ < MatrixCols) {
225 matrix_write_char_inner(matrix, ' ');
230 matrix_write_char_inner(matrix, c);
233 void iota_gfx_write_char(uint8_t c) {
234 matrix_write_char(&display, c);
237 void matrix_write(struct CharacterMatrix *matrix, const char *data) {
238 const char *end = data + strlen(data);
240 matrix_write_char(matrix, *data);
245 void iota_gfx_write(const char *data) {
246 matrix_write(&display, data);
249 void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
251 uint8_t c = pgm_read_byte(data);
255 matrix_write_char(matrix, c);
260 void iota_gfx_write_P(const char *data) {
261 matrix_write_P(&display, data);
264 void matrix_clear(struct CharacterMatrix *matrix) {
265 memset(matrix->display, ' ', sizeof(matrix->display));
266 matrix->cursor = &matrix->display[0][0];
267 matrix->dirty = true;
270 void iota_gfx_clear_screen(void) {
271 matrix_clear(&display);
274 void matrix_render(struct CharacterMatrix *matrix) {
275 last_flush = timer_read();
281 // Move to the home position
282 send_cmd3(PageAddr, 0, MatrixRows - 1);
283 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
285 if (i2c_start_write(SSD1306_ADDRESS)) {
288 if (i2c_master_write(0x40)) {
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);
297 for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
298 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
299 i2c_master_write(colBits);
302 // 1 column of space between chars (it's not included in the glyph)
303 //i2c_master_write(0);
307 matrix->dirty = false;
316 void iota_gfx_flush(void) {
317 matrix_render(&display);
320 __attribute__ ((weak))
321 void iota_gfx_task_user(void) {
324 void iota_gfx_task(void) {
325 iota_gfx_task_user();
327 if (display.dirty|| force_dirty) {
332 if (timer_elapsed(last_flush) > ScreenOffInterval) {
337 bool process_record_gfx(uint16_t keycode, keyrecord_t *record) {