2019-10-21 08:06:37 +02:00
|
|
|
#include "data_source.h"
|
|
|
|
|
2019-10-21 08:57:58 +02:00
|
|
|
DataSource::DataSource(void){
|
2019-11-18 15:07:46 +01:00
|
|
|
protocol = P_NULL;
|
2019-10-21 08:57:58 +02:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:33:13 +01:00
|
|
|
DataSource::DataSource(TwoWire* i2c_, int addr){
|
2019-11-18 15:07:46 +01:00
|
|
|
protocol = P_I2C;
|
|
|
|
this->i2c = i2c_;
|
2019-11-27 15:33:13 +01:00
|
|
|
this->i2CAddr = addr;
|
|
|
|
|
2019-11-27 17:17:18 +01:00
|
|
|
this->i2c->end();
|
|
|
|
this->i2c->begin();
|
2019-10-21 08:57:58 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 15:07:46 +01:00
|
|
|
DataSource::DataSource(HardwareSerial* ser_, int baud){
|
|
|
|
protocol = P_RXTX;
|
|
|
|
this->ser = ser_;
|
2019-10-21 08:57:58 +02:00
|
|
|
ser->begin(baud);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSource::DataSource(int pin_, bool analog){
|
2019-11-18 14:37:55 +01:00
|
|
|
this->pin = pin_;
|
2019-11-18 15:07:46 +01:00
|
|
|
if(analog) protocol = P_APIN;
|
|
|
|
else {
|
|
|
|
protocol = P_DPIN;
|
|
|
|
digitalWrite(pin, OUTPUT);
|
|
|
|
}
|
2019-10-21 08:57:58 +02:00
|
|
|
}
|
|
|
|
|
2019-10-21 08:06:37 +02:00
|
|
|
int DataSource::getValue(){
|
|
|
|
return value;
|
2019-10-21 08:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSource::update(){
|
|
|
|
readSensor();
|
|
|
|
postProcess();
|
|
|
|
}
|
|
|
|
|
2019-11-18 14:37:55 +01:00
|
|
|
void DataSource::postProcess(){ }
|
2019-10-21 15:30:28 +02:00
|
|
|
|
2019-10-21 08:57:58 +02:00
|
|
|
void DataSource::readSensor(){
|
|
|
|
if(protocol == 1) value = i2c->read();
|
|
|
|
else if(protocol == 2) while(ser->available() > 0) value = ser->read();
|
|
|
|
else if(protocol == 3) analogRead(pin);
|
|
|
|
else if(protocol == 4) digitalRead(pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSource::test(){
|
2019-11-18 14:37:55 +01:00
|
|
|
this->update();
|
|
|
|
DEBUG_PRINT.println(this->getValue());
|
2019-10-21 08:06:37 +02:00
|
|
|
}
|