86 lines
2.9 KiB
C++
86 lines
2.9 KiB
C++
#pragma once
|
|
#include <stdint.h>
|
|
#include "ringbuffer.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;
|
|
|
|
private:
|
|
|
|
static W433DataReciver* instance;
|
|
|
|
//constants
|
|
static constexpr uint8_t CLOCK_DEVIDER = 1;
|
|
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.20;
|
|
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 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;
|
|
|
|
RingBuffer<RINGBUFFER_LENGTH, uint8_t> _ringBuffer;
|
|
|
|
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 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);
|
|
|
|
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 interrupt();
|
|
RingBuffer<RINGBUFFER_LENGTH, uint8_t>* getRingBuffer();
|
|
};
|