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 3.8KB

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