Skip to content

Commit

Permalink
✨ (BLEKit): Add temperature characteristic
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Jan 22, 2024
1 parent 4c5dfca commit 52b2769
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
19 changes: 16 additions & 3 deletions libs/BLEKit/include/BLEServiceMonitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class BLEServiceMonitoring : public interface::BLEService
sendData(data);
}

void setTemperature(float value)
{
std::memcpy(temperature.data(), &value, 4);

auto data = std::make_tuple(_temperature_characteristic.getValueHandle(), temperature);
sendData(data);
}

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

void onDataReceived(const data_received_handle_t &params) final
Expand Down Expand Up @@ -63,6 +71,11 @@ class BLEServiceMonitoring : public interface::BLEService
service::monitoring::characteristic::charging_status, &charging_status,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY};

std::array<uint8_t, 4> temperature {};
ReadOnlyArrayGattCharacteristic<uint8_t, 4> _temperature_characteristic {
service::monitoring::characteristic::temperature, temperature.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 @@ -76,9 +89,9 @@ class BLEServiceMonitoring : public interface::BLEService
WriteOnlyGattCharacteristic<bool> hard_reboot_characteristic {service::monitoring::characteristic::hard_reboot,
&hard_reboot};

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

} // namespace leka
9 changes: 5 additions & 4 deletions libs/BLEKit/include/internal/ServicesCharacteristics.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ namespace monitoring {
inline constexpr uint16_t uuid = 0x7779;

namespace characteristic {
inline constexpr uint16_t charging_status = 0x6783;
inline constexpr uint16_t screensaver_enable = 0x8369;
inline constexpr uint16_t soft_reboot = 0x8382;
inline constexpr uint16_t hard_reboot = 0x7282;
inline constexpr uint16_t charging_status = 0x6783;
inline constexpr UUID::LongUUIDBytes_t temperature = {"Temperature"};
inline constexpr uint16_t screensaver_enable = 0x8369;
inline constexpr uint16_t soft_reboot = 0x8382;
inline constexpr uint16_t hard_reboot = 0x7282;
} // namespace characteristic

} // namespace monitoring
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 @@ -54,6 +54,24 @@ TEST_F(BLEServiceMonitoringTest, setChargingStatus)
EXPECT_FALSE(actual_charging_status);
}

TEST_F(BLEServiceMonitoringTest, setTemperature)
{
std::array<uint8_t, 4> actual_temperature {};
std::array<uint8_t, 4> expected_temperature {};

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

service_monitoring.onDataReadyToSend(spy_callback);

service_monitoring.setTemperature(3.14159);
expected_temperature = {0xD0, 0x0F, 0x49, 0x40}; // 3.14159, little-endian as in Swift
EXPECT_EQ(actual_temperature, expected_temperature);
}

TEST_F(BLEServiceMonitoringTest, isScreensaverEnableDefault)
{
auto actual_is_screensaver_enable = service_monitoring.isScreensaverEnable();
Expand Down

0 comments on commit 52b2769

Please sign in to comment.