S&B Volcano vaporizer remote control with Pi Pico W
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

state_wifi.c 2.9KB

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