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.

volcano.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * volcano.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 "volcano.h"
  22. #define UUID_SRVC_1 0x10
  23. #define UUID_FIRMWARE 0x03
  24. #define UUID_PRJSTAT1 0x0C
  25. #define UUID_PRJSTAT2 0x0D
  26. #define UUID_PRJSTAT3 0x0E
  27. #define UUID_SRVC_2 0x11
  28. #define UUID_WRITE_SRVC 0x00
  29. #define UUID_CURRENT_TEMP 0x01
  30. #define UUID_TARGET_TEMP 0x03
  31. #define UUID_BRIGHTNESS 0x05
  32. #define UUID_SHUTOFF_TIME 0x0D
  33. #define UUID_HEATER_ON 0x0F
  34. #define UUID_HEATER_OFF 0x10
  35. #define UUID_PUMP_ON 0x13
  36. #define UUID_PUMP_OFF 0x14
  37. #define UUID_HEAT_HOURS 0x15
  38. #define UUID_HEAT_MINUTES 0x16
  39. #define MASK_PRJSTAT1_HEIZUNG_ENA 0x0020
  40. #define MASK_PRJSTAT1_AUTOBLESHUTDOWN 0x0200
  41. #define MASK_PRJSTAT1_PUMPE_FET_ENABLE 0x2000
  42. #define MASK_PRJSTAT2_FAHRENHEIT_ENA 0x0200
  43. #define MASK_PRJSTAT2_DISPLAY_ON_COOLING 0x1000
  44. #define MASK_PRJSTAT3_VIBRATION 0x0400
  45. // "10xx00xx-5354-4f52-5a26-4249434b454c"
  46. static uint8_t uuid_base[16] = {
  47. 0x10, 0xFF, 0x00, 0xFF, 0x53, 0x54, 0x4f, 0x52,
  48. 0x5a, 0x26, 0x42, 0x49, 0x43, 0x4b, 0x45, 0x4c,
  49. };
  50. static uint8_t uuid_base2[16] = {
  51. 0x10, 0xFF, 0x00, 0xFF, 0x53, 0x54, 0x4f, 0x52,
  52. 0x5a, 0x26, 0x42, 0x49, 0x43, 0x4b, 0x45, 0x4c,
  53. };
  54. int8_t volcano_discover_characteristics(bool wf, bool conf) {
  55. if (wf) {
  56. uuid_base[1] = UUID_SRVC_2;
  57. uuid_base2[1] = UUID_SRVC_2;
  58. uuid_base[3] = UUID_WRITE_SRVC;
  59. int8_t r;
  60. uuid_base2[3] = UUID_TARGET_TEMP;
  61. r = ble_discover(uuid_base, uuid_base2);
  62. if (r < 0) {
  63. return r;
  64. }
  65. uuid_base2[3] = UUID_HEATER_ON;
  66. r = ble_discover(uuid_base, uuid_base2);
  67. if (r < 0) {
  68. return r;
  69. }
  70. uuid_base2[3] = UUID_HEATER_OFF;
  71. r = ble_discover(uuid_base, uuid_base2);
  72. if (r < 0) {
  73. return r;
  74. }
  75. uuid_base2[3] = UUID_PUMP_ON;
  76. r = ble_discover(uuid_base, uuid_base2);
  77. if (r < 0) {
  78. return r;
  79. }
  80. uuid_base2[3] = UUID_PUMP_OFF;
  81. r = ble_discover(uuid_base, uuid_base2);
  82. if (r < 0) {
  83. return r;
  84. }
  85. }
  86. if (conf) {
  87. uuid_base[1] = UUID_SRVC_1;
  88. uuid_base2[1] = UUID_SRVC_1;
  89. uuid_base[3] = UUID_WRITE_SRVC;
  90. int8_t r;
  91. uuid_base2[3] = UUID_PRJSTAT1;
  92. r = ble_discover(uuid_base, uuid_base2);
  93. if (r < 0) {
  94. return r;
  95. }
  96. uuid_base2[3] = UUID_PRJSTAT2;
  97. r = ble_discover(uuid_base, uuid_base2);
  98. if (r < 0) {
  99. return r;
  100. }
  101. uuid_base2[3] = UUID_PRJSTAT3;
  102. r = ble_discover(uuid_base, uuid_base2);
  103. if (r < 0) {
  104. return r;
  105. }
  106. }
  107. return 0;
  108. }
  109. int16_t volcano_get_current_temp(void) {
  110. uuid_base[1] = UUID_SRVC_2;
  111. uuid_base[3] = UUID_CURRENT_TEMP;
  112. uint8_t buff[4];
  113. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  114. if (r != sizeof(buff)) {
  115. debug("ble_read unexpected value %ld", r);
  116. return -1;
  117. }
  118. uint32_t *v = (uint32_t *)buff;
  119. return *v;
  120. }
  121. int16_t volcano_get_target_temp(void) {
  122. uuid_base[3] = UUID_TARGET_TEMP;
  123. uint8_t buff[4];
  124. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  125. if (r != sizeof(buff)) {
  126. debug("ble_read unexpected value %ld", r);
  127. return -1;
  128. }
  129. uint32_t *v = (uint32_t *)buff;
  130. return *v;
  131. }
  132. int8_t volcano_set_target_temp(uint16_t value) {
  133. uuid_base[1] = UUID_SRVC_2;
  134. uuid_base2[1] = UUID_SRVC_2;
  135. uuid_base[3] = UUID_WRITE_SRVC;
  136. uuid_base2[3] = UUID_TARGET_TEMP;
  137. uint8_t buff[4];
  138. uint32_t *v = (uint32_t *)buff;
  139. *v = value;
  140. int8_t r = ble_write(uuid_base, uuid_base2, buff, sizeof(buff));
  141. if (r != 0) {
  142. debug("ble_write unexpected value %d", r);
  143. }
  144. return r;
  145. }
  146. int8_t volcano_set_heater_state(bool value) {
  147. uuid_base[1] = UUID_SRVC_2;
  148. uuid_base2[1] = UUID_SRVC_2;
  149. uuid_base[3] = UUID_WRITE_SRVC;
  150. if (value) {
  151. uuid_base2[3] = UUID_HEATER_ON;
  152. } else {
  153. uuid_base2[3] = UUID_HEATER_OFF;
  154. }
  155. uint8_t d = 0;
  156. int8_t r = ble_write(uuid_base, uuid_base2, &d, sizeof(d));
  157. if (r != 0) {
  158. debug("ble_write unexpected value %d", r);
  159. }
  160. return r;
  161. }
  162. int8_t volcano_set_pump_state(bool value) {
  163. uuid_base[1] = UUID_SRVC_2;
  164. uuid_base2[1] = UUID_SRVC_2;
  165. uuid_base[3] = UUID_WRITE_SRVC;
  166. if (value) {
  167. uuid_base2[3] = UUID_PUMP_ON;
  168. } else {
  169. uuid_base2[3] = UUID_PUMP_OFF;
  170. }
  171. uint8_t d = 0;
  172. int8_t r = ble_write(uuid_base, uuid_base2, &d, sizeof(d));
  173. if (r != 0) {
  174. debug("ble_write unexpected value %d", r);
  175. }
  176. return r;
  177. }
  178. enum unit volcano_get_unit(void) {
  179. uuid_base[1] = UUID_SRVC_1;
  180. uuid_base[3] = UUID_PRJSTAT2;
  181. uint8_t buff[4];
  182. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  183. if (r != sizeof(buff)) {
  184. debug("ble_read unexpected value %ld", r);
  185. return UNIT_INVALID;
  186. }
  187. uint32_t *v = (uint32_t *)buff;
  188. return (*v & MASK_PRJSTAT2_FAHRENHEIT_ENA) ? UNIT_F : UNIT_C;
  189. }
  190. enum volcano_state volcano_get_state(void) {
  191. uuid_base[1] = UUID_SRVC_1;
  192. uuid_base[3] = UUID_PRJSTAT1;
  193. uint8_t buff[4];
  194. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  195. if (r != sizeof(buff)) {
  196. debug("ble_read unexpected value %ld", r);
  197. return VOLCANO_STATE_INVALID;
  198. }
  199. uint32_t *v = (uint32_t *)buff;
  200. uint32_t heater = (*v & MASK_PRJSTAT1_HEIZUNG_ENA);
  201. uint32_t pump = (*v & MASK_PRJSTAT1_PUMPE_FET_ENABLE);
  202. return (heater ? VOLCANO_STATE_HEATER : 0) | (pump ? VOLCANO_STATE_PUMP : 0);
  203. }
  204. int8_t volcano_set_unit(enum unit unit) {
  205. uuid_base[1] = UUID_SRVC_1;
  206. uuid_base2[1] = UUID_SRVC_1;
  207. uuid_base[3] = UUID_WRITE_SRVC;
  208. uuid_base2[3] = UUID_PRJSTAT2;
  209. uint32_t v = MASK_PRJSTAT2_FAHRENHEIT_ENA;
  210. if (unit == UNIT_F) {
  211. v |= 0x10000;
  212. }
  213. int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
  214. if (r != 0) {
  215. debug("ble_write unexpected value %d", r);
  216. }
  217. return r;
  218. }
  219. int8_t volcano_set_vibration(bool value) {
  220. uuid_base[1] = UUID_SRVC_1;
  221. uuid_base2[1] = UUID_SRVC_1;
  222. uuid_base[3] = UUID_WRITE_SRVC;
  223. uuid_base2[3] = UUID_PRJSTAT3;
  224. uint32_t v = MASK_PRJSTAT3_VIBRATION;
  225. if (!value) {
  226. v |= 0x10000;
  227. }
  228. int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
  229. if (r != 0) {
  230. debug("ble_write unexpected value %d", r);
  231. }
  232. return r;
  233. }
  234. int8_t volcano_get_vibration(void) {
  235. uuid_base[1] = UUID_SRVC_1;
  236. uuid_base[3] = UUID_PRJSTAT3;
  237. uint8_t buff[4];
  238. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  239. if (r != sizeof(buff)) {
  240. debug("ble_read unexpected value %ld", r);
  241. return -1;
  242. }
  243. uint32_t *v = (uint32_t *)buff;
  244. return (*v & MASK_PRJSTAT3_VIBRATION) ? 0 : 1;
  245. }
  246. int8_t volcano_set_display_cooling(bool value) {
  247. uuid_base[1] = UUID_SRVC_1;
  248. uuid_base2[1] = UUID_SRVC_1;
  249. uuid_base[3] = UUID_WRITE_SRVC;
  250. uuid_base2[3] = UUID_PRJSTAT2;
  251. uint32_t v = MASK_PRJSTAT2_DISPLAY_ON_COOLING;
  252. if (!value) {
  253. v |= 0x10000;
  254. }
  255. int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
  256. if (r != 0) {
  257. debug("ble_write unexpected value %d", r);
  258. }
  259. return r;
  260. }
  261. int8_t volcano_get_display_cooling(void) {
  262. uuid_base[1] = UUID_SRVC_1;
  263. uuid_base[3] = UUID_PRJSTAT2;
  264. uint8_t buff[4];
  265. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  266. if (r != sizeof(buff)) {
  267. debug("ble_read unexpected value %ld", r);
  268. return -1;
  269. }
  270. uint32_t *v = (uint32_t *)buff;
  271. return (*v & MASK_PRJSTAT2_DISPLAY_ON_COOLING) ? 0 : 1;
  272. }
  273. int16_t volcano_get_auto_shutoff(void) {
  274. uuid_base[1] = UUID_SRVC_2;
  275. uuid_base[3] = UUID_SHUTOFF_TIME;
  276. uint8_t buff[2];
  277. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  278. if (r != sizeof(buff)) {
  279. debug("ble_read unexpected value %ld", r);
  280. return -1;
  281. }
  282. uint16_t *v = (uint16_t *)buff;
  283. return *v;
  284. }
  285. int8_t volcano_set_auto_shutoff(uint16_t v) {
  286. uuid_base[1] = UUID_SRVC_2;
  287. uuid_base2[1] = UUID_SRVC_2;
  288. uuid_base[3] = UUID_WRITE_SRVC;
  289. uuid_base2[3] = UUID_SHUTOFF_TIME;
  290. int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
  291. if (r != 0) {
  292. debug("ble_write unexpected value %d", r);
  293. }
  294. return r;
  295. }
  296. int8_t volcano_get_brightness(void) {
  297. uuid_base[1] = UUID_SRVC_2;
  298. uuid_base[3] = UUID_BRIGHTNESS;
  299. uint8_t buff[2];
  300. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  301. if (r != sizeof(buff)) {
  302. debug("ble_read unexpected value %ld", r);
  303. return -1;
  304. }
  305. uint16_t *v = (uint16_t *)buff;
  306. return *v;
  307. }
  308. int8_t volcano_set_brightness(uint8_t val) {
  309. uuid_base[1] = UUID_SRVC_2;
  310. uuid_base2[1] = UUID_SRVC_2;
  311. uuid_base[3] = UUID_WRITE_SRVC;
  312. uuid_base2[3] = UUID_BRIGHTNESS;
  313. uint16_t v = val;
  314. int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&v, sizeof(v));
  315. if (r != 0) {
  316. debug("ble_write unexpected value %d", r);
  317. }
  318. return r;
  319. }
  320. int8_t volcano_get_firmware(char *val) {
  321. if (val == NULL) {
  322. return -1;
  323. }
  324. uuid_base[1] = UUID_SRVC_1;
  325. uuid_base[3] = UUID_FIRMWARE;
  326. uint8_t buff[VOLCANO_FW_LEN];
  327. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  328. if (r != sizeof(buff)) {
  329. debug("ble_read unexpected value %ld", r);
  330. return -1;
  331. }
  332. memcpy(val, buff, sizeof(buff));
  333. return 0;
  334. }
  335. int32_t volcano_get_runtime(void) {
  336. int32_t val = 0;
  337. uuid_base[1] = UUID_SRVC_2;
  338. uuid_base[3] = UUID_HEAT_HOURS;
  339. uint32_t buff;
  340. int32_t r = ble_read(uuid_base, (uint8_t *)&buff, sizeof(buff));
  341. if (r != sizeof(buff)) {
  342. debug("ble_read 1 unexpected value %ld", r);
  343. return -1;
  344. }
  345. val = buff * 60;
  346. uuid_base[3] = UUID_HEAT_MINUTES;
  347. uint16_t buff2;
  348. r = ble_read(uuid_base, (uint8_t *)&buff2, sizeof(buff2));
  349. if (r != sizeof(buff2)) {
  350. debug("ble_read 2 unexpected value %ld", r);
  351. return -1;
  352. }
  353. val += buff2;
  354. return val;
  355. }