Some Arduino refactoring and it builds now

This commit is contained in:
Ziver Koc 2016-05-02 10:56:15 +02:00
parent 606e767f34
commit d377bbc0a9
14 changed files with 92 additions and 62 deletions

View file

@ -1,24 +1,22 @@
#ifndef HALCONFIGURATION_H #ifndef HALCONFIGURATION_H
#define HALCONFIGURATION_H #define HALCONFIGURATION_H
#include "HalDefinitions.h"
// POWER CONSUMPTION SENSOR // POWER CONSUMPTION SENSOR
#define POWERCON_ENABLED // comment out to disable sensor #define POWERCON_ENABLED // comment out to disable sensor
#define POWERCON_HARDWARE HardwareBH1750() #define POWERCON_SENSOR SensorBH1750()
#define POWERCON_PROTOCOL ProtocolOregon(118) #define POWERCON_PROTOCOL ProtocolOregon(118)
#define POWER_TIMER_MULTIPLIER 1 // poling in minutes #define POWER_TIMER_MULTIPLIER 1 // poling in minutes
// TEMPERATURE SENSOR // TEMPERATURE SENSOR
#define TEMPERATURE_ENABLED // comment out to disable 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_PROTOCOL ProtocolOregon(100)
#define TEMPERATURE_TIMER_MULTIPLIER 1 // poling in minutes #define TEMPERATURE_TIMER_MULTIPLIER 1 // poling in minutes
// LIGHT SENSOR // LIGHT SENSOR
//#define LIGHT_ENABLED // comment out to disable sensor //#define LIGHT_ENABLED // comment out to disable sensor
#define LIGHT_HARDWARE HardwareDH1750() #define LIGHT_SENSOR SensorDH1750()
#define LIGHT_PROTOCOL ? #define LIGHT_PROTOCOL ?
#define LIGHT_TIMER_MULTIPLIER 1 // poling in minutes #define LIGHT_TIMER_MULTIPLIER 1 // poling in minutes

View file

@ -1,10 +1,10 @@
#ifndef HALDEFINITIONS_H #ifndef HALDEFINITIONS_H
#define HALDEFINITIONS_H #define HALDEFINITIONS_H
/////// HARDWARE /////// SENSORS
#include "HardwareBH1750.h" #include "SensorBH1750.h"
#include "HardwareDHT11.h" #include "SensorDHT11.h"
#include "HardwarePhotocell.h" #include "SensorPhotocell.h"
//////// PROTOCOLS //////// PROTOCOLS
#include "ProtocolNexa.h" #include "ProtocolNexa.h"

View file

