2019-10-21 08:06:37 +02:00
|
|
|
#include "data_source.h"
|
|
|
|
|
2019-10-21 08:57:58 +02:00
|
|
|
DataSource::DataSource(void){
|
|
|
|
protocol = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSource::DataSource(TwoWire i2c_){
|
|
|
|
|
2019-11-18 14:37:55 +01:00
|
|
|
this->i2c = &(i2c_);
|
2019-10-21 08:57:58 +02:00
|
|
|
protocol = 1;
|
|
|
|
|
|
|
|
i2c->begin();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-18 14:42:43 +01:00
|
|
|
DataSource::DataSource(usb_serial_class ser_, int baud){
|
2019-11-18 14:37:55 +01:00
|
|
|
this->ser = &(ser_);
|
2019-10-21 08:57:58 +02:00
|
|
|
protocol = 2;
|
|
|
|
|
|
|
|
ser->begin(baud);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSource::DataSource(int pin_, bool analog){
|
2019-11-18 14:37:55 +01:00
|
|
|
this->pin = pin_;
|
2019-10-21 08:57:58 +02:00
|
|
|
if(analog) protocol = 3;
|
|
|
|
else protocol = 4;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|