Skip to content

Commit fa0828d

Browse files
committed
🚧 WIP, dsx - Attempt towards encoding multiple sensor events in one update() looop
1 parent 922f0a6 commit fa0828d

File tree

7 files changed

+127
-124
lines changed

7 files changed

+127
-124
lines changed

‎src/Wippersnapper_V2.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
#endif
5050

5151
// Components (API v2)
52-
#include "components/checkin/model.h"
5352
#include "components/sensor/model.h"
53+
#include "components/checkin/model.h"
5454
#include "components/digitalIO/controller.h"
5555
#include "components/analogio/controller.h"
5656
#include "components/ds18x20/controller.h"

‎src/components/ds18x20/controller.cpp‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,46 @@ void DS18X20Controller::update() {
9191
// Create a temporary DS18X20Hardware driver
9292
DS18X20Hardware &temp_dsx_driver = _DS18X20_pins[i];
9393
// Check if the driver's timer has not expired yet
94-
if (!temp_dsx_driver.IsTimerExpired())
94+
if (!temp_dsx_driver.IsTimerExpired()) {
9595
continue;
96+
}
97+
// Create a new sensor_event
98+
_DS18X20_model->InitDS18x20EventMsg();
9699
// Are we reading the temperature in Celsius, Fahrenheit, or both?
97-
if (temp_dsx_driver.is_read_temp_c)
98-
temp_dsx_driver.GetTemperatureC();
99-
if (temp_dsx_driver.is_read_temp_f)
100-
temp_dsx_driver.GetTemperatureF();
101-
// wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE
100+
if (temp_dsx_driver.is_read_temp_c) {
101+
WS_DEBUG_PRINTLN("Reading temperature in Celsius"); // TODO: Debug remove
102+
float temp_c = temp_dsx_driver.GetTemperatureC();
103+
_DS18X20_model->addSensorEvent(
104+
wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE,
105+
temp_c);
106+
}
107+
if (temp_dsx_driver.is_read_temp_f) {
108+
WS_DEBUG_PRINTLN(
109+
"Reading temperature in Fahrenheit"); // TODO: Debug remove
110+
float temp_f = temp_dsx_driver.GetTemperatureF();
111+
_DS18X20_model->addSensorEvent(
112+
wippersnapper_sensor_SensorType_SENSOR_TYPE_AMBIENT_TEMPERATURE_FAHRENHEIT,
113+
temp_f);
114+
}
115+
// Print out the SensorEvent message's contents for debugging
116+
// TODO: After debugging, maybe remove this
117+
wippersnapper_ds18x20_Ds18x20Event event_msg =
118+
*_DS18X20_model->GetDS18x20EventMsg();
119+
WS_DEBUG_PRINTLN("SensorEvent message:");
120+
WS_DEBUG_PRINT("Sensor OneWire bus pin: ");
121+
WS_DEBUG_PRINTLN(event_msg.onewire_pin);
122+
WS_DEBUG_PRINT("Sensor events count: ");
123+
WS_DEBUG_PRINTLN(event_msg.sensor_events_count);
124+
125+
for (int i = 0;
126+
i < _DS18X20_model->GetDS18x20EventMsg()->sensor_events_count; i++) {
127+
WS_DEBUG_PRINT("Sensor type: ");
128+
WS_DEBUG_PRINTLN(event_msg.sensor_events[i].type);
129+
WS_DEBUG_PRINT("Sensor value: ");
130+
WS_DEBUG_PRINTLN(event_msg.sensor_events[i].value.float_value);
131+
WS_DEBUG_PRINT("Sensor value type: ");
132+
WS_DEBUG_PRINTLN(event_msg.sensor_events[i].which_value);
133+
}
102134
}
135+
// TODO: Encode and publish the Ds18x20Event message to the broker
103136
}

‎src/components/ds18x20/controller.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class DS18X20Controller {
3333
~DS18X20Controller();
3434
// Routing
3535
bool Handle_Ds18x20Add(pb_istream_t *stream);
36+
// TODO: Implement Handle DS18x20 Remove message!
3637
// Polling
3738
void update();
3839

‎src/components/ds18x20/model.cpp‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,54 @@ bool DS18X20Model::EncodeDS18x20Added(char *onewire_pin, bool is_init) {
4343
strcpy(_msg_DS18x20Added.onewire_pin, onewire_pin);
4444

4545
// Encode the Ds18x20Added message
46-
size_t sz_aio_event_msg;
47-
if (!pb_get_encoded_size(&sz_aio_event_msg,
48-
wippersnapper_ds18x20_Ds18x20Added_fields,
46+
size_t sz_msg;
47+
if (!pb_get_encoded_size(&sz_msg, wippersnapper_ds18x20_Ds18x20Added_fields,
4948
&_msg_DS18x20Added))
5049
return false;
5150

52-
uint8_t buf[sz_aio_event_msg];
51+
uint8_t buf[sz_msg];
5352
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
5453
return pb_encode(&msg_stream, wippersnapper_ds18x20_Ds18x20Added_fields,
5554
&_msg_DS18x20Added);
5655
}
56+
57+
wippersnapper_ds18x20_Ds18x20Event *DS18X20Model::GetDS18x20EventMsg() {
58+
return &_msg_DS18x20Event;
59+
}
60+
61+
bool DS18X20Model::EncodeDs18x20Event(
62+
char *onewire_pin, pb_size_t sensor_events_count,
63+
const wippersnapper_sensor_SensorEvent sensor_events[2]) {
64+
// Fill the Ds18x20Event message
65+
_msg_DS18x20Event = wippersnapper_ds18x20_Ds18x20Event_init_zero;
66+
strcpy(_msg_DS18x20Event.onewire_pin, onewire_pin);
67+
_msg_DS18x20Event.sensor_events_count = sensor_events_count;
68+
// Fill with sensor_events
69+
for (int i = 0; i < _msg_DS18x20Event.sensor_events_count; i++) {
70+
_msg_DS18x20Event.sensor_events[i] = sensor_events[i];
71+
}
72+
// Encode and return the Ds18x20Event message
73+
size_t sz_msg;
74+
if (!pb_get_encoded_size(&sz_msg, wippersnapper_ds18x20_Ds18x20Event_fields,
75+
&_msg_DS18x20Event))
76+
return false;
77+
uint8_t buf[sz_msg];
78+
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
79+
return pb_encode(&msg_stream, wippersnapper_ds18x20_Ds18x20Event_fields,
80+
&_msg_DS18x20Event);
81+
}
82+
83+
void DS18X20Model::InitDS18x20EventMsg() {
84+
_msg_DS18x20Event = wippersnapper_ds18x20_Ds18x20Event_init_zero;
85+
_msg_DS18x20Event.sensor_events_count = 0;
86+
}
87+
88+
void DS18X20Model::addSensorEvent(wippersnapper_sensor_SensorType sensor_type,
89+
float sensor_value) {
90+
_msg_DS18x20Event.sensor_events[_msg_DS18x20Event.sensor_events_count].type =
91+
sensor_type;
92+
_msg_DS18x20Event.sensor_events[_msg_DS18x20Event.sensor_events_count]
93+
.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
94+
_msg_DS18x20Event.sensor_events[_msg_DS18x20Event.sensor_events_count]
95+
.value.float_value = sensor_value;
96+
}

‎src/components/ds18x20/model.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,26 @@ class DS18X20Model {
3232
// Ds18x20Remove Message
3333
bool EncodeDS18x20Added(char *onewire_pin, bool is_init);
3434
wippersnapper_ds18x20_Ds18x20Added *GetDS18x20AddedMsg();
35+
// Ds18x20Event Message
36+
bool
37+
EncodeDs18x20Event(char *onewire_pin, pb_size_t sensor_events_count,
38+
const wippersnapper_sensor_SensorEvent sensor_events[2]);
39+
wippersnapper_ds18x20_Ds18x20Event *GetDS18x20EventMsg();
40+
// TODO: move the below to private if we arent using it in controller?
41+
wippersnapper_ds18x20_Ds18x20Event
42+
_msg_DS18x20Event; ///< wippersnapper_ds18x20_Ds18x20Event message
43+
void InitDS18x20EventMsg();
44+
void addSensorEvent(wippersnapper_sensor_SensorType sensor_type,
45+
float sensor_value);
3546

3647
private:
3748
wippersnapper_ds18x20_Ds18x20Add
3849
_msg_DS18x20Add; ///< wippersnapper_ds18x20_Ds18x20Add message
3950
wippersnapper_ds18x20_Ds18x20Added
4051
_msg_DS18x20Added; ///< wippersnapper_ds18x20_Ds18x20Added message
52+
wippersnapper_ds18x20_Ds18x20Event
53+
_msg_DS18x20Event; ///< wippersnapper_ds18x20_Ds18x20Event message
54+
wippersnapper_ds18x20_Ds18x20Remove
55+
_msg_DS18x20Remove; ///< wippersnapper_ds18x20_Ds18x20Remove message
4156
};
4257
#endif // WS_DIGITALIO_MODEL_H

‎src/components/sensor/model.cpp‎

Lines changed: 23 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -20,119 +20,42 @@
2020
*/
2121
/***********************************************************************/
2222
SensorModel::SensorModel() {
23-
_msg_sensor_event = wippersnapper_sensor_SensorEvent_init_default;
23+
_msg_sensor_event = wippersnapper_sensor_SensorEvent_init_zero;
2424
}
2525

2626
/***********************************************************************/
2727
/*!
2828
@brief SensorModel destructor
2929
*/
3030
/***********************************************************************/
31-
SensorModel::~SensorModel() {}
32-
33-
/***********************************************************************/
34-
/*!
35-
@brief Decodes a SensorEvent message into a SensorModel object.
36-
@param stream
37-
The input stream
38-
@returns True if the SensorEvent message was successfully decoded,
39-
False otherwise.
40-
*/
41-
/***********************************************************************/
42-
bool SensorModel::decodeSensorEvent(pb_istream_t *stream) {
43-
// Decode the stream into theSensorEvent message
44-
if (!pb_decode(stream, wippersnapper_sensor_SensorEvent_fields,
45-
&_msg_sensor_event)) {
46-
return false;
47-
}
48-
return true;
49-
}
50-
51-
/***********************************************************************/
52-
/*!
53-
@brief Clears the SensorEvent message.
54-
*/
55-
/***********************************************************************/
56-
void SensorModel::clearSensorEvent() {
57-
_msg_sensor_event = wippersnapper_sensor_SensorEvent_init_default;
58-
}
59-
60-
/***********************************************************************/
61-
/*!
62-
@brief Returns a SensorEvent message.
63-
@returns The SensorEvent message.
64-
*/
65-
/***********************************************************************/
66-
wippersnapper_sensor_SensorEvent *SensorModel::getSensorEvent() {
67-
return &_msg_sensor_event;
68-
}
69-
70-
/***********************************************************************/
71-
/*!
72-
@brief Returns the the SensorEvent message's type.
73-
@returns The type of the SensorEvent message, as a SensorType.
74-
*/
75-
/***********************************************************************/
76-
wippersnapper_sensor_SensorType SensorModel::getSensorType() {
77-
return _msg_sensor_event.type;
78-
}
79-
80-
/***********************************************************************/
81-
/*!
82-
@brief Returns the the SensorEvent message's which_value field.
83-
@returns The which_value field of the SensorEvent message.
84-
*/
85-
/***********************************************************************/
86-
pb_size_t SensorModel::getSensorEventWhichValue() {
87-
return _msg_sensor_event.which_value;
31+
SensorModel::~SensorModel() {
32+
// Zero-out the SensorEvent message
33+
_msg_sensor_event = wippersnapper_sensor_SensorEvent_init_zero;
8834
}
8935

90-
/***********************************************************************/
91-
/*!
92-
@brief Returns the the SensorEvent message's float value field.
93-
@returns The float value of the SensorEvent message.
94-
*/
95-
/***********************************************************************/
96-
float SensorModel::getValueFloat() {
97-
return _msg_sensor_event.value.float_value;
36+
wippersnapper_sensor_SensorEvent *SensorModel::GetSensorEventMessage() {
37+
return &_msg_sensor_event;
9838
}
9939

100-
/***********************************************************************/
101-
/*!
102-
@brief Returns the the SensorEvent message's bool value field.
103-
@returns The bool value of the SensorEvent message.
104-
*/
105-
/***********************************************************************/
106-
bool SensorModel::getValueBool() { return _msg_sensor_event.value.bool_value; }
40+
bool SensorModel::EncodeSensorEventMessage() {
41+
// Obtain size of the class' SensorEvent message
42+
size_t sz_msg;
43+
if (!pb_get_encoded_size(&sz_msg, wippersnapper_sensor_SensorEvent_fields, &_msg_sensor_event))
44+
return false;
10745

108-
/***********************************************************************/
109-
/*!
110-
@brief Returns the the SensorEvent message's vector value field.
111-
@returns The vector value of the SensorEvent message.
112-
*/
113-
/***********************************************************************/
114-
wippersnapper_sensor_SensorEvent_SensorEvent3DVector
115-
SensorModel::getValueVector() {
116-
return _msg_sensor_event.value.vector_value;
46+
// Encode the class' SensorEvent message
47+
uint8_t buf[sz_msg];
48+
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
49+
return pb_encode(&msg_stream, wippersnapper_sensor_SensorEvent_fields,
50+
&_msg_sensor_event);
11751
}
11852

119-
/***********************************************************************/
120-
/*!
121-
@brief Returns the the SensorEvent message's orientation value field.
122-
@returns The orientation value of the SensorEvent message.
123-
*/
124-
/***********************************************************************/
125-
wippersnapper_sensor_SensorEvent_SensorEventOrientation
126-
SensorModel::getValueOrientation() {
127-
return _msg_sensor_event.value.orientation_value;
128-
}
12953

130-
/***********************************************************************/
131-
/*!
132-
@brief Returns the the SensorEvent message's color value field.
133-
@returns The color value of the SensorEvent message.
134-
*/
135-
/***********************************************************************/
136-
wippersnapper_sensor_SensorEvent_SensorEventColor SensorModel::getValueColor() {
137-
return _msg_sensor_event.value.color_value;
54+
void SensorModel::CreateSensorEventFloat(wippersnapper_sensor_SensorType sensor_type, float sensor_value) {
55+
// Zero-out the previous SensorEvent message
56+
_msg_sensor_event = wippersnapper_sensor_SensorEvent_init_zero;
57+
// Fill in the SensorEvent message
58+
_msg_sensor_event.type = sensor_type;
59+
_msg_sensor_event.which_value = wippersnapper_sensor_SensorEvent_float_value_tag;
60+
_msg_sensor_event.value.float_value = sensor_value;
13861
}

‎src/components/sensor/model.h‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,10 @@ class SensorModel {
2727
public:
2828
SensorModel();
2929
~SensorModel();
30-
bool decodeSensorEvent(pb_istream_t *stream);
31-
void clearSensorEvent();
32-
33-
wippersnapper_sensor_SensorEvent *getSensorEvent();
34-
wippersnapper_sensor_SensorType getSensorType();
35-
pb_size_t getSensorEventWhichValue();
36-
37-
float getValueFloat();
38-
bool getValueBool();
39-
wippersnapper_sensor_SensorEvent_SensorEvent3DVector getValueVector();
40-
wippersnapper_sensor_SensorEvent_SensorEventOrientation getValueOrientation();
41-
wippersnapper_sensor_SensorEvent_SensorEventColor getValueColor();
42-
30+
bool EncodeSensorEventMessage();
31+
wippersnapper_sensor_SensorEvent *GetSensorEventMessage();
32+
// These functions create a SensorEvent message
33+
void CreateSensorEventFloat(wippersnapper_sensor_SensorType sensor_type, float sensor_value);
4334
private:
4435
wippersnapper_sensor_SensorEvent _msg_sensor_event; ///< SensorEvent message
4536
};

0 commit comments

Comments
 (0)