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.

ota_shim.c 3.4KB

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