No Description
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.

lcd.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "hardware/i2c.h"
  19. #include "ssd1306.h"
  20. #include "buttons.h"
  21. #include "main.h"
  22. #include "lcd.h"
  23. static i2c_inst_t *gpio_i2c_proto = i2c0;
  24. static const uint gpio_num_proto[2] = { 0, 1 };
  25. static i2c_inst_t *gpio_i2c_v2 = i2c0;
  26. static const uint gpio_num_v2[2] = { 16, 17 };
  27. #define LCD_ADDR 0x3C
  28. static ssd1306_t disp = {0};
  29. static bool buttons[NUM_BTNS] = {0};
  30. static bool changed = true;
  31. static void lcd_debug_buttons_callback(enum buttons btn, bool v) {
  32. buttons[btn] = v;
  33. changed = true;
  34. }
  35. void lcd_debug_buttons(void) {
  36. buttons_callback(lcd_debug_buttons_callback);
  37. while (1) {
  38. buttons_run();
  39. handle_serial_input();
  40. if (changed) {
  41. changed = false;
  42. ssd1306_clear(&disp);
  43. ssd1306_draw_string(&disp, 0, 0, 3, "Buttons");
  44. for (uint i = 0; i < NUM_BTNS; i++) {
  45. if ((hw_type == HW_PROTOTYPE) && (i >= BTN_D) && (i <= BTN_H)) {
  46. continue;
  47. }
  48. ssd1306_draw_char(&disp,
  49. i * 12, LCD_HEIGHT - 20 - 16 - 1,
  50. 2, '0' + i);
  51. if (buttons[i]) {
  52. ssd1306_draw_square(&disp,
  53. i * 12, LCD_HEIGHT - 20 - 1,
  54. 10, 20);
  55. } else {
  56. ssd1306_draw_empty_square(&disp,
  57. i * 12, LCD_HEIGHT - 20 - 1,
  58. 10, 20);
  59. }
  60. }
  61. ssd1306_show(&disp);
  62. }
  63. }
  64. }
  65. void lcd_draw_bitmap(uint8_t *data, int width, int height, int x_off, int y_off) {
  66. ssd1306_clear(&disp);
  67. for (int y = 0; y < height; y++) {
  68. for (int x = 0; x < width; x++) {
  69. const uint pos = y * width + x;
  70. const uint bit = 7 - (pos % 8);
  71. if (data[pos / 8] & (1 << bit)) {
  72. ssd1306_draw_pixel(&disp, x + x_off, y + y_off);
  73. }
  74. }
  75. }
  76. ssd1306_show(&disp);
  77. }
  78. void lcd_init(void) {
  79. if (hw_type == HW_PROTOTYPE) {
  80. i2c_init(gpio_i2c_proto, 2UL * 1000UL * 1000UL);
  81. for (uint i = 0; i < sizeof(gpio_num_proto) / sizeof(gpio_num_proto[0]); i++) {
  82. gpio_set_function(gpio_num_proto[i], GPIO_FUNC_I2C);
  83. gpio_pull_up(gpio_num_proto[i]);
  84. }
  85. ssd1306_init(&disp,
  86. LCD_WIDTH, LCD_HEIGHT,
  87. LCD_ADDR, gpio_i2c_proto);
  88. } else if (hw_type == HW_V2) {
  89. i2c_init(gpio_i2c_v2, 2UL * 1000UL * 1000UL);
  90. for (uint i = 0; i < sizeof(gpio_num_v2) / sizeof(gpio_num_v2[0]); i++) {
  91. gpio_set_function(gpio_num_v2[i], GPIO_FUNC_I2C);
  92. gpio_pull_up(gpio_num_v2[i]);
  93. }
  94. ssd1306_init(&disp,
  95. LCD_WIDTH, LCD_HEIGHT,
  96. LCD_ADDR, gpio_i2c_v2);
  97. }
  98. }
  99. void lcd_draw(const char *mode, const char *val, const char *bat) {
  100. ssd1306_clear(&disp);
  101. ssd1306_draw_string(&disp, 0, 0, 2, mode);
  102. ssd1306_draw_string(&disp, 0, 20, 4, val);
  103. ssd1306_draw_string(&disp, 0, LCD_HEIGHT - 8, 1, bat);
  104. ssd1306_show(&disp);
  105. }
  106. void lcd_draw_bye(void) {
  107. ssd1306_clear(&disp);
  108. ssd1306_draw_string(&disp, 6, 5, 3, " Boot-");
  109. ssd1306_draw_string(&disp, 8, LCD_HEIGHT / 2 + 5, 3, "loader");
  110. ssd1306_show(&disp);
  111. }
  112. void lcd_draw_version(void) {
  113. ssd1306_clear(&disp);
  114. // TODO
  115. ssd1306_draw_string(&disp, 0, LCD_HEIGHT / 2 - 4, 1, "TODO");
  116. ssd1306_show(&disp);
  117. }