saved some bytes

This commit is contained in:
2019-05-08 11:05:36 +02:00
parent 63ff73a066
commit a109991e7a
10 changed files with 94 additions and 50 deletions

View File

@ -3,7 +3,7 @@
#include "softspim.h" #include "softspim.h"
#define PRECOMP_COMPENSATION //#define PRECOMP_COMPENSATION
class BMP280 class BMP280
{ {

View File

@ -1,7 +1,7 @@
#include "W433DataTransmitter.h" #include "W433DataTransmitter.h"
#include "writepin.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)
{ {
} }

View File

@ -14,7 +14,7 @@ private:
static constexpr uint8_t signature = 0xA5; static constexpr uint8_t signature = 0xA5;
volatile unsigned char * const _port; volatile unsigned char * const _port;
unsigned char _pin; const unsigned char _pin;
void sendBit(const bool bit); void sendBit(const bool bit);
void sendSyncpulse(); void sendSyncpulse();
@ -23,9 +23,8 @@ private:
public: 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* const data, uint16_t length);
void send(const uint8_t data); void send(const uint8_t data);
void sendPacket(const uint32_t data); void sendPacket(const uint32_t data);
}; };

View File

@ -1,6 +1,6 @@
#include "dht11.h" #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() uint8_t Dht11::read()
@ -54,8 +54,6 @@ uint8_t Dht11::read()
else cnt--; else cnt--;
} }
// WRITE TO RIGHT VARS
// as bits[1] and bits[3] are allways zero they are omitted in formulas.
uint8_t sum; uint8_t sum;
if constexpr(DHT22) sum= bits[0] + bits[1] + bits[2] + bits[3]; if constexpr(DHT22) sum= bits[0] + bits[1] + bits[2] + bits[3];
else sum = bits[1] + bits[3]; else sum = bits[1] + bits[3];

View File

@ -16,7 +16,7 @@ class Dht11
const unsigned char _pin; const unsigned char _pin;
public: 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(); uint8_t read();
int16_t humidity = 0; int16_t humidity = 0;
uint16_t temperature = 0; uint16_t temperature = 0;

View File

@ -8,6 +8,7 @@
#include "watchdog.h" #include "watchdog.h"
#include "softspim.h" #include "softspim.h"
#include "BMP280.h" #include "BMP280.h"
#include "comperator.h"
#define TEMP_SENSOR_DATA PB4 #define TEMP_SENSOR_DATA PB4
#define TRANSMITTER_DATA PD0 #define TRANSMITTER_DATA PD0
@ -15,10 +16,12 @@
#define POWER_PIN PD5 #define POWER_PIN PD5
#define LED_PIN PD1 #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 WATCH_DOOR = false;
static constexpr bool BMP_280 = false;
static constexpr bool DHT22 = true;
static void power(const bool power) static void power(const bool power)
@ -27,16 +30,25 @@ static void power(const bool power)
{ {
PORTD |= 1 << POWER_PIN | 1 << LED_PIN; PORTD |= 1 << POWER_PIN | 1 << LED_PIN;
DDRD = 1 << POWER_PIN | 1 << LED_PIN | 1 << TRANSMITTER_DATA; DDRD = 1 << POWER_PIN | 1 << LED_PIN | 1 << TRANSMITTER_DATA;
DDRB = 1 << PB0 | 1 << PB1 | 1 << PB3; DDRB = 1 << PB0 | 1 << PB3;
} }
else else
{ {
PORTD &= ~(1 << POWER_PIN | 1 << LED_PIN | 1 << TRANSMITTER_DATA); PORTD &= ~(1 << POWER_PIN | 1 << LED_PIN | 1 << TRANSMITTER_DATA);
//DDRD &= ~(1 << TRANSMITTER_DATA);
DDRB = 0; 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() int main()
{ {
@ -49,53 +61,88 @@ int main()
//Transmitter and transmiter power //Transmitter and transmiter power
W433DataTransmitter transmiter(&PORTD, TRANSMITTER_DATA); W433DataTransmitter transmiter(&PORTD, TRANSMITTER_DATA);
Comperator comp;
sei(); sei();
wdt_set(WDTO_8S); wdt_set(WDTO_8S);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); set_sleep_mode(SLEEP_MODE_PWR_DOWN);
uint8_t couter = 50; uint8_t couter = 200;
while(true) while(true)
{ {
if(++couter > 1) if(++couter > 80)
{ {
couter = 0; couter = 0;
power(true); 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<uint8_t*>(&paketUint);
paket[1] = id;
//temperature sensor //temperature sensor
tempSensor.read(); if constexpr (DHT22)
uint8_t paket[4]; {
debugBlink(false);
uint8_t readret;
readret = tempSensor.read();
paket[0] = 1; paket[0] = 1;
paket[1] = id;
paket[2] = tempSensor.temperature >> 8; paket[2] = tempSensor.temperature >> 8;
paket[3] = tempSensor.temperature; paket[3] = tempSensor.temperature;
transmiter.send(paket, 4); transmiter.sendPacket(paketUint);
}
//humidity sensor //humidity sensor
if constexpr (DHT22)
{
debugBlink(false);
paket[0] = 2; paket[0] = 2;
paket[2] = tempSensor.humidity >> 8; paket[2] = tempSensor.humidity >> 8;
paket[3] = tempSensor.humidity; paket[3] = tempSensor.humidity;
transmiter.send(paket, 4); 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 //presure sensor
if constexpr(BMP_280)
{
debugBlink(false);
uint16_t pressure = pressSensor.getPressure(); uint16_t pressure = pressSensor.getPressure();
paket[0] = 3; paket[0] = 3;
paket[2] = pressure >> 8; paket[2] = pressure >> 8;
paket[3] = pressure; paket[3] = pressure;
transmiter.send(paket, 4); transmiter.sendPacket(paketUint);
}
//door //door
if constexpr(WATCH_DOOR) if constexpr(WATCH_DOOR)
{ {
debugBlink(false);
paket[0] = 0; paket[0] = 0;
paket[1] = 2; paket[1] = 2;
paket[2] = 0; paket[2] = 0;
paket[3] = readPin(&PIND, DOOR_PIN); paket[3] = readPin(&PIND, DOOR_PIN);
transmiter.send(paket, 4); transmiter.sendPacket(paketUint);
} }
comp.off();
power(false); power(false);
} }
sleep_mode(); sleep_mode();

View File

@ -10,8 +10,8 @@ private:
static constexpr uint8_t CLOCK_PHASE = 1; static constexpr uint8_t CLOCK_PHASE = 1;
static constexpr uint8_t BIT_ORDER = 1; static constexpr uint8_t BIT_ORDER = 1;
volatile uint8_t *_port = &PORTB; volatile uint8_t * const _port = &PORTB;
volatile uint8_t *_pinReg = &PINB; volatile uint8_t * const _pinReg = &PINB;
static constexpr uint8_t _pinIn = PB2; static constexpr uint8_t _pinIn = PB2;
static constexpr uint8_t _pinOut = PB0; static constexpr uint8_t _pinOut = PB0;
static constexpr uint8_t _pinClock = PB3; static constexpr uint8_t _pinClock = PB3;

View File

@ -9,7 +9,7 @@
"sts %0,%1" "\n\t" \ "sts %0,%1" "\n\t" \
"out __SREG__,__tmp_reg__" "\n\t" \ "out __SREG__,__tmp_reg__" "\n\t" \
"sts %0,%2" \ "sts %0,%2" \
: /* no outputs */ \ : \
: "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
"r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
"r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
@ -25,7 +25,7 @@
"wdr" "\n\t" \ "wdr" "\n\t" \
"sts %0,%1" "\n\t" \ "sts %0,%1" "\n\t" \
"sts %0,%2" \ "sts %0,%2" \
: /* no outputs */ \ : \
: "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
"r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
"r" ((uint8_t) (0x00)) \ "r" ((uint8_t) (0x00)) \

View File

@ -1,19 +1,20 @@
#include "writepin.h" #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); *port = (*port & ~(1 << pin)) | (1 << pin)*state;
else *port |= (1 << pin); //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); 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); 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));}

View File

@ -2,13 +2,12 @@
#define WRITEPIN_H #define WRITEPIN_H
#include <avr/io.h> #include <avr/io.h>
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 const unsigned char * const inPort, const unsigned char pin);
bool readPin( volatile unsigned char *inPort, const unsigned char pin);
#endif #endif