st7565.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. Copyright 2021
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "st7565.h"
  15. #include <string.h>
  16. #include "keyboard.h"
  17. #include "progmem.h"
  18. #include "timer.h"
  19. #include "wait.h"
  20. #include ST7565_FONT_H
  21. // Fundamental Commands
  22. #define CONTRAST 0x81
  23. #define DISPLAY_ALL_ON 0xA5
  24. #define DISPLAY_ALL_ON_RESUME 0xA4
  25. #define NORMAL_DISPLAY 0xA6
  26. #define INVERT_DISPLAY 0xA7
  27. #define DISPLAY_ON 0xAF
  28. #define DISPLAY_OFF 0xAE
  29. #define NOP 0xE3
  30. // Addressing Setting Commands
  31. #define PAM_SETCOLUMN_LSB 0x00
  32. #define PAM_SETCOLUMN_MSB 0x10
  33. #define PAM_PAGE_ADDR 0xB0 // 0xb0 -- 0xb7
  34. // Hardware Configuration Commands
  35. #define DISPLAY_START_LINE 0x40
  36. #define SEGMENT_REMAP 0xA0
  37. #define SEGMENT_REMAP_INV 0xA1
  38. #define COM_SCAN_INC 0xC0
  39. #define COM_SCAN_DEC 0xC8
  40. #define LCD_BIAS_7 0xA3
  41. #define LCD_BIAS_9 0xA2
  42. #define RESISTOR_RATIO 0x20
  43. #define POWER_CONTROL 0x28
  44. // Misc defines
  45. #ifndef ST7565_BLOCK_COUNT
  46. # define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8)
  47. #endif
  48. #ifndef ST7565_BLOCK_SIZE
  49. # define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT)
  50. #endif
  51. #define ST7565_ALL_BLOCKS_MASK (((((ST7565_BLOCK_TYPE)1 << (ST7565_BLOCK_COUNT - 1)) - 1) << 1) | 1)
  52. #define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
  53. // Display buffer's is the same as the display memory layout
  54. // this is so we don't end up with rounding errors with
  55. // parts of the display unusable or don't get cleared correctly
  56. // and also allows for drawing & inverting
  57. uint8_t st7565_buffer[ST7565_MATRIX_SIZE];
  58. uint8_t * st7565_cursor;
  59. ST7565_BLOCK_TYPE st7565_dirty = 0;
  60. bool st7565_initialized = false;
  61. bool st7565_active = false;
  62. bool st7565_inverted = false;
  63. display_rotation_t st7565_rotation = DISPLAY_ROTATION_0;
  64. #if ST7565_TIMEOUT > 0
  65. uint32_t st7565_timeout;
  66. #endif
  67. #if ST7565_UPDATE_INTERVAL > 0
  68. uint16_t st7565_update_timeout;
  69. #endif
  70. // Flips the rendering bits for a character at the current cursor position
  71. static void InvertCharacter(uint8_t *cursor) {
  72. const uint8_t *end = cursor + ST7565_FONT_WIDTH;
  73. while (cursor < end) {
  74. *cursor = ~(*cursor);
  75. cursor++;
  76. }
  77. }
  78. bool st7565_init(display_rotation_t rotation) {
  79. gpio_set_pin_output(ST7565_A0_PIN);
  80. gpio_write_pin_high(ST7565_A0_PIN);
  81. gpio_set_pin_output(ST7565_RST_PIN);
  82. gpio_write_pin_high(ST7565_RST_PIN);
  83. st7565_rotation = st7565_init_user(rotation);
  84. spi_init();
  85. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  86. st7565_reset();
  87. st7565_send_cmd(LCD_BIAS_7);
  88. if (!HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
  89. st7565_send_cmd(SEGMENT_REMAP);
  90. st7565_send_cmd(COM_SCAN_DEC);
  91. } else {
  92. st7565_send_cmd(SEGMENT_REMAP_INV);
  93. st7565_send_cmd(COM_SCAN_INC);
  94. }
  95. st7565_send_cmd(DISPLAY_START_LINE | 0x00);
  96. st7565_send_cmd(CONTRAST);
  97. st7565_send_cmd(ST7565_CONTRAST);
  98. st7565_send_cmd(RESISTOR_RATIO | 0x01);
  99. st7565_send_cmd(POWER_CONTROL | 0x04);
  100. wait_ms(50);
  101. st7565_send_cmd(POWER_CONTROL | 0x06);
  102. wait_ms(50);
  103. st7565_send_cmd(POWER_CONTROL | 0x07);
  104. wait_ms(10);
  105. st7565_send_cmd(DISPLAY_ON);
  106. st7565_send_cmd(DISPLAY_ALL_ON_RESUME);
  107. st7565_send_cmd(NORMAL_DISPLAY);
  108. spi_stop();
  109. #if ST7565_TIMEOUT > 0
  110. st7565_timeout = timer_read32() + ST7565_TIMEOUT;
  111. #endif
  112. st7565_clear();
  113. st7565_initialized = true;
  114. st7565_active = true;
  115. return true;
  116. }
  117. __attribute__((weak)) display_rotation_t st7565_init_user(display_rotation_t rotation) {
  118. return rotation;
  119. }
  120. void st7565_clear(void) {
  121. memset(st7565_buffer, 0, sizeof(st7565_buffer));
  122. st7565_cursor = &st7565_buffer[0];
  123. st7565_dirty = ST7565_ALL_BLOCKS_MASK;
  124. }
  125. uint8_t crot(uint8_t a, int8_t n) {
  126. const uint8_t mask = 0x7;
  127. n &= mask;
  128. return a << n | a >> (-n & mask);
  129. }
  130. void st7565_render(void) {
  131. if (!st7565_initialized) {
  132. return;
  133. }
  134. // Do we have work to do?
  135. st7565_dirty &= ST7565_ALL_BLOCKS_MASK;
  136. if (!st7565_dirty) {
  137. return;
  138. }
  139. // Find first dirty block
  140. uint8_t update_start = 0;
  141. while (!(st7565_dirty & ((ST7565_BLOCK_TYPE)1 << update_start))) {
  142. ++update_start;
  143. }
  144. // Calculate commands to set memory addressing bounds.
  145. uint8_t start_page = ST7565_BLOCK_SIZE * update_start / ST7565_DISPLAY_WIDTH;
  146. uint8_t start_column = ST7565_BLOCK_SIZE * update_start % ST7565_DISPLAY_WIDTH;
  147. // IC has 132 segment drivers, for panels with less width we need to offset the starting column
  148. if (HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
  149. start_column += (132 - ST7565_DISPLAY_WIDTH);
  150. }
  151. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  152. st7565_send_cmd(PAM_PAGE_ADDR | start_page);
  153. st7565_send_cmd(PAM_SETCOLUMN_LSB | ((ST7565_COLUMN_OFFSET + start_column) & 0x0f));
  154. st7565_send_cmd(PAM_SETCOLUMN_MSB | ((ST7565_COLUMN_OFFSET + start_column) >> 4 & 0x0f));
  155. st7565_send_data(&st7565_buffer[ST7565_BLOCK_SIZE * update_start], ST7565_BLOCK_SIZE);
  156. spi_stop();
  157. // Turn on display if it is off
  158. st7565_on();
  159. // Clear dirty flag
  160. st7565_dirty &= ~((ST7565_BLOCK_TYPE)1 << update_start);
  161. }
  162. void st7565_set_cursor(uint8_t col, uint8_t line) {
  163. uint16_t index = line * ST7565_DISPLAY_WIDTH + col * ST7565_FONT_WIDTH;
  164. // Out of bounds?
  165. if (index >= ST7565_MATRIX_SIZE) {
  166. index = 0;
  167. }
  168. st7565_cursor = &st7565_buffer[index];
  169. }
  170. void st7565_advance_page(bool clearPageRemainder) {
  171. uint16_t index = st7565_cursor - &st7565_buffer[0];
  172. uint8_t remaining = ST7565_DISPLAY_WIDTH - (index % ST7565_DISPLAY_WIDTH);
  173. if (clearPageRemainder) {
  174. // Remaining Char count
  175. remaining = remaining / ST7565_FONT_WIDTH;
  176. // Write empty character until next line
  177. while (remaining--)
  178. st7565_write_char(' ', false);
  179. } else {
  180. // Next page index out of bounds?
  181. if (index + remaining >= ST7565_MATRIX_SIZE) {
  182. index = 0;
  183. remaining = 0;
  184. }
  185. st7565_cursor = &st7565_buffer[index + remaining];
  186. }
  187. }
  188. void st7565_advance_char(void) {
  189. uint16_t nextIndex = st7565_cursor - &st7565_buffer[0] + ST7565_FONT_WIDTH;
  190. uint8_t remainingSpace = ST7565_DISPLAY_WIDTH - (nextIndex % ST7565_DISPLAY_WIDTH);
  191. // Do we have enough space on the current line for the next character
  192. if (remainingSpace < ST7565_FONT_WIDTH) {
  193. nextIndex += remainingSpace;
  194. }
  195. // Did we go out of bounds
  196. if (nextIndex >= ST7565_MATRIX_SIZE) {
  197. nextIndex = 0;
  198. }
  199. // Update cursor position
  200. st7565_cursor = &st7565_buffer[nextIndex];
  201. }
  202. // Main handler that writes character data to the display buffer
  203. void st7565_write_char(const char data, bool invert) {
  204. // Advance to the next line if newline
  205. if (data == '\n') {
  206. // Old source wrote ' ' until end of line...
  207. st7565_advance_page(true);
  208. return;
  209. }
  210. if (data == '\r') {
  211. st7565_advance_page(false);
  212. return;
  213. }
  214. // copy the current render buffer to check for dirty after
  215. static uint8_t st7565_temp_buffer[ST7565_FONT_WIDTH];
  216. memcpy(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH);
  217. _Static_assert(sizeof(font) >= ((ST7565_FONT_END + 1 - ST7565_FONT_START) * ST7565_FONT_WIDTH), "ST7565_FONT_END references outside array");
  218. // set the reder buffer data
  219. uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
  220. if (cast_data < ST7565_FONT_START || cast_data > ST7565_FONT_END) {
  221. memset(st7565_cursor, 0x00, ST7565_FONT_WIDTH);
  222. } else {
  223. const uint8_t *glyph = &font[(cast_data - ST7565_FONT_START) * ST7565_FONT_WIDTH];
  224. memcpy_P(st7565_cursor, glyph, ST7565_FONT_WIDTH);
  225. }
  226. // Invert if needed
  227. if (invert) {
  228. InvertCharacter(st7565_cursor);
  229. }
  230. // Dirty check
  231. if (memcmp(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH)) {
  232. uint16_t index = st7565_cursor - &st7565_buffer[0];
  233. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
  234. // Edgecase check if the written data spans the 2 chunks
  235. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << ((index + ST7565_FONT_WIDTH - 1) / ST7565_BLOCK_SIZE));
  236. }
  237. // Finally move to the next char
  238. st7565_advance_char();
  239. }
  240. void st7565_write(const char *data, bool invert) {
  241. const char *end = data + strlen(data);
  242. while (data < end) {
  243. st7565_write_char(*data, invert);
  244. data++;
  245. }
  246. }
  247. void st7565_write_ln(const char *data, bool invert) {
  248. st7565_write(data, invert);
  249. st7565_advance_page(true);
  250. }
  251. void st7565_pan(bool left) {
  252. uint16_t i = 0;
  253. for (uint16_t y = 0; y < ST7565_DISPLAY_HEIGHT / 8; y++) {
  254. if (left) {
  255. for (uint16_t x = 0; x < ST7565_DISPLAY_WIDTH - 1; x++) {
  256. i = y * ST7565_DISPLAY_WIDTH + x;
  257. st7565_buffer[i] = st7565_buffer[i + 1];
  258. }
  259. } else {
  260. for (uint16_t x = ST7565_DISPLAY_WIDTH - 1; x > 0; x--) {
  261. i = y * ST7565_DISPLAY_WIDTH + x;
  262. st7565_buffer[i] = st7565_buffer[i - 1];
  263. }
  264. }
  265. }
  266. st7565_dirty = ST7565_ALL_BLOCKS_MASK;
  267. }
  268. display_buffer_reader_t st7565_read_raw(uint16_t start_index) {
  269. if (start_index > ST7565_MATRIX_SIZE) start_index = ST7565_MATRIX_SIZE;
  270. display_buffer_reader_t ret_reader;
  271. ret_reader.current_element = &st7565_buffer[start_index];
  272. ret_reader.remaining_element_count = ST7565_MATRIX_SIZE - start_index;
  273. return ret_reader;
  274. }
  275. void st7565_write_raw_byte(const char data, uint16_t index) {
  276. if (index > ST7565_MATRIX_SIZE) index = ST7565_MATRIX_SIZE;
  277. if (st7565_buffer[index] == data) return;
  278. st7565_buffer[index] = data;
  279. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
  280. }
  281. void st7565_write_raw(const char *data, uint16_t size) {
  282. uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
  283. if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
  284. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  285. uint8_t c = *data++;
  286. if (st7565_buffer[i] == c) continue;
  287. st7565_buffer[i] = c;
  288. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
  289. }
  290. }
  291. void st7565_write_pixel(uint8_t x, uint8_t y, bool on) {
  292. if (x >= ST7565_DISPLAY_WIDTH) {
  293. return;
  294. }
  295. uint16_t index = x + (y / 8) * ST7565_DISPLAY_WIDTH;
  296. if (index >= ST7565_MATRIX_SIZE) {
  297. return;
  298. }
  299. uint8_t data = st7565_buffer[index];
  300. if (on) {
  301. data |= (1 << (y % 8));
  302. } else {
  303. data &= ~(1 << (y % 8));
  304. }
  305. if (st7565_buffer[index] != data) {
  306. st7565_buffer[index] = data;
  307. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
  308. }
  309. }
  310. #if defined(__AVR__)
  311. void st7565_write_P(const char *data, bool invert) {
  312. uint8_t c = pgm_read_byte(data);
  313. while (c != 0) {
  314. st7565_write_char(c, invert);
  315. c = pgm_read_byte(++data);
  316. }
  317. }
  318. void st7565_write_ln_P(const char *data, bool invert) {
  319. st7565_write_P(data, invert);
  320. st7565_advance_page(true);
  321. }
  322. void st7565_write_raw_P(const char *data, uint16_t size) {
  323. uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
  324. if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
  325. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  326. uint8_t c = pgm_read_byte(data++);
  327. if (st7565_buffer[i] == c) continue;
  328. st7565_buffer[i] = c;
  329. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
  330. }
  331. }
  332. #endif // defined(__AVR__)
  333. bool st7565_on(void) {
  334. if (!st7565_initialized) {
  335. return st7565_active;
  336. }
  337. #if ST7565_TIMEOUT > 0
  338. st7565_timeout = timer_read32() + ST7565_TIMEOUT;
  339. #endif
  340. if (!st7565_active) {
  341. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  342. st7565_send_cmd(DISPLAY_ON);
  343. spi_stop();
  344. st7565_active = true;
  345. st7565_on_user();
  346. }
  347. return st7565_active;
  348. }
  349. __attribute__((weak)) void st7565_on_user(void) {}
  350. bool st7565_off(void) {
  351. if (!st7565_initialized) {
  352. return !st7565_active;
  353. }
  354. if (st7565_active) {
  355. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  356. st7565_send_cmd(DISPLAY_OFF);
  357. spi_stop();
  358. st7565_active = false;
  359. st7565_off_user();
  360. }
  361. return !st7565_active;
  362. }
  363. __attribute__((weak)) void st7565_off_user(void) {}
  364. bool st7565_is_on(void) {
  365. return st7565_active;
  366. }
  367. bool st7565_invert(bool invert) {
  368. if (!st7565_initialized) {
  369. return st7565_inverted;
  370. }
  371. if (invert != st7565_inverted) {
  372. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  373. st7565_send_cmd(invert ? INVERT_DISPLAY : NORMAL_DISPLAY);
  374. spi_stop();
  375. st7565_inverted = invert;
  376. }
  377. return st7565_inverted;
  378. }
  379. uint8_t st7565_max_chars(void) {
  380. return ST7565_DISPLAY_WIDTH / ST7565_FONT_WIDTH;
  381. }
  382. uint8_t st7565_max_lines(void) {
  383. return ST7565_DISPLAY_HEIGHT / ST7565_FONT_HEIGHT;
  384. }
  385. void st7565_task(void) {
  386. if (!st7565_initialized) {
  387. return;
  388. }
  389. #if ST7565_UPDATE_INTERVAL > 0
  390. if (timer_elapsed(st7565_update_timeout) >= ST7565_UPDATE_INTERVAL) {
  391. st7565_update_timeout = timer_read();
  392. st7565_set_cursor(0, 0);
  393. st7565_task_user();
  394. }
  395. #else
  396. st7565_set_cursor(0, 0);
  397. st7565_task_user();
  398. #endif
  399. // Smart render system, no need to check for dirty
  400. st7565_render();
  401. // Display timeout check
  402. #if ST7565_TIMEOUT > 0
  403. if (st7565_active && timer_expired32(timer_read32(), st7565_timeout)) {
  404. st7565_off();
  405. }
  406. #endif
  407. }
  408. __attribute__((weak)) void st7565_task_user(void) {}
  409. void st7565_reset(void) {
  410. gpio_write_pin_low(ST7565_RST_PIN);
  411. wait_ms(20);
  412. gpio_write_pin_high(ST7565_RST_PIN);
  413. wait_ms(20);
  414. }
  415. spi_status_t st7565_send_cmd(uint8_t cmd) {
  416. gpio_write_pin_low(ST7565_A0_PIN);
  417. return spi_write(cmd);
  418. }
  419. spi_status_t st7565_send_data(uint8_t *data, uint16_t length) {
  420. gpio_write_pin_high(ST7565_A0_PIN);
  421. return spi_transmit(data, length);
  422. }