Skip to content

Commit

Permalink
✨ (BLEKit): Add humidity characteristic
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Feb 5, 2024
1 parent 99aa7ad commit 8a540d2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
19 changes: 16 additions & 3 deletions libs/BLEKit/include/BLEServiceMonitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class BLEServiceMonitoring : public interface::BLEService
sendData(data);
}

void setHumidity(float value)
{
std::memcpy(humidity.data(), &value, 4);

auto data = std::make_tuple(_humidity_characteristic.getValueHandle(), humidity);
sendData(data);
}

auto isScreensaverEnable() const -> bool { return screensaver_enable; }

void onTemperatureRequested(const std::function<void()> &callback)
Expand Down Expand Up @@ -85,6 +93,11 @@ class BLEServiceMonitoring : public interface::BLEService
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY};
std::function<void()> _on_temperature_requested_callback {};

std::array<uint8_t, 4> humidity {};
ReadOnlyArrayGattCharacteristic<uint8_t, 4> _humidity_characteristic {
service::monitoring::characteristic::humidity, humidity.begin(),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY};

bool screensaver_enable {true};
WriteOnlyGattCharacteristic<bool> screensaver_enable_characteristic {
service::monitoring::characteristic::screensaver_enable, &screensaver_enable};
Expand All @@ -98,9 +111,9 @@ class BLEServiceMonitoring : public interface::BLEService
WriteOnlyGattCharacteristic<bool> hard_reboot_characteristic {service::monitoring::characteristic::hard_reboot,
&hard_reboot};

std::array<GattCharacteristic *, 5> _characteristic_table {
&_charging_status_characteristic, &_temperature_characteristic, &screensaver_enable_characteristic,
&soft_reboot_characteristic, &hard_reboot_characteristic};
std::array<GattCharacteristic *, 6> _characteristic_table {
&_charging_status_characteristic, &_temperature_characteristic, &_humidity_characteristic,
&screensaver_enable_characteristic, &soft_reboot_characteristic, &hard_reboot_characteristic};
};

} // namespace leka
1 change: 1 addition & 0 deletions libs/BLEKit/include/internal/ServicesCharacteristics.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace monitoring {
namespace characteristic {
inline constexpr uint16_t charging_status = 0x6783;
inline constexpr UUID::LongUUIDBytes_t temperature = {"Temperature"};
inline constexpr UUID::LongUUIDBytes_t humidity = {"Humidity"};
inline constexpr uint16_t screensaver_enable = 0x8369;
inline constexpr uint16_t soft_reboot = 0x8382;
inline constexpr uint16_t hard_reboot = 0x7282;
Expand Down
18 changes: 18 additions & 0 deletions libs/BLEKit/tests/BLEServiceMonitoring_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ TEST_F(BLEServiceMonitoringTest, setTemperature)
EXPECT_EQ(actual_temperature, expected_temperature);
}

TEST_F(BLEServiceMonitoringTest, setHumidity)
{
std::array<uint8_t, 4> actual_humidity {};
std::array<uint8_t, 4> expected_humidity {};

auto spy_callback = [&actual_humidity](const BLEServiceMonitoring::data_to_send_handle_t &handle) {
for (auto i = 0; i < std::size(actual_humidity); i++) {
actual_humidity.at(i) = std::get<1>(handle)[i];
}
};

service_monitoring.onDataReadyToSend(spy_callback);

service_monitoring.setHumidity(51.24);
expected_humidity = {0xC3, 0xF5, 0x4C, 0x42}; // 51.24 little-endian as in Swift
EXPECT_EQ(actual_humidity, expected_humidity);
}

TEST_F(BLEServiceMonitoringTest, onTemperatureRequested)
{
testing::MockFunction<void()> mock_callback {};
Expand Down

0 comments on commit 8a540d2

Please sign in to comment.