@@ -40,7 +40,7 @@ bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
4040 }
4141
4242 // Get the pin name
43- int pin_name = atoi (_analogio_model->GetAnalogIOAddMsg ()->pin_name + 1 );
43+ uint8_t pin_name = atoi (_analogio_model->GetAnalogIOAddMsg ()->pin_name + 1 );
4444
4545 // Create a new analogioPin object
4646 // TODO: Replicate this within the digitalio controller, much cleaner way to
@@ -85,7 +85,35 @@ bool AnalogIOController::Handle_AnalogIORemove(pb_istream_t *stream) {
8585}
8686
8787bool AnalogIOController::IsPinTimerExpired (analogioPin *pin, ulong cur_time) {
88- return cur_time - pin->prv_pin_time > pin->pin_period ;
88+ return cur_time - pin->prv_period > pin->period ;
89+ }
90+
91+ bool AnalogIOController::EncodePublishPinValue (uint8_t pin, uint16_t value) {
92+ // TODO!
93+ return true ;
94+ }
95+
96+ bool AnalogIOController::EncodePublishPinVoltage (uint8_t pin, float value) {
97+ char c_pin_name[12 ];
98+ sprintf (c_pin_name, " D%d" , pin);
99+
100+ // Encode the DigitalIOEvent message
101+ if (!_analogio_model->EncodeAnalogIOVoltageEvent (c_pin_name, value)) {
102+ WS_DEBUG_PRINTLN (" ERROR: Unable to encode DigitalIOEvent message!" );
103+ return false ;
104+ }
105+
106+ // Publish the DigitalIOEvent message to the broker
107+ if (!WsV2.PublishSignal (
108+ wippersnapper_signal_DeviceToBroker_digitalio_event_tag,
109+ _analogio_model->GetAnalogIOEvent ())) {
110+ WS_DEBUG_PRINTLN (" ERROR: Unable to publish analogio voltage event message, "
111+ " moving onto the next pin!" );
112+ return false ;
113+ }
114+ WS_DEBUG_PRINTLN (" Published AnalogIOEvent message to broker!" )
115+
116+ return true ;
89117}
90118
91119void AnalogIOController::update () {
@@ -103,16 +131,17 @@ void AnalogIOController::update() {
103131 continue ;
104132
105133 // Pins timer has expired, lets read the pin
106- if (pin.read_mode == wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE) {
107- // TODO: Read and store the pin's voltage
134+ // Read the pin's raw value
135+ uint16_t value = _analogio_hardware->GetPinValue (pin.name );
136+ if (pin.read_mode == wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW) {
137+ // Since we already read the raw value, encode and publish it to the
138+ // broker
139+ // TODO
108140 } else if (pin.read_mode ==
109- wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW) {
110- // TODO: Read and store the pin's raw value
111- uint16_t value = _analogio_hardware->GetPinValue (pin.name );
112- } else {
113- WS_DEBUG_PRINT (" ERROR: Invalid read mode for analog pin: " );
114- WS_DEBUG_PRINTLN (pin.name );
115- continue ;
141+ wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE) {
142+ // Convert the raw value into voltage
143+ float pin_value = _analogio_hardware->CalculatePinVoltage (value);
144+ // Encode and publish the voltage value to the broker
116145 }
117146 }
118147}
0 commit comments