7 #ifdef ADAFRUIT_BLE_ENABLE
8 #include "adafruit_ble.h"
16 static const unsigned char font[] PROGMEM;
18 // Set this to 1 to help diagnose early startup problems
19 // when testing power-on with ble. Turn it off otherwise,
20 // as the latency of printing most of the debug info messes
21 // with the matrix scan, causing keys to drop.
22 #define DEBUG_TO_SCREEN 0
24 //static uint16_t last_battery_update;
25 //static uint32_t vbat;
26 //#define BatteryUpdateInterval 10000 /* milliseconds */
27 #define ScreenOffInterval 300000 /* milliseconds */
29 static uint8_t displaying;
31 static uint16_t last_flush;
33 // Write command sequence.
34 // Returns true on success.
35 static inline bool _send_cmd1(uint8_t cmd) {
38 if (i2c_start_write(SSD1306_ADDRESS)) {
39 xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
43 if (i2c_master_write(0x0 /* command byte follows */)) {
44 print("failed to write control byte\n");
49 if (i2c_master_write(cmd)) {
50 xprintf("failed to write command %d\n", cmd);
59 // Write 2-byte command sequence.
60 // Returns true on success
61 static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
62 if (!_send_cmd1(cmd)) {
65 return _send_cmd1(opr);
68 // Write 3-byte command sequence.
69 // Returns true on success
70 static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
71 if (!_send_cmd1(cmd)) {
74 if (!_send_cmd1(opr1)) {
77 return _send_cmd1(opr2);
80 #define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
81 #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
82 #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
84 static void clear_display(void) {
85 matrix_clear(&display);
87 // Clear all of the display bits (there can be random noise
88 // in the RAM on startup)
89 send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
90 send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
92 if (i2c_start_write(SSD1306_ADDRESS)) {
95 if (i2c_master_write(0x40)) {
99 for (uint8_t row = 0; row < MatrixRows; ++row) {
100 for (uint8_t col = 0; col < DisplayWidth; ++col) {
105 display.dirty = false;
113 static int8_t capture_sendchar(uint8_t c) {
115 iota_gfx_write_char(c);
124 bool iota_gfx_init(bool rotate) {
125 bool success = false;
128 send_cmd1(DisplayOff);
129 send_cmd2(SetDisplayClockDiv, 0x80);
130 send_cmd2(SetMultiPlex, DisplayHeight - 1);
132 send_cmd2(SetDisplayOffset, 0);
135 send_cmd1(SetStartLine | 0x0);
136 send_cmd2(SetChargePump, 0x14 /* Enable */);
137 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
140 // the following Flip the display orientation 180 degrees
142 send_cmd1(ComScanInc);
144 // Flips the display orientation 0 degrees
145 send_cmd1(SegRemap | 0x1);
146 send_cmd1(ComScanDec);
149 send_cmd2(SetComPins, 0x2);
150 send_cmd2(SetContrast, 0x8f);
151 send_cmd2(SetPreCharge, 0xf1);
152 send_cmd2(SetVComDetect, 0x40);
153 send_cmd1(DisplayAllOnResume);
154 send_cmd1(NormalDisplay);
155 send_cmd1(DeActivateScroll);
156 send_cmd1(DisplayOn);
158 send_cmd2(SetContrast, 0); // Dim
167 print_set_sendchar(capture_sendchar);
174 bool iota_gfx_off(void) {
175 bool success = false;
177 send_cmd1(DisplayOff);
184 bool iota_gfx_on(void) {
185 bool success = false;
187 send_cmd1(DisplayOn);
194 void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
198 if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
199 // We went off the end; scroll the display upwards by one line
200 memmove(&matrix->display[0], &matrix->display[1],
201 MatrixCols * (MatrixRows - 1));
202 matrix->cursor = &matrix->display[MatrixRows - 1][0];
203 memset(matrix->cursor, ' ', MatrixCols);
207 void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
208 matrix->dirty = true;
211 // Clear to end of line from the cursor and then move to the
212 // start of the next line
213 uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
215 while (cursor_col++ < MatrixCols) {
216 matrix_write_char_inner(matrix, ' ');
221 matrix_write_char_inner(matrix, c);
224 void iota_gfx_write_char(uint8_t c) {
225 matrix_write_char(&display, c);
228 void matrix_write(struct CharacterMatrix *matrix, const char *data) {
229 const char *end = data + strlen(data);
231 matrix_write_char(matrix, *data);
236 void matrix_write_ln(struct CharacterMatrix *matrix, const char *data) {
237 char data_ln[strlen(data)+2];
238 snprintf(data_ln, sizeof(data_ln), "%s\n", data);
239 matrix_write(matrix, data_ln);
242 void iota_gfx_write(const char *data) {
243 matrix_write(&display, data);
246 void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
248 uint8_t c = pgm_read_byte(data);
252 matrix_write_char(matrix, c);
257 void iota_gfx_write_P(const char *data) {
258 matrix_write_P(&display, data);
261 void matrix_clear(struct CharacterMatrix *matrix) {
262 memset(matrix->display, ' ', sizeof(matrix->display));
263 matrix->cursor = &matrix->display[0][0];
264 matrix->dirty = true;
267 void iota_gfx_clear_screen(void) {
268 matrix_clear(&display);
271 void matrix_render(struct CharacterMatrix *matrix) {
272 last_flush = timer_read();
278 // Move to the home position
279 send_cmd3(PageAddr, 0, MatrixRows - 1);
280 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
282 if (i2c_start_write(SSD1306_ADDRESS)) {
285 if (i2c_master_write(0x40)) {
290 for (uint8_t row = 0; row < MatrixRows; ++row) {
291 for (uint8_t col = 0; col < MatrixCols; ++col) {
292 const uint8_t *glyph = font + (matrix->display[row][col] * FontWidth);
294 for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
295 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
296 i2c_master_write(colBits);
299 // 1 column of space between chars (it's not included in the glyph)
300 //i2c_master_write(0);
304 matrix->dirty = false;
313 void iota_gfx_flush(void) {
314 matrix_render(&display);
317 __attribute__ ((weak))
318 void iota_gfx_task_user(void) {
321 void iota_gfx_task(void) {
322 iota_gfx_task_user();
328 if (timer_elapsed(last_flush) > ScreenOffInterval) {