Bez popisu
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * lcd.c
  3. *
  4. * Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "hardware/i2c.h"
  21. #include "hardware/watchdog.h"
  22. #include "ssd1306.h"
  23. #include "git.h"
  24. #include "config.h"
  25. #include "buttons.h"
  26. #include "console.h"
  27. #include "main.h"
  28. #include "usb.h"
  29. #include "lcd.h"
  30. static i2c_inst_t *gpio_i2c_proto = i2c0;
  31. static const uint gpio_num_proto[2] = { 0, 1 };
  32. static i2c_inst_t *gpio_i2c_v2 = i2c0;
  33. static const uint gpio_num_v2[2] = { 16, 17 };
  34. #define LCD_ADDR 0x3C
  35. static ssd1306_t disp = {0};
  36. static bool buttons[NUM_BTNS] = {0};
  37. static bool changed = true;
  38. static bool found = false;
  39. static void lcd_debug_buttons_callback(enum buttons btn, bool v) {
  40. buttons[btn] = v;
  41. changed = true;
  42. }
  43. void lcd_debug_buttons(void) {
  44. if (!found) {
  45. return;
  46. }
  47. buttons_callback(lcd_debug_buttons_callback);
  48. while (1) {
  49. watchdog_update();
  50. buttons_run();
  51. usb_run();
  52. cnsl_run();
  53. if (changed) {
  54. changed = false;
  55. ssd1306_clear(&disp);
  56. ssd1306_draw_string(&disp, 0, 0, 3, "Buttons");
  57. for (uint i = 0; i < NUM_BTNS; i++) {
  58. if ((hw_type == HW_PROTOTYPE) && (i >= BTN_D) && (i <= BTN_H)) {
  59. continue;
  60. }
  61. ssd1306_draw_char(&disp,
  62. i * 12, LCD_HEIGHT - 20 - 16 - 1,
  63. 2, '0' + i);
  64. if (buttons[i]) {
  65. ssd1306_draw_square(&disp,
  66. i * 12, LCD_HEIGHT - 20 - 1,
  67. 10, 20);
  68. } else {
  69. ssd1306_draw_empty_square(&disp,
  70. i * 12, LCD_HEIGHT - 20 - 1,
  71. 10, 20);
  72. }
  73. }
  74. ssd1306_show(&disp);
  75. }
  76. }
  77. }
  78. void lcd_draw_bitmap(uint8_t *data, int width, int height, int x_off, int y_off) {
  79. if (!found) {
  80. return;
  81. }
  82. ssd1306_clear(&disp);
  83. for (int y = 0; y < height; y++) {
  84. for (int x = 0; x < width; x++) {
  85. const uint pos = y * width + x;
  86. const uint bit = 7 - (pos % 8);
  87. if (data[pos / 8] & (1 << bit)) {
  88. ssd1306_draw_pixel(&disp, x + x_off, y + y_off);
  89. }
  90. }
  91. }
  92. ssd1306_show(&disp);
  93. }
  94. void lcd_init(void) {
  95. if (hw_type == HW_PROTOTYPE) {
  96. i2c_init(gpio_i2c_proto, 2UL * 1000UL * 1000UL);
  97. for (uint i = 0; i < sizeof(gpio_num_proto) / sizeof(gpio_num_proto[0]); i++) {
  98. gpio_set_function(gpio_num_proto[i], GPIO_FUNC_I2C);
  99. gpio_pull_up(gpio_num_proto[i]);
  100. }
  101. bool r = ssd1306_init(&disp,
  102. LCD_WIDTH, LCD_HEIGHT,
  103. LCD_ADDR, gpio_i2c_proto);
  104. found = r;
  105. } else if (hw_type == HW_V2) {
  106. i2c_init(gpio_i2c_v2, 2UL * 1000UL * 1000UL);
  107. for (uint i = 0; i < sizeof(gpio_num_v2) / sizeof(gpio_num_v2[0]); i++) {
  108. gpio_set_function(gpio_num_v2[i], GPIO_FUNC_I2C);
  109. gpio_pull_up(gpio_num_v2[i]);
  110. }
  111. bool r = ssd1306_init(&disp,
  112. LCD_WIDTH, LCD_HEIGHT,
  113. LCD_ADDR, gpio_i2c_v2);
  114. found = r;
  115. }
  116. }
  117. void lcd_draw(const char *mode, const char *val, const char *bat) {
  118. if (!found) {
  119. return;
  120. }
  121. ssd1306_clear(&disp);
  122. ssd1306_draw_string(&disp, 0, 0, 2, mode);
  123. ssd1306_draw_string(&disp, 0, 20, 4, val);
  124. ssd1306_draw_string(&disp, 0, LCD_HEIGHT - 8, 1, bat);
  125. ssd1306_show(&disp);
  126. }
  127. void lcd_draw_bye(void) {
  128. if (!found) {
  129. return;
  130. }
  131. ssd1306_clear(&disp);
  132. ssd1306_draw_string(&disp, 6, 5, 3, " Boot-");
  133. ssd1306_draw_string(&disp, 8, LCD_HEIGHT / 2 + 5, 3, "loader");
  134. ssd1306_show(&disp);
  135. }
  136. void lcd_draw_version(void) {
  137. if (!found) {
  138. return;
  139. }
  140. ssd1306_clear(&disp);
  141. ssd1306_draw_string(&disp, 0, 0, 2,
  142. "LARS");
  143. ssd1306_draw_string(&disp, 0, FONT_HEIGHT * 2 + 1, 1,
  144. "Release: " VERSION_STR);
  145. ssd1306_draw_string(&disp, 0, FONT_HEIGHT * 2 + 1 + FONT_HEIGHT + 1, 1,
  146. __DATE__ " " __TIME__);
  147. if (git_IsPopulated()) {
  148. const char *hash = git_CommitSHA1();
  149. char short_hash[6 + 7 + 6 + 1] = {0};
  150. memcpy(short_hash, "Hash: ", 6);
  151. memcpy(short_hash + 6, hash, 7);
  152. if (git_AnyUncommittedChanges()) {
  153. memcpy(short_hash + 6 + 7, " dirty", 6);
  154. }
  155. ssd1306_draw_string(&disp, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 2, 1,
  156. short_hash);
  157. } else {
  158. ssd1306_draw_string(&disp, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 2, 1,
  159. "No Git Repo");
  160. }
  161. char hw_id_str[42] = {0};
  162. if (hw_type == HW_PROTOTYPE) {
  163. snprintf(hw_id_str, sizeof(hw_id_str) - 1,
  164. "HW Prototype");
  165. } else if (hw_type == HW_V2) {
  166. snprintf(hw_id_str, sizeof(hw_id_str) - 1,
  167. "HW V2");
  168. } else {
  169. snprintf(hw_id_str, sizeof(hw_id_str) - 1,
  170. "HW unknown %X", hw_type);
  171. }
  172. ssd1306_draw_string(&disp, 0, FONT_HEIGHT * 2 + 1 + (FONT_HEIGHT + 1) * 3, 1,
  173. hw_id_str);
  174. ssd1306_show(&disp);
  175. }