ssd1306.c 11 KB

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