Skip to content

Commit faf58e9

Browse files
committed
sns: allow sensor instance to provide slot id
implement i2c address as slot id for the current sensors ref. #2639
1 parent 58181d2 commit faf58e9

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

code/espurna/sensor.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ using Slot = std::array<char, 16>;
371371

372372
struct SlotValues {
373373
unsigned char id;
374-
unsigned char index;
374+
unsigned char slot_id;
375375
unsigned char type;
376376
unsigned char slot;
377377
};
@@ -418,7 +418,7 @@ Slot make_slot(SlotValues values) {
418418

419419
auto it = out.begin();
420420
it = append_slot_number_impl(it, values.id);
421-
it = append_slot_number_impl(it, values.index);
421+
it = append_slot_number_impl(it, values.slot_id);
422422
it = append_slot_number_impl(it, values.type);
423423
append_slot_number_impl(it, values.slot);
424424

@@ -440,11 +440,14 @@ class Magnitude {
440440
{}
441441

442442
BaseSensorPtr sensor; // Sensor object, *cannot be empty*
443+
443444
unsigned char type; // Type of measurement, returned by the BaseSensor::type(slot)
444445
unsigned char slot; // Sensor slot # taken by the magnitude, used to access the measurement
445446

446447
unsigned char index_global; // N'th magnitude of it's type, across all of the active sensors
447-
unsigned char slot_global; // Global slot aka index of the sensor, across all of the active sensors
448+
unsigned char slot_global; // Global slot aka index of the sensor, across all of the sensors with the same ID
449+
450+
unsigned char slot_id; // Slot ID provided by the sensor. Expected to be varying between different instances of the sensor
448451

449452
Unit units { Unit::None }; // Current units of measurement
450453
unsigned char decimals { 0u }; // Number of decimals in textual representation
@@ -1350,7 +1353,9 @@ String format_slot(const Magnitude& magnitude) {
13501353
const auto slot = make_slot(
13511354
SlotValues{
13521355
.id = magnitude.sensor->id(),
1353-
.index = static_cast<unsigned char>(magnitude.slot_global + 1),
1356+
.slot_id = (magnitude.slot_id != 0)
1357+
? magnitude.slot_id
1358+
: static_cast<unsigned char>(magnitude.slot_global + 1),
13541359
.type = magnitude.type,
13551360
.slot = static_cast<unsigned char>(magnitude.slot + 1),
13561361
});
@@ -1770,6 +1775,7 @@ Magnitude& add(BaseSensorPtr sensor, unsigned char type, unsigned char slot) {
17701775
out.slot = slot;
17711776
out.index_global = types_count_add(type);
17721777
out.slot_global = instance_count_add(sensor->id());
1778+
out.slot_id = sensor->address_u8(slot);
17731779

17741780
internal::magnitudes.emplace_back(std::move(out));
17751781
return internal::magnitudes.back();

code/espurna/sensors/BaseSensor.h

+5
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ class BaseSensor {
194194
}
195195

196196
// Address of the sensor (it could be the GPIO or I2C address)
197+
virtual unsigned char address_u8(unsigned char) const {
198+
return 0u;
199+
}
200+
201+
// String representation of the sensor address
197202
virtual String address(unsigned char) const {
198203
return String();
199204
}

code/espurna/sensors/DummySensor.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ struct Sensor : public BaseEmonSensor {
3535

3636
Sensor() :
3737
BaseEmonSensor(Magnitudes)
38-
{}
38+
{
39+
++_counter;
40+
}
3941

4042
unsigned char id() const override {
4143
return SENSOR_DUMMY_ID;
@@ -60,6 +62,10 @@ struct Sensor : public BaseEmonSensor {
6062
return STRING_VIEW("DummySensor").toString();
6163
}
6264

65+
unsigned char address_u8(unsigned char) const override {
66+
return _counter;
67+
}
68+
6369
String address(unsigned char) const override {
6470
return STRING_VIEW("/dev/null").toString();
6571
}
@@ -138,6 +144,8 @@ struct Sensor : public BaseEmonSensor {
138144
}
139145

140146
private:
147+
static uint8_t _counter;
148+
141149
bool _fail_begin { true };
142150
bool _fail_pre { true };
143151

@@ -149,6 +157,8 @@ struct Sensor : public BaseEmonSensor {
149157
double _delta { 0.0 };
150158
};
151159

160+
uint8_t Sensor::_counter { 123 };
161+
152162
#ifndef __cpp_inline_variables
153163
constexpr BaseSensor::Magnitude Sensor::Magnitudes[];
154164
#endif

code/espurna/sensors/I2CSensor.h

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class I2CSensor : public T {
9898
};
9999

100100
// Address of the sensor (it could be the GPIO or I2C address)
101+
unsigned char address_u8(unsigned char) const override {
102+
return _sensor_address.address();
103+
}
104+
105+
// String representation of the sensor address
101106
String address(unsigned char) const override {
102107
char buffer[5];
103108
snprintf_P(buffer, sizeof(buffer),

0 commit comments

Comments
 (0)