@ -2,13 +2,13 @@
#define HALINTERFACES_H #define HALINTERFACES_H
class Hardware class Sensor
{ {
public: public:
virtual void setup() = 0; virtual void setup() = 0;
}; };
class HardwarePowerConsumption : public Hardware class SensorPowerConsumption : public Sensor
{ {
public: public:
// returns number of pulses from power meter // returns number of pulses from power meter
@ -16,14 +16,14 @@ public:
virtual void reset() = 0; virtual void reset() = 0;
}; };
class HardwareTemperature : public Hardware class SensorTemperature : public Sensor
{ {
public: public:
virtual int getTemperature() = 0; virtual int getTemperature() = 0;
virtual int getHumidity() = 0; virtual int getHumidity() = 0;
}; };
class HardwareLight : public Hardware class SensorLight : public Sensor
{ {
public: public:
virtual void setup() = 0; virtual void setup() = 0;

View file

@ -21,9 +21,9 @@ unsigned int timerMultiplierMAX;
unsigned int timerMultiplier = 0; unsigned int timerMultiplier = 0;
// Sensors // Sensors
HardwarePowerConsumption* powerSensor; SensorPowerConsumption* powerSensor;
HardwareTemperature* tempSensor; SensorTemperature* tempSensor;
HardwareLight* lightSensor; SensorLight* lightSensor;
// Protocols // Protocols
ProtocolPowerConsumption* powerProtocol; ProtocolPowerConsumption* powerProtocol;
@ -42,21 +42,21 @@ void setup()
// Setup Sensors and protocols // Setup Sensors and protocols
#ifdef POWERCON_ENABLED #ifdef POWERCON_ENABLED
powerSensor = new POWERCON_HARDWARE; powerSensor = new POWERCON_SENSOR;
powerSensor->setup(); powerSensor->setup();
powerProtocol = new POWERCON_PROTOCOL; powerProtocol = new POWERCON_PROTOCOL;
powerProtocol->setup(); powerProtocol->setup();
#endif #endif
#ifdef TEMPERATURE_ENABLED #ifdef TEMPERATURE_ENABLED
tempSensor = new TEMPERATURE_HARDWARE; tempSensor = new TEMPERATURE_SENSOR;
tempSensor->setup(); tempSensor->setup();
tempProtocol = new TEMPERATURE_PROTOCOL; tempProtocol = new TEMPERATURE_PROTOCOL;
tempProtocol->setup(); tempProtocol->setup();
#endif #endif
#ifdef LIGHT_ENABLED #ifdef LIGHT_ENABLED
lightSensor = new LIGHT_HARDWARE; lightSensor = new LIGHT_SENSOR;
lightSensor->setup(); lightSensor->setup();
lightProtocol = new LIGHT_PROTOCOL; lightProtocol = new LIGHT_PROTOCOL;
lightProtocol->setup(); lightProtocol->setup();

View file

@ -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 // 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. // 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. * 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_adc_disable();
power_spi_disable(); power_spi_disable();

View file

@ -16,7 +16,6 @@ public:
void setupTimerInterrupt(unsigned int milliseconds); void setupTimerInterrupt(unsigned int milliseconds);
private: private:
InterruptFunction callback; InterruptFunction callback;
}; };
#endif // INTERRUPT_H #endif // INTERRUPT_H

View file

@ -27,10 +27,12 @@ void ProtocolOregon::setConsumption(unsigned int cons)
this->humidity = 0; this->humidity = 0;
} }
void ProtocolOregon::send() void ProtocolOregon::send()
{ {
byte buffer[9]; byte buffer[9];
setType(buffer, { 0x1A,0x2D }); //temperature/humidity sensor (THGR2228N) setType(buffer, 0x1A,0x2D); //temperature/humidity sensor (THGR2228N)
setChannel(buffer, 0x20); setChannel(buffer, 0x20);
setId(buffer, address); //set id of the sensor, BB=187 setId(buffer, address); //set id of the sensor, BB=187
setBatteryLevel(buffer, true); // false : low, true : high setBatteryLevel(buffer, true); // false : low, true : high
@ -53,10 +55,10 @@ void ProtocolOregon::send()
* \param data Oregon message * \param data Oregon message
* \param type Sensor type * \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[0] = b1;
data[1] = type[1]; data[1] = b2;
} }
/** /**
@ -64,13 +66,13 @@ inline void setType(byte *data, byte* type)
* \param data Oregon message * \param data Oregon message
* \param channel Sensor channel (0x10, 0x20, 0x30) * \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; data[2] = channel;
} }
inline void setId(byte *data, byte id) inline void ProtocolOregon::setId(byte *data, byte id)
{ {
data[3] = id; data[3] = id;
} }
@ -78,13 +80,13 @@ inline void setId(byte *data, byte id)
/** /**
* \param level false: low, true: high * \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; if(!level) data[4] = 0x0C;
else data[4] = 0x00; else data[4] = 0x00;
} }
inline void setTemperature(byte *data, float temp) inline void ProtocolOregon::setTemperature(byte *data, float temp)
{ {
// Set temperature sign // Set temperature sign
if(temp < 0) if(temp < 0)
@ -112,13 +114,13 @@ inline void setTemperature(byte *data, float temp)
data[4] |= (tempFloat << 4); data[4] |= (tempFloat << 4);
} }
inline void setHumidity(byte* data, byte hum) inline void ProtocolOregon::setHumidity(byte* data, byte hum)
{ {
data[7] = (hum/10); data[7] = (hum/10);
data[6] |= (hum - data[7]*10) << 4; data[6] |= (hum - data[7]*10) << 4;
} }
inline void calculateAndSetChecksum(byte* data) inline void ProtocolOregon::calculateAndSetChecksum(byte* data)
{ {
int sum = 0; int sum = 0;
for(byte i = 0; i<8;i++) 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. * \ of the RF signal at the middle of a clock period.
* \ Remember, the Oregon v2.1 protocol adds an inverted bit first * \ Remember, the Oregon v2.1 protocol adds an inverted bit first
*/ */
inline void sendZero(void) inline void ProtocolOregon::sendZero(void)
{ {
RF_SEND_HIGH(); RF_SEND_HIGH();
delayMicroseconds(RF_DELAY); delayMicroseconds(RF_DELAY);
@ -157,7 +159,7 @@ inline void sendZero(void)
* \ of the RF signal at the middle of a clock period. * \ of the RF signal at the middle of a clock period.
* \ Remember, the Oregon v2.1 protocol add an inverted bit first * \ Remember, the Oregon v2.1 protocol add an inverted bit first
*/ */
inline void sendOne(void) inline void ProtocolOregon::sendOne(void)
{ {
RF_SEND_LOW(); RF_SEND_LOW();
delayMicroseconds(RF_DELAY); delayMicroseconds(RF_DELAY);
@ -177,7 +179,7 @@ inline void sendOne(void)
* \param data Data to send * \param data Data to send
* \param length size of data array * \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) for(byte i = 0; i < length; ++i)
{ {
@ -196,15 +198,17 @@ void sendData(byte *data, byte length)
* \brief Send an Oregon message * \brief Send an Oregon message
* \param data The Oregon message * \param data The Oregon message
*/ */
void rfSend(byte *data, byte size) void ProtocolOregon::rfSend(byte *data, byte size)
{ {
// Send preamble // Send preamble
sendData({ 0xFF,0xFF }, 2); byte preamble[] = { 0xFF,0xFF };
sendData(preamble, 2);
// Send sync nibble // Send sync nibble
//sendQuarterLSB(0xA); // It is not use in this version since the sync nibble is include in the Oregon message to send. //sendQuarterLSB(0xA); // It is not use in this version since the sync nibble is include in the Oregon message to send.
// Send data // Send data
sendData(data, size); sendData(data, size);
// Send postamble // Send postamble
sendData({ 0x00 }, 1); byte postamble[] = { 0x00 };
sendData(postamble, 1);
} }

View file

@ -19,6 +19,19 @@ private:
unsigned char address; unsigned char address;
float temperature; float temperature;
unsigned char humidity; 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 #endif // PROTOCOLOREGON_H

View file

@ -11,17 +11,17 @@ based on Christopher Laws, March, 2013 code.
*/ */
#include "HardwareBH1750.h" #include "SensorBH1750.h"
#include <util/delay.h> #include <util/delay.h>
void HardwareBH1750::setup() { void SensorBH1750::setup() {
Wire.begin(); Wire.begin();
configure(BH1750_CONTINUOUS_HIGH_RES_MODE); configure(BH1750_CONTINUOUS_HIGH_RES_MODE);
} }
void HardwareBH1750::configure(uint8_t mode) { void SensorBH1750::configure(uint8_t mode) {
switch (mode) { switch (mode) {
case BH1750_CONTINUOUS_HIGH_RES_MODE: case BH1750_CONTINUOUS_HIGH_RES_MODE:
case BH1750_CONTINUOUS_HIGH_RES_MODE_2: 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; return pulses;
} }
void HardwareBH1750::reset() void SensorBH1750::reset()
{ {
pulses = 0; pulses = 0;
} }
unsigned int HardwareBH1750::getLuminosity(void) { unsigned int SensorBH1750::getLuminosity(void) {
uint16_t level; uint16_t level;
Wire.beginTransmission(BH1750_I2CADDR); 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); Wire.beginTransmission(BH1750_I2CADDR);
#if (ARDUINO >= 100) #if (ARDUINO >= 100)
Wire.write(d); Wire.write(d);

View file

@ -12,8 +12,8 @@ http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-
based on Christopher Laws, March, 2013 code. based on Christopher Laws, March, 2013 code.
*/ */
#ifndef HARDWAREBH1750_H #ifndef SensorBH1750_H
#define HARDWAREBH1750_H #define SensorBH1750_H
#include <Wire.h> #include <Wire.h>
#include "HalInterfaces.h" #include "HalInterfaces.h"
@ -54,7 +54,7 @@ based on Christopher Laws, March, 2013 code.
#define BH1750_ONE_TIME_LOW_RES_MODE 0x23 #define BH1750_ONE_TIME_LOW_RES_MODE 0x23
class HardwareBH1750 : public HardwarePowerConsumption, public HardwareLight{ class SensorBH1750 : public SensorPowerConsumption, public SensorLight{
public: public:
virtual void setup(); virtual void setup();
virtual unsigned int getLuminosity(); virtual unsigned int getLuminosity();
@ -68,5 +68,5 @@ private:
}; };
#endif // HARDWAREBH1750_H #endif // SensorBH1750_H

View file

@ -15,13 +15,29 @@
// + added references // + 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: // returnvalues:
// 0 : OK // 0 : OK
// -1 : checksum error // -1 : checksum error
// -2 : timeout // -2 : timeout
int HardwareDHT11::read() int SensorDHT11::read()
{ {
// BUFFER TO RECEIVE // BUFFER TO RECEIVE
uint8_t bits[5]; uint8_t bits[5];

View file

@ -20,11 +20,11 @@
#define DHT11LIB_VERSION "0.3.2" #define DHT11LIB_VERSION "0.3.2"
class HardwareDHT11 : public HardwareTemperature class SensorDHT11 : public SensorTemperature
{ {
public: public:
HardwareDHT11(int pin); SensorDHT11(int pin);
virtual void setup(){}; virtual void setup();
virtual int getTemperature(); virtual int getTemperature();
virtual int getHumidity(); virtual int getHumidity();
int read(); int read();

View file

@ -1,4 +1,4 @@
#include "HardwarePhotocell.h" #include "SensorPhotocell.h"
void pinInterrupt() void pinInterrupt()
@ -7,18 +7,18 @@ void pinInterrupt()
} }
void HardwarePhotocell::setup() void SensorPhotocell::setup()
{ {
interrupt = new Interrupt(pinInterrupt); interrupt = new Interrupt(pinInterrupt);
interrupt->setupPinInterrupt(PC2); //PC3 interrupt->setupPinInterrupt(PC2); //PC3
} }
unsigned int HardwarePhotocell::getConsumption() unsigned int SensorPhotocell::getConsumption()
{ {
return pulse; return pulse;
} }
void HardwarePhotocell::reset() void SensorPhotocell::reset()
{ {
pulse = 0; pulse = 0;
} }

View file

@ -1,11 +1,11 @@
#ifndef HARDWAREPHOTOCELL_H #ifndef SensorPhotocell_H
#define HARDWAREPHOTOCELL_H #define SensorPhotocell_H
#include "HalInterfaces.h" #include "HalInterfaces.h"
#include "Interrupt.h" #include "Interrupt.h"
class HardwarePhotocell : public HardwarePowerConsumption class SensorPhotocell : public SensorPowerConsumption
{ {
public: public:
virtual void setup(); virtual void setup();
@ -17,4 +17,4 @@ private:
unsigned int pulse; unsigned int pulse;
}; };
#endif // HARDWAREPHOTOCELL_H #endif // SensorPhotocell_H