S&B Volcano vaporizer remote control with Pi Pico W
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ota_shim.c 3.6KB

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