From 9547cf935b4d86d00414805310fbeaa15f547e52 Mon Sep 17 00:00:00 2001 From: u-siri-ous Date: Mon, 18 Nov 2019 15:07:46 +0100 Subject: [PATCH] Using enums instead of ints for transmission protocols --- include/data_source.h | 11 ++++++----- include/data_source_ball.h | 2 +- src/data_source.cpp | 25 ++++++++++++------------- src/data_source_ball.cpp | 2 +- src/data_source_bno055.cpp | 2 +- src/main.cpp | 3 ++- src/sensors.cpp | 2 +- 7 files changed, 24 insertions(+), 23 deletions(-) diff --git a/include/data_source.h b/include/data_source.h index f5596b8..6952b68 100644 --- a/include/data_source.h +++ b/include/data_source.h @@ -9,8 +9,8 @@ class DataSource { public: DataSource(); - DataSource(usb_serial_class, int); - DataSource(TwoWire); + DataSource(HardwareSerial*, int); + DataSource(TwoWire*); DataSource(int, bool); public: @@ -23,17 +23,18 @@ class DataSource { public: enum Protocols { + P_NULL, P_I2C, P_RXTX, P_APIN, - P_PIND + P_DPIN }; - usb_serial_class* ser; + HardwareSerial* ser; TwoWire* i2c; + Protocols protocol; int pin; - int protocol; int value; diff --git a/include/data_source_ball.h b/include/data_source_ball.h index 857df3e..c6b7886 100644 --- a/include/data_source_ball.h +++ b/include/data_source_ball.h @@ -3,7 +3,7 @@ class DataSourceBall : public DataSource{ public: - DataSourceBall(usb_serial_class ser, int baud); + DataSourceBall(HardwareSerial* ser, int baud); void postProcess() override; void test() override; int angle,distance; diff --git a/src/data_source.cpp b/src/data_source.cpp index 9324adf..56535c9 100644 --- a/src/data_source.cpp +++ b/src/data_source.cpp @@ -1,29 +1,28 @@ #include "data_source.h" DataSource::DataSource(void){ - protocol = 0; + protocol = P_NULL; } -DataSource::DataSource(TwoWire i2c_){ - - this->i2c = &(i2c_); - protocol = 1; - +DataSource::DataSource(TwoWire* i2c_){ + protocol = P_I2C; + this->i2c = i2c_; i2c->begin(); - } -DataSource::DataSource(usb_serial_class ser_, int baud){ - this->ser = &(ser_); - protocol = 2; - +DataSource::DataSource(HardwareSerial* ser_, int baud){ + protocol = P_RXTX; + this->ser = ser_; ser->begin(baud); } DataSource::DataSource(int pin_, bool analog){ this->pin = pin_; - if(analog) protocol = 3; - else protocol = 4; + if(analog) protocol = P_APIN; + else { + protocol = P_DPIN; + digitalWrite(pin, OUTPUT); + } } int DataSource::getValue(){ diff --git a/src/data_source_ball.cpp b/src/data_source_ball.cpp index 6434c51..bf4cd39 100644 --- a/src/data_source_ball.cpp +++ b/src/data_source_ball.cpp @@ -1,7 +1,7 @@ #include "data_source_ball.h" #include "vars.h" -DataSourceBall::DataSourceBall(usb_serial_class ser_, int baud) : DataSource(ser_, baud) { +DataSourceBall::DataSourceBall(HardwareSerial* ser_, int baud) : DataSource(ser_, baud) { } void DataSourceBall :: postProcess(){ diff --git a/src/data_source_bno055.cpp b/src/data_source_bno055.cpp index 9d37b2a..aff1970 100644 --- a/src/data_source_bno055.cpp +++ b/src/data_source_bno055.cpp @@ -3,7 +3,7 @@ //bool loaded = false; DataSourceBNO055::DataSourceBNO055(){ - // protocol = Protocols.P_I2C; + protocol = P_I2C; bno = Adafruit_BNO055(); bno.begin(bno.OPERATION_MODE_IMUPLUS); //Posizione impostata a P7 alle righe 105,107 di Adafruit_BNO55.cpp diff --git a/src/main.cpp b/src/main.cpp index 0ef2899..bec2e0b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,5 +17,6 @@ void loop() { updateSensors(); //should recenter using predefined values // drive->drive(0, 0, 0); - ball->test(); + compass->test(); + delay(100); } \ No newline at end of file diff --git a/src/sensors.cpp b/src/sensors.cpp index 29efefb..fa0c6f8 100644 --- a/src/sensors.cpp +++ b/src/sensors.cpp @@ -4,7 +4,7 @@ void initSensors(){ compass = new DataSourceBNO055(); drive = new DriveController(new Motor(12, 11, 2, 45),new Motor(25, 24, 5, 135), new Motor(27, 26, 6, 225), new Motor(21, 22, 23, 315)); - ball = new DataSourceBall(Serial, 57600); + ball = new DataSourceBall(&Serial4, 57600); } void updateSensors(){