SPQR-Team1-2020/src/data_source.cpp

52 lines
1.0 KiB
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 = P_NULL;
2019-10-21 08:57:58 +02:00
}
DataSource::DataSource(TwoWire* i2c_, int addr){
protocol = P_I2C;
this->i2c = i2c_;
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
}
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_;
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 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
}