adds check if sensor is still on bus
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
df5da0f82e
commit
5d88bf4bf8
@ -13,17 +13,21 @@ Sensor::Sensor(int id, float resolution, float accuracy, DallasTemperature &bus,
|
||||
};
|
||||
|
||||
float Sensor::read() const {
|
||||
if(!bus.isConnected(address)) return NAN;
|
||||
|
||||
bus.requestTemperaturesByAddress(address);
|
||||
return bus.getTempC(address);
|
||||
}
|
||||
|
||||
uint16_t Sensor::makeDataPacket(byte (&buffer)[dataPacketMaxSize]) const {
|
||||
bool Sensor::makeDataPacket(byte (&buffer)[dataPacketSize]) const {
|
||||
float readVal = read();
|
||||
if(isnan(readVal)) return false;
|
||||
|
||||
size_t written = 0;
|
||||
|
||||
written += writeType(&buffer[written], id);
|
||||
written += writeType(&buffer[written], read());
|
||||
written += writeType(&buffer[written], readVal);
|
||||
|
||||
return written + 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Sensor::registerSensor(byte (&buffer)[serializedSize]) const {
|
||||
|
@ -15,7 +15,7 @@ class Sensor {
|
||||
|
||||
public:
|
||||
static constexpr uint16_t serializedSize = sizeof(id) + sizeof(resolution) + sizeof(accuracy);
|
||||
static constexpr uint16_t dataPacketMaxSize = 256;
|
||||
static constexpr uint16_t dataPacketSize = sizeof(id) + sizeof(float);
|
||||
|
||||
|
||||
Sensor(int id, float resolution, float accuracy, DallasTemperature &bus, DeviceAddress address);
|
||||
@ -24,7 +24,7 @@ class Sensor {
|
||||
|
||||
uint16_t getId() const {return id;};
|
||||
|
||||
uint16_t makeDataPacket(byte (&buffer)[dataPacketMaxSize]) const;
|
||||
bool makeDataPacket(byte (&buffer)[dataPacketSize]) const;
|
||||
|
||||
void registerSensor(byte (&buffer)[serializedSize]) const;
|
||||
};
|
||||
|
@ -40,15 +40,15 @@ int main() {
|
||||
packetSerial.send(buff, sizeof(buff));
|
||||
}
|
||||
|
||||
byte dataBuff[Sensor::dataPacketMaxSize + 1];
|
||||
byte dataBuff[Sensor::dataPacketSize + 1];
|
||||
dataBuff[0] = (byte)PacketType::data;
|
||||
uint16_t written;
|
||||
while(true) {
|
||||
for(int i = 0; i < sensors.size(); ++i) {
|
||||
written = sensors.get(i)->makeDataPacket((byte(&)[Sensor::dataPacketMaxSize])dataBuff[1]);
|
||||
packetSerial.send(dataBuff, written);
|
||||
if(sensors.get(i)->makeDataPacket((byte(&)[Sensor::dataPacketSize])dataBuff[1])) {
|
||||
packetSerial.send(dataBuff, sizeof(dataBuff));
|
||||
}
|
||||
delay(500);
|
||||
}
|
||||
delay(5000);
|
||||
}
|
||||
|
||||
for(int i = 0; i < sensors.size(); ++i) {
|
||||
|
Loading…
Reference in New Issue
Block a user