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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // Volcano UUIDs are always the same, except for the 4th byte
  23. #define UUID_WRITE_SRVC 0x00
  24. #define UUID_CURRENT_TEMP 0x01
  25. #define UUID_TARGET_TEMP 0x03
  26. #define UUID_HEATER_ON 0x0F
  27. #define UUID_HEATER_OFF 0x10
  28. #define UUID_PUMP_ON 0x13
  29. #define UUID_PUMP_OFF 0x14
  30. // "101100xx-5354-4f52-5a26-4249434b454c"
  31. static uint8_t uuid_base[16] = {
  32. 0x10, 0x11, 0x00, 0xFF, 0x53, 0x54, 0x4f, 0x52,
  33. 0x5a, 0x26, 0x42, 0x49, 0x43, 0x4b, 0x45, 0x4c,
  34. };
  35. static uint8_t uuid_base2[16] = {
  36. 0x10, 0x11, 0x00, 0xFF, 0x53, 0x54, 0x4f, 0x52,
  37. 0x5a, 0x26, 0x42, 0x49, 0x43, 0x4b, 0x45, 0x4c,
  38. };
  39. int8_t volcano_discover_characteristics(void) {
  40. uuid_base[3] = UUID_WRITE_SRVC;
  41. int8_t r;
  42. uuid_base2[3] = UUID_TARGET_TEMP;
  43. r = ble_discover(uuid_base, uuid_base2);
  44. if (r < 0) {
  45. return r;
  46. }
  47. uuid_base2[3] = UUID_HEATER_ON;
  48. r = ble_discover(uuid_base, uuid_base2);
  49. if (r < 0) {
  50. return r;
  51. }
  52. uuid_base2[3] = UUID_HEATER_OFF;
  53. r = ble_discover(uuid_base, uuid_base2);
  54. if (r < 0) {
  55. return r;
  56. }
  57. uuid_base2[3] = UUID_PUMP_ON;
  58. r = ble_discover(uuid_base, uuid_base2);
  59. if (r < 0) {
  60. return r;
  61. }
  62. uuid_base2[3] = UUID_PUMP_OFF;
  63. r = ble_discover(uuid_base, uuid_base2);
  64. if (r < 0) {
  65. return r;
  66. }
  67. return 0;
  68. }
  69. int16_t volcano_get_current_temp(void) {
  70. uuid_base[3] = UUID_CURRENT_TEMP;
  71. uint8_t buff[4];
  72. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  73. if (r != sizeof(buff)) {
  74. debug("ble_read unexpected value %ld", r);
  75. return -1;
  76. }
  77. uint32_t *v = (uint32_t *)buff;
  78. return *v;
  79. }
  80. int16_t volcano_get_target_temp(void) {
  81. uuid_base[3] = UUID_TARGET_TEMP;
  82. uint8_t buff[4];
  83. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  84. if (r != sizeof(buff)) {
  85. debug("ble_read unexpected value %ld", r);
  86. return -1;
  87. }
  88. uint32_t *v = (uint32_t *)buff;
  89. return *v;
  90. }
  91. int8_t volcano_set_target_temp(uint16_t value) {
  92. uuid_base[3] = UUID_WRITE_SRVC;
  93. uuid_base2[3] = UUID_TARGET_TEMP;
  94. uint8_t buff[4];
  95. uint32_t *v = (uint32_t *)buff;
  96. *v = value;
  97. int8_t r = ble_write(uuid_base, uuid_base2, buff, sizeof(buff));
  98. if (r != 0) {
  99. debug("ble_write unexpected value %d", r);
  100. }
  101. return r;
  102. }
  103. int8_t volcano_set_heater_state(bool value) {
  104. uuid_base[3] = UUID_WRITE_SRVC;
  105. if (value) {
  106. uuid_base2[3] = UUID_HEATER_ON;
  107. } else {
  108. uuid_base2[3] = UUID_HEATER_OFF;
  109. }
  110. uint8_t d = 0;
  111. int8_t r = ble_write(uuid_base, uuid_base2, &d, sizeof(d));
  112. if (r != 0) {
  113. debug("ble_write unexpected value %d", r);
  114. }
  115. return r;
  116. }
  117. int8_t volcano_set_pump_state(bool value) {
  118. uuid_base[3] = UUID_WRITE_SRVC;
  119. if (value) {
  120. uuid_base2[3] = UUID_PUMP_ON;
  121. } else {
  122. uuid_base2[3] = UUID_PUMP_OFF;
  123. }
  124. uint8_t d = 0;
  125. int8_t r = ble_write(uuid_base, uuid_base2, &d, sizeof(d));
  126. if (r != 0) {
  127. debug("ble_write unexpected value %d", r);
  128. }
  129. return r;
  130. }