aboutsummaryrefslogtreecommitdiff
path: root/apps/SoilMoisture.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2016-04-12 20:06:47 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2016-04-12 20:06:47 +0200
commit2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d (patch)
tree19f9ce796886e216a608fa5e938bd2bd4f0d0b55 /apps/SoilMoisture.cpp
parentce07550c57172443c10a66957b50085e273d20b3 (diff)
downloadble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.tar.gz
ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.tar.bz2
ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.tar.xz
ble-toys-2ca532122d60cff4dbdc7f24fbc5783bcc5ad68d.zip
Soil Moisture: Adding support for controlling lights.
Bluetooth: refectorying, trying to be more c++ idiomatic and modern. SM/Diller: adding bluetooth to Diller bridge.
Diffstat (limited to 'apps/SoilMoisture.cpp')
-rw-r--r--apps/SoilMoisture.cpp50
1 files changed, 35 insertions, 15 deletions
diff --git a/apps/SoilMoisture.cpp b/apps/SoilMoisture.cpp
index 13578f6..4b9dfe5 100644
--- a/apps/SoilMoisture.cpp
+++ b/apps/SoilMoisture.cpp
@@ -16,6 +16,7 @@ auto bluetooth_base_uuid =
const boost::uuids::uuid soil_moisture_service = makeUuid(trygvis_io_base_uuid, 0x00, 0x10);
const boost::uuids::uuid soil_moisture_characteristic = makeUuid(trygvis_io_base_uuid, 0x00, 0x11);
const boost::uuids::uuid temperature_characteristic = makeUuid(bluetooth_base_uuid, 0x2a, 0x1e);
+const boost::uuids::uuid light_characteristic = makeUuid(trygvis_io_base_uuid, 0x00, 0x12);
using namespace trygvis::bluetooth;
@@ -64,13 +65,15 @@ ByteBuffer createSetUpdateInterval(uint8_t sensor, uint8_t interval_in_seconds)
SoilMoisture SoilMoisture::create(shared_ptr<BluetoothGatt> gatt) {
gatt->discoverServices();
- auto service = gatt->findService(soil_moisture_service);
+ o<shared_ptr<BluetoothGattService>> s = gatt->findService(soil_moisture_service);
- if (!service) {
+ if (!s) {
throw runtime_error("The device is missing the soil moisture service");
}
- auto c = service->findCharacteristic(soil_moisture_characteristic);
+ shared_ptr<BluetoothGattService> &service = *s;
+
+ o<shared_ptr<BluetoothGattCharacteristic>> c = service->findCharacteristic(soil_moisture_characteristic);
if (!c) {
throw runtime_error("The device is missing the soil moisture characteristic");
@@ -78,16 +81,20 @@ SoilMoisture SoilMoisture::create(shared_ptr<BluetoothGatt> gatt) {
auto temperature = service->findCharacteristic(temperature_characteristic);
- return SoilMoisture(gatt, *service, c.get(), temperature);
+ auto light = service->findCharacteristic(light_characteristic);
+
+ return SoilMoisture(gatt, service, c.operator*(), temperature, light);
}
-SoilMoisture::SoilMoisture(const shared_ptr<BluetoothGatt> &gatt, BluetoothGattService &s,
- const BluetoothGattCharacteristic &soilMoistureCharacteristic,
- const o<const BluetoothGattCharacteristic &> temperatureCharacteristic)
+SoilMoisture::SoilMoisture(const shared_ptr<BluetoothGatt> &gatt,
+ const shared_ptr<BluetoothGattService> &s,
+ const BluetoothGattCharacteristicPtr &soilMoistureCharacteristic,
+ const o<BluetoothGattCharacteristicPtr> temperatureCharacteristic,
+ const o<BluetoothGattCharacteristicPtr> lightCharacteristic)
: gatt(gatt), s(s), soilMoistureCharacteristic(soilMoistureCharacteristic),
- temperatureCharacteristic(temperatureCharacteristic) {}
+ temperatureCharacteristic(temperatureCharacteristic), lightCharacteristic(lightCharacteristic) { }
-ByteBuffer SoilMoisture::writeAndRead(const BluetoothGattCharacteristic &c, ByteBuffer &requestBytes) {
+ByteBuffer SoilMoisture::writeAndRead(const BluetoothGattCharacteristicPtr &c, ByteBuffer &requestBytes) {
requestBytes.setCursor(0);
uint8_t expectedCode = requestBytes.get8(0);
@@ -131,14 +138,15 @@ string SoilMoisture::getName(uint8_t sensor) {
buffer.copy(bytes, bytesLeft);
if (bytesLeft == 0 || bytesLeft < (bytes[0] + 1)) {
- throw runtime_error("Bad response from device. buffer size: " + to_string(bytesLeft) + ", buffer[0]=" + to_string((int)bytes[0]));
+ throw runtime_error("Bad response from device. buffer size: " + to_string(bytesLeft) + ", buffer[0]=" +
+ to_string((int) bytes[0]));
}
- return string((const char *)&bytes[1], bytes[0]);
+ return string((const char *) &bytes[1], bytes[0]);
}
bool SoilMoisture::hasTemperatureSensor() {
- return temperatureCharacteristic.is_initialized();
+ return (bool) temperatureCharacteristic;
}
/**
@@ -149,9 +157,7 @@ o<double> SoilMoisture::readTemperature() {
return o<double>();
}
- auto &c = temperatureCharacteristic.get();
-
- auto responseBytes = gatt->readValue(c);
+ auto responseBytes = gatt->readValue(*temperatureCharacteristic);
if (responseBytes.getSize() < 2) {
throw runtime_error("Unexpected number of bytes read: " + to_string(responseBytes.getSize()));
@@ -168,5 +174,19 @@ o<double> SoilMoisture::readTemperature() {
return o<double>(temperature);
}
+void SoilMoisture::setLight(uint8_t light, uint8_t value) {
+ if (!lightCharacteristic) {
+ return;
+ }
+
+ auto responseBytes = gatt->readValue(*lightCharacteristic);
+
+ if (responseBytes.getSize() < 2) {
+ throw runtime_error("Unexpected number of bytes read: " + to_string(responseBytes.getSize()));
+ }
+
+ bitset<8> b0 = responseBytes.read8();
+}
+
}
}