S&B Volcano vaporizer remote control with Pi Pico W
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * 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 "config.h"
  19. #include "log.h"
  20. #include "ble.h"
  21. #include "crafty.h"
  22. // serviceUuidCrafty1 "00000001-4c45-4b43-4942-265a524f5453"
  23. // characteristicWriteTemp "00000021-4c45-4b43-4942-265a524f5453"
  24. // charactersiticCurrTemp "00000011-4c45-4b43-4942-265a524f5453"
  25. // characteristicPower "00000041-4c45-4b43-4942-265a524f5453"
  26. // characteristicHeaterOn "00000081-4c45-4b43-4942-265a524f5453"
  27. // characteristicHeaterOff "00000091-4c45-4b43-4942-265a524f5453"
  28. // "000000xx-4c45-4b43-4942-265a524f5453"
  29. static uint8_t uuid_base[16] = {
  30. 0x00, 0x00, 0x00, 0xFF, 0x4c, 0x45, 0x4b, 0x43,
  31. 0x49, 0x42, 0x26, 0x5a, 0x52, 0x4f, 0x54, 0x53,
  32. };
  33. static uint8_t uuid_base2[16] = {
  34. 0x00, 0x00, 0x00, 0xFF, 0x4c, 0x45, 0x4b, 0x43,
  35. 0x49, 0x42, 0x26, 0x5a, 0x52, 0x4f, 0x54, 0x53,
  36. };
  37. // Crafty UUIDs are always the same, except for the 4th byte
  38. #define UUID_WRITE_SRVC 0x01
  39. #define UUID_CURRENT_TEMP 0x11
  40. #define UUID_TARGET_TEMP 0x21
  41. #define UUID_BATTERY 0x41
  42. #define UUID_HEATER_ON 0x81
  43. #define UUID_HEATER_OFF 0x91
  44. int16_t crafty_get_current_temp(void) {
  45. uuid_base[3] = UUID_CURRENT_TEMP;
  46. uint8_t buff[2];
  47. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  48. if (r != sizeof(buff)) {
  49. debug("ble_read unexpected value %ld", r);
  50. return -1;
  51. }
  52. uint16_t *v = (uint16_t *)buff;
  53. return *v;
  54. }
  55. int16_t crafty_get_target_temp(void) {
  56. uuid_base[3] = UUID_TARGET_TEMP;
  57. uint8_t buff[2];
  58. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  59. if (r != sizeof(buff)) {
  60. debug("ble_read unexpected value %ld", r);
  61. return -1;
  62. }
  63. uint16_t *v = (uint16_t *)buff;
  64. return *v;
  65. }
  66. int8_t crafty_set_target_temp(uint16_t value) {
  67. uuid_base[3] = UUID_WRITE_SRVC;
  68. uuid_base2[3] = UUID_TARGET_TEMP;
  69. uint8_t buff[2];
  70. uint16_t *v = (uint16_t *)buff;
  71. *v = value;
  72. int8_t r = ble_write(uuid_base, uuid_base2, buff, sizeof(buff));
  73. if (r != 0) {
  74. debug("ble_write unexpected value %d", r);
  75. }
  76. return r;
  77. }
  78. int8_t crafty_set_heater_state(bool value) {
  79. uuid_base[3] = UUID_WRITE_SRVC;
  80. if (value) {
  81. uuid_base2[3] = UUID_HEATER_ON;
  82. } else {
  83. uuid_base2[3] = UUID_HEATER_OFF;
  84. }
  85. uint16_t d = 0;
  86. int8_t r = ble_write(uuid_base, uuid_base2, (uint8_t *)&d, sizeof(d));
  87. if (r != 0) {
  88. debug("ble_write unexpected value %d", r);
  89. }
  90. return r;
  91. }
  92. int8_t crafty_get_battery_state(void) {
  93. uuid_base[3] = UUID_BATTERY;
  94. uint8_t buff[2];
  95. int32_t r = ble_read(uuid_base, buff, sizeof(buff));
  96. if (r != sizeof(buff)) {
  97. debug("ble_read unexpected value %ld", r);
  98. return -1;
  99. }
  100. uint16_t *v = (uint16_t *)buff;
  101. return *v;
  102. }