diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d0cd2a..1ff9cc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ set(CMAKE_LINKER /usr/bin/avr-ld) # Compiler flags add_definitions(-mmcu=${MCU} -DF_CPU=${CPU_SPEED}) -add_definitions(-s -c -g -Os -Wall -std=c++17 ) +add_definitions(-s -c -g -Os -flto -Wall -std=c++17 ) add_definitions(-fno-exceptions -ffunction-sections -fdata-sections) # Linker flags diff --git a/W433DataReciver.cpp b/W433DataReciver.cpp index af9babf..c5500aa 100644 --- a/W433DataReciver.cpp +++ b/W433DataReciver.cpp @@ -29,18 +29,18 @@ int8_t W433DataReciver::reciveBit(uint8_t index) { if( timesBuffer[index] < 0 && - isTime(timesBuffer[index+1], SMALL_TIME, true) && - isTime(timesBuffer[index+2], LARGE_TIME, false) && - isTime(timesBuffer[index+3], SMALL_TIME, true) + isTime(timesBuffer[index+1], SMALL_TIME, true, SMALL_TIME_TOLERANCE) && + isTime(timesBuffer[index+2], LARGE_TIME, false, LARGE_TIME_TOLERANCE) && + isTime(timesBuffer[index+3], SMALL_TIME, true, SMALL_TIME_TOLERANCE) ) { return 1; } else if( timesBuffer[index] < 0 && - isTime(timesBuffer[index+1], LARGE_TIME, true) && - isTime(timesBuffer[index+2], SMALL_TIME, false) && - isTime(timesBuffer[index+3], SMALL_TIME, true) + isTime(timesBuffer[index+1], LARGE_TIME, true, LARGE_TIME_TOLERANCE) && + isTime(timesBuffer[index+2], SMALL_TIME, false, SMALL_TIME_TOLERANCE) && + isTime(timesBuffer[index+3], SMALL_TIME, true, SMALL_TIME_TOLERANCE) ) { return 0; @@ -58,7 +58,7 @@ bool W433DataReciver::isTime(int16_t input, const uint16_t time, const bool stat bool W433DataReciver::reciveSync(const uint16_t elapsedTime) { - if(elapsedTime < SYNC_TIME+50 && elapsedTime > SYNC_TIME-50) + if(elapsedTime < SYNC_TIME+SYNC_TIME_TOLERANCE && elapsedTime > SYNC_TIME-SYNC_TIME_TOLERANCE) { ++syncCount; } diff --git a/W433DataReciver.h b/W433DataReciver.h index 6c6e36e..d9eb1c9 100644 --- a/W433DataReciver.h +++ b/W433DataReciver.h @@ -17,6 +17,9 @@ private: static constexpr uint16_t SYNC_TIME = 800; static constexpr uint16_t LARGE_TIME = 2000; static constexpr uint16_t SMALL_TIME = 500; + static constexpr uint8_t SYNC_TIME_TOLERANCE = SYNC_TIME*0.15; + static constexpr uint8_t LARGE_TIME_TOLERANCE = LARGE_TIME*0.15; + static constexpr uint8_t SMALL_TIME_TOLERANCE = SMALL_TIME*0.15; static constexpr uint16_t TICKS_PER_US = (F_CPU) / (1000000*CLOCK_DEVIDER) ; static constexpr uint8_t signature = 0xA5; @@ -59,7 +62,7 @@ private: inline bool recivedByte(const uint16_t elapsedTime); inline bool reciveSync(const uint16_t elapsedTime); - static inline bool isTime(int16_t input, const uint16_t time, const bool state = true, const uint16_t tollerance = 50); + static inline bool isTime(int16_t input, const uint16_t time, const bool state = true, const uint16_t tollerance = 100); public: diff --git a/main.cpp b/main.cpp index 1644ac1..f635401 100644 --- a/main.cpp +++ b/main.cpp @@ -13,7 +13,7 @@ #include "W433DataReciver.h" #define COMMAND_BUFFER_SIZE 64 -#define SNPRINTF_BUFFER_SIZE 64 +#define SNPRINTF_BUFFER_SIZE 96 #define MAX_RELAYS 32 #define RELAY_VECTOR_EEPROM_ADDR 32 @@ -49,7 +49,6 @@ inline static void printHelp(Serial* serial) rgb pattern [id] : RGB pattern.\n\ rgb preset [id] : set preset color.\n\ rgb fade [on/off] : turn Colorfade on or off.\n\ - aux [on/off] : Turn on/off PWM Channel.\n\ aux set [VAL] : Set PWM value.\n")); } @@ -58,6 +57,11 @@ void save() EEPROM_write_class< SVector > (RELAY_VECTOR_EEPROM_ADDR, relays); } +void loadRGB(RgbLed* rgbled) +{ + rgbled->setSolidColor(EEPROM_read_char(1), EEPROM_read_char(2), EEPROM_read_char(3)); +} + void load() { EEPROM_read_class< SVector > (RELAY_VECTOR_EEPROM_ADDR, &relays); @@ -66,12 +70,12 @@ void load() void writeRelayState(Serial* serial, WirelessRelay* relay, uint8_t number) { uint16_t id = relay->getId(); - snprintf(buffer, SNPRINTF_BUFFER_SIZE, "RELAY NUMBER: %u ID: %s%s%s NAME: %s STATE: %u\n", number, + snprintf(buffer, SNPRINTF_BUFFER_SIZE, "RELAY NUMBER: %u ID: %s%s%s STATE: %u NAME: %s\n", number, bit_rep[ id >> 12], bit_rep[(id & 0x0F00) >> 8 ], bit_rep[(id & 0x00F0) >> 4 ], - relay->getName(), - relay->getExpectedState() + relay->getExpectedState(), + relay->getName() ); serial->write(buffer, SNPRINTF_BUFFER_SIZE); } @@ -176,7 +180,13 @@ void rgbDispatch(RgbLed* rgbled, char* token, Serial* serial) char* bToken = strtok(NULL, " \n"); if(rToken != NULL && gToken != NULL && bToken != NULL) { - rgbled->setSolidColor(atoi(rToken), atoi(gToken), atoi(bToken)); + uint8_t r = atoi(rToken); + uint8_t g = atoi(gToken); + uint8_t b = atoi(bToken); + rgbled->setSolidColor(r,g,b); + EEPROM_write_char(1, r); + EEPROM_write_char(2, g); + EEPROM_write_char(3, b); serial->write_p(PSTR("Set RGB values\n")); } else serial->write_p(PSTR("Usage: rgb set [RRR] [GGG] [BBB]\n")); @@ -228,21 +238,13 @@ void rgbDispatch(RgbLed* rgbled, char* token, Serial* serial) void auxDispatch(Pwm16b* auxPwm, char* token, Serial* serial) { - if(strcmp(token, "on") == 0 ) - { - auxPwm->on(); - serial->write_p(PSTR("Aux pwm on\n")); - } - else if(strcmp(token, "off") == 0 ) - { - auxPwm->off(); - serial->write_p(PSTR("Aux pwm off\n")); - } - else if(strcmp(token, "set") == 0 ) + if(strcmp(token, "set") == 0 ) { token = strtok(NULL, " \n"); if(token != NULL) { + if(atoi(token) == 0) auxPwm->off(); + else auxPwm->on(); auxPwm->setDutyA(atoi(token) << 8); serial->write_p(PSTR("Set PWM value\n")); } @@ -251,7 +253,7 @@ void auxDispatch(Pwm16b* auxPwm, char* token, Serial* serial) else { serial->write(token, COMMAND_BUFFER_SIZE-4); - serial->write_p(PSTR(" is not a valid subcommand: aux [on/off/set]\n")); + serial->write_p(PSTR(" is not a valid subcommand: aux set [value]\n")); } } @@ -264,7 +266,7 @@ void serialDispatch(Serial* serial, SVector* relays, unsigned int length = serial->getString(buffer, COMMAND_BUFFER_SIZE); if(length > 2) { - cli(); + setBit(&PCICR, PCIE1, false); char* token = strtok(buffer, " \n"); if(strcmp(token, "relay") == 0) { @@ -294,6 +296,7 @@ void serialDispatch(Serial* serial, SVector* relays, for(uint8_t i = 0; i < relays->count(); i++) { writeRelayState(serial, &relays->at(i), i); + //serial->putChar('\n'); } serial->write_p(PSTR("EOL\n")); } @@ -320,7 +323,7 @@ void serialDispatch(Serial* serial, SVector* relays, { serial->write_p(PSTR("Not a valid command\n")); } - sei(); + setBit(&PCICR, PCIE1, true); } } } @@ -369,6 +372,7 @@ int main() pwmTc2.off(); RgbLed rgbled( &pwmTc0, &pwmTc2 ); + loadRGB(&rgbled); Pwm16b pwmTc1 ( &TCCR1A, &TCCR1B, &OCR1A, &OCR1B, &ICR1, 0b00000001, true, false); @@ -423,3 +427,4 @@ int main() return 0; } + diff --git a/rgbled.cpp b/rgbled.cpp index 745f1d8..27ee7a2 100644 --- a/rgbled.cpp +++ b/rgbled.cpp @@ -8,7 +8,6 @@ void RgbLed::setSolidColor( const uint8_t r, const uint8_t g, const uint8_t b) _targetR = r; _targetG = g; _targetB = b; - (!_fade && r+b+g == 0) ? off() : on(); } void RgbLed::setPreset( const uint8_t preset) @@ -148,7 +147,7 @@ void RgbLed::patternStep() if(_counter<65530) _counter++; else _pwmB->setDutyB(140); - _delay_ms(18); //honey dose this make me look slow? + _delay_ms(18); } } @@ -185,13 +184,13 @@ void RgbLed::logic() uint8_t RgbLed::getR() { - return OCR0B; + return _pwmA->getValueB(); } uint8_t RgbLed::getB() { - return OCR2B; + return _pwmB->getValueB(); } uint8_t RgbLed::getG() { - return OCR0A; + return _pwmA->getValueA(); } diff --git a/rgbled.h b/rgbled.h index 8d9af1e..1d6d15b 100644 --- a/rgbled.h +++ b/rgbled.h @@ -6,6 +6,10 @@ class RgbLed private: Pwm8b* _pwmA; Pwm8b* _pwmB; + + static constexpr uint16_t calBlue[] = {1000, 1000, 1000}; + static constexpr uint16_t calGreen[] = {1000, 1000, 1000}; + static constexpr uint16_t calRed[] = {1000, 1000, 1000}; uint8_t _pattern = 0;