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_crafty.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * state_crafty.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 "buttons.h"
  22. #include "crafty.h"
  23. #include "log.h"
  24. #include "state.h"
  25. #include "state_crafty.h"
  26. #include "menu.h"
  27. #define CRAFTY_UPDATE_TIME_MS 3000
  28. static bd_addr_t ble_addr = {0};
  29. static bd_addr_type_t ble_type = 0;
  30. static bool wait_for_connect = false;
  31. static bool wait_for_disconnect = false;
  32. static bool heater_state = false;
  33. static int16_t target_temp = 0;
  34. static void crafty_buttons(enum buttons btn, bool state) {
  35. if (state && (btn == BTN_Y)) {
  36. if ((!wait_for_connect) && (!wait_for_disconnect)) {
  37. debug("crafty disconnect");
  38. ble_disconnect();
  39. wait_for_disconnect = true;
  40. } else {
  41. debug("invalid state for disconnect");
  42. }
  43. } else if (state && (btn == BTN_A)) {
  44. heater_state = !heater_state;
  45. debug("crafty heater %s", heater_state ? "on" : "off");
  46. crafty_set_heater_state(heater_state);
  47. } else if (state && (btn == BTN_LEFT)) {
  48. target_temp -= 10;
  49. if (target_temp < 400) {
  50. target_temp = 400;
  51. } else if (target_temp > 2100) {
  52. target_temp = 2100;
  53. }
  54. debug("crafty temp to %.1f", target_temp / 10.0f);
  55. crafty_set_target_temp(target_temp);
  56. } else if (state && (btn == BTN_RIGHT)) {
  57. target_temp += 10;
  58. if (target_temp < 400) {
  59. target_temp = 400;
  60. } else if (target_temp > 2100) {
  61. target_temp = 2100;
  62. }
  63. debug("crafty temp to %.1f", target_temp / 10.0f);
  64. crafty_set_target_temp(target_temp);
  65. }
  66. }
  67. void state_crafty_target(bd_addr_t addr, bd_addr_type_t type) {
  68. memcpy(ble_addr, addr, sizeof(bd_addr_t));
  69. ble_type = type;
  70. }
  71. void state_crafty_enter(void) {
  72. debug("crafty connect");
  73. ble_connect(ble_addr, ble_type);
  74. wait_for_connect = true;
  75. buttons_callback(crafty_buttons);
  76. }
  77. void state_crafty_exit(void) {
  78. buttons_callback(NULL);
  79. }
  80. static void draw(struct menu_state *menu) {
  81. if (wait_for_connect) {
  82. snprintf(menu->buff, MENU_MAX_LEN, "Connecting...");
  83. } else if (wait_for_disconnect) {
  84. snprintf(menu->buff, MENU_MAX_LEN, "Disconnecting...");
  85. } else {
  86. target_temp = crafty_get_target_temp();
  87. int16_t ct = crafty_get_current_temp();
  88. int8_t bs = crafty_get_battery_state();
  89. snprintf(menu->buff, MENU_MAX_LEN,
  90. "Target: %.1f C\n"
  91. "Current: %.1f C\n"
  92. "Battery: %d %%",
  93. target_temp / 10.0f, ct / 10.0f, bs);
  94. }
  95. }
  96. void state_crafty_run(void) {
  97. if (wait_for_connect && ble_is_connected()) {
  98. wait_for_connect = false;
  99. debug("crafty start");
  100. }
  101. static uint32_t last = 0;
  102. uint32_t now = to_ms_since_boot(get_absolute_time());
  103. if (((now - last) >= CRAFTY_UPDATE_TIME_MS) || (last == 0)) {
  104. menu_run(draw, true);
  105. last = to_ms_since_boot(get_absolute_time());
  106. }
  107. if (wait_for_disconnect && !ble_is_connected()) {
  108. wait_for_disconnect = false;
  109. debug("crafty done");
  110. state_switch(STATE_SCAN);
  111. }
  112. }