aboutsummaryrefslogtreecommitdiff
path: root/drivers/avr/ssd1306.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/avr/ssd1306.c')
-rw-r--r--drivers/avr/ssd1306.c427
1 files changed, 211 insertions, 216 deletions
diff --git a/drivers/avr/ssd1306.c b/drivers/avr/ssd1306.c
index bb8938bba..61d7a9953 100644
--- a/drivers/avr/ssd1306.c
+++ b/drivers/avr/ssd1306.c
@@ -1,325 +1,320 @@
1#ifdef SSD1306OLED 1#ifdef SSD1306OLED
2 2
3#include "ssd1306.h" 3# include "ssd1306.h"
4#include "i2c.h" 4# include "i2c.h"
5#include <string.h> 5# include <string.h>
6#include "print.h" 6# include "print.h"
7#include "glcdfont.c" 7# include "glcdfont.c"
8#ifdef ADAFRUIT_BLE_ENABLE 8# ifdef ADAFRUIT_BLE_ENABLE
9#include "adafruit_ble.h" 9# include "adafruit_ble.h"
10#endif 10# endif
11#ifdef PROTOCOL_LUFA 11# ifdef PROTOCOL_LUFA
12#include "lufa.h" 12# include "lufa.h"
13#endif 13# endif
14#include "sendchar.h" 14# include "sendchar.h"
15#include "timer.h" 15# include "timer.h"
16 16
17// Set this to 1 to help diagnose early startup problems 17// Set this to 1 to help diagnose early startup problems
18// when testing power-on with ble. Turn it off otherwise, 18// when testing power-on with ble. Turn it off otherwise,
19// as the latency of printing most of the debug info messes 19// as the latency of printing most of the debug info messes
20// with the matrix scan, causing keys to drop. 20// with the matrix scan, causing keys to drop.
21#define DEBUG_TO_SCREEN 0 21# define DEBUG_TO_SCREEN 0
22 22
23//static uint16_t last_battery_update; 23// static uint16_t last_battery_update;
24//static uint32_t vbat; 24// static uint32_t vbat;
25//#define BatteryUpdateInterval 10000 /* milliseconds */ 25//#define BatteryUpdateInterval 10000 /* milliseconds */
26#define ScreenOffInterval 300000 /* milliseconds */ 26# define ScreenOffInterval 300000 /* milliseconds */
27#if DEBUG_TO_SCREEN 27# if DEBUG_TO_SCREEN
28static uint8_t displaying; 28static uint8_t displaying;
29#endif 29# endif
30static uint16_t last_flush; 30static uint16_t last_flush;
31 31
32// Write command sequence. 32// Write command sequence.
33// Returns true on success. 33// Returns true on success.
34static inline bool _send_cmd1(uint8_t cmd) { 34static inline bool _send_cmd1(uint8_t cmd) {
35 bool res = false; 35 bool res = false;
36 36
37 if (i2c_start_write(SSD1306_ADDRESS)) { 37 if (i2c_start_write(SSD1306_ADDRESS)) {
38 xprintf("failed to start write to %d\n", SSD1306_ADDRESS); 38 xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
39 goto done; 39 goto done;
40 } 40 }
41 41
42 if (i2c_master_write(0x0 /* command byte follows */)) { 42 if (i2c_master_write(0x0 /* command byte follows */)) {
43 print("failed to write control byte\n"); 43 print("failed to write control byte\n");
44 44
45 goto done; 45 goto done;
46 } 46 }
47 47
48 if (i2c_master_write(cmd)) { 48 if (i2c_master_write(cmd)) {
49 xprintf("failed to write command %d\n", cmd); 49 xprintf("failed to write command %d\n", cmd);
50 goto done; 50 goto done;
51 } 51 }
52 res = true; 52 res = true;
53done: 53done:
54 i2c_master_stop(); 54 i2c_master_stop();
55 return res; 55 return res;
56} 56}
57 57
58// Write 2-byte command sequence. 58// Write 2-byte command sequence.
59// Returns true on success 59// Returns true on success
60static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) { 60static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
61 if (!_send_cmd1(cmd)) { 61 if (!_send_cmd1(cmd)) {
62 return false; 62 return false;
63 } 63 }
64 return _send_cmd1(opr); 64 return _send_cmd1(opr);
65} 65}
66 66
67// Write 3-byte command sequence. 67// Write 3-byte command sequence.
68// Returns true on success 68// Returns true on success
69static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) { 69static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
70 if (!_send_cmd1(cmd)) { 70 if (!_send_cmd1(cmd)) {
71 return false; 71 return false;
72 } 72 }
73 if (!_send_cmd1(opr1)) { 73 if (!_send_cmd1(opr1)) {
74 return false; 74 return false;
75 } 75 }
76 return _send_cmd1(opr2); 76 return _send_cmd1(opr2);
77} 77}
78 78
79#define send_cmd1(c) if (!_send_cmd1(c)) {goto done;} 79# define send_cmd1(c) \
80#define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;} 80 if (!_send_cmd1(c)) { \
81#define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;} 81 goto done; \
82 }
83# define send_cmd2(c, o) \
84 if (!_send_cmd2(c, o)) { \
85 goto done; \
86 }
87# define send_cmd3(c, o1, o2) \
88 if (!_send_cmd3(c, o1, o2)) { \
89 goto done; \
90 }
82 91
83static void clear_display(void) { 92static void clear_display(void) {
84 matrix_clear(&display); 93 matrix_clear(&display);
85 94
86 // Clear all of the display bits (there can be random noise 95 // Clear all of the display bits (there can be random noise
87 // in the RAM on startup) 96 // in the RAM on startup)
88 send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1); 97 send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
89 send_cmd3(ColumnAddr, 0, DisplayWidth - 1); 98 send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
90 99
91 if (i2c_start_write(SSD1306_ADDRESS)) { 100 if (i2c_start_write(SSD1306_ADDRESS)) {
92 goto done; 101 goto done;
93 } 102 }
94 if (i2c_master_write(0x40)) { 103 if (i2c_master_write(0x40)) {
95 // Data mode 104 // Data mode
96 goto done; 105 goto done;
97 } 106 }
98 for (uint8_t row = 0; row < MatrixRows; ++row) { 107 for (uint8_t row = 0; row < MatrixRows; ++row) {
99 for (uint8_t col = 0; col < DisplayWidth; ++col) { 108 for (uint8_t col = 0; col < DisplayWidth; ++col) {
100 i2c_master_write(0); 109 i2c_master_write(0);
110 }
101 } 111 }
102 }
103 112
104 display.dirty = false; 113 display.dirty = false;
105 114
106done: 115done:
107 i2c_master_stop(); 116 i2c_master_stop();
108} 117}
109 118
110#if DEBUG_TO_SCREEN 119# if DEBUG_TO_SCREEN
111#undef sendchar 120# undef sendchar
112static int8_t capture_sendchar(uint8_t c) { 121static int8_t capture_sendchar(uint8_t c) {
113 sendchar(c); 122 sendchar(c);
114 iota_gfx_write_char(c); 123 iota_gfx_write_char(c);
115 124
116 if (!displaying) { 125 if (!displaying) {
117 iota_gfx_flush(); 126 iota_gfx_flush();
118 } 127 }
119 return 0; 128 return 0;
120} 129}
121#endif 130# endif
122 131
123bool iota_gfx_init(void) { 132bool iota_gfx_init(void) {
124 bool success = false; 133 bool success = false;
125 134
126 send_cmd1(DisplayOff); 135 send_cmd1(DisplayOff);
127 send_cmd2(SetDisplayClockDiv, 0x80); 136 send_cmd2(SetDisplayClockDiv, 0x80);
128 send_cmd2(SetMultiPlex, DisplayHeight - 1); 137 send_cmd2(SetMultiPlex, DisplayHeight - 1);
129 138
130 send_cmd2(SetDisplayOffset, 0); 139 send_cmd2(SetDisplayOffset, 0);
131 140
141 send_cmd1(SetStartLine | 0x0);
142 send_cmd2(SetChargePump, 0x14 /* Enable */);
143 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
132 144
133 send_cmd1(SetStartLine | 0x0); 145# ifdef OLED_ROTATE180
134 send_cmd2(SetChargePump, 0x14 /* Enable */); 146 // the following Flip the display orientation 180 degrees
135 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */); 147 send_cmd1(SegRemap);
148 send_cmd1(ComScanInc);
149# endif
150# ifndef OLED_ROTATE180
151 // Flips the display orientation 0 degrees
152 send_cmd1(SegRemap | 0x1);
153 send_cmd1(ComScanDec);
154# endif
136 155
137#ifdef OLED_ROTATE180 156 send_cmd2(SetComPins, 0x2);
138// the following Flip the display orientation 180 degrees 157 send_cmd2(SetContrast, 0x8f);
139 send_cmd1(SegRemap); 158 send_cmd2(SetPreCharge, 0xf1);
140 send_cmd1(ComScanInc); 159 send_cmd2(SetVComDetect, 0x40);
141#endif 160 send_cmd1(DisplayAllOnResume);
142#ifndef OLED_ROTATE180 161 send_cmd1(NormalDisplay);
143// Flips the display orientation 0 degrees 162 send_cmd1(DeActivateScroll);
144 send_cmd1(SegRemap | 0x1); 163 send_cmd1(DisplayOn);
145 send_cmd1(ComScanDec);
146#endif
147
148 send_cmd2(SetComPins, 0x2);
149 send_cmd2(SetContrast, 0x8f);
150 send_cmd2(SetPreCharge, 0xf1);
151 send_cmd2(SetVComDetect, 0x40);
152 send_cmd1(DisplayAllOnResume);
153 send_cmd1(NormalDisplay);
154 send_cmd1(DeActivateScroll);
155 send_cmd1(DisplayOn);
156 164
157 send_cmd2(SetContrast, 0); // Dim 165 send_cmd2(SetContrast, 0); // Dim
158 166
159 clear_display(); 167 clear_display();
160 168
161 success = true; 169 success = true;
162 170
163 iota_gfx_flush(); 171 iota_gfx_flush();
164 172
165#if DEBUG_TO_SCREEN 173# if DEBUG_TO_SCREEN
166 print_set_sendchar(capture_sendchar); 174 print_set_sendchar(capture_sendchar);
167#endif 175# endif
168 176
169done: 177done:
170 return success; 178 return success;
171} 179}
172 180
173bool iota_gfx_off(void) { 181bool iota_gfx_off(void) {
174 bool success = false; 182 bool success = false;
175 183
176 send_cmd1(DisplayOff); 184 send_cmd1(DisplayOff);
177 success = true; 185 success = true;
178 186
179done: 187done:
180 return success; 188 return success;
181} 189}
182 190
183bool iota_gfx_on(void) { 191bool iota_gfx_on(void) {
184 bool success = false; 192 bool success = false;
185 193
186 send_cmd1(DisplayOn); 194 send_cmd1(DisplayOn);
187 success = true; 195 success = true;
188 196
189done: 197done:
190 return success; 198 return success;
191} 199}
192 200
193void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) { 201void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
194 *matrix->cursor = c; 202 *matrix->cursor = c;
195 ++matrix->cursor; 203 ++matrix->cursor;
196 204
197 if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) { 205 if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
198 // We went off the end; scroll the display upwards by one line 206 // We went off the end; scroll the display upwards by one line
199 memmove(&matrix->display[0], &matrix->display[1], 207 memmove(&matrix->display[0], &matrix->display[1], MatrixCols * (MatrixRows - 1));
200 MatrixCols * (MatrixRows - 1)); 208 matrix->cursor = &matrix->display[MatrixRows - 1][0];
201 matrix->cursor = &matrix->display[MatrixRows - 1][0]; 209 memset(matrix->cursor, ' ', MatrixCols);
202 memset(matrix->cursor, ' ', MatrixCols); 210 }
203 }
204} 211}
205 212
206void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) { 213void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
207 matrix->dirty = true; 214 matrix->dirty = true;
208 215
209 if (c == '\n') { 216 if (c == '\n') {
210 // Clear to end of line from the cursor and then move to the 217 // Clear to end of line from the cursor and then move to the
211 // start of the next line 218 // start of the next line
212 uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols; 219 uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
213 220
214 while (cursor_col++ < MatrixCols) { 221 while (cursor_col++ < MatrixCols) {
215 matrix_write_char_inner(matrix, ' '); 222 matrix_write_char_inner(matrix, ' ');
223 }
224 return;
216 } 225 }
217 return;
218 }
219 226
220 matrix_write_char_inner(matrix, c); 227 matrix_write_char_inner(matrix, c);
221} 228}
222 229
223void iota_gfx_write_char(uint8_t c) { 230void iota_gfx_write_char(uint8_t c) { matrix_write_char(&display, c); }
224 matrix_write_char(&display, c);
225}
226 231
227void matrix_write(struct CharacterMatrix *matrix, const char *data) { 232void matrix_write(struct CharacterMatrix *matrix, const char *data) {
228 const char *end = data + strlen(data); 233 const char *end = data + strlen(data);
229 while (data < end) { 234 while (data < end) {
230 matrix_write_char(matrix, *data); 235 matrix_write_char(matrix, *data);
231 ++data; 236 ++data;
232 } 237 }
233} 238}
234 239
235void iota_gfx_write(const char *data) { 240void iota_gfx_write(const char *data) { matrix_write(&display, data); }
236 matrix_write(&display, data);
237}
238 241
239void matrix_write_P(struct CharacterMatrix *matrix, const char *data) { 242void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
240 while (true) { 243 while (true) {
241 uint8_t c = pgm_read_byte(data); 244 uint8_t c = pgm_read_byte(data);
242 if (c == 0) { 245 if (c == 0) {
243 return; 246 return;
247 }
248 matrix_write_char(matrix, c);
249 ++data;
244 } 250 }
245 matrix_write_char(matrix, c);
246 ++data;
247 }
248} 251}
249 252
250void iota_gfx_write_P(const char *data) { 253void iota_gfx_write_P(const char *data) { matrix_write_P(&display, data); }
251 matrix_write_P(&display, data);
252}
253 254
254void matrix_clear(struct CharacterMatrix *matrix) { 255void matrix_clear(struct CharacterMatrix *matrix) {
255 memset(matrix->display, ' ', sizeof(matrix->display)); 256 memset(matrix->display, ' ', sizeof(matrix->display));
256 matrix->cursor = &matrix->display[0][0]; 257 matrix->cursor = &matrix->display[0][0];
257 matrix->dirty = true; 258 matrix->dirty = true;
258} 259}
259 260
260void iota_gfx_clear_screen(void) { 261void iota_gfx_clear_screen(void) { matrix_clear(&display); }
261 matrix_clear(&display);
262}
263 262
264void matrix_render(struct CharacterMatrix *matrix) { 263void matrix_render(struct CharacterMatrix *matrix) {
265 last_flush = timer_read(); 264 last_flush = timer_read();
266 iota_gfx_on(); 265 iota_gfx_on();
267#if DEBUG_TO_SCREEN 266# if DEBUG_TO_SCREEN
268 ++displaying; 267 ++displaying;
269#endif 268# endif
269
270 // Move to the home position
271 send_cmd3(PageAddr, 0, MatrixRows - 1);
272 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
273
274 if (i2c_start_write(SSD1306_ADDRESS)) {
275 goto done;
276 }
277 if (i2c_master_write(0x40)) {
278 // Data mode
279 goto done;
280 }
281
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 - 1));
285
286 for (uint8_t glyphCol = 0; glyphCol < FontWidth - 1; ++glyphCol) {
287 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
288 i2c_master_write(colBits);
289 }
270 290
271 // Move to the home position 291 // 1 column of space between chars (it's not included in the glyph)
272 send_cmd3(PageAddr, 0, MatrixRows - 1); 292 i2c_master_write(0);
273 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1); 293 }
274
275 if (i2c_start_write(SSD1306_ADDRESS)) {
276 goto done;
277 }
278 if (i2c_master_write(0x40)) {
279 // Data mode
280 goto done;
281 }
282
283 for (uint8_t row = 0; row < MatrixRows; ++row) {
284 for (uint8_t col = 0; col < MatrixCols; ++col) {
285 const uint8_t *glyph = font + (matrix->display[row][col] * (FontWidth - 1));
286
287 for (uint8_t glyphCol = 0; glyphCol < FontWidth - 1; ++glyphCol) {
288 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
289 i2c_master_write(colBits);
290 }
291
292 // 1 column of space between chars (it's not included in the glyph)
293 i2c_master_write(0);
294 } 294 }
295 }
296 295
297 matrix->dirty = false; 296 matrix->dirty = false;
298 297
299done: 298done:
300 i2c_master_stop(); 299 i2c_master_stop();
301#if DEBUG_TO_SCREEN 300# if DEBUG_TO_SCREEN
302 --displaying; 301 --displaying;
303#endif 302# endif
304} 303}
305 304
306void iota_gfx_flush(void) { 305void iota_gfx_flush(void) { matrix_render(&display); }
307 matrix_render(&display);
308}
309 306
310__attribute__ ((weak)) 307__attribute__((weak)) void iota_gfx_task_user(void) {}
311void iota_gfx_task_user(void) {
312}
313 308
314void iota_gfx_task(void) { 309void iota_gfx_task(void) {
315 iota_gfx_task_user(); 310 iota_gfx_task_user();
316 311
317 if (display.dirty) { 312 if (display.dirty) {
318 iota_gfx_flush(); 313 iota_gfx_flush();
319 } 314 }
320 315
321 if (timer_elapsed(last_flush) > ScreenOffInterval) { 316 if (timer_elapsed(last_flush) > ScreenOffInterval) {
322 iota_gfx_off(); 317 iota_gfx_off();
323 } 318 }
324} 319}
325#endif 320#endif