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.

state_wifi.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * state_wifi.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 <stdio.h>
  19. #include <string.h>
  20. #include "config.h"
  21. #include "mem.h"
  22. #include "menu.h"
  23. #include "state.h"
  24. #include "state_wifi.h"
  25. static void enter_cb(int selection) {
  26. if ((selection >= 0) && (selection < mem_data()->net_count)) {
  27. //state_volcano_run_index(selection);
  28. //state_switch(STATE_VOLCANO_RUN);
  29. }
  30. }
  31. static void wifi_move_down(uint16_t index) {
  32. if ((index < 1) || (index >= mem_data()->net_count)) {
  33. return;
  34. }
  35. struct net_credentials tmp = mem_data()->net[index - 1];
  36. mem_data()->net[index - 1] = mem_data()->net[index];
  37. mem_data()->net[index] = tmp;
  38. }
  39. static void wifi_move_up(uint16_t index) {
  40. if (index >= (mem_data()->net_count - 1)) {
  41. return;
  42. }
  43. struct net_credentials tmp = mem_data()->net[index + 1];
  44. mem_data()->net[index + 1] = mem_data()->net[index];
  45. mem_data()->net[index] = tmp;
  46. }
  47. static void lower_cb(int selection) {
  48. if ((selection > 0) && (selection < mem_data()->net_count)) {
  49. wifi_move_down(selection);
  50. selection--;
  51. }
  52. }
  53. static void upper_cb(int selection) {
  54. if ((selection >= 0) && (selection < (mem_data()->net_count - 1))) {
  55. wifi_move_up(selection);
  56. selection++;
  57. }
  58. }
  59. static void exit_cb(void) {
  60. state_switch(STATE_SETTINGS);
  61. }
  62. void state_wifi_enter(void) {
  63. menu_init(enter_cb, lower_cb, upper_cb, exit_cb);
  64. }
  65. void state_wifi_exit(void) {
  66. menu_deinit();
  67. }
  68. static void draw(struct menu_state *menu) {
  69. menu->length = mem_data()->net_count;
  70. int pos = 0;
  71. for (uint16_t i = 0; i < menu->length; i++) {
  72. if ((i < menu->off)
  73. || ((i - menu->off) >= MENU_MAX_LINES)) {
  74. continue;
  75. }
  76. if (i == menu->selection) {
  77. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, "> ");
  78. } else {
  79. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos, " ");
  80. }
  81. pos += snprintf(menu->buff + pos, MENU_MAX_LEN - pos,
  82. "%s\n", mem_data()->net[i].name);
  83. }
  84. if ((menu->selection < 0) && (menu->length > 0)) {
  85. menu->selection = 0;
  86. }
  87. if (menu->length == 0) {
  88. strncpy(menu->buff, "NONE", MENU_MAX_LEN);
  89. }
  90. }
  91. void state_wifi_run(void) {
  92. menu_run(draw, false);
  93. }