8 #include "common/glcdfont.c"
10 #include <helixfont.h>
12 #ifdef ADAFRUIT_BLE_ENABLE
13 #include "adafruit_ble.h"
21 // Set this to 1 to help diagnose early startup problems
22 // when testing power-on with ble. Turn it off otherwise,
23 // as the latency of printing most of the debug info messes
24 // with the matrix scan, causing keys to drop.
25 #define DEBUG_TO_SCREEN 0
27 //static uint16_t last_battery_update;
28 //static uint32_t vbat;
29 //#define BatteryUpdateInterval 10000 /* milliseconds */
30 #define ScreenOffInterval 300000 /* milliseconds */
32 static uint8_t displaying;
34 static uint16_t last_flush;
36 // Write command sequence.
37 // Returns true on success.
38 static inline bool _send_cmd1(uint8_t cmd) {
41 if (i2c_start_write(SSD1306_ADDRESS)) {
42 xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
46 if (i2c_master_write(0x0 /* command byte follows */)) {
47 print("failed to write control byte\n");
52 if (i2c_master_write(cmd)) {
53 xprintf("failed to write command %d\n", cmd);
62 // Write 2-byte command sequence.
63 // Returns true on success
64 static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
65 if (!_send_cmd1(cmd)) {
68 return _send_cmd1(opr);
71 // Write 3-byte command sequence.
72 // Returns true on success
73 static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
74 if (!_send_cmd1(cmd)) {
77 if (!_send_cmd1(opr1)) {
80 return _send_cmd1(opr2);
83 #define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
84 #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
85 #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
87 static void clear_display(void) {
88 matrix_clear(&display);
90 // Clear all of the display bits (there can be random noise
91 // in the RAM on startup)
92 send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
93 send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
95 if (i2c_start_write(SSD1306_ADDRESS)) {
98 if (i2c_master_write(0x40)) {
102 for (uint8_t row = 0; row < MatrixRows; ++row) {
103 for (uint8_t col = 0; col < DisplayWidth; ++col) {
108 display.dirty = false;
116 static int8_t capture_sendchar(uint8_t c) {
118 iota_gfx_write_char(c);
127 bool iota_gfx_init(bool rotate) {
128 bool success = false;
131 send_cmd1(DisplayOff);
132 send_cmd2(SetDisplayClockDiv, 0x80);
133 send_cmd2(SetMultiPlex, DisplayHeight - 1);
135 send_cmd2(SetDisplayOffset, 0);
138 send_cmd1(SetStartLine | 0x0);
139 send_cmd2(SetChargePump, 0x14 /* Enable */);
140 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
143 // the following Flip the display orientation 180 degrees
145 send_cmd1(ComScanInc);
147 // Flips the display orientation 0 degrees
148 send_cmd1(SegRemap | 0x1);
149 send_cmd1(ComScanDec);
152 send_cmd2(SetComPins, 0x2);
153 send_cmd2(SetContrast, 0x8f);
154 send_cmd2(SetPreCharge, 0xf1);
155 send_cmd2(SetVComDetect, 0x40);
156 send_cmd1(DisplayAllOnResume);
157 send_cmd1(NormalDisplay);
158 send_cmd1(DeActivateScroll);
159 send_cmd1(DisplayOn);
161 send_cmd2(SetContrast, 0); // Dim
170 print_set_sendchar(capture_sendchar);
177 bool iota_gfx_off(void) {
178 bool success = false;
180 send_cmd1(DisplayOff);
187 bool iota_gfx_on(void) {
188 bool success = false;
190 send_cmd1(DisplayOn);
197 void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
201 if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
202 // We went off the end; scroll the display upwards by one line
203 memmove(&matrix->display[0], &matrix->display[1],
204 MatrixCols * (MatrixRows - 1));
205 matrix->cursor = &matrix->display[MatrixRows - 1][0];
206 memset(matrix->cursor, ' ', MatrixCols);
210 void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
211 matrix->dirty = true;
214 // Clear to end of line from the cursor and then move to the
215 // start of the next line
216 uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
218 while (cursor_col++ < MatrixCols) {
219 matrix_write_char_inner(matrix, ' ');
224 matrix_write_char_inner(matrix, c);
227 void iota_gfx_write_char(uint8_t c) {
228 matrix_write_char(&display, c);
231 void matrix_write(struct CharacterMatrix *matrix, const char *data) {
232 const char *end = data + strlen(data);
234 matrix_write_char(matrix, *data);
239 void iota_gfx_write(const char *data) {
240 matrix_write(&display, data);
243 void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
245 uint8_t c = pgm_read_byte(data);
249 matrix_write_char(matrix, c);
254 void iota_gfx_write_P(const char *data) {
255 matrix_write_P(&display, data);
258 void matrix_clear(struct CharacterMatrix *matrix) {
259 memset(matrix->display, ' ', sizeof(matrix->display));
260 matrix->cursor = &matrix->display[0][0];
261 matrix->dirty = true;
264 void iota_gfx_clear_screen(void) {
265 matrix_clear(&display);
268 void matrix_render(struct CharacterMatrix *matrix) {
269 last_flush = timer_read();
275 // Move to the home position
276 send_cmd3(PageAddr, 0, MatrixRows - 1);
277 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
279 if (i2c_start_write(SSD1306_ADDRESS)) {
282 if (i2c_master_write(0x40)) {
287 for (uint8_t row = 0; row < MatrixRows; ++row) {
288 for (uint8_t col = 0; col < MatrixCols; ++col) {
289 const uint8_t *glyph = font + (matrix->display[row][col] * FontWidth);
291 for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
292 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
293 i2c_master_write(colBits);
296 // 1 column of space between chars (it's not included in the glyph)
297 //i2c_master_write(0);
301 matrix->dirty = false;
310 void iota_gfx_flush(void) {
311 matrix_render(&display);
314 __attribute__ ((weak))
315 void iota_gfx_task_user(void) {
318 void iota_gfx_task(void) {
319 iota_gfx_task_user();
325 if (timer_elapsed(last_flush) > ScreenOffInterval) {