From d91e08db15a5a493707f3f338f158e6ec2d862ba Mon Sep 17 00:00:00 2001 From: Carl Klemm Date: Sun, 6 Oct 2019 17:27:58 +0200 Subject: [PATCH] reciver changes --- CMakeCache.txt | 6 +++--- W433DataReciver.cpp | 25 +++++++++++++++---------- W433DataReciver.h | 11 +++++++++-- WirelessRelay.h | 6 +++--- main.cpp | 9 ++++++--- staticvector.h | 2 +- 6 files changed, 37 insertions(+), 22 deletions(-) diff --git a/CMakeCache.txt b/CMakeCache.txt index a376a3d..3d982c5 100644 --- a/CMakeCache.txt +++ b/CMakeCache.txt @@ -242,9 +242,9 @@ CMAKE_CACHEFILE_DIR:INTERNAL=/home/philipp/Programming/avr/RGBcontroller //Major version of cmake used to create the current loaded cache CMAKE_CACHE_MAJOR_VERSION:INTERNAL=3 //Minor version of cmake used to create the current loaded cache -CMAKE_CACHE_MINOR_VERSION:INTERNAL=13 +CMAKE_CACHE_MINOR_VERSION:INTERNAL=14 //Patch version of cmake used to create the current loaded cache -CMAKE_CACHE_PATCH_VERSION:INTERNAL=2 +CMAKE_CACHE_PATCH_VERSION:INTERNAL=4 //ADVANCED property for variable: CMAKE_COLOR_MAKEFILE CMAKE_COLOR_MAKEFILE-ADVANCED:INTERNAL=1 //Path to CMake executable. @@ -343,7 +343,7 @@ CMAKE_PLATFORM_INFO_INITIALIZED:INTERNAL=1 //ADVANCED property for variable: CMAKE_RANLIB CMAKE_RANLIB-ADVANCED:INTERNAL=1 //Path to CMake installation. -CMAKE_ROOT:INTERNAL=/usr/share/cmake-3.13 +CMAKE_ROOT:INTERNAL=/usr/share/cmake-3.14 //ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS-ADVANCED:INTERNAL=1 //ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS_DEBUG diff --git a/W433DataReciver.cpp b/W433DataReciver.cpp index 613a4ae..e9b8387 100644 --- a/W433DataReciver.cpp +++ b/W433DataReciver.cpp @@ -30,8 +30,8 @@ int8_t W433DataReciver::reciveBit(uint8_t index) if( timesBuffer[index] < 0 && 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) + isTime(timesBuffer[index+2], LARGE_TIME, false, LARGE_TIME_TOLERANCE) //&& + //isTime(timesBuffer[index+3], SMALL_TIME, true, SMALL_TIME_TOLERANCE) ) { return 1; @@ -39,8 +39,8 @@ int8_t W433DataReciver::reciveBit(uint8_t index) else if( timesBuffer[index] < 0 && 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) + isTime(timesBuffer[index+2], SMALL_TIME, false, SMALL_TIME_TOLERANCE) //&& + //isTime(timesBuffer[index+3], SMALL_TIME, true, SMALL_TIME_TOLERANCE) ) { return 0; @@ -67,8 +67,8 @@ bool W433DataReciver::reciveSync(const uint16_t elapsedTime) if(syncCount > 4 && syncFailCount < 3) ++syncFailCount; else { - syncCount = 0; - syncFailCount = 0; + //if(syncCount > 7) error(ERR_SYNC_FAIL); + setState(LOOKING_FOR_SYNC); } } if(syncCount > 10) return true; @@ -79,6 +79,7 @@ bool W433DataReciver::recivedByte(const uint16_t elapsedTime) { timesBuffer[timesBufferIndex] = readPin(_port, _pin) ? 0-elapsedTime : elapsedTime; ++timesBufferIndex; + if(timesBufferIndex == 32) writePin(&PORTC, PC0, true); return timesBufferIndex == 32; } @@ -119,14 +120,14 @@ void W433DataReciver::interrupt() if(elapsedTime < DISCARD_TIME) { - if(timesBufferIndex > 0 && elapsedTime + timesBuffer[timesBufferIndex-1] < LARGE_TIME+LARGE_TIME_TOLERANCE) + if(timesBufferIndex > 0 && elapsedTime + abs(timesBuffer[timesBufferIndex-1]) < LARGE_TIME+LARGE_TIME_TOLERANCE) { + writePin(&PORTC, PC2, true); previousTime = *_timerRegister - elapsedTime - abs(timesBuffer[timesBufferIndex-1]); - --timesBufferIndex; + timesBufferIndex-=1; } return; } - previousTime = *_timerRegister; *_timerOverflowRegister = *_timerOverflowRegister | 0x01; @@ -136,7 +137,7 @@ void W433DataReciver::interrupt() } else if(state == LOOKING_FOR_SYNC_END) { - if(elapsedTime > SYNC_TIME + SYNC_TIME_TOLERANCE) + if(elapsedTime > SYNC_TIME + SYNC_END_TIME_TOLERANCE) { if(elapsedTime < LARGE_TIME - LARGE_TIME_TOLERANCE) { @@ -194,7 +195,9 @@ void W433DataReciver::interrupt() if(computedChecksum == recivedChecksum) { + #ifdef USE_RINGBUFFER _ringBuffer.write(const_cast(buffer), sizeof(packet)); + #endif if(_packetCallback != nullptr)(*_packetCallback)(packet, _userData); } else error(ERR_CHECKSUM); @@ -204,7 +207,9 @@ void W433DataReciver::interrupt() } } +#ifdef USE_RINGBUFFER RingBuffer* W433DataReciver::getRingBuffer() { return &_ringBuffer; } +#endif diff --git a/W433DataReciver.h b/W433DataReciver.h index bc2c30b..01baf58 100644 --- a/W433DataReciver.h +++ b/W433DataReciver.h @@ -2,10 +2,12 @@ #include #include "ringbuffer.h" +//#define USE_RINGBUFFER + class W433DataReciver { public: - + static constexpr uint8_t RINGBUFFER_LENGTH = 32; //errors @@ -25,9 +27,10 @@ private: static constexpr uint16_t LARGE_TIME = 2000; static constexpr uint16_t SMALL_TIME = 500; static constexpr uint8_t SYNC_TIME_TOLERANCE = SYNC_TIME*0.20; + static constexpr uint16_t SYNC_END_TIME_TOLERANCE = SYNC_TIME*0.80; static constexpr uint16_t LARGE_TIME_TOLERANCE = LARGE_TIME*0.30; static constexpr uint8_t SMALL_TIME_TOLERANCE = SMALL_TIME*0.30; - static constexpr uint8_t DISCARD_TIME = SMALL_TIME*0.3; + static constexpr uint16_t DISCARD_TIME = SMALL_TIME*0.6; static constexpr uint16_t TICKS_PER_US = (F_CPU) / (1000000*CLOCK_DEVIDER) ; static constexpr uint8_t signature = 0xA5; @@ -46,7 +49,9 @@ private: volatile uint16_t *_timerRegister; volatile uint8_t *_timerOverflowRegister; +#ifdef USE_RINGBUFFER RingBuffer _ringBuffer; +#endif volatile uint16_t previousTime = 0; volatile uint8_t timesBufferIndex = 0; @@ -82,5 +87,7 @@ public: static void initTimer(); static void staticInterrupt(); void interrupt(); +#ifdef USE_RINGBUFFER RingBuffer* getRingBuffer(); +#endif }; diff --git a/WirelessRelay.h b/WirelessRelay.h index c73c7da..c26a19e 100644 --- a/WirelessRelay.h +++ b/WirelessRelay.h @@ -7,10 +7,10 @@ class WirelessRelay { public: - static const uint16_t LARGE_TIME = 750; - static const uint8_t SMALL_TIME = 250; + static constexpr uint16_t LARGE_TIME = 750; + static constexpr uint8_t SMALL_TIME = 250; - static const uint16_t MAX_NAME_LENGTH = 16; + static constexpr uint16_t MAX_NAME_LENGTH = 16; private: bool _state = false; diff --git a/main.cpp b/main.cpp index 04f0472..fceccbb 100644 --- a/main.cpp +++ b/main.cpp @@ -49,7 +49,7 @@ inline static void printHelp(Serial* serial) serial->write_p(PSTR("Available Commands: \n\ help : Show this prompt.\n\ relay add [id] [name] : Add Wireless Relay.\n\ - relay delete : Delete last Relay.\n\ + relay delete [n] : Delete n'th Relay.\n\ relay [on/off] [nn] : Turn on/off nth relay.\n\ relay resend [on/off] : Turn on/off periodic auto resend.\n\ state : Get machine readable state.\n\ @@ -134,11 +134,14 @@ void relayDispatch(SVector* relays, Pwm16b* auxPwm, c } else if( strcmp(token, "delete") == 0 ) { + token = strtok(NULL, " \n"); if(relays->count() > 0) { - relays->erase(relays->count()); - snprintf(buffer, SNPRINTF_BUFFER_SIZE, "Deleting relay NUMBER: %u NAME: %s\n", relays->count(), relays->at(relays->count()).getName()); + uint16_t index = relays->count(); + if( token != NULL) index = atoi(token); + snprintf(buffer, SNPRINTF_BUFFER_SIZE, "Deleting relay NUMBER: %u NAME: %s\n", index, relays->at(index).getName()); serial->write(buffer, SNPRINTF_BUFFER_SIZE); + relays->erase(index); save(); } } diff --git a/staticvector.h b/staticvector.h index d63dfc7..786456b 100644 --- a/staticvector.h +++ b/staticvector.h @@ -71,7 +71,7 @@ public: if(position > stored) return false; array[position].~T(); --stored; - for( size_t i = position; i < stored; i++ ) array[i] = array[i+1]; + for( size_t i = position; i < stored; i++ ) memcpy(&array[i], &array[i+1], sizeof(T)); return true; }