diff --git a/BMP280.h b/BMP280.h index f6a2949..4722f4b 100644 --- a/BMP280.h +++ b/BMP280.h @@ -3,7 +3,7 @@ #include "softspim.h" -#define PRECOMP_COMPENSATION +//#define PRECOMP_COMPENSATION class BMP280 { diff --git a/W433DataTransmitter.cpp b/W433DataTransmitter.cpp index ececa05..1065d75 100644 --- a/W433DataTransmitter.cpp +++ b/W433DataTransmitter.cpp @@ -1,7 +1,7 @@ #include "W433DataTransmitter.h" #include "writepin.h" -W433DataTransmitter::W433DataTransmitter(volatile unsigned char *port, unsigned char pin): _port(port), _pin(pin) +W433DataTransmitter::W433DataTransmitter(volatile unsigned char *port, const unsigned char pin): _port(port), _pin(pin) { } @@ -10,7 +10,7 @@ void W433DataTransmitter::sendBit(const bool bit) switch(bit) { case true: - writePin(_port,_pin,true); + writePin(_port,_pin,true); _delay_us(SMALL_TIME); writePin(_port,_pin,false); _delay_us(LARGE_TIME); diff --git a/W433DataTransmitter.h b/W433DataTransmitter.h index 8d3304e..a6ed52c 100644 --- a/W433DataTransmitter.h +++ b/W433DataTransmitter.h @@ -14,7 +14,7 @@ private: static constexpr uint8_t signature = 0xA5; volatile unsigned char * const _port; - unsigned char _pin; + const unsigned char _pin; void sendBit(const bool bit); void sendSyncpulse(); @@ -23,9 +23,8 @@ private: public: - W433DataTransmitter(volatile unsigned char * const port, unsigned char pin); + W433DataTransmitter(volatile unsigned char * const port, const unsigned char pin); void send(const uint8_t* const data, uint16_t length); void send(const uint8_t data); void sendPacket(const uint32_t data); - }; diff --git a/dht11.cpp b/dht11.cpp index f1c2dc3..d8920ef 100644 --- a/dht11.cpp +++ b/dht11.cpp @@ -1,6 +1,6 @@ #include "dht11.h" -Dht11::Dht11(volatile unsigned char *port, volatile unsigned char *inPort, volatile unsigned char *port_ctl, const unsigned char pin): _port(port), _inPort(inPort), _port_ctl(port_ctl), _pin(pin) +Dht11::Dht11(volatile unsigned char * const port, volatile unsigned char * const inPort, volatile unsigned char * const port_ctl, const unsigned char pin): _port(port), _inPort(inPort), _port_ctl(port_ctl), _pin(pin) {} uint8_t Dht11::read() @@ -53,9 +53,7 @@ uint8_t Dht11::read() } else cnt--; } - - // WRITE TO RIGHT VARS - // as bits[1] and bits[3] are allways zero they are omitted in formulas. + uint8_t sum; if constexpr(DHT22) sum= bits[0] + bits[1] + bits[2] + bits[3]; else sum = bits[1] + bits[3]; diff --git a/dht11.h b/dht11.h index fbd0973..b1cb7e4 100644 --- a/dht11.h +++ b/dht11.h @@ -16,7 +16,7 @@ class Dht11 const unsigned char _pin; public: - Dht11(volatile unsigned char *port, volatile unsigned char *inPort, volatile unsigned char *port_ctl, const unsigned char pin); + Dht11(volatile unsigned char * const port, volatile unsigned char * const inPort, volatile unsigned char * const port_ctl, const unsigned char pin); uint8_t read(); int16_t humidity = 0; uint16_t temperature = 0; diff --git a/main.cpp b/main.cpp index 179c685..b7b6959 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,7 @@ #include "watchdog.h" #include "softspim.h" #include "BMP280.h" +#include "comperator.h" #define TEMP_SENSOR_DATA PB4 #define TRANSMITTER_DATA PD0 @@ -15,10 +16,12 @@ #define POWER_PIN PD5 #define LED_PIN PD1 -EMPTY_INTERRUPT( WDT_OVERFLOW_vect); +EMPTY_INTERRUPT(WDT_OVERFLOW_vect); -static constexpr uint8_t id = 1; +static constexpr uint8_t id = 3; static constexpr bool WATCH_DOOR = false; +static constexpr bool BMP_280 = false; +static constexpr bool DHT22 = true; static void power(const bool power) @@ -27,16 +30,25 @@ static void power(const bool power) { PORTD |= 1 << POWER_PIN | 1 << LED_PIN; DDRD = 1 << POWER_PIN | 1 << LED_PIN | 1 << TRANSMITTER_DATA; - DDRB = 1 << PB0 | 1 << PB1 | 1 << PB3; + DDRB = 1 << PB0 | 1 << PB3; } else { PORTD &= ~(1 << POWER_PIN | 1 << LED_PIN | 1 << TRANSMITTER_DATA); - //DDRD &= ~(1 << TRANSMITTER_DATA); DDRB = 0; } } +void debugBlink(bool fast = true) +{ + _delay_ms(100); + if(!fast) _delay_ms(200); + PORTD &= ~(1 << LED_PIN); + _delay_ms(100); + if(!fast) _delay_ms(200); + PORTD |= 1 << LED_PIN; +} + int main() { @@ -49,53 +61,88 @@ int main() //Transmitter and transmiter power W433DataTransmitter transmiter(&PORTD, TRANSMITTER_DATA); + Comperator comp; + sei(); wdt_set(WDTO_8S); set_sleep_mode(SLEEP_MODE_PWR_DOWN); - uint8_t couter = 50; + uint8_t couter = 200; while(true) { - if(++couter > 1) + if(++couter > 80) { couter = 0; power(true); - _delay_ms(2000); + comp.on(); + for(uint8_t i = 0; i < 20; ++i) + { + debugBlink(); + } + PORTD &= ~(1 << LED_PIN); + + uint32_t paketUint; + uint8_t * const paket = reinterpret_cast(&paketUint); + paket[1] = id; + //temperature sensor - tempSensor.read(); - uint8_t paket[4]; - paket[0] = 1; - paket[1] = id; - paket[2] = tempSensor.temperature >> 8; - paket[3] = tempSensor.temperature; - transmiter.send(paket, 4); + if constexpr (DHT22) + { + debugBlink(false); + uint8_t readret; + readret = tempSensor.read(); + paket[0] = 1; + paket[2] = tempSensor.temperature >> 8; + paket[3] = tempSensor.temperature; + transmiter.sendPacket(paketUint); + } //humidity sensor - paket[0] = 2; - paket[2] = tempSensor.humidity >> 8; - paket[3] = tempSensor.humidity; - transmiter.send(paket, 4); + if constexpr (DHT22) + { + debugBlink(false); + paket[0] = 2; + paket[2] = tempSensor.humidity >> 8; + paket[3] = tempSensor.humidity; + transmiter.sendPacket(paketUint); + } + + //low battery waring + if(comp.compare()) + { + debugBlink(false); + paket[0] = 128; + paket[2] = 0; + paket[3] = 1; + transmiter.sendPacket(paketUint); + } //presure sensor - uint16_t pressure = pressSensor.getPressure(); - paket[0] = 3; - paket[2] = pressure >> 8; - paket[3] = pressure; - transmiter.send(paket, 4); + if constexpr(BMP_280) + { + debugBlink(false); + uint16_t pressure = pressSensor.getPressure(); + paket[0] = 3; + paket[2] = pressure >> 8; + paket[3] = pressure; + transmiter.sendPacket(paketUint); + } //door if constexpr(WATCH_DOOR) { + debugBlink(false); paket[0] = 0; paket[1] = 2; paket[2] = 0; paket[3] = readPin(&PIND, DOOR_PIN); - transmiter.send(paket, 4); + transmiter.sendPacket(paketUint); } + comp.off(); power(false); } sleep_mode(); diff --git a/softspim.h b/softspim.h index bb31f1c..65567b3 100644 --- a/softspim.h +++ b/softspim.h @@ -10,8 +10,8 @@ private: static constexpr uint8_t CLOCK_PHASE = 1; static constexpr uint8_t BIT_ORDER = 1; - volatile uint8_t *_port = &PORTB; - volatile uint8_t *_pinReg = &PINB; + volatile uint8_t * const _port = &PORTB; + volatile uint8_t * const _pinReg = &PINB; static constexpr uint8_t _pinIn = PB2; static constexpr uint8_t _pinOut = PB0; static constexpr uint8_t _pinClock = PB3; diff --git a/watchdog.h b/watchdog.h index de49320..c683aa0 100644 --- a/watchdog.h +++ b/watchdog.h @@ -9,7 +9,7 @@ "sts %0,%1" "\n\t" \ "out __SREG__,__tmp_reg__" "\n\t" \ "sts %0,%2" \ - : /* no outputs */ \ + : \ : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ @@ -25,7 +25,7 @@ "wdr" "\n\t" \ "sts %0,%1" "\n\t" \ "sts %0,%2" \ - : /* no outputs */ \ + : \ : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ "r" ((uint8_t) (0x00)) \ diff --git a/writepin.cpp b/writepin.cpp index 744a47d..2caaf88 100644 --- a/writepin.cpp +++ b/writepin.cpp @@ -1,19 +1,20 @@ #include "writepin.h" -void writePin(volatile unsigned char *port, const unsigned char pin, const bool state) //waste 2 cycles +void writePin(volatile unsigned char * const port, const unsigned char pin, const bool state) { - if(!state) *port &= ~(1 << pin); - else *port |= (1 << pin); + *port = (*port & ~(1 << pin)) | (1 << pin)*state; + //if(!state) *port &= ~(1 << pin); + //else *port |= (1 << pin); } -void setBit( volatile unsigned char *reg, const unsigned char bit, bool value ) +void setBit( volatile unsigned char * const reg, const unsigned char bit, const bool value ) { writePin(reg, bit, value); } -void setDirection( volatile unsigned char *portDirReg, const unsigned char pin, bool makeOutput ) +void setDirection( volatile unsigned char * const portDirReg, const unsigned char pin, const bool makeOutput ) { writePin(portDirReg, pin, makeOutput); } -bool readPin( volatile unsigned char *inPort, const unsigned char pin){ return (bool) (*inPort & (1 << pin));} +bool readPin( volatile const unsigned char * const inPort, const unsigned char pin){ return (bool) (*inPort & (1 << pin));} diff --git a/writepin.h b/writepin.h index 616d4c5..0cf8943 100644 --- a/writepin.h +++ b/writepin.h @@ -2,13 +2,12 @@ #define WRITEPIN_H #include +void writePin(volatile unsigned char * const port, const unsigned char pin, const bool state); -void writePin(volatile unsigned char *port, const unsigned char pin, const bool state); +void setBit( volatile unsigned char * const reg, const unsigned char bit, const bool value ); -void setBit( volatile unsigned char *reg, const unsigned char bit, bool value ); +void setDirection( volatile unsigned char * const portDirReg, const unsigned char pin, const bool makeOutput ); -void setDirection( volatile unsigned char *portDirReg, const unsigned char pin, bool makeOutput ); - -bool readPin( volatile unsigned char *inPort, const unsigned char pin); +bool readPin( volatile const unsigned char * const inPort, const unsigned char pin); #endif