reciver changes

This commit is contained in:
2019-10-06 17:27:58 +02:00
parent 53c42b050a
commit d91e08db15
6 changed files with 37 additions and 22 deletions

View File

@ -242,9 +242,9 @@ CMAKE_CACHEFILE_DIR:INTERNAL=/home/philipp/Programming/avr/RGBcontroller
//Major version of cmake used to create the current loaded cache //Major version of cmake used to create the current loaded cache
CMAKE_CACHE_MAJOR_VERSION:INTERNAL=3 CMAKE_CACHE_MAJOR_VERSION:INTERNAL=3
//Minor version of cmake used to create the current loaded cache //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 //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 //ADVANCED property for variable: CMAKE_COLOR_MAKEFILE
CMAKE_COLOR_MAKEFILE-ADVANCED:INTERNAL=1 CMAKE_COLOR_MAKEFILE-ADVANCED:INTERNAL=1
//Path to CMake executable. //Path to CMake executable.
@ -343,7 +343,7 @@ CMAKE_PLATFORM_INFO_INITIALIZED:INTERNAL=1
//ADVANCED property for variable: CMAKE_RANLIB //ADVANCED property for variable: CMAKE_RANLIB
CMAKE_RANLIB-ADVANCED:INTERNAL=1 CMAKE_RANLIB-ADVANCED:INTERNAL=1
//Path to CMake installation. //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 //ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS-ADVANCED:INTERNAL=1 CMAKE_SHARED_LINKER_FLAGS-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS_DEBUG //ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS_DEBUG

View File

@ -30,8 +30,8 @@ int8_t W433DataReciver::reciveBit(uint8_t index)
if( if(
timesBuffer[index] < 0 && timesBuffer[index] < 0 &&
isTime(timesBuffer[index+1], SMALL_TIME, true, SMALL_TIME_TOLERANCE) && isTime(timesBuffer[index+1], SMALL_TIME, true, SMALL_TIME_TOLERANCE) &&
isTime(timesBuffer[index+2], LARGE_TIME, false, LARGE_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+3], SMALL_TIME, true, SMALL_TIME_TOLERANCE)
) )
{ {
return 1; return 1;
@ -39,8 +39,8 @@ int8_t W433DataReciver::reciveBit(uint8_t index)
else if( else if(
timesBuffer[index] < 0 && timesBuffer[index] < 0 &&
isTime(timesBuffer[index+1], LARGE_TIME, true, LARGE_TIME_TOLERANCE) && isTime(timesBuffer[index+1], LARGE_TIME, true, LARGE_TIME_TOLERANCE) &&
isTime(timesBuffer[index+2], SMALL_TIME, false, SMALL_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+3], SMALL_TIME, true, SMALL_TIME_TOLERANCE)
) )
{ {
return 0; return 0;
@ -67,8 +67,8 @@ bool W433DataReciver::reciveSync(const uint16_t elapsedTime)
if(syncCount > 4 && syncFailCount < 3) ++syncFailCount; if(syncCount > 4 && syncFailCount < 3) ++syncFailCount;
else else
{ {
syncCount = 0; //if(syncCount > 7) error(ERR_SYNC_FAIL);
syncFailCount = 0; setState(LOOKING_FOR_SYNC);
} }
} }
if(syncCount > 10) return true; if(syncCount > 10) return true;
@ -79,6 +79,7 @@ bool W433DataReciver::recivedByte(const uint16_t elapsedTime)
{ {
timesBuffer[timesBufferIndex] = readPin(_port, _pin) ? 0-elapsedTime : elapsedTime; timesBuffer[timesBufferIndex] = readPin(_port, _pin) ? 0-elapsedTime : elapsedTime;
++timesBufferIndex; ++timesBufferIndex;
if(timesBufferIndex == 32) writePin(&PORTC, PC0, true);
return timesBufferIndex == 32; return timesBufferIndex == 32;
} }
@ -119,14 +120,14 @@ void W433DataReciver::interrupt()
if(elapsedTime < DISCARD_TIME) 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]); previousTime = *_timerRegister - elapsedTime - abs(timesBuffer[timesBufferIndex-1]);
--timesBufferIndex; timesBufferIndex-=1;
} }
return; return;
} }
previousTime = *_timerRegister; previousTime = *_timerRegister;
*_timerOverflowRegister = *_timerOverflowRegister | 0x01; *_timerOverflowRegister = *_timerOverflowRegister | 0x01;
@ -136,7 +137,7 @@ void W433DataReciver::interrupt()
} }
else if(state == LOOKING_FOR_SYNC_END) 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) if(elapsedTime < LARGE_TIME - LARGE_TIME_TOLERANCE)
{ {
@ -194,7 +195,9 @@ void W433DataReciver::interrupt()
if(computedChecksum == recivedChecksum) if(computedChecksum == recivedChecksum)
{ {
#ifdef USE_RINGBUFFER
_ringBuffer.write(const_cast<uint8_t*>(buffer), sizeof(packet)); _ringBuffer.write(const_cast<uint8_t*>(buffer), sizeof(packet));
#endif
if(_packetCallback != nullptr)(*_packetCallback)(packet, _userData); if(_packetCallback != nullptr)(*_packetCallback)(packet, _userData);
} }
else error(ERR_CHECKSUM); else error(ERR_CHECKSUM);
@ -204,7 +207,9 @@ void W433DataReciver::interrupt()
} }
} }
#ifdef USE_RINGBUFFER
RingBuffer<W433DataReciver::RINGBUFFER_LENGTH, uint8_t>* W433DataReciver::getRingBuffer() RingBuffer<W433DataReciver::RINGBUFFER_LENGTH, uint8_t>* W433DataReciver::getRingBuffer()
{ {
return &_ringBuffer; return &_ringBuffer;
} }
#endif

View File

@ -2,6 +2,8 @@
#include <stdint.h> #include <stdint.h>
#include "ringbuffer.h" #include "ringbuffer.h"
//#define USE_RINGBUFFER
class W433DataReciver class W433DataReciver
{ {
public: public:
@ -25,9 +27,10 @@ private:
static constexpr uint16_t LARGE_TIME = 2000; static constexpr uint16_t LARGE_TIME = 2000;
static constexpr uint16_t SMALL_TIME = 500; static constexpr uint16_t SMALL_TIME = 500;
static constexpr uint8_t SYNC_TIME_TOLERANCE = SYNC_TIME*0.20; 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 uint16_t LARGE_TIME_TOLERANCE = LARGE_TIME*0.30;
static constexpr uint8_t SMALL_TIME_TOLERANCE = SMALL_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 uint16_t TICKS_PER_US = (F_CPU) / (1000000*CLOCK_DEVIDER) ;
static constexpr uint8_t signature = 0xA5; static constexpr uint8_t signature = 0xA5;
@ -46,7 +49,9 @@ private:
volatile uint16_t *_timerRegister; volatile uint16_t *_timerRegister;
volatile uint8_t *_timerOverflowRegister; volatile uint8_t *_timerOverflowRegister;
#ifdef USE_RINGBUFFER
RingBuffer<RINGBUFFER_LENGTH, uint8_t> _ringBuffer; RingBuffer<RINGBUFFER_LENGTH, uint8_t> _ringBuffer;
#endif
volatile uint16_t previousTime = 0; volatile uint16_t previousTime = 0;
volatile uint8_t timesBufferIndex = 0; volatile uint8_t timesBufferIndex = 0;
@ -82,5 +87,7 @@ public:
static void initTimer(); static void initTimer();
static void staticInterrupt(); static void staticInterrupt();
void interrupt(); void interrupt();
#ifdef USE_RINGBUFFER
RingBuffer<RINGBUFFER_LENGTH, uint8_t>* getRingBuffer(); RingBuffer<RINGBUFFER_LENGTH, uint8_t>* getRingBuffer();
#endif
}; };

View File

@ -7,10 +7,10 @@
class WirelessRelay class WirelessRelay
{ {
public: public:
static const uint16_t LARGE_TIME = 750; static constexpr uint16_t LARGE_TIME = 750;
static const uint8_t SMALL_TIME = 250; static constexpr uint8_t SMALL_TIME = 250;
static const uint16_t MAX_NAME_LENGTH = 16; static constexpr uint16_t MAX_NAME_LENGTH = 16;
private: private:
bool _state = false; bool _state = false;

View File

@ -49,7 +49,7 @@ inline static void printHelp(Serial* serial)
serial->write_p(PSTR("Available Commands: \n\ serial->write_p(PSTR("Available Commands: \n\
help : Show this prompt.\n\ help : Show this prompt.\n\
relay add [id] [name] : Add Wireless Relay.\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 [on/off] [nn] : Turn on/off nth relay.\n\
relay resend [on/off] : Turn on/off periodic auto resend.\n\ relay resend [on/off] : Turn on/off periodic auto resend.\n\
state : Get machine readable state.\n\ state : Get machine readable state.\n\
@ -134,11 +134,14 @@ void relayDispatch(SVector<WirelessRelay, MAX_RELAYS>* relays, Pwm16b* auxPwm, c
} }
else if( strcmp(token, "delete") == 0 ) else if( strcmp(token, "delete") == 0 )
{ {
token = strtok(NULL, " \n");
if(relays->count() > 0) if(relays->count() > 0)
{ {
relays->erase(relays->count()); uint16_t index = relays->count();
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "Deleting relay NUMBER: %u NAME: %s\n", relays->count(), relays->at(relays->count()).getName()); 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); serial->write(buffer, SNPRINTF_BUFFER_SIZE);
relays->erase(index);
save(); save();
} }
} }

View File

@ -71,7 +71,7 @@ public:
if(position > stored) return false; if(position > stored) return false;
array[position].~T(); array[position].~T();
--stored; --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; return true;
} }