S&B Volcano vaporizer remote control with Pi Pico W
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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