7 #ifdef ADAFRUIT_BLE_ENABLE
8 #include "adafruit_ble.h"
16 extern 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 */
28 // 'last_flush' is declared as uint16_t,
29 // so this must be less than 65535
30 #define ScreenOffInterval 30000 /* milliseconds */
32 static uint8_t displaying;
34 static uint16_t last_flush;
36 static bool force_dirty = true;
38 // Write command sequence.
39 // Returns true on success.
40 static inline bool _send_cmd1(uint8_t cmd) {
43 if (i2c_start_write(SSD1306_ADDRESS)) {
44 xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
48 if (i2c_master_write(0x0 /* command byte follows */)) {
49 print("failed to write control byte\n");
54 if (i2c_master_write(cmd)) {
55 xprintf("failed to write command %d\n", cmd);
64 // Write 2-byte command sequence.
65 // Returns true on success
66 static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
67 if (!_send_cmd1(cmd)) {
70 return _send_cmd1(opr);
73 // Write 3-byte command sequence.
74 // Returns true on success
75 static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
76 if (!_send_cmd1(cmd)) {
79 if (!_send_cmd1(opr1)) {
82 return _send_cmd1(opr2);
85 #define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
86 #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
87 #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
89 static void clear_display(void) {
90 matrix_clear(&display);
92 // Clear all of the display bits (there can be random noise
93 // in the RAM on startup)
94 send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
95 send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
97 if (i2c_start_write(SSD1306_ADDRESS)) {
100 if (i2c_master_write(0x40)) {
104 for (uint8_t row = 0; row < MatrixRows; ++row) {
105 for (uint8_t col = 0; col < DisplayWidth; ++col) {
110 display.dirty = false;
118 static int8_t capture_sendchar(uint8_t c) {
120 iota_gfx_write_char(c);
129 bool iota_gfx_init(bool rotate) {
130 bool success = false;
133 send_cmd1(DisplayOff);
134 send_cmd2(SetDisplayClockDiv, 0x80);
135 send_cmd2(SetMultiPlex, DisplayHeight - 1);
137 send_cmd2(SetDisplayOffset, 0);
140 send_cmd1(SetStartLine | 0x0);
141 send_cmd2(SetChargePump, 0x14 /* Enable */);
142 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
145 // the following Flip the display orientation 180 degrees
147 send_cmd1(ComScanInc);
149 // Flips the display orientation 0 degrees
150 send_cmd1(SegRemap | 0x1);
151 send_cmd1(ComScanDec);
154 #ifdef SSD1306_128X64
155 send_cmd2(SetComPins, 0x12);
157 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 matrix_write_ln(struct CharacterMatrix *matrix, const char *data) {
246 char data_ln[strlen(data)+2];
247 snprintf(data_ln, sizeof(data_ln), "%s\n", data);
248 matrix_write(matrix, data_ln);
251 void iota_gfx_write(const char *data) {
252 matrix_write(&display, data);
255 void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
257 uint8_t c = pgm_read_byte(data);
261 matrix_write_char(matrix, c);
266 void iota_gfx_write_P(const char *data) {
267 matrix_write_P(&display, data);
270 void matrix_clear(struct CharacterMatrix *matrix) {
271 memset(matrix->display, ' ', sizeof(matrix->display));
272 matrix->cursor = &matrix->display[0][0];
273 matrix->dirty = true;
276 void iota_gfx_clear_screen(void) {
277 matrix_clear(&display);
280 void matrix_render(struct CharacterMatrix *matrix) {
281 last_flush = timer_read();
287 // Move to the home position
288 send_cmd3(PageAddr, 0, MatrixRows - 1);
289 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
291 if (i2c_start_write(SSD1306_ADDRESS)) {
294 if (i2c_master_write(0x40)) {
299 for (uint8_t row = 0; row < MatrixRows; ++row) {
300 for (uint8_t col = 0; col < MatrixCols; ++col) {
301 const uint8_t *glyph = font + (matrix->display[row][col] * FontWidth);
303 for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
304 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
305 i2c_master_write(colBits);
308 // 1 column of space between chars (it's not included in the glyph)
309 //i2c_master_write(0);
313 matrix->dirty = false;
322 void iota_gfx_flush(void) {
323 matrix_render(&display);
326 __attribute__ ((weak))
327 void iota_gfx_task_user(void) {
330 void iota_gfx_task(void) {
331 iota_gfx_task_user();
333 if (display.dirty|| force_dirty) {
338 if (timer_elapsed(last_flush) > ScreenOffInterval) {
343 bool process_record_gfx(uint16_t keycode, keyrecord_t *record) {