aboutsummaryrefslogtreecommitdiff
path: root/apps/SoilMoisture.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'apps/SoilMoisture.cpp')
-rw-r--r--apps/SoilMoisture.cpp69
1 files changed, 37 insertions, 32 deletions
diff --git a/apps/SoilMoisture.cpp b/apps/SoilMoisture.cpp
index 03aeb00..79f168f 100644
--- a/apps/SoilMoisture.cpp
+++ b/apps/SoilMoisture.cpp
@@ -22,42 +22,43 @@ using namespace trygvis::bluetooth;
using std::to_string;
static
-ByteBuffer createGetSensorCount() {
- return ByteBuffer::alloc(100) //
+void createGetSensorCount(ByteBuffer &buffer) {
+ buffer
.write8(static_cast<uint8_t>(sm_cmd_code::SM_CMD_GET_SENSOR_COUNT));
}
static
-ByteBuffer createGetValue(uint8_t sensor) {
- return ByteBuffer::alloc(100) //
- .write8(static_cast<uint8_t>(sm_cmd_code::SM_CMD_GET_VALUE)) //
+void createGetValue(ByteBuffer &buffer, uint8_t sensor) {
+ buffer
+ .write8(static_cast<uint8_t>(sm_cmd_code::SM_CMD_GET_VALUE))
.write16le(sensor);
}
static
-ByteBuffer createSetWarningValue(uint8_t sensor, uint16_t warning_value) {
- return ByteBuffer::alloc(100)
+void createSetWarningValue(ByteBuffer &buffer, uint8_t sensor, uint16_t warning_value) {
+ buffer
.write8(static_cast<uint8_t>(sm_cmd_code::SM_CMD_SET_WARNING_VALUE))
.write8(sensor)
.write16le(warning_value);
}
static
-ByteBuffer createSetSensorName(uint8_t sensor, string name) {
- return ByteBuffer::alloc(100)
+void createSetSensorName(ByteBuffer &buffer, uint8_t sensor, string name) {
+ buffer
.write8(static_cast<uint8_t>(sm_cmd_code::SM_CMD_SET_SENSOR_NAME))
.write8(sensor)
.write(reinterpret_cast<const uint8_t *>(name.c_str()), name.length());
}
static
-ByteBuffer createGetSensorName(uint8_t sensor) {
- return ByteBuffer::alloc(100).write8(static_cast<uint8_t>(sm_cmd_code::SM_CMD_GET_SENSOR_NAME)).write8(sensor);
+void createGetSensorName(ByteBuffer &buffer, uint8_t sensor) {
+ buffer
+ .write8(static_cast<uint8_t>(sm_cmd_code::SM_CMD_GET_SENSOR_NAME)).write8(sensor);
}
static
-ByteBuffer createSetUpdateInterval(uint8_t sensor, uint8_t interval_in_seconds) {
- return ByteBuffer::alloc(100)
+void createSetUpdateInterval(ByteBuffer buffer, uint8_t sensor, uint8_t interval_in_seconds) {
+ buffer
.write8(static_cast<uint8_t>(sm_cmd_code::SM_CMD_SET_UPDATE_INTERVAL))
.write8(sensor)
.write8(interval_in_seconds);
@@ -93,47 +94,49 @@ SoilMoisture::SoilMoisture(const shared_ptr<BluetoothGatt> &gatt,
const o<BluetoothGattCharacteristicPtr> temperatureCharacteristic,
const o<BluetoothGattCharacteristicPtr> lightCharacteristic)
: gatt(gatt), s(s), soilMoistureCharacteristic(soilMoistureCharacteristic),
- temperatureCharacteristic(temperatureCharacteristic), lightCharacteristic(lightCharacteristic) { }
+ temperatureCharacteristic(temperatureCharacteristic), lightCharacteristic(lightCharacteristic) {}
-ByteBuffer SoilMoisture::writeAndRead(const BluetoothGattCharacteristicPtr &c, ByteBuffer &requestBytes) {
- requestBytes.setCursor(0);
+void SoilMoisture::writeAndRead(const BluetoothGattCharacteristicPtr &c, ByteBuffer &buffer) {
+ buffer.setCursor(0);
- uint8_t expectedCode = requestBytes.peek8(0);
+ uint8_t expectedCode = buffer.peek8(0);
- gatt->writeValue(c, requestBytes);
+ gatt->writeValue(c, buffer);
- auto responseBytes = gatt->readValue(c);
+ gatt->readValue(c, buffer);
- if (responseBytes.getSize() < 1) {
- throw runtime_error("Unexpected number of bytes read: " + to_string(responseBytes.getSize()));
+ if (buffer.getSize() < 1) {
+ throw runtime_error("Unexpected number of bytes read: " + to_string(buffer.getSize()));
}
- uint8_t actualCode = responseBytes.read8();
+ uint8_t actualCode = buffer.read8();
if (actualCode != expectedCode) {
throw runtime_error("Unexpected response code: " + to_string(actualCode) + ", expected " +
to_string(expectedCode));
}
-
- return responseBytes;
}
uint8_t SoilMoisture::getSensorCount() {
- auto buffer = createGetSensorCount();
- return writeAndRead(soilMoistureCharacteristic, buffer).read8();
+ StaticByteBuffer<100> buffer;
+ createGetSensorCount(buffer);
+ writeAndRead(soilMoistureCharacteristic, buffer);
+ return buffer.read8();
}
uint16_t SoilMoisture::getValue(uint8_t sensor) {
- auto req = createGetValue(sensor);
+ StaticByteBuffer<100> buffer;
+ createGetValue(buffer, sensor);
- auto buffer = writeAndRead(soilMoistureCharacteristic, req);
+ writeAndRead(soilMoistureCharacteristic, buffer);
buffer.read8(); // sensor index
return buffer.read16le();
}
string SoilMoisture::getName(uint8_t sensor) {
- auto req = createGetSensorName(sensor);
+ StaticByteBuffer<100> buffer;
+ createGetSensorName(buffer, sensor);
- auto buffer = writeAndRead(soilMoistureCharacteristic, req);
+ writeAndRead(soilMoistureCharacteristic, buffer);
size_t bytesLeft = buffer.getBytesLeft();
uint8_t bytes[bytesLeft];
buffer.copy(bytes, bytesLeft);
@@ -158,7 +161,8 @@ o<double> SoilMoisture::readTemperature() {
return o<double>();
}
- auto responseBytes = gatt->readValue(*temperatureCharacteristic);
+ StaticByteBuffer<100> responseBytes;
+ gatt->readValue(*temperatureCharacteristic, responseBytes);
if (responseBytes.getSize() < 2) {
throw runtime_error("Unexpected number of bytes read: " + to_string(responseBytes.getSize()));
@@ -180,7 +184,8 @@ void SoilMoisture::setLight(uint8_t light, uint8_t value) {
return;
}
- auto responseBytes = gatt->readValue(*lightCharacteristic);
+ StaticByteBuffer<100> responseBytes;
+ gatt->readValue(*lightCharacteristic, responseBytes);
if (responseBytes.getSize() < 2) {
throw runtime_error("Unexpected number of bytes read: " + to_string(responseBytes.getSize()));