From da20da5e0af35ae0a64d40bc73f158c1f0fec0f4 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Mon, 21 Oct 2019 08:57:58 +0200 Subject: [PATCH] Maybe now it less confusing --- .vscode/settings.json | 10 ++++++++- include/data_source.h | 21 +++++++++++++++--- include/data_source_analog.h | 11 --------- include/data_source_bno055.h | 14 ++++++------ include/data_source_i2c.h | 14 ------------ include/data_source_rxtx.h | 13 ----------- include/vars.h | 1 + src/data_source.cpp | 43 ++++++++++++++++++++++++++++++++++++ src/data_source_bno055.cpp | 12 +++++----- src/data_source_i2c.cpp | 5 ----- src/data_source_rxtx.cpp | 7 ------ src/main.cpp | 3 ++- 12 files changed, 86 insertions(+), 68 deletions(-) delete mode 100644 include/data_source_analog.h delete mode 100644 include/data_source_i2c.h delete mode 100644 include/data_source_rxtx.h create mode 100644 include/vars.h delete mode 100644 src/data_source_i2c.cpp delete mode 100644 src/data_source_rxtx.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 0c3b333..77b68ea 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,6 +11,14 @@ "functional": "cpp", "tuple": "cpp", "type_traits": "cpp", - "utility": "cpp" + "utility": "cpp", + "hash_map": "cpp", + "deque": "cpp", + "list": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "string_view": "cpp", + "initializer_list": "cpp" } } \ No newline at end of file diff --git a/include/data_source.h b/include/data_source.h index d047e99..2144df0 100644 --- a/include/data_source.h +++ b/include/data_source.h @@ -1,16 +1,31 @@ #include "Wire.h" #include "Arduino.h" #include "HardwareSerial.h" +#include "vars.h" class DataSource { - + public: - float value; - + DataSource(); + DataSource(HardwareSerial, int); + DataSource(TwoWire); + DataSource(int, bool); + public: void update(); void test(); + void readSensor(); void postProcess(); int getValue(); + + public: + HardwareSerial* ser; + TwoWire* i2c; + + int pin; + int protocol; + int value; + + }; \ No newline at end of file diff --git a/include/data_source_analog.h b/include/data_source_analog.h deleted file mode 100644 index 252c6cf..0000000 --- a/include/data_source_analog.h +++ /dev/null @@ -1,11 +0,0 @@ -#include "data_source.h" - -class DataSourceAnalog : public DataSource{ - - public: - DataSourceAnalog(int); - - public: - int pin; - -}; \ No newline at end of file diff --git a/include/data_source_bno055.h b/include/data_source_bno055.h index 0e56991..dbb4ede 100644 --- a/include/data_source_bno055.h +++ b/include/data_source_bno055.h @@ -1,15 +1,15 @@ -#include "data_source_i2c.h" +#include "data_source.h" #include #include -class DataSourceBN0O55 : public DataSourceI2C{ +class DataSourceBNO055 : public DataSource{ public: - DataSourceBN0O55(); - void update(); - + DataSourceBNO055(); + void readSensor(); + void postProcess(); + public: - int value; Adafruit_BNO055 bno; - + }; \ No newline at end of file diff --git a/include/data_source_i2c.h b/include/data_source_i2c.h deleted file mode 100644 index f532983..0000000 --- a/include/data_source_i2c.h +++ /dev/null @@ -1,14 +0,0 @@ -#include "data_source.h" -//i2c_t3 - -class DataSourceI2C : public DataSource{ - - public: - DataSourceI2C(); - DataSourceI2C(TwoWire); - void update(); - - public: - TwoWire i2c; - -}; diff --git a/include/data_source_rxtx.h b/include/data_source_rxtx.h deleted file mode 100644 index 9fa8bd3..0000000 --- a/include/data_source_rxtx.h +++ /dev/null @@ -1,13 +0,0 @@ -#include "data_source.h" - -class DataSourceRXTX : public DataSource{ - - public: - DataSourceRXTX(HardwareSerial); - - void update(); - - public: - HardwareSerial ser; - -}; \ No newline at end of file diff --git a/include/vars.h b/include/vars.h new file mode 100644 index 0000000..c27f673 --- /dev/null +++ b/include/vars.h @@ -0,0 +1 @@ +#define DEBUG_PRINT Serial \ No newline at end of file diff --git a/src/data_source.cpp b/src/data_source.cpp index cb7591e..28f4418 100644 --- a/src/data_source.cpp +++ b/src/data_source.cpp @@ -1,5 +1,48 @@ #include "data_source.h" +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; +} + int DataSource::getValue(){ return value; +} + +void DataSource::update(){ + readSensor(); + postProcess(); +} + +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(value); } \ No newline at end of file diff --git a/src/data_source_bno055.cpp b/src/data_source_bno055.cpp index 98fccea..c9192ba 100644 --- a/src/data_source_bno055.cpp +++ b/src/data_source_bno055.cpp @@ -1,15 +1,15 @@ #include "data_source_bno055.h" -DataSourceBN0O55::DataSourceBN0O55(){ - bno.begin(bno.OPERATION_MODE_IMUPLUS); //Posizione impostata a P7 alle righe 105,107 di Adafruit_BNO55.cpp - bno.setExtCrystalUse(true); +DataSourceBNO055::DataSourceBNO055(){ + bno = Adafruit_BNO055(); + bno.begin(bno.OPERATION_MODE_IMUPLUS); //Posizione impostata a P7 alle righe 105,107 di Adafruit_BNO55.cpp + bno.setExtCrystalUse(true); } -void DataSourceBN0O55::update(){ - imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER); +void DataSourceBNO055::readSensor(){ + imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER); if (euler.x() != value) { value = euler.x(); } - return; } \ No newline at end of file diff --git a/src/data_source_i2c.cpp b/src/data_source_i2c.cpp deleted file mode 100644 index 33cc48e..0000000 --- a/src/data_source_i2c.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "data_source_i2c.h" - -void DataSourceI2C::update(){ - value = i2c.read(); -} \ No newline at end of file diff --git a/src/data_source_rxtx.cpp b/src/data_source_rxtx.cpp deleted file mode 100644 index 52daf56..0000000 --- a/src/data_source_rxtx.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "data_source_rxtx.h" - -void DataSourceRXTX::update(){ - while(ser.available() > 0) value = ser.read(); - - postProcess(); -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f70ec05..fae2d5e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,10 @@ #include +#include "vars.h" // #include //cyao c: void setup() { - Serial.begin(9600); + DEBUG_PRINT.begin(9600); } void loop() {