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_settings.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * state_settings.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 "picowota/reboot.h"
  21. #include "config.h"
  22. #include "log.h"
  23. #include "menu.h"
  24. #include "mem.h"
  25. #include "state.h"
  26. #include "state_value.h"
  27. #include "state_workflow.h"
  28. #include "state_settings.h"
  29. static void enter_cb(int selection) {
  30. switch (selection) {
  31. case 0:
  32. // Auto Connect
  33. state_value_set(&mem_data()->wf_auto_connect,
  34. sizeof(mem_data()->wf_auto_connect),
  35. 0, 1, VAL_STEP_INCREMENT, 1,
  36. "Auto Connect");
  37. state_value_return(STATE_SETTINGS);
  38. state_switch(STATE_VALUE);
  39. break;
  40. case 1:
  41. // Brightness
  42. state_value_set(&mem_data()->backlight,
  43. sizeof(mem_data()->backlight),
  44. 0x00FF, 0xFF00, VAL_STEP_SHIFT, 1,
  45. "Brightness");
  46. state_value_return(STATE_SETTINGS);
  47. state_switch(STATE_VALUE);
  48. break;
  49. case 2:
  50. // Edit Workflows
  51. state_wf_edit(true);
  52. state_switch(STATE_WORKFLOW);
  53. break;
  54. case 3:
  55. // Enable WiFi
  56. state_value_set(&mem_data()->enable_wifi,
  57. sizeof(mem_data()->enable_wifi),
  58. 0, 1, VAL_STEP_INCREMENT, 1,
  59. "Enable WiFi");
  60. state_value_return(STATE_SETTINGS);
  61. state_switch(STATE_VALUE);
  62. break;
  63. case 4:
  64. // Factory Reset
  65. mem_load_defaults();
  66. break;
  67. case 5:
  68. // OTA Update
  69. picowota_reboot(true);
  70. break;
  71. }
  72. }
  73. static void exit_cb(void) {
  74. state_switch(STATE_SCAN);
  75. }
  76. void state_settings_enter(void) {
  77. menu_init(enter_cb, NULL, NULL, exit_cb);
  78. }
  79. void state_settings_exit(void) {
  80. menu_deinit();
  81. mem_write();
  82. // apply changed wifi state
  83. if (mem_data()->enable_wifi) {
  84. if (!wifi_initialized()) {
  85. wifi_init();
  86. }
  87. } else {
  88. if (wifi_initialized()) {
  89. wifi_deinit();
  90. }
  91. }
  92. }
  93. static void draw(struct menu_state *menu) {
  94. int pos = 0;
  95. menu->length = 0;
  96. ADD_STATIC_ELEMENT("Auto Connect (%d)", mem_data()->wf_auto_connect);
  97. ADD_STATIC_ELEMENT("Brightness (%d)", __builtin_ffs(mem_data()->backlight));
  98. ADD_STATIC_ELEMENT("Edit Workflows");
  99. ADD_STATIC_ELEMENT("Enable WiFi (%d)", mem_data()->enable_wifi);
  100. ADD_STATIC_ELEMENT("Factory Reset");
  101. ADD_STATIC_ELEMENT("OTA Update");
  102. if (menu->selection < 0) {
  103. menu->selection = 0;
  104. }
  105. }
  106. void state_settings_run(void) {
  107. menu_run(draw, false);
  108. }