S&B Volcano vaporizer remote control with Pi Pico W
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * ota_shim.c
  3. *
  4. * Copyright (c) 2023 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 <stdarg.h>
  19. #include <stdio.h>
  20. #include "pico/cyw43_arch.h"
  21. #include "config.h"
  22. #include "buttons.h"
  23. #include "lcd.h"
  24. #include "log.h"
  25. #include "lcd.h"
  26. #include "textbox.h"
  27. #include "mem.h"
  28. #include "util.h"
  29. #include "usb_descriptors.h"
  30. #include "wifi.h"
  31. static int16_t lcd_off = 0;
  32. static size_t text_window = 420; // TODO
  33. static size_t text_off = 0;
  34. static size_t prev_len = 0;
  35. static bool redraw = false;
  36. static void lcd_write(const void *buf, size_t len) {
  37. char tmp[len + 1];
  38. memcpy(tmp, buf, len);
  39. tmp[len] = '\0';
  40. lcd_off = text_box(tmp, false,
  41. "fixed_5x8",
  42. 0, LCD_WIDTH,
  43. lcd_off, LCD_HEIGHT - lcd_off,
  44. 0);
  45. }
  46. static void log_dump_to_lcd(void) {
  47. // TODO length is not good as indicator.
  48. // TODO will stop working when log buffer is filled.
  49. size_t len = rb_len(log_get());
  50. if ((!redraw) && (len == prev_len)) {
  51. return;
  52. }
  53. prev_len = len;
  54. redraw = false;
  55. lcd_off = 0;
  56. text_off = 0;
  57. if (len > text_window) {
  58. text_off = len - text_window;
  59. }
  60. rb_dump(log_get(), lcd_write, text_off);
  61. }
  62. static void ota_buttons(enum buttons btn, bool state) {
  63. if (state && (btn == BTN_Y)) {
  64. reset_to_main();
  65. } else if (state && (btn == BTN_LEFT)) {
  66. if (text_window > 10) {
  67. text_window -= 10;
  68. redraw = true;
  69. }
  70. } else if (state && (btn == BTN_RIGHT)) {
  71. if (text_window < 1000) {
  72. text_window += 10;
  73. redraw = true;
  74. }
  75. } else if (state && (btn == BTN_UP)) {
  76. if (text_off > 10) {
  77. text_off -= 10;
  78. redraw = true;
  79. }
  80. } else if (state && (btn == BTN_DOWN)) {
  81. if (text_off < (prev_len - 10)) {
  82. text_off += 10;
  83. redraw = true;
  84. }
  85. }
  86. }
  87. void picowota_printf_init(void) { }
  88. void picowota_printf(const char* format, ...) {
  89. va_list args;
  90. va_start(args, format);
  91. debug_log_va(true, format, args);
  92. va_end(args);
  93. log_dump_to_lcd();
  94. }
  95. void picowota_poll(void) {
  96. buttons_run();
  97. cyw43_arch_poll();
  98. wifi_run();
  99. log_dump_to_lcd();
  100. }
  101. int picowota_init(void) {
  102. usb_descriptor_init_id();
  103. buttons_init();
  104. buttons_callback(ota_buttons);
  105. mem_load();
  106. lcd_init();
  107. lcd_set_backlight(mem_data()->backlight);
  108. log_dump_to_lcd();
  109. debug("cyw43_arch_init");
  110. log_dump_to_lcd();
  111. if (cyw43_arch_init_with_country(COUNTRY_CODE)) {
  112. debug("failed to init cyw43");
  113. log_dump_to_lcd();
  114. return 1;
  115. }
  116. debug("wifi_init");
  117. log_dump_to_lcd();
  118. wifi_init();
  119. const char *prev = NULL;
  120. while (!wifi_ready()) {
  121. const char *state = wifi_state();
  122. if (state != prev) {
  123. prev = state;
  124. debug("new state: %s", state);
  125. }
  126. picowota_poll();
  127. }
  128. debug("wifi ready");
  129. picowota_poll();
  130. return 0;
  131. }
  132. void picowota_deinit(void) {
  133. debug("wifi_deinit");
  134. log_dump_to_lcd();
  135. wifi_deinit();
  136. }