SPQR-Team1-2020/src/data_source.cpp

52 lines
895 B
C++
Raw Normal View History

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_){
i2c = &(i2c_);
protocol = 1;
i2c->begin();
}
DataSource::DataSource(HardwareSerial ser_, int baud){
ser = &(ser_);
protocol = 2;
ser->begin(baud);
}
DataSource::DataSource(int pin_, bool analog){
pin = pin_;
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();
}
void DataSource::postProcess(){
}
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(){
update();
DEBUG_PRINT.println(getValue());
2019-10-21 08:06:37 +02:00
}