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.

venty.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * venty.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 "config.h"
  19. #include "log.h"
  20. #include "ble.h"
  21. #include "main.h"
  22. #include "venty.h"
  23. #define VENTY_READ_TIMEOUT_MS 500
  24. enum venty_values {
  25. MASK_SET_TEMPERATURE = 1 << 1,
  26. MASK_SET_BOOST = 1 << 2,
  27. MASK_SET_SUPERBOOST = 1 << 3,
  28. MASK_HEATER = 1 << 5,
  29. MASK_SETTINGS = 1 << 7,
  30. };
  31. enum venty_settings {
  32. SETTING_UNIT = 1 << 0,
  33. SETTING_SETPOINT_REACHED = 1 << 1,
  34. SETTING_FACTORY_RESET = 1 << 2,
  35. SETTING_ECOMODE_CHARGE = 1 << 3,
  36. SETTING_BUTTON_CHANGED = 1 << 4,
  37. SETTING_ECOMODE_VOLTAGE = 1 << 5,
  38. SETTING_BOOST_VISUALIZATION = 1 << 6,
  39. };
  40. // "00000000-5354-4f52-5a26-4249434b454c"
  41. static uint8_t uuid_service[16] = {
  42. 0x00, 0x00, 0x00, 0x00, 0x53, 0x54, 0x4f, 0x52,
  43. 0x5a, 0x26, 0x42, 0x49, 0x43, 0x4b, 0x45, 0x4c,
  44. };
  45. // "00000001-5354-4f52-5a26-4249434b454c"
  46. static uint8_t uuid_characteristic[16] = {
  47. 0x00, 0x00, 0x00, 0x01, 0x53, 0x54, 0x4f, 0x52,
  48. 0x5a, 0x26, 0x42, 0x49, 0x43, 0x4b, 0x45, 0x4c,
  49. };
  50. static int8_t venty_cmd(uint8_t *rx, size_t rx_len,
  51. const uint8_t *tx, size_t tx_len) {
  52. int8_t r = ble_notification_enable(uuid_service, uuid_characteristic);
  53. if (r < 0) {
  54. debug("ble_notification_enable failed: %d", r);
  55. ble_notification_disable(uuid_service, uuid_characteristic);
  56. return r;
  57. }
  58. r = ble_write(uuid_service, uuid_characteristic,
  59. tx, tx_len);
  60. if (r < 0) {
  61. debug("ble_write failed: %d", r);
  62. ble_notification_disable(uuid_service, uuid_characteristic);
  63. return r;
  64. }
  65. uint32_t start_time = to_ms_since_boot(get_absolute_time());
  66. while (!ble_notification_ready()) {
  67. sleep_ms(1);
  68. main_loop_hw();
  69. uint32_t now = to_ms_since_boot(get_absolute_time());
  70. if ((now - start_time) >= VENTY_READ_TIMEOUT_MS) {
  71. debug("timeout waiting for notification");
  72. ble_notification_disable(uuid_service, uuid_characteristic);
  73. return -1;
  74. }
  75. }
  76. uint8_t uuid[16] = {0};
  77. uint16_t len = ble_notification_get(rx, rx_len, uuid);
  78. if (memcmp(uuid, uuid_characteristic, 16) != 0) {
  79. debug("got notification for unexpected uuid");
  80. len = 0;
  81. }
  82. ble_notification_disable(uuid_service, uuid_characteristic);
  83. return (len <= 0) ? -1 : 0;
  84. }
  85. int16_t venty_get_target_temp(void) {
  86. uint8_t cmd[20] = {0};
  87. uint8_t buff[20] = {0};
  88. cmd[0] = 1;
  89. int8_t r = venty_cmd(buff, sizeof(buff), cmd, sizeof(cmd));
  90. if (r < 0) {
  91. return r;
  92. }
  93. int16_t *val = (int16_t *)(buff + 4);
  94. return *val;
  95. }
  96. int8_t venty_set_target_temp(uint16_t value) {
  97. uint8_t cmd[20] = {0};
  98. uint8_t buff[20] = {0};
  99. cmd[0] = 1;
  100. cmd[1] = MASK_SET_TEMPERATURE;
  101. uint16_t *val = (uint16_t *)(cmd + 4);
  102. *val = value;
  103. int8_t r = venty_cmd(buff, sizeof(buff), cmd, sizeof(cmd));
  104. return r;
  105. }
  106. int8_t venty_set_heater_state(bool value) {
  107. uint8_t cmd[20] = {0};
  108. uint8_t buff[20] = {0};
  109. cmd[0] = 1;
  110. cmd[1] = MASK_HEATER;
  111. cmd[11] = value ? 1 : 0;
  112. int8_t r = venty_cmd(buff, sizeof(buff), cmd, sizeof(cmd));
  113. return r;
  114. }
  115. int8_t venty_get_battery_state(void) {
  116. uint8_t cmd[20] = {0};
  117. uint8_t buff[20] = {0};
  118. cmd[0] = 1;
  119. int8_t r = venty_cmd(buff, sizeof(buff), cmd, sizeof(cmd));
  120. if (r < 0) {
  121. return r;
  122. }
  123. int8_t *val = (int8_t *)(buff + 8);
  124. return *val;
  125. }
  126. int8_t venty_get_eco_current(void) {
  127. uint8_t cmd[20] = {0};
  128. uint8_t buff[20] = {0};
  129. cmd[0] = 1;
  130. int8_t r = venty_cmd(buff, sizeof(buff), cmd, sizeof(cmd));
  131. if (r < 0) {
  132. return r;
  133. }
  134. int8_t *val = (int8_t *)(buff + 14);
  135. return (*val & SETTING_ECOMODE_CHARGE) ? 1 : 0;
  136. }
  137. int8_t venty_get_eco_voltage(void) {
  138. uint8_t cmd[20] = {0};
  139. uint8_t buff[20] = {0};
  140. cmd[0] = 1;
  141. int8_t r = venty_cmd(buff, sizeof(buff), cmd, sizeof(cmd));
  142. if (r < 0) {
  143. return r;
  144. }
  145. int8_t *val = (int8_t *)(buff + 14);
  146. return (*val & SETTING_ECOMODE_VOLTAGE) ? 1 : 0;
  147. }
  148. int8_t venty_set_eco_current(bool value) {
  149. uint8_t cmd[20] = {0};
  150. uint8_t buff[20] = {0};
  151. cmd[0] = 1;
  152. cmd[1] = MASK_SETTINGS;
  153. cmd[14] = value ? SETTING_ECOMODE_CHARGE : 0;
  154. cmd[15] = SETTING_ECOMODE_CHARGE;
  155. int8_t r = venty_cmd(buff, sizeof(buff), cmd, sizeof(cmd));
  156. return r;
  157. }
  158. int8_t venty_set_eco_voltage(bool value) {
  159. uint8_t cmd[20] = {0};
  160. uint8_t buff[20] = {0};
  161. cmd[0] = 1;
  162. cmd[1] = MASK_SETTINGS;
  163. cmd[14] = value ? SETTING_ECOMODE_VOLTAGE : 0;
  164. cmd[15] = SETTING_ECOMODE_VOLTAGE;
  165. int8_t r = venty_cmd(buff, sizeof(buff), cmd, sizeof(cmd));
  166. return r;
  167. }