Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

allow support of ESP32 esp-idf framework by BThome #20

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
8 changes: 7 additions & 1 deletion components/bthome_base/bthome_base_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/

#include <string>

#ifndef USE_ESP32
#include <pgmspace.h>
#endif

#include "bthome_base_common.h"
#include "bthome_common_generated.h"
Expand Down Expand Up @@ -50,8 +53,11 @@ namespace bthome_base
// check range
if (obj_meas_type >= sizeof(MEAS_TYPES_FLAGS) / sizeof(uint8_t))
return {};

#ifndef USE_ESP32
const uint8_t meas_type_flags = pgm_read_byte_near(MEAS_TYPES_FLAGS + obj_meas_type);
#else
const uint8_t meas_type_flags = MEAS_TYPES_FLAGS[obj_meas_type];
#endif
const uint8_t factor_raw = (meas_type_flags & 0b01100000) >> 5;
BTHomeDataFormat retval = {
.factor_raw = factor_raw,
Expand Down
27 changes: 24 additions & 3 deletions components/bthome_base/bthome_common_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
/* auto generated, do not edit */

#pragma once

#ifndef USE_ESP32
#include <pgmspace.h>
#endif


namespace bthome_base
{
Expand Down Expand Up @@ -88,7 +92,8 @@ typedef enum {
BTHOME_BUTTON_TRIPLE_CLICK = 0x03,
BTHOME_BUTTON_LONG_CLICK = 0x04,
BTHOME_BUTTON_LONG_DOUBLE_CLICK = 0x05,
BTHOME_BUTTON_LONG_TRIPLE_CLICK = 0x06
BTHOME_BUTTON_LONG_TRIPLE_CLICK = 0x06,
BTHOME_BUTTON_HOLD_CLICK = 0x80
} BTHome_Button_e;

typedef enum {
Expand All @@ -97,10 +102,14 @@ typedef enum {
BTHOME_DIMMER_ROTATE_RIGHT = 0x02
} BTHome_Dimmer_e;

static const uint8_t PROGMEM MEAS_TYPES_FLAGS[] = { /* 8th bit Unused | 6-7th bits Factor | 4-5th bits DataType | 1-2-3rd bits DataLen */
#ifndef USE_ESP32
static const uint8_t PROGMEM MEAS_TYPES_FLAGS[] = { /* 8th bit Unused | 6-7th bits Factor | 4-5th bits DataType | 1-2-3rd bits DataLen */
#else
static const uint8_t MEAS_TYPES_FLAGS[] = { /* 8th bit Unused | 6-7th bits Factor | 4-5th bits DataType | 1-2-3rd bits DataLen */
#endif
/* 0x00 */ 0b00000001, /* packet_id | uint8 (1 byte) | numeric * 1.0 */
/* 0x01 */ 0b00000001, /* battery | uint8 (1 byte) | numeric * 1.0 */
/* 0x02 */ 0b01001010, /* temperature | sint16 (2 bytes) | numeric * 0.01 */
/* 0x02 */ 0b01001010, /* temperature | sint16 (2 bytes) | numeric * 0.01 */
/* 0x03 */ 0b01000010, /* humidity | uint16 (2 bytes) | numeric * 0.01 */
/* 0x04 */ 0b01000011, /* pressure | uint24 (3 bytes) | numeric * 0.01 */
/* 0x05 */ 0b01000011, /* illuminance | uint24 (3 bytes) | numeric * 0.01 */
Expand Down Expand Up @@ -180,6 +189,18 @@ static const uint8_t PROGMEM MEAS_TYPES_FLAGS[] = { /* 8th bit Unused | 6-7th bi
/* 0x4f */ 0b01100100, /* water | uint32 (4 bytes) | numeric * 0.001 */
/* 0x50 */ 0b00000100, /* timestamp | uint48 (4 bytes) | numeric * 1.0 */
/* 0x51 */ 0b01100010, /* acceleration | uint16 (2 bytes) | numeric * 0.001 */
/* 0x52 */ 0b01100010, /* gyroscope | uint16 (2 bytes) | numeric * 0.001 */
/* 0x53 */ 0b00000000, /* text */
/* 0x54 */ 0b00000000, /* raw */
/* 0x55 */ 0b01100100, /* volume storage | uint32 (4 bytes) | numeric * 0.001 */
/* 0x56 */ 0b00000010, /* conductivity | uint16 (2 bytes) | numeric * 1.0 */
/* 0x57 */ 0b00001001, /* temperature | sint8 (1 byte) | numeric 1.0 */
/* 0x58 */ 0b00001001, /* temperature | sint8 (1 btes) | numeric * 0.35 !!!!! */
/* 0x59 */ 0b00001001, /* count | sint8 (1 byte) | numeric 1.0 */
/* 0x5a */ 0b00001010, /* count | sint16 (2 bytes) | numeric 1.0 */
/* 0x5b */ 0b00001100, /* count | sint32 (4 bytes) | numeric * 1.0 */
/* 0x5c */ 0b01001100, /* power | sint32 (4 bytes) | numeric * 0.01 */
/* 0x5d */ 0b01101010, /* current | sint16 (2 bytes) | numeric * 0.001 */
};

}
20 changes: 19 additions & 1 deletion components/bthome_base/bthome_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#include <vector>
#include <map>
#include <string>
#include <cstring>
#ifndef USE_ESP32
#include <pgmspace.h>
#endif

#include "bthome_parser.h"
#include "bthome_common_generated.h"
Expand Down Expand Up @@ -66,6 +69,21 @@ namespace bthome_base
uint8_t obj_data_start;
float obj_data_factor;

if (log_cb)
{
char buffer [4];
char msg [70];
int n;
strcpy(msg, "rec BT payload is : ");
for (int i = 0; i < payload_length; ++i)
{
n=sprintf (buffer, "%.2x ", payload_data[i]);
strlcat(msg, buffer, sizeof(msg));
}
log_cb(msg);
}


while (payload_length >= next_obj_start + 1)
{
auto obj_start = next_obj_start;
Expand Down Expand Up @@ -160,4 +178,4 @@ namespace bthome_base
return true;
}

}
}
55 changes: 55 additions & 0 deletions components/bthome_base/const_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@
"measurement_type": 0x3a,
"event_id": 6
},
"button_hold_click": {
"device_event_type": 0x3a80,
"measurement_type": 0x3a,
"event_id": 128
},
"dimmer_none": {
"device_event_type": 0x3c00,
"measurement_type": 0x3c,
Expand Down Expand Up @@ -436,5 +441,55 @@
"measurement_type": 0x52,
"accuracy_decimals": 3,
"unit_of_measurement": "°/s"
},
"volume storage": {
"measurement_type": 0x55,
"accuracy_decimals": 3,
"unit_of_measurement": "l",
"device_class": "volume"
},
"conductivity": {
"measurement_type": 0x56,
"accuracy_decimals": 0,
"unit_of_measurement": "µS/cm"
},
"temperature": {
"measurement_type": 0x57,
"accuracy_decimals": 0,
"unit_of_measurement": "°C",
"device_class": "temperature"
},
"temperature": {
"measurement_type": 0x58,
"accuracy_decimals": 0,
"unit_of_measurement": "°C",
"device_class": "temperature"
},
"count": {
"measurement_type": 0x59,
"accuracy_decimals": 0,
"unit_of_measurement": ""
},
"count": {
"measurement_type": 0x5a,
"accuracy_decimals": 0,
"unit_of_measurement": ""
},
"count": {
"measurement_type": 0x5b,
"accuracy_decimals": 0,
"unit_of_measurement": ""
},
"power": {
"measurement_type": 0x5c,
"accuracy_decimals": 2,
"unit_of_measurement": "W",
"device_class": "power"
},
"current": {
"measurement_type": 0x5d,
"accuracy_decimals": 3,
"unit_of_measurement": "A",
"device_class": "current"
}
}
6 changes: 3 additions & 3 deletions components/bthome_ble_receiver/bthome_ble_receiver_hub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ namespace esphome
// bthome_receiver_mac_reversed = data[1:7]
// mac_readable = to_mac(bthome_receiver_mac_reversed[::-1])
payload_data += 7;
payload_length = -7;
payload_length -= 7;
}
else
{
// mac_readable = service_info.address;
payload_data += 1;
payload_length = -1;
payload_length -= 1;
}
}

Expand All @@ -106,4 +106,4 @@ namespace esphome
}
}

#endif
#endif