diff --git a/CL56.cpp b/CL56.cpp index 3e8ff8e..732ccc2 100644 --- a/CL56.cpp +++ b/CL56.cpp @@ -9,9 +9,14 @@ _shiftReg(shiftReg) void DualCl56::tick() { ++_currentLit; - if(_currentLit > 7)_currentLit = 0; - unsigned char bits[2] = {0b10000000>>_currentLit, ~(_segments[_currentLit])}; - _shiftReg->write(reinterpret_cast(&bits)); + if(_currentLit > 7) + _currentLit = 0; + + unsigned char bits[2] = {static_cast(0b10000000 >> _currentLit), + static_cast(~(_segments[_currentLit]))}; + + if(!_blank) + _shiftReg->write(reinterpret_cast(&bits)); } void DualCl56::setString(const char string[], const uint8_t dp) @@ -166,5 +171,17 @@ void DualCl56::setString(const char string[], const uint8_t dp) void DualCl56::setSegments(const uint8_t segments, const uint8_t place) { - _segments[place] = segments; + _segments[place] = segments; +} + +void DualCl56::setBlank(bool blankIn) +{ + _blank = blankIn; + if(_blank) + _shiftReg->clear(); +} + +bool DualCl56::getBlank() +{ + return _blank; } diff --git a/CL56.h b/CL56.h index 2a46660..3468050 100644 --- a/CL56.h +++ b/CL56.h @@ -6,15 +6,15 @@ class DualCl56 { public: - - static constexpr uint8_t SEG_A = 0b10000000; - static constexpr uint8_t SEG_B = 0b01000000; - static constexpr uint8_t SEG_C = 0b00100000; - static constexpr uint8_t SEG_D = 0b00010000; - static constexpr uint8_t SEG_E = 0b00001000; - static constexpr uint8_t SEG_F = 0b00000100; - static constexpr uint8_t SEG_G = 0b00000010; - static constexpr uint8_t SEG_DP= 0b00000001; + + static constexpr uint8_t SEG_A = 0b10000000; + static constexpr uint8_t SEG_B = 0b01000000; + static constexpr uint8_t SEG_C = 0b00100000; + static constexpr uint8_t SEG_D = 0b00010000; + static constexpr uint8_t SEG_E = 0b00001000; + static constexpr uint8_t SEG_F = 0b00000100; + static constexpr uint8_t SEG_G = 0b00000010; + static constexpr uint8_t SEG_DP= 0b00000001; static constexpr uint8_t COLEN_A = 0b00000010; static constexpr uint8_t COLEN_B = 0b00100000; @@ -71,14 +71,18 @@ class DualCl56 uint8_t _segments[8] = {SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_G, SEG_DP, FIVE}; ShiftReg<16>* _shiftReg; + + bool _blank = false; public: DualCl56(ShiftReg<16>* shiftReg); void tick(); + void setBlank(bool blank); + bool getBlank(); void setString(const char* string, const uint8_t dp = 0); - void setSegments(const uint8_t segments, const uint8_t place); + void setSegments(const uint8_t segments, const uint8_t place); }; diff --git a/WirelessRelay.cpp b/WirelessRelay.cpp new file mode 100644 index 0000000..547f69d --- /dev/null +++ b/WirelessRelay.cpp @@ -0,0 +1,85 @@ +#include"WirelessRelay.h" +#include +#include + +volatile unsigned char *_port = &PORTD; +unsigned char _pin = PD2; + +void WirelessRelay::sendId() +{ + writePin(_port,_pin,true); + _delay_us(SMALL_TIME); + writePin(_port,_pin,false); + _delay_us(LARGE_TIME); + + for(short i = 0; i<10; i++) + { + sendBit( id & 1 << (15 - i) ); + } +} + +void WirelessRelay::sendBit(const bool in) +{ + switch(in) + { + case true: + //Der Code fuer '0' + writePin(_port,_pin,true); + _delay_us(SMALL_TIME); + writePin(_port,_pin,false); + _delay_us(LARGE_TIME); + writePin(_port,_pin,true); + _delay_us(SMALL_TIME); + writePin(_port,_pin,false); + _delay_us(LARGE_TIME); + break; + + case false: + //Der Code fuer '1' + writePin(_port,_pin,true); + _delay_us(LARGE_TIME); + writePin(_port,_pin,false); + _delay_us(SMALL_TIME); + writePin(_port,_pin,true); + _delay_us(SMALL_TIME); + writePin(_port,_pin,false); + _delay_us(LARGE_TIME); + break; + } +} + +void WirelessRelay::sync() +{ + writePin(_port,_pin,false); + _delay_us(SMALL_TIME*31); +} + +void WirelessRelay::setValue(const uint8_t value) +{ + lastValue = value; + for(short z = 0; z<10; z++) + { + sendId(); + sendBit(value); + sendBit(!value); + sync(); + } +} + +void WirelessRelay::resend() +{ + setValue(lastValue); +} + +WirelessRelay::WirelessRelay(const uint16_t idIn, char nameIn[]) +{ + id = idIn; + setName(nameIn); + type = 0; +} + +WirelessRelay::WirelessRelay(const Item& item) +{ + Item::operator=(item); + type = 0; +} diff --git a/WirelessRelay.h b/WirelessRelay.h new file mode 100644 index 0000000..7306fd0 --- /dev/null +++ b/WirelessRelay.h @@ -0,0 +1,27 @@ +#ifndef RF433_H +#define RF433_H + +#include +#include"writepin.h" +#include "item.h" + +class WirelessRelay: public Item +{ +public: + static constexpr uint16_t LARGE_TIME = 750; + static constexpr uint8_t SMALL_TIME = 250; + + static constexpr uint16_t MAX_NAME_LENGTH = 16; + +private: + void sendBit(const bool i); + void sync(); + void sendId(); + +public: + WirelessRelay(const uint16_t idIn, char nameIn[]); + WirelessRelay(const Item& item); + void setValue(const uint8_t value); + void resend(); +}; +#endif diff --git a/dht11.cpp b/dht11.cpp new file mode 100644 index 0000000..e23b3a9 --- /dev/null +++ b/dht11.cpp @@ -0,0 +1,88 @@ +#include "dht11.h" + +Dht11::Dht11(volatile unsigned char * const port, volatile unsigned char * const inPort, volatile unsigned char * const port_ctl, const unsigned char pin): _port(port), _port_ctl(port_ctl), _inPort(inPort), _pin(pin) +{} + +uint8_t Dht11::read() +{ + // BUFFER TO RECEIVE + uint8_t bits[5]; + uint8_t cnt = 7; + uint8_t idx = 0; + + // EMPTY BUFFER + for (uint8_t i=0; i< 5; i++) bits[i] = 0; + + // REQUEST SAMPLE + setDirection(_port_ctl, _pin, true); + writePin(_port, _pin, false); + _delay_ms(18); + writePin(_port, _pin, true); + setDirection(_port_ctl, _pin, false); + _delay_us(42); + + // ACKNOWLEDGE or TIMEOUT + unsigned int loopCnt = LOOP_TIMEOUT_COUNT; + while(!readPin(_inPort, _pin)) if (loopCnt-- == 0) return 1; + + loopCnt = LOOP_TIMEOUT_COUNT; + while(readPin(_inPort, _pin) ) if (loopCnt-- == 0) return 2; + + PORTD |= 1 << PD1; + PORTD &= ~(1 << PD1); + PORTD |= 1 << PD1; + PORTD &= ~(1 << PD1); + + // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT + for (uint8_t i=0; i<40; i++) + { + loopCnt = LOOP_TIMEOUT_COUNT; + while(!readPin(_inPort, _pin)) + if (loopCnt-- == 0) + { + PORTD |= 1 << PD1; + return 3; + } + + loopCnt = 0; + while(readPin(_inPort, _pin)) + { + _delay_us(10); + if (loopCnt++ > LOOP_TIMEOUT_SMALL_COUNT) return 4; + } + + if( loopCnt > BIT_COUNT ) + { + PORTD |= 1 << PD1; + PORTD &= ~(1 << PD1); + bits[idx] |= (1 << cnt); + } + if (cnt == 0) // next byte? + { + cnt = 7; // restart at MSB + idx++; // next byte! + } + else cnt--; + } + + uint8_t sum; + if constexpr(DHT22) sum= bits[0] + bits[1] + bits[2] + bits[3]; + else sum = bits[0] + bits[2]; + + if((bits[4] == sum && sum != 0) || true) + { + if constexpr(DHT22) + { + humidity = (static_cast(bits[0]) << 8) + bits[1]; + temperature = (static_cast((bits[2] & 0b01111111) << 8) + bits[3]); + if(bits[2] & 0b10000000) temperature=temperature*-1; + } + else + { + humidity = bits[0]*10; + temperature = bits[2]*10; + } + return 0; + } + else return 5; +} diff --git a/dht11.h b/dht11.h new file mode 100644 index 0000000..bc3aa36 --- /dev/null +++ b/dht11.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include +#include "writepin.h" + +class Dht11 +{ + static constexpr uint16_t LOOP_TIMEOUT_COUNT = (10000.0 / 16000000.0)*F_CPU; + static constexpr uint8_t LOOP_TIMEOUT_SMALL_COUNT = (100.0 / 16000000.0)*F_CPU; + static constexpr uint8_t BIT_COUNT = 5;//(6.0 / 16000000.0)*F_CPU; + static constexpr bool DHT22 = true; + + volatile unsigned char * const _port; + volatile unsigned char * const _port_ctl; + volatile unsigned char * const _inPort; + const unsigned char _pin; + +public: + 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; + int16_t temperature = 0; +}; diff --git a/item.cpp b/item.cpp new file mode 100644 index 0000000..ad7bb30 --- /dev/null +++ b/item.cpp @@ -0,0 +1,9 @@ +#include "item.h" +#include "string.h" + +void Item::setName(const char * const nameN) +{ + size_t len = strlen(nameN); + if(len < MAX_NAME_LENGTH)memcpy(name, nameN, len+1); +} + diff --git a/item.h b/item.h new file mode 100644 index 0000000..0197de5 --- /dev/null +++ b/item.h @@ -0,0 +1,14 @@ +#pragma once +#include + +class Item +{ +public: + static constexpr uint16_t MAX_NAME_LENGTH = 16; + bool lastValue = 0; + uint16_t id; + char name[MAX_NAME_LENGTH]=""; + uint8_t type = 0; + + void setName(const char * const name); +}; diff --git a/main.cpp b/main.cpp index 27e43ff..cd8888c 100644 --- a/main.cpp +++ b/main.cpp @@ -66,13 +66,10 @@ volatile uint8_t displayDevider = 1; ISR(TIMER1_COMPA_vect) { - if(displayDevider > 1) - { - TIMSK2 = 0; - W433DataReciver::staticInterrupt(); - writePin(&PORTB, PB5, !readPin(&PORTB, PB5)); - TIMSK2 = 1; - } + TIMSK2 = 0; + W433DataReciver::staticInterrupt(); + writePin(&PORTB, PB5, !readPin(&PORTB, PB5)); + TIMSK2 = 1; } ISR(TIMER2_OVF_vect) @@ -89,6 +86,12 @@ ISR(TIMER2_OVF_vect) void buttonHandler(uint8_t index, uint8_t type, void* data) { + if(display.getBlank()) + { + display.setBlank(false); + return; + } + if(!setting) { if(index == 0 && type == Buttons::RELEASED) @@ -483,10 +486,8 @@ int main() } #endif - if(time.min > 45) - displayDevider = 16; - else - displayDevider = 1; + if(time.min == 45) + display.setBlank(true); if(time.hour == 0 && time.min == 0 && time.sec == timeOffsetSeconds+10) {