Update
This commit is contained in:
@ -1,94 +1,63 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "ringbuffer.h"
|
||||
|
||||
//#define USE_RINGBUFFER
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
class W433DataReciver
|
||||
{
|
||||
public:
|
||||
|
||||
static constexpr uint8_t RINGBUFFER_LENGTH = 32;
|
||||
|
||||
//errors
|
||||
static constexpr uint8_t ERR_SYNC_FAIL = 1;
|
||||
static constexpr uint8_t ERR_NO_SYNC_END = 2;
|
||||
static constexpr uint8_t ERR_BYTE_ASM = 3;
|
||||
static constexpr uint8_t ERR_WRONG_SIG = 4;
|
||||
static constexpr uint8_t ERR_CHECKSUM = 5;
|
||||
|
||||
static constexpr int ERROR_CRC = 1;
|
||||
|
||||
static constexpr int SAMPLES_PER_BIT = 8;
|
||||
static constexpr int PLL_RAMP_LEN = 160;
|
||||
static constexpr int PLL_RAMP_INC_STEP = PLL_RAMP_LEN/SAMPLES_PER_BIT;
|
||||
static constexpr int PLL_RAMP_TRANSITION = PLL_RAMP_LEN/2;
|
||||
static constexpr int PLL_RAMP_INC_RETARD = PLL_RAMP_INC_STEP-9;
|
||||
static constexpr int PLL_RAMP_INC_ADVANCE = PLL_RAMP_INC_STEP+9;
|
||||
static constexpr int PLL_HEADER_LEN_BITS = 8;
|
||||
static constexpr int PACKET_LENGTH = sizeof(uint32_t)+2;
|
||||
|
||||
private:
|
||||
|
||||
static W433DataReciver* instance;
|
||||
|
||||
//constants
|
||||
static constexpr uint8_t CLOCK_DEVIDER = 1;
|
||||
static constexpr uint16_t LARGE_TIME = 2000;
|
||||
static constexpr uint16_t SMALL_TIME = 500;
|
||||
static constexpr uint16_t SYNC_TIME = 800;
|
||||
static constexpr uint8_t SYNC_TIME_TOLERANCE = SYNC_TIME*0.20;
|
||||
static constexpr uint16_t SYNC_END_TIME_TOLERANCE = SYNC_TIME*0.50;
|
||||
static constexpr uint16_t LARGE_TIME_TOLERANCE = LARGE_TIME*0.30;
|
||||
static constexpr uint8_t SMALL_TIME_TOLERANCE = SMALL_TIME*0.30;
|
||||
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;
|
||||
|
||||
static constexpr int8_t polarity = 1;
|
||||
|
||||
static constexpr uint8_t LOOKING_FOR_SYNC = 0;
|
||||
static constexpr uint8_t LOOKING_FOR_SYNC_END = 1;
|
||||
static constexpr uint8_t LOOKING_FOR_SIGNATURE = 2;
|
||||
static constexpr uint8_t RECVING_PACKET = 3;
|
||||
static constexpr uint8_t RECVING_PACKET_CHECKSUM = 4;
|
||||
|
||||
//variables
|
||||
volatile unsigned char *_port;
|
||||
unsigned char _pin;
|
||||
|
||||
volatile uint16_t *_timerRegister;
|
||||
volatile uint8_t *_timerOverflowRegister;
|
||||
|
||||
#ifdef USE_RINGBUFFER
|
||||
RingBuffer<RINGBUFFER_LENGTH, uint8_t> _ringBuffer;
|
||||
#endif
|
||||
|
||||
volatile uint16_t previousTime = 0;
|
||||
volatile uint8_t timesBufferIndex = 0;
|
||||
volatile int16_t timesBuffer[33];
|
||||
|
||||
volatile uint8_t packetIndex = 0;
|
||||
volatile uint32_t packet = 0;
|
||||
|
||||
void (* const _packetCallback)(uint32_t, void*);
|
||||
void (* const _errorCodeHandler)(uint8_t, void*);
|
||||
void* const _userData;
|
||||
|
||||
volatile uint8_t syncCount = 0;
|
||||
volatile uint8_t syncFailCount = 0;
|
||||
volatile uint8_t state = 0;
|
||||
|
||||
//private functions
|
||||
int8_t reciveBit(uint8_t index);
|
||||
|
||||
inline uint8_t assmbleByte();
|
||||
inline void setState(const uint8_t stateIn);
|
||||
inline bool recivedByte(const uint16_t elapsedTime);
|
||||
inline bool reciveSync(const uint16_t elapsedTime);
|
||||
|
||||
inline void error(const uint8_t errorCode);
|
||||
|
||||
static inline bool isTime(int16_t input, const uint16_t time, const bool state = true, const uint16_t tollerance = 100);
|
||||
|
||||
|
||||
static constexpr int MODE_SEARCHING = 0;
|
||||
static constexpr int MODE_RECEIVING = 1;
|
||||
|
||||
static W433DataReciver* instance;
|
||||
|
||||
volatile unsigned char *port;
|
||||
unsigned char pin;
|
||||
|
||||
bool prevSample = 0;
|
||||
uint8_t pllRamp = 0;
|
||||
uint8_t rxIntegrator = 0;
|
||||
|
||||
uint16_t rxBits = 0;
|
||||
|
||||
uint8_t mode = MODE_SEARCHING;
|
||||
|
||||
uint8_t rxBuf[PACKET_LENGTH];
|
||||
uint8_t rxCount = 0;
|
||||
volatile uint8_t rxLen = 0;
|
||||
|
||||
void (* const packetCallback)(uint32_t, void*);
|
||||
void (* const errorCodeHandler)(uint8_t, void*);
|
||||
void* const userData;
|
||||
|
||||
private:
|
||||
|
||||
static uint16_t calcCrc(uint32_t data);
|
||||
static uint8_t symbolDecode(uint8_t symbol);
|
||||
void pll(bool sample);
|
||||
|
||||
public:
|
||||
|
||||
W433DataReciver(volatile unsigned char* const port , const unsigned char pin, volatile uint16_t * timerRegister, volatile uint8_t* const timerOverflowRegister, void (*packetCallback)(uint32_t, void*) = nullptr, void* userData = nullptr, void (*errorCodeHandler)(uint8_t, void*) = nullptr );
|
||||
~W433DataReciver();
|
||||
static void initTimer();
|
||||
static void staticInterrupt();
|
||||
void waitForReciveIdle(const uint16_t timeoutMs = 10000);
|
||||
void interrupt();
|
||||
#ifdef USE_RINGBUFFER
|
||||
RingBuffer<RINGBUFFER_LENGTH, uint8_t>* getRingBuffer();
|
||||
#endif
|
||||
|
||||
W433DataReciver(volatile unsigned char* const portIn, const unsigned char pinIn, void (*packetCallbackIn)(uint32_t,
|
||||
void*),
|
||||
void* userDataIn = nullptr, void (*errorCodeHandlerIn)(uint8_t, void*) = nullptr );
|
||||
~W433DataReciver();
|
||||
|
||||
static void staticInterrupt();
|
||||
void interrupt();
|
||||
|
||||
static uint16_t calculateOverflowRegister(uint16_t bitRate, uint16_t devisor);
|
||||
};
|
||||
|
Reference in New Issue
Block a user