7 #include "common/glcdfont.c"
8 #ifdef ADAFRUIT_BLE_ENABLE
9 #include "adafruit_ble.h"
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
23 //static uint16_t last_battery_update;
24 //static uint32_t vbat;
25 //#define BatteryUpdateInterval 10000 /* milliseconds */
26 #define ScreenOffInterval 300000 /* milliseconds */
28 static uint8_t displaying;
30 static uint16_t last_flush;
32 // Write command sequence.
33 // Returns true on success.
34 static inline bool _send_cmd1(uint8_t cmd) {
37 if (i2c_start_write(SSD1306_ADDRESS)) {
38 xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
42 if (i2c_master_write(0x0 /* command byte follows */)) {
43 print("failed to write control byte\n");
48 if (i2c_master_write(cmd)) {
49 xprintf("failed to write command %d\n", cmd);
58 // Write 2-byte command sequence.
59 // Returns true on success
60 static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
61 if (!_send_cmd1(cmd)) {
64 return _send_cmd1(opr);
67 // Write 3-byte command sequence.
68 // Returns true on success
69 static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
70 if (!_send_cmd1(cmd)) {
73 if (!_send_cmd1(opr1)) {
76 return _send_cmd1(opr2);
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;}
83 static void clear_display(void) {
84 matrix_clear(&display);
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);
91 if (i2c_start_write(SSD1306_ADDRESS)) {
94 if (i2c_master_write(0x40)) {
98 for (uint8_t row = 0; row < MatrixRows; ++row) {
99 for (uint8_t col = 0; col < DisplayWidth; ++col) {
104 display.dirty = false;
112 static int8_t capture_sendchar(uint8_t c) {
114 iota_gfx_write_char(c);
123 bool iota_gfx_init(bool rotate) {
124 bool success = false;
126 send_cmd1(DisplayOff);
127 send_cmd2(SetDisplayClockDiv, 0x80);
128 send_cmd2(SetMultiPlex, DisplayHeight - 1);
130 send_cmd2(SetDisplayOffset, 0);
133 send_cmd1(SetStartLine | 0x0);
134 send_cmd2(SetChargePump, 0x14 /* Enable */);
135 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
138 // the following Flip the display orientation 180 degrees
140 send_cmd1(ComScanInc);
142 // Flips the display orientation 0 degrees
143 send_cmd1(SegRemap | 0x1);
144 send_cmd1(ComScanDec);
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);
156 send_cmd2(SetContrast, 0); // Dim
165 print_set_sendchar(capture_sendchar);
172 bool iota_gfx_off(void) {
173 bool success = false;
175 send_cmd1(DisplayOff);
182 bool iota_gfx_on(void) {
183 bool success = false;
185 send_cmd1(DisplayOn);
192 void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
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);
205 void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
206 matrix->dirty = true;
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;
213 while (cursor_col++ < MatrixCols) {
214 matrix_write_char_inner(matrix, ' ');
219 matrix_write_char_inner(matrix, c);
222 void iota_gfx_write_char(uint8_t c) {
223 matrix_write_char(&display, c);
226 void matrix_write(struct CharacterMatrix *matrix, const char *data) {
227 const char *end = data + strlen(data);
229 matrix_write_char(matrix, *data);
234 void iota_gfx_write(const char *data) {
235 matrix_write(&display, data);
238 void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
240 uint8_t c = pgm_read_byte(data);
244 matrix_write_char(matrix, c);
249 void iota_gfx_write_P(const char *data) {
250 matrix_write_P(&display, data);
253 void matrix_clear(struct CharacterMatrix *matrix) {
254 memset(matrix->display, ' ', sizeof(matrix->display));
255 matrix->cursor = &matrix->display[0][0];
256 matrix->dirty = true;
259 void iota_gfx_clear_screen(void) {
260 matrix_clear(&display);
263 void matrix_render(struct CharacterMatrix *matrix) {
264 last_flush = timer_read();
270 // Move to the home position
271 send_cmd3(PageAddr, 0, MatrixRows - 1);
272 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
274 if (i2c_start_write(SSD1306_ADDRESS)) {
277 if (i2c_master_write(0x40)) {
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);
286 for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
287 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
288 i2c_master_write(colBits);
291 // 1 column of space between chars (it's not included in the glyph)
292 //i2c_master_write(0);
296 matrix->dirty = false;
305 void iota_gfx_flush(void) {
306 matrix_render(&display);
309 __attribute__ ((weak))
310 void iota_gfx_task_user(void) {
313 void iota_gfx_task(void) {
314 iota_gfx_task_user();
320 if (timer_elapsed(last_flush) > ScreenOffInterval) {