ssd1306.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #ifdef SSD1306OLED
  2. #include "ssd1306.h"
  3. #include "config.h"
  4. #include "i2c.h"
  5. #include <string.h>
  6. #include "print.h"
  7. #include "lets_split.h"
  8. #include "glcdfont.c"
  9. #ifdef ADAFRUIT_BLE_ENABLE
  10. #include "adafruit_ble.h"
  11. #endif
  12. #ifdef PROTOCOL_LUFA
  13. #include "lufa.h"
  14. #endif
  15. #include "sendchar.h"
  16. #include "pincontrol.h"
  17. //assign the right code to your layers
  18. #define _BASE 0
  19. #define _LOWER 8
  20. #define _RAISE 16
  21. #define _FNLAYER 64
  22. #define _NUMLAY 128
  23. #define _NLOWER 136
  24. #define _NFNLAYER 192
  25. #define _MOUSECURSOR 256
  26. #define _ADJUST 65560
  27. // Set this to 1 to help diagnose early startup problems
  28. // when testing power-on with ble. Turn it off otherwise,
  29. // as the latency of printing most of the debug info messes
  30. // with the matrix scan, causing keys to drop.
  31. #define DEBUG_TO_SCREEN 0
  32. // Controls the SSD1306 128x32 OLED display via i2c
  33. #define i2cAddress 0x3C
  34. #define DisplayHeight 32
  35. #define DisplayWidth 128
  36. #define FontHeight 8
  37. #define FontWidth 6
  38. #define MatrixRows (DisplayHeight / FontHeight)
  39. #define MatrixCols (DisplayWidth / FontWidth)
  40. struct CharacterMatrix {
  41. uint8_t display[MatrixRows][MatrixCols];
  42. uint8_t *cursor;
  43. bool dirty;
  44. };
  45. static struct CharacterMatrix display;
  46. //static uint16_t last_battery_update;
  47. //static uint32_t vbat;
  48. //#define BatteryUpdateInterval 10000 /* milliseconds */
  49. #define ScreenOffInterval 300000 /* milliseconds */
  50. #if DEBUG_TO_SCREEN
  51. static uint8_t displaying;
  52. #endif
  53. static uint16_t last_flush;
  54. enum ssd1306_cmds {
  55. DisplayOff = 0xAE,
  56. DisplayOn = 0xAF,
  57. SetContrast = 0x81,
  58. DisplayAllOnResume = 0xA4,
  59. DisplayAllOn = 0xA5,
  60. NormalDisplay = 0xA6,
  61. InvertDisplay = 0xA7,
  62. SetDisplayOffset = 0xD3,
  63. SetComPins = 0xda,
  64. SetVComDetect = 0xdb,
  65. SetDisplayClockDiv = 0xD5,
  66. SetPreCharge = 0xd9,
  67. SetMultiPlex = 0xa8,
  68. SetLowColumn = 0x00,
  69. SetHighColumn = 0x10,
  70. SetStartLine = 0x40,
  71. SetMemoryMode = 0x20,
  72. ColumnAddr = 0x21,
  73. PageAddr = 0x22,
  74. ComScanInc = 0xc0,
  75. ComScanDec = 0xc8,
  76. SegRemap = 0xa0,
  77. SetChargePump = 0x8d,
  78. ExternalVcc = 0x01,
  79. SwitchCapVcc = 0x02,
  80. ActivateScroll = 0x2f,
  81. DeActivateScroll = 0x2e,
  82. SetVerticalScrollArea = 0xa3,
  83. RightHorizontalScroll = 0x26,
  84. LeftHorizontalScroll = 0x27,
  85. VerticalAndRightHorizontalScroll = 0x29,
  86. VerticalAndLeftHorizontalScroll = 0x2a,
  87. };
  88. // Write command sequence.
  89. // Returns true on success.
  90. static inline bool _send_cmd1(uint8_t cmd) {
  91. bool res = false;
  92. if (i2c_start_write(i2cAddress)) {
  93. xprintf("failed to start write to %d\n", i2cAddress);
  94. goto done;
  95. }
  96. if (i2c_master_write(0x0 /* command byte follows */)) {
  97. print("failed to write control byte\n");
  98. goto done;
  99. }
  100. if (i2c_master_write(cmd)) {
  101. xprintf("failed to write command %d\n", cmd);
  102. goto done;
  103. }
  104. res = true;
  105. done:
  106. i2c_master_stop();
  107. return res;
  108. }
  109. // Write 2-byte command sequence.
  110. // Returns true on success
  111. static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
  112. if (!_send_cmd1(cmd)) {
  113. return false;
  114. }
  115. return _send_cmd1(opr);
  116. }
  117. // Write 3-byte command sequence.
  118. // Returns true on success
  119. static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
  120. if (!_send_cmd1(cmd)) {
  121. return false;
  122. }
  123. if (!_send_cmd1(opr1)) {
  124. return false;
  125. }
  126. return _send_cmd1(opr2);
  127. }
  128. #define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
  129. #define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
  130. #define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
  131. static void matrix_clear(struct CharacterMatrix *matrix);
  132. static void clear_display(void) {
  133. matrix_clear(&display);
  134. // Clear all of the display bits (there can be random noise
  135. // in the RAM on startup)
  136. send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
  137. send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
  138. if (i2c_start_write(i2cAddress)) {
  139. goto done;
  140. }
  141. if (i2c_master_write(0x40)) {
  142. // Data mode
  143. goto done;
  144. }
  145. for (uint8_t row = 0; row < MatrixRows; ++row) {
  146. for (uint8_t col = 0; col < DisplayWidth; ++col) {
  147. i2c_master_write(0);
  148. }
  149. }
  150. display.dirty = false;
  151. done:
  152. i2c_master_stop();
  153. }
  154. #if DEBUG_TO_SCREEN
  155. #undef sendchar
  156. static int8_t capture_sendchar(uint8_t c) {
  157. sendchar(c);
  158. iota_gfx_write_char(c);
  159. if (!displaying) {
  160. iota_gfx_flush();
  161. }
  162. return 0;
  163. }
  164. #endif
  165. bool iota_gfx_init(void) {
  166. bool success = false;
  167. send_cmd1(DisplayOff);
  168. send_cmd2(SetDisplayClockDiv, 0x80);
  169. send_cmd2(SetMultiPlex, DisplayHeight - 1);
  170. send_cmd2(SetDisplayOffset, 0);
  171. send_cmd1(SetStartLine | 0x0);
  172. send_cmd2(SetChargePump, 0x14 /* Enable */);
  173. send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
  174. /// Flips the display orientation 0 degrees
  175. send_cmd1(SegRemap | 0x1);
  176. send_cmd1(ComScanDec);
  177. /*
  178. // the following Flip the display orientation 180 degrees
  179. send_cmd1(SegRemap);
  180. send_cmd1(ComScanInc);
  181. // end flip */
  182. send_cmd2(SetComPins, 0x2);
  183. send_cmd2(SetContrast, 0x8f);
  184. send_cmd2(SetPreCharge, 0xf1);
  185. send_cmd2(SetVComDetect, 0x40);
  186. send_cmd1(DisplayAllOnResume);
  187. send_cmd1(NormalDisplay);
  188. send_cmd1(DeActivateScroll);
  189. send_cmd1(DisplayOn);
  190. send_cmd2(SetContrast, 0); // Dim
  191. clear_display();
  192. success = true;
  193. iota_gfx_flush();
  194. #if DEBUG_TO_SCREEN
  195. print_set_sendchar(capture_sendchar);
  196. #endif
  197. done:
  198. return success;
  199. }
  200. bool iota_gfx_off(void) {
  201. bool success = false;
  202. send_cmd1(DisplayOff);
  203. success = true;
  204. done:
  205. return success;
  206. }
  207. bool iota_gfx_on(void) {
  208. bool success = false;
  209. send_cmd1(DisplayOn);
  210. success = true;
  211. done:
  212. return success;
  213. }
  214. static void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
  215. *matrix->cursor = c;
  216. ++matrix->cursor;
  217. if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
  218. // We went off the end; scroll the display upwards by one line
  219. memmove(&matrix->display[0], &matrix->display[1],
  220. MatrixCols * (MatrixRows - 1));
  221. matrix->cursor = &matrix->display[MatrixRows - 1][0];
  222. memset(matrix->cursor, ' ', MatrixCols);
  223. }
  224. }
  225. static void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
  226. matrix->dirty = true;
  227. if (c == '\n') {
  228. // Clear to end of line from the cursor and then move to the
  229. // start of the next line
  230. uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
  231. while (cursor_col++ < MatrixCols) {
  232. matrix_write_char_inner(matrix, ' ');
  233. }
  234. return;
  235. }
  236. matrix_write_char_inner(matrix, c);
  237. }
  238. void iota_gfx_write_char(uint8_t c) {
  239. matrix_write_char(&display, c);
  240. }
  241. static void matrix_write(struct CharacterMatrix *matrix, const char *data) {
  242. const char *end = data + strlen(data);
  243. while (data < end) {
  244. matrix_write_char(matrix, *data);
  245. ++data;
  246. }
  247. }
  248. void iota_gfx_write(const char *data) {
  249. matrix_write(&display, data);
  250. }
  251. static void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
  252. while (true) {
  253. uint8_t c = pgm_read_byte(data);
  254. if (c == 0) {
  255. return;
  256. }
  257. matrix_write_char(matrix, c);
  258. ++data;
  259. }
  260. }
  261. void iota_gfx_write_P(const char *data) {
  262. matrix_write_P(&display, data);
  263. }
  264. static void matrix_clear(struct CharacterMatrix *matrix) {
  265. memset(matrix->display, ' ', sizeof(matrix->display));
  266. matrix->cursor = &matrix->display[0][0];
  267. matrix->dirty = true;
  268. }
  269. void iota_gfx_clear_screen(void) {
  270. matrix_clear(&display);
  271. }
  272. static void matrix_render(struct CharacterMatrix *matrix) {
  273. last_flush = timer_read();
  274. iota_gfx_on();
  275. #if DEBUG_TO_SCREEN
  276. ++displaying;
  277. #endif
  278. // Move to the home position
  279. send_cmd3(PageAddr, 0, MatrixRows - 1);
  280. send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
  281. if (i2c_start_write(i2cAddress)) {
  282. goto done;
  283. }
  284. if (i2c_master_write(0x40)) {
  285. // Data mode
  286. goto done;
  287. }
  288. for (uint8_t row = 0; row < MatrixRows; ++row) {
  289. for (uint8_t col = 0; col < MatrixCols; ++col) {
  290. const uint8_t *glyph = font + (matrix->display[row][col] * (FontWidth - 1));
  291. for (uint8_t glyphCol = 0; glyphCol < FontWidth - 1; ++glyphCol) {
  292. uint8_t colBits = pgm_read_byte(glyph + glyphCol);
  293. i2c_master_write(colBits);
  294. }
  295. // 1 column of space between chars (it's not included in the glyph)
  296. i2c_master_write(0);
  297. }
  298. }
  299. matrix->dirty = false;
  300. done:
  301. i2c_master_stop();
  302. #if DEBUG_TO_SCREEN
  303. --displaying;
  304. #endif
  305. }
  306. void iota_gfx_flush(void) {
  307. matrix_render(&display);
  308. }
  309. static void matrix_update(struct CharacterMatrix *dest,
  310. const struct CharacterMatrix *source) {
  311. if (memcmp(dest->display, source->display, sizeof(dest->display))) {
  312. memcpy(dest->display, source->display, sizeof(dest->display));
  313. dest->dirty = true;
  314. }
  315. }
  316. static void render_status_info(void) {
  317. #if DEBUG_TO_SCREEN
  318. if (debug_enable) {
  319. return;
  320. }
  321. #endif
  322. struct CharacterMatrix matrix;
  323. matrix_clear(&matrix);
  324. matrix_write_P(&matrix, PSTR("USB: "));
  325. #ifdef PROTOCOL_LUFA
  326. switch (USB_DeviceState) {
  327. case DEVICE_STATE_Unattached:
  328. matrix_write_P(&matrix, PSTR("Unattached"));
  329. break;
  330. case DEVICE_STATE_Suspended:
  331. matrix_write_P(&matrix, PSTR("Suspended"));
  332. break;
  333. case DEVICE_STATE_Configured:
  334. matrix_write_P(&matrix, PSTR("Connected"));
  335. break;
  336. case DEVICE_STATE_Powered:
  337. matrix_write_P(&matrix, PSTR("Powered"));
  338. break;
  339. case DEVICE_STATE_Default:
  340. matrix_write_P(&matrix, PSTR("Default"));
  341. break;
  342. case DEVICE_STATE_Addressed:
  343. matrix_write_P(&matrix, PSTR("Addressed"));
  344. break;
  345. default:
  346. matrix_write_P(&matrix, PSTR("Invalid"));
  347. }
  348. #endif
  349. // Define layers here, Have not worked out how to have text displayed for each layer. Copy down the number you see and add a case for it below
  350. char buf[40];
  351. snprintf(buf,sizeof(buf), "Undef-%ld", layer_state);
  352. matrix_write_P(&matrix, PSTR("\n\nLayer: "));
  353. switch (layer_state) {
  354. case _BASE:
  355. matrix_write_P(&matrix, PSTR("Default"));
  356. break;
  357. case _RAISE:
  358. matrix_write_P(&matrix, PSTR("Raise"));
  359. break;
  360. case _LOWER:
  361. matrix_write_P(&matrix, PSTR("Lower"));
  362. break;
  363. case _ADJUST:
  364. matrix_write_P(&matrix, PSTR("ADJUST"));
  365. break;
  366. default:
  367. matrix_write(&matrix, buf);
  368. }
  369. // Host Keyboard LED Status
  370. char led[40];
  371. snprintf(led, sizeof(led), "\n%s %s %s",
  372. (host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? "NUMLOCK" : " ",
  373. (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? "CAPS" : " ",
  374. (host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) ? "SCLK" : " ");
  375. matrix_write(&matrix, led);
  376. matrix_update(&display, &matrix);
  377. }
  378. void iota_gfx_task(void) {
  379. render_status_info();
  380. if (display.dirty) {
  381. iota_gfx_flush();
  382. }
  383. if (timer_elapsed(last_flush) > ScreenOffInterval) {
  384. iota_gfx_off();
  385. }
  386. }
  387. #endif