You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

505 lines
15 KiB

  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. setPinOutput(ST7565_A0_PIN);
  80. writePinHigh(ST7565_A0_PIN);
  81. setPinOutput(ST7565_RST_PIN);
  82. writePinHigh(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. // Turn on display if it is off
  157. st7565_on();
  158. // Clear dirty flag
  159. st7565_dirty &= ~((ST7565_BLOCK_TYPE)1 << update_start);
  160. }
  161. void st7565_set_cursor(uint8_t col, uint8_t line) {
  162. uint16_t index = line * ST7565_DISPLAY_WIDTH + col * ST7565_FONT_WIDTH;
  163. // Out of bounds?
  164. if (index >= ST7565_MATRIX_SIZE) {
  165. index = 0;
  166. }
  167. st7565_cursor = &st7565_buffer[index];
  168. }
  169. void st7565_advance_page(bool clearPageRemainder) {
  170. uint16_t index = st7565_cursor - &st7565_buffer[0];
  171. uint8_t remaining = ST7565_DISPLAY_WIDTH - (index % ST7565_DISPLAY_WIDTH);
  172. if (clearPageRemainder) {
  173. // Remaining Char count
  174. remaining = remaining / ST7565_FONT_WIDTH;
  175. // Write empty character until next line
  176. while (remaining--)
  177. st7565_write_char(' ', false);
  178. } else {
  179. // Next page index out of bounds?
  180. if (index + remaining >= ST7565_MATRIX_SIZE) {
  181. index = 0;
  182. remaining = 0;
  183. }
  184. st7565_cursor = &st7565_buffer[index + remaining];
  185. }
  186. }
  187. void st7565_advance_char(void) {
  188. uint16_t nextIndex = st7565_cursor - &st7565_buffer[0] + ST7565_FONT_WIDTH;
  189. uint8_t remainingSpace = ST7565_DISPLAY_WIDTH - (nextIndex % ST7565_DISPLAY_WIDTH);
  190. // Do we have enough space on the current line for the next character
  191. if (remainingSpace < ST7565_FONT_WIDTH) {
  192. nextIndex += remainingSpace;
  193. }
  194. // Did we go out of bounds
  195. if (nextIndex >= ST7565_MATRIX_SIZE) {
  196. nextIndex = 0;
  197. }
  198. // Update cursor position
  199. st7565_cursor = &st7565_buffer[nextIndex];
  200. }
  201. // Main handler that writes character data to the display buffer
  202. void st7565_write_char(const char data, bool invert) {
  203. // Advance to the next line if newline
  204. if (data == '\n') {
  205. // Old source wrote ' ' until end of line...
  206. st7565_advance_page(true);
  207. return;
  208. }
  209. if (data == '\r') {
  210. st7565_advance_page(false);
  211. return;
  212. }
  213. // copy the current render buffer to check for dirty after
  214. static uint8_t st7565_temp_buffer[ST7565_FONT_WIDTH];
  215. memcpy(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH);
  216. _Static_assert(sizeof(font) >= ((ST7565_FONT_END + 1 - ST7565_FONT_START) * ST7565_FONT_WIDTH), "ST7565_FONT_END references outside array");
  217. // set the reder buffer data
  218. uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
  219. if (cast_data < ST7565_FONT_START || cast_data > ST7565_FONT_END) {
  220. memset(st7565_cursor, 0x00, ST7565_FONT_WIDTH);
  221. } else {
  222. const uint8_t *glyph = &font[(cast_data - ST7565_FONT_START) * ST7565_FONT_WIDTH];
  223. memcpy_P(st7565_cursor, glyph, ST7565_FONT_WIDTH);
  224. }
  225. // Invert if needed
  226. if (invert) {
  227. InvertCharacter(st7565_cursor);
  228. }
  229. // Dirty check
  230. if (memcmp(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH)) {
  231. uint16_t index = st7565_cursor - &st7565_buffer[0];
  232. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
  233. // Edgecase check if the written data spans the 2 chunks
  234. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << ((index + ST7565_FONT_WIDTH - 1) / ST7565_BLOCK_SIZE));
  235. }
  236. // Finally move to the next char
  237. st7565_advance_char();
  238. }
  239. void st7565_write(const char *data, bool invert) {
  240. const char *end = data + strlen(data);
  241. while (data < end) {
  242. st7565_write_char(*data, invert);
  243. data++;
  244. }
  245. }
  246. void st7565_write_ln(const char *data, bool invert) {
  247. st7565_write(data, invert);
  248. st7565_advance_page(true);
  249. }
  250. void st7565_pan(bool left) {
  251. uint16_t i = 0;
  252. for (uint16_t y = 0; y < ST7565_DISPLAY_HEIGHT / 8; y++) {
  253. if (left) {
  254. for (uint16_t x = 0; x < ST7565_DISPLAY_WIDTH - 1; x++) {
  255. i = y * ST7565_DISPLAY_WIDTH + x;
  256. st7565_buffer[i] = st7565_buffer[i + 1];
  257. }
  258. } else {
  259. for (uint16_t x = ST7565_DISPLAY_WIDTH - 1; x > 0; x--) {
  260. i = y * ST7565_DISPLAY_WIDTH + x;
  261. st7565_buffer[i] = st7565_buffer[i - 1];
  262. }
  263. }
  264. }
  265. st7565_dirty = ST7565_ALL_BLOCKS_MASK;
  266. }
  267. display_buffer_reader_t st7565_read_raw(uint16_t start_index) {
  268. if (start_index > ST7565_MATRIX_SIZE) start_index = ST7565_MATRIX_SIZE;
  269. display_buffer_reader_t ret_reader;
  270. ret_reader.current_element = &st7565_buffer[start_index];
  271. ret_reader.remaining_element_count = ST7565_MATRIX_SIZE - start_index;
  272. return ret_reader;
  273. }
  274. void st7565_write_raw_byte(const char data, uint16_t index) {
  275. if (index > ST7565_MATRIX_SIZE) index = ST7565_MATRIX_SIZE;
  276. if (st7565_buffer[index] == data) return;
  277. st7565_buffer[index] = data;
  278. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
  279. }
  280. void st7565_write_raw(const char *data, uint16_t size) {
  281. uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
  282. if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
  283. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  284. uint8_t c = *data++;
  285. if (st7565_buffer[i] == c) continue;
  286. st7565_buffer[i] = c;
  287. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
  288. }
  289. }
  290. void st7565_write_pixel(uint8_t x, uint8_t y, bool on) {
  291. if (x >= ST7565_DISPLAY_WIDTH) {
  292. return;
  293. }
  294. uint16_t index = x + (y / 8) * ST7565_DISPLAY_WIDTH;
  295. if (index >= ST7565_MATRIX_SIZE) {
  296. return;
  297. }
  298. uint8_t data = st7565_buffer[index];
  299. if (on) {
  300. data |= (1 << (y % 8));
  301. } else {
  302. data &= ~(1 << (y % 8));
  303. }
  304. if (st7565_buffer[index] != data) {
  305. st7565_buffer[index] = data;
  306. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
  307. }
  308. }
  309. #if defined(__AVR__)
  310. void st7565_write_P(const char *data, bool invert) {
  311. uint8_t c = pgm_read_byte(data);
  312. while (c != 0) {
  313. st7565_write_char(c, invert);
  314. c = pgm_read_byte(++data);
  315. }
  316. }
  317. void st7565_write_ln_P(const char *data, bool invert) {
  318. st7565_write_P(data, invert);
  319. st7565_advance_page(true);
  320. }
  321. void st7565_write_raw_P(const char *data, uint16_t size) {
  322. uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
  323. if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
  324. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  325. uint8_t c = pgm_read_byte(data++);
  326. if (st7565_buffer[i] == c) continue;
  327. st7565_buffer[i] = c;
  328. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
  329. }
  330. }
  331. #endif // defined(__AVR__)
  332. bool st7565_on(void) {
  333. if (!st7565_initialized) {
  334. return st7565_active;
  335. }
  336. #if ST7565_TIMEOUT > 0
  337. st7565_timeout = timer_read32() + ST7565_TIMEOUT;
  338. #endif
  339. if (!st7565_active) {
  340. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  341. st7565_send_cmd(DISPLAY_ON);
  342. spi_stop();
  343. st7565_active = true;
  344. st7565_on_user();
  345. }
  346. return st7565_active;
  347. }
  348. __attribute__((weak)) void st7565_on_user(void) {}
  349. bool st7565_off(void) {
  350. if (!st7565_initialized) {
  351. return !st7565_active;
  352. }
  353. if (st7565_active) {
  354. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  355. st7565_send_cmd(DISPLAY_OFF);
  356. spi_stop();
  357. st7565_active = false;
  358. st7565_off_user();
  359. }
  360. return !st7565_active;
  361. }
  362. __attribute__((weak)) void st7565_off_user(void) {}
  363. bool st7565_is_on(void) {
  364. return st7565_active;
  365. }
  366. bool st7565_invert(bool invert) {
  367. if (!st7565_initialized) {
  368. return st7565_inverted;
  369. }
  370. if (invert != st7565_inverted) {
  371. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  372. st7565_send_cmd(invert ? INVERT_DISPLAY : NORMAL_DISPLAY);
  373. spi_stop();
  374. st7565_inverted = invert;
  375. }
  376. return st7565_inverted;
  377. }
  378. uint8_t st7565_max_chars(void) {
  379. return ST7565_DISPLAY_WIDTH / ST7565_FONT_WIDTH;
  380. }
  381. uint8_t st7565_max_lines(void) {
  382. return ST7565_DISPLAY_HEIGHT / ST7565_FONT_HEIGHT;
  383. }
  384. void st7565_task(void) {
  385. if (!st7565_initialized) {
  386. return;
  387. }
  388. #if ST7565_UPDATE_INTERVAL > 0
  389. if (timer_elapsed(st7565_update_timeout) >= ST7565_UPDATE_INTERVAL) {
  390. st7565_update_timeout = timer_read();
  391. st7565_set_cursor(0, 0);
  392. st7565_task_user();
  393. }
  394. #else
  395. st7565_set_cursor(0, 0);
  396. st7565_task_user();
  397. #endif
  398. // Smart render system, no need to check for dirty
  399. st7565_render();
  400. // Display timeout check
  401. #if ST7565_TIMEOUT > 0
  402. if (st7565_active && timer_expired32(timer_read32(), st7565_timeout)) {
  403. st7565_off();
  404. }
  405. #endif
  406. }
  407. __attribute__((weak)) void st7565_task_user(void) {}
  408. void st7565_reset(void) {
  409. writePinLow(ST7565_RST_PIN);
  410. wait_ms(20);
  411. writePinHigh(ST7565_RST_PIN);
  412. wait_ms(20);
  413. }
  414. spi_status_t st7565_send_cmd(uint8_t cmd) {
  415. writePinLow(ST7565_A0_PIN);
  416. return spi_write(cmd);
  417. }
  418. spi_status_t st7565_send_data(uint8_t *data, uint16_t length) {
  419. writePinHigh(ST7565_A0_PIN);
  420. return spi_transmit(data, length);
  421. }