From d377bbc0a9e1c5d7bf81abb3c8d4db2d3e04314e Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Mon, 2 May 2016 10:56:15 +0200 Subject: [PATCH] Some Arduino refactoring and it builds now --- arduino/HalMultiSensor/HalConfiguration.h | 8 ++--- arduino/HalMultiSensor/HalDefinitions.h | 8 ++--- arduino/HalMultiSensor/HalInterfaces.h | 8 ++--- arduino/HalMultiSensor/HalMultiSensor.ino | 12 +++---- arduino/HalMultiSensor/Interrupt.cpp | 4 +-- arduino/HalMultiSensor/Interrupt.h | 1 - arduino/HalMultiSensor/ProtocolOregon.cpp | 36 ++++++++++--------- arduino/HalMultiSensor/ProtocolOregon.h | 13 +++++++ .../{HardwareBH1750.cpp => SensorBH1750.cpp} | 14 ++++---- .../{HardwareBH1750.h => SensorBH1750.h} | 8 ++--- .../{HardwareDHT11.cpp => SensorDHT11.cpp} | 20 +++++++++-- .../{HardwareDHT11.h => SensorDHT11.h} | 6 ++-- ...dwarePhotocell.cpp => SensorPhotocell.cpp} | 8 ++--- ...{HardwarePhotocell.h => SensorPhotocell.h} | 8 ++--- 14 files changed, 92 insertions(+), 62 deletions(-) rename arduino/HalMultiSensor/{HardwareBH1750.cpp => SensorBH1750.cpp} (86%) rename arduino/HalMultiSensor/{HardwareBH1750.h => SensorBH1750.h} (92%) rename arduino/HalMultiSensor/{HardwareDHT11.cpp => SensorDHT11.cpp} (87%) rename arduino/HalMultiSensor/{HardwareDHT11.h => SensorDHT11.h} (87%) rename arduino/HalMultiSensor/{HardwarePhotocell.cpp => SensorPhotocell.cpp} (55%) rename arduino/HalMultiSensor/{HardwarePhotocell.h => SensorPhotocell.h} (60%) diff --git a/arduino/HalMultiSensor/HalConfiguration.h b/arduino/HalMultiSensor/HalConfiguration.h index 09c03eb3..85715ad3 100755 --- a/arduino/HalMultiSensor/HalConfiguration.h +++ b/arduino/HalMultiSensor/HalConfiguration.h @@ -1,24 +1,22 @@ #ifndef HALCONFIGURATION_H #define HALCONFIGURATION_H -#include "HalDefinitions.h" - // POWER CONSUMPTION SENSOR #define POWERCON_ENABLED // comment out to disable sensor -#define POWERCON_HARDWARE HardwareBH1750() +#define POWERCON_SENSOR SensorBH1750() #define POWERCON_PROTOCOL ProtocolOregon(118) #define POWER_TIMER_MULTIPLIER 1 // poling in minutes // TEMPERATURE SENSOR #define TEMPERATURE_ENABLED // comment out to disable sensor -#define TEMPERATURE_HARDWARE HardwareDHT11(5) +#define TEMPERATURE_SENSOR SensorDHT11(5) #define TEMPERATURE_PROTOCOL ProtocolOregon(100) #define TEMPERATURE_TIMER_MULTIPLIER 1 // poling in minutes // LIGHT SENSOR //#define LIGHT_ENABLED // comment out to disable sensor -#define LIGHT_HARDWARE HardwareDH1750() +#define LIGHT_SENSOR SensorDH1750() #define LIGHT_PROTOCOL ? #define LIGHT_TIMER_MULTIPLIER 1 // poling in minutes diff --git a/arduino/HalMultiSensor/HalDefinitions.h b/arduino/HalMultiSensor/HalDefinitions.h index 54b7ef86..d1b7979f 100755 --- a/arduino/HalMultiSensor/HalDefinitions.h +++ b/arduino/HalMultiSensor/HalDefinitions.h @@ -1,10 +1,10 @@ #ifndef HALDEFINITIONS_H #define HALDEFINITIONS_H -/////// HARDWARE -#include "HardwareBH1750.h" -#include "HardwareDHT11.h" -#include "HardwarePhotocell.h" +/////// SENSORS +#include "SensorBH1750.h" +#include "SensorDHT11.h" +#include "SensorPhotocell.h" //////// PROTOCOLS #include "ProtocolNexa.h" diff --git a/arduino/HalMultiSensor/HalInterfaces.h b/arduino/HalMultiSensor/HalInterfaces.h index d0cc3073..c8aac447 100755 --- a/arduino/HalMultiSensor/HalInterfaces.h +++ b/arduino/HalMultiSensor/HalInterfaces.h @@ -2,13 +2,13 @@ #define HALINTERFACES_H -class Hardware +class Sensor { public: virtual void setup() = 0; }; -class HardwarePowerConsumption : public Hardware +class SensorPowerConsumption : public Sensor { public: // returns number of pulses from power meter @@ -16,14 +16,14 @@ public: virtual void reset() = 0; }; -class HardwareTemperature : public Hardware +class SensorTemperature : public Sensor { public: virtual int getTemperature() = 0; virtual int getHumidity() = 0; }; -class HardwareLight : public Hardware +class SensorLight : public Sensor { public: virtual void setup() = 0; diff --git a/arduino/HalMultiSensor/HalMultiSensor.ino b/arduino/HalMultiSensor/HalMultiSensor.ino index 4f2a1f30..f458ea74 100755 --- a/arduino/HalMultiSensor/HalMultiSensor.ino +++ b/arduino/HalMultiSensor/HalMultiSensor.ino @@ -21,9 +21,9 @@ unsigned int timerMultiplierMAX; unsigned int timerMultiplier = 0; // Sensors -HardwarePowerConsumption* powerSensor; -HardwareTemperature* tempSensor; -HardwareLight* lightSensor; +SensorPowerConsumption* powerSensor; +SensorTemperature* tempSensor; +SensorLight* lightSensor; // Protocols ProtocolPowerConsumption* powerProtocol; @@ -42,21 +42,21 @@ void setup() // Setup Sensors and protocols #ifdef POWERCON_ENABLED - powerSensor = new POWERCON_HARDWARE; + powerSensor = new POWERCON_SENSOR; powerSensor->setup(); powerProtocol = new POWERCON_PROTOCOL; powerProtocol->setup(); #endif #ifdef TEMPERATURE_ENABLED - tempSensor = new TEMPERATURE_HARDWARE; + tempSensor = new TEMPERATURE_SENSOR; tempSensor->setup(); tempProtocol = new TEMPERATURE_PROTOCOL; tempProtocol->setup(); #endif #ifdef LIGHT_ENABLED - lightSensor = new LIGHT_HARDWARE; + lightSensor = new LIGHT_SENSOR; lightSensor->setup(); lightProtocol = new LIGHT_PROTOCOL; lightProtocol->setup(); diff --git a/arduino/HalMultiSensor/Interrupt.cpp b/arduino/HalMultiSensor/Interrupt.cpp index 36942f7e..3000fdf5 100755 --- a/arduino/HalMultiSensor/Interrupt.cpp +++ b/arduino/HalMultiSensor/Interrupt.cpp @@ -5,7 +5,7 @@ -void pinInterrupt() // here the interrupt is handled after wakeup +void __Interrupt_pinInterrupt__() // the interrupt is handled here after wakeup { // execute code here after wake-up before returning to the loop() function // timers and code using timers (serial.print and more...) will not work here. @@ -49,7 +49,7 @@ void Interrupt::setupTimerInterrupt(unsigned int milliseconds) * * In all but the IDLE sleep modes only LOW can be used. */ - attachInterrupt(0,pinInterrupt, LOW); + attachInterrupt(0,__Interrupt_pinInterrupt__, LOW); /* power_adc_disable(); power_spi_disable(); diff --git a/arduino/HalMultiSensor/Interrupt.h b/arduino/HalMultiSensor/Interrupt.h index 51065cd9..f4aa9e6a 100755 --- a/arduino/HalMultiSensor/Interrupt.h +++ b/arduino/HalMultiSensor/Interrupt.h @@ -16,7 +16,6 @@ public: void setupTimerInterrupt(unsigned int milliseconds); private: InterruptFunction callback; - }; #endif // INTERRUPT_H \ No newline at end of file diff --git a/arduino/HalMultiSensor/ProtocolOregon.cpp b/arduino/HalMultiSensor/ProtocolOregon.cpp index 4b58131a..392e13b7 100755 --- a/arduino/HalMultiSensor/ProtocolOregon.cpp +++ b/arduino/HalMultiSensor/ProtocolOregon.cpp @@ -27,10 +27,12 @@ void ProtocolOregon::setConsumption(unsigned int cons) this->humidity = 0; } + + void ProtocolOregon::send() { byte buffer[9]; - setType(buffer, { 0x1A,0x2D }); //temperature/humidity sensor (THGR2228N) + setType(buffer, 0x1A,0x2D); //temperature/humidity sensor (THGR2228N) setChannel(buffer, 0x20); setId(buffer, address); //set id of the sensor, BB=187 setBatteryLevel(buffer, true); // false : low, true : high @@ -53,10 +55,10 @@ void ProtocolOregon::send() * \param data Oregon message * \param type Sensor type */ -inline void setType(byte *data, byte* type) +inline void ProtocolOregon::setType(byte *data, byte b1, byte b2) { - data[0] = type[0]; - data[1] = type[1]; + data[0] = b1; + data[1] = b2; } /** @@ -64,13 +66,13 @@ inline void setType(byte *data, byte* type) * \param data Oregon message * \param channel Sensor channel (0x10, 0x20, 0x30) */ -inline void setChannel(byte *data, byte channel) +inline void ProtocolOregon::setChannel(byte *data, byte channel) { data[2] = channel; } -inline void setId(byte *data, byte id) +inline void ProtocolOregon::setId(byte *data, byte id) { data[3] = id; } @@ -78,13 +80,13 @@ inline void setId(byte *data, byte id) /** * \param level false: low, true: high */ -inline void setBatteryLevel(byte *data, bool level) +inline void ProtocolOregon::setBatteryLevel(byte *data, bool level) { if(!level) data[4] = 0x0C; else data[4] = 0x00; } -inline void setTemperature(byte *data, float temp) +inline void ProtocolOregon::setTemperature(byte *data, float temp) { // Set temperature sign if(temp < 0) @@ -112,13 +114,13 @@ inline void setTemperature(byte *data, float temp) data[4] |= (tempFloat << 4); } -inline void setHumidity(byte* data, byte hum) +inline void ProtocolOregon::setHumidity(byte* data, byte hum) { data[7] = (hum/10); data[6] |= (hum - data[7]*10) << 4; } -inline void calculateAndSetChecksum(byte* data) +inline void ProtocolOregon::calculateAndSetChecksum(byte* data) { int sum = 0; for(byte i = 0; i<8;i++) @@ -141,7 +143,7 @@ inline void calculateAndSetChecksum(byte* data) * \ of the RF signal at the middle of a clock period. * \ Remember, the Oregon v2.1 protocol adds an inverted bit first */ -inline void sendZero(void) +inline void ProtocolOregon::sendZero(void) { RF_SEND_HIGH(); delayMicroseconds(RF_DELAY); @@ -157,7 +159,7 @@ inline void sendZero(void) * \ of the RF signal at the middle of a clock period. * \ Remember, the Oregon v2.1 protocol add an inverted bit first */ -inline void sendOne(void) +inline void ProtocolOregon::sendOne(void) { RF_SEND_LOW(); delayMicroseconds(RF_DELAY); @@ -177,7 +179,7 @@ inline void sendOne(void) * \param data Data to send * \param length size of data array */ -void sendData(byte *data, byte length) +void ProtocolOregon::sendData(byte *data, byte length) { for(byte i = 0; i < length; ++i) { @@ -196,15 +198,17 @@ void sendData(byte *data, byte length) * \brief Send an Oregon message * \param data The Oregon message */ -void rfSend(byte *data, byte size) +void ProtocolOregon::rfSend(byte *data, byte size) { // Send preamble - sendData({ 0xFF,0xFF }, 2); + byte preamble[] = { 0xFF,0xFF }; + sendData(preamble, 2); // Send sync nibble //sendQuarterLSB(0xA); // It is not use in this version since the sync nibble is include in the Oregon message to send. // Send data sendData(data, size); // Send postamble - sendData({ 0x00 }, 1); + byte postamble[] = { 0x00 }; + sendData(postamble, 1); } diff --git a/arduino/HalMultiSensor/ProtocolOregon.h b/arduino/HalMultiSensor/ProtocolOregon.h index 13bbac5f..76da986f 100755 --- a/arduino/HalMultiSensor/ProtocolOregon.h +++ b/arduino/HalMultiSensor/ProtocolOregon.h @@ -19,6 +19,19 @@ private: unsigned char address; float temperature; unsigned char humidity; + + void setType(byte *data, byte b1, byte b2); + void setChannel(byte *data, byte channel); + void setId(byte *data, byte id); + void setBatteryLevel(byte *data, bool level); + void setTemperature(byte *data, float temp); + void setHumidity(byte* data, byte hum); + void calculateAndSetChecksum(byte* data); + + void sendZero(void); + void sendOne(void); + void sendData(byte *data, byte length); + void rfSend(byte *data, byte size); }; #endif // PROTOCOLOREGON_H diff --git a/arduino/HalMultiSensor/HardwareBH1750.cpp b/arduino/HalMultiSensor/SensorBH1750.cpp similarity index 86% rename from arduino/HalMultiSensor/HardwareBH1750.cpp rename to arduino/HalMultiSensor/SensorBH1750.cpp index d7a301e3..9710334d 100755 --- a/arduino/HalMultiSensor/HardwareBH1750.cpp +++ b/arduino/HalMultiSensor/SensorBH1750.cpp @@ -11,17 +11,17 @@ based on Christopher Laws, March, 2013 code. */ -#include "HardwareBH1750.h" +#include "SensorBH1750.h" #include -void HardwareBH1750::setup() { +void SensorBH1750::setup() { Wire.begin(); configure(BH1750_CONTINUOUS_HIGH_RES_MODE); } -void HardwareBH1750::configure(uint8_t mode) { +void SensorBH1750::configure(uint8_t mode) { switch (mode) { case BH1750_CONTINUOUS_HIGH_RES_MODE: case BH1750_CONTINUOUS_HIGH_RES_MODE_2: @@ -42,16 +42,16 @@ void HardwareBH1750::configure(uint8_t mode) { } } -unsigned int HardwareBH1750::getConsumption() +unsigned int SensorBH1750::getConsumption() { return pulses; } -void HardwareBH1750::reset() +void SensorBH1750::reset() { pulses = 0; } -unsigned int HardwareBH1750::getLuminosity(void) { +unsigned int SensorBH1750::getLuminosity(void) { uint16_t level; Wire.beginTransmission(BH1750_I2CADDR); @@ -86,7 +86,7 @@ unsigned int HardwareBH1750::getLuminosity(void) { /*********************************************************************/ -void HardwareBH1750::write8(uint8_t d) { +void SensorBH1750::write8(uint8_t d) { Wire.beginTransmission(BH1750_I2CADDR); #if (ARDUINO >= 100) Wire.write(d); diff --git a/arduino/HalMultiSensor/HardwareBH1750.h b/arduino/HalMultiSensor/SensorBH1750.h similarity index 92% rename from arduino/HalMultiSensor/HardwareBH1750.h rename to arduino/HalMultiSensor/SensorBH1750.h index 56354ba5..5a6f91a2 100755 --- a/arduino/HalMultiSensor/HardwareBH1750.h +++ b/arduino/HalMultiSensor/SensorBH1750.h @@ -12,8 +12,8 @@ http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi- based on Christopher Laws, March, 2013 code. */ -#ifndef HARDWAREBH1750_H -#define HARDWAREBH1750_H +#ifndef SensorBH1750_H +#define SensorBH1750_H #include #include "HalInterfaces.h" @@ -54,7 +54,7 @@ based on Christopher Laws, March, 2013 code. #define BH1750_ONE_TIME_LOW_RES_MODE 0x23 -class HardwareBH1750 : public HardwarePowerConsumption, public HardwareLight{ +class SensorBH1750 : public SensorPowerConsumption, public SensorLight{ public: virtual void setup(); virtual unsigned int getLuminosity(); @@ -68,5 +68,5 @@ private: }; -#endif // HARDWAREBH1750_H +#endif // SensorBH1750_H diff --git a/arduino/HalMultiSensor/HardwareDHT11.cpp b/arduino/HalMultiSensor/SensorDHT11.cpp similarity index 87% rename from arduino/HalMultiSensor/HardwareDHT11.cpp rename to arduino/HalMultiSensor/SensorDHT11.cpp index f9b74e90..28cf7b0e 100755 --- a/arduino/HalMultiSensor/HardwareDHT11.cpp +++ b/arduino/HalMultiSensor/SensorDHT11.cpp @@ -15,13 +15,29 @@ // + added references // -#include "HardwareDHT11.h" +#include "SensorDHT11.h" + + +SensorDHT11::SensorDHT11(int pin) +{ + this->pin = pin; +} + +void SensorDHT11::setup(){} +int SensorDHT11::getTemperature() +{ + return temperature; +} +int SensorDHT11::getHumidity() +{ + return humidity; +} // returnvalues: // 0 : OK // -1 : checksum error // -2 : timeout -int HardwareDHT11::read() +int SensorDHT11::read() { // BUFFER TO RECEIVE uint8_t bits[5]; diff --git a/arduino/HalMultiSensor/HardwareDHT11.h b/arduino/HalMultiSensor/SensorDHT11.h similarity index 87% rename from arduino/HalMultiSensor/HardwareDHT11.h rename to arduino/HalMultiSensor/SensorDHT11.h index 91e66f82..329332f8 100755 --- a/arduino/HalMultiSensor/HardwareDHT11.h +++ b/arduino/HalMultiSensor/SensorDHT11.h @@ -20,11 +20,11 @@ #define DHT11LIB_VERSION "0.3.2" -class HardwareDHT11 : public HardwareTemperature +class SensorDHT11 : public SensorTemperature { public: - HardwareDHT11(int pin); - virtual void setup(){}; + SensorDHT11(int pin); + virtual void setup(); virtual int getTemperature(); virtual int getHumidity(); int read(); diff --git a/arduino/HalMultiSensor/HardwarePhotocell.cpp b/arduino/HalMultiSensor/SensorPhotocell.cpp similarity index 55% rename from arduino/HalMultiSensor/HardwarePhotocell.cpp rename to arduino/HalMultiSensor/SensorPhotocell.cpp index 937c83ca..d535168e 100755 --- a/arduino/HalMultiSensor/HardwarePhotocell.cpp +++ b/arduino/HalMultiSensor/SensorPhotocell.cpp @@ -1,4 +1,4 @@ -#include "HardwarePhotocell.h" +#include "SensorPhotocell.h" void pinInterrupt() @@ -7,18 +7,18 @@ void pinInterrupt() } -void HardwarePhotocell::setup() +void SensorPhotocell::setup() { interrupt = new Interrupt(pinInterrupt); interrupt->setupPinInterrupt(PC2); //PC3 } -unsigned int HardwarePhotocell::getConsumption() +unsigned int SensorPhotocell::getConsumption() { return pulse; } -void HardwarePhotocell::reset() +void SensorPhotocell::reset() { pulse = 0; } \ No newline at end of file diff --git a/arduino/HalMultiSensor/HardwarePhotocell.h b/arduino/HalMultiSensor/SensorPhotocell.h similarity index 60% rename from arduino/HalMultiSensor/HardwarePhotocell.h rename to arduino/HalMultiSensor/SensorPhotocell.h index 77245452..9c6d6079 100755 --- a/arduino/HalMultiSensor/HardwarePhotocell.h +++ b/arduino/HalMultiSensor/SensorPhotocell.h @@ -1,11 +1,11 @@ -#ifndef HARDWAREPHOTOCELL_H -#define HARDWAREPHOTOCELL_H +#ifndef SensorPhotocell_H +#define SensorPhotocell_H #include "HalInterfaces.h" #include "Interrupt.h" -class HardwarePhotocell : public HardwarePowerConsumption +class SensorPhotocell : public SensorPowerConsumption { public: virtual void setup(); @@ -17,4 +17,4 @@ private: unsigned int pulse; }; -#endif // HARDWAREPHOTOCELL_H +#endif // SensorPhotocell_H