145 lines
2.7 KiB
C++
145 lines
2.7 KiB
C++
#include "W433DataReciver.h"
|
|
#include <util/crc16.h>
|
|
#include "writepin.h"
|
|
|
|
W433DataReciver* W433DataReciver::instance = nullptr;
|
|
|
|
W433DataReciver::W433DataReciver(volatile unsigned char* const portIn, const unsigned char pinIn,
|
|
void (*packetCallbackIn)(uint32_t, void*), void* userDataIn,
|
|
void (*errorCodeHandlerIn)(uint8_t, void*)):
|
|
port(portIn), pin(pinIn),
|
|
packetCallback(packetCallbackIn), errorCodeHandler(errorCodeHandlerIn), userData(userDataIn)
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
W433DataReciver::~W433DataReciver()
|
|
{
|
|
instance = nullptr;
|
|
}
|
|
|
|
void W433DataReciver::staticInterrupt()
|
|
{
|
|
if(instance != nullptr)
|
|
instance->interrupt();
|
|
}
|
|
|
|
uint16_t W433DataReciver::calcCrc(uint32_t data)
|
|
{
|
|
uint16_t crc = 0xffff;
|
|
uint8_t* ptr = reinterpret_cast<uint8_t*>(&data);
|
|
for(size_t i = 0; i < sizeof(data); ++i)
|
|
crc = _crc_ccitt_update(crc, ptr[i]);
|
|
return crc;
|
|
}
|
|
|
|
uint8_t W433DataReciver::symbolDecode(uint8_t symbol)
|
|
{
|
|
switch(symbol)
|
|
{
|
|
case 0xe:
|
|
return 1;
|
|
case 0x13:
|
|
return 2;
|
|
case 0x15:
|
|
return 3;
|
|
case 0x16:
|
|
return 4;
|
|
case 0x19:
|
|
return 5;
|
|
case 0x1a:
|
|
return 6;
|
|
case 0x1c:
|
|
return 7;
|
|
case 0x23:
|
|
return 8;
|
|
case 0x25:
|
|
return 9;
|
|
case 0x26:
|
|
return 10;
|
|
case 0x29:
|
|
return 11;
|
|
case 0x2a:
|
|
return 12;
|
|
case 0x2c:
|
|
return 13;
|
|
case 0x32:
|
|
return 14;
|
|
case 0x34:
|
|
return 15;
|
|
case 0xd:
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
void W433DataReciver::pll(bool sample)
|
|
{
|
|
// Integrate each sample
|
|
if(sample)
|
|
rxIntegrator++;
|
|
|
|
if (sample != prevSample)
|
|
{
|
|
pllRamp += pllRamp < PLL_RAMP_TRANSITION ? PLL_RAMP_INC_RETARD : PLL_RAMP_INC_ADVANCE;
|
|
prevSample = sample;
|
|
}
|
|
else
|
|
{
|
|
pllRamp += PLL_RAMP_INC_STEP;
|
|
}
|
|
if (pllRamp >= PLL_RAMP_LEN)
|
|
{
|
|
rxBits >>= 1;
|
|
|
|
if (rxIntegrator >= 5)
|
|
rxBits |= 0x800;
|
|
|
|
pllRamp -= PLL_RAMP_LEN;
|
|
rxIntegrator = 0;
|
|
|
|
if(mode == MODE_RECEIVING)
|
|
{
|
|
if (++rxCount >= 12)
|
|
{
|
|
uint8_t currentByte = (symbolDecode(rxBits & 0x3f)) | symbolDecode(rxBits >> 6) << 4;
|
|
rxBuf[PACKET_LENGTH-1-(rxLen++)] = currentByte;
|
|
|
|
if (rxLen >= PACKET_LENGTH)
|
|
{
|
|
mode = MODE_SEARCHING;
|
|
uint32_t* packet = reinterpret_cast<uint32_t*>(rxBuf+2);
|
|
uint16_t crc = rxBuf[0] << 8 | rxBuf[1];
|
|
uint16_t crcC = calcCrc(*packet);
|
|
if(crc != crcC)
|
|
{
|
|
if(errorCodeHandler)
|
|
errorCodeHandler(ERROR_CRC, userData);
|
|
}
|
|
else
|
|
{
|
|
packetCallback(*packet, userData);
|
|
}
|
|
}
|
|
rxCount = 0;
|
|
}
|
|
}
|
|
else if (rxBits == 0xb38)
|
|
{
|
|
mode = MODE_RECEIVING;
|
|
rxCount = 0;
|
|
rxLen = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
void W433DataReciver::interrupt()
|
|
{
|
|
pll(readPin(port, pin));
|
|
}
|
|
|
|
uint16_t W433DataReciver::calculateOverflowRegister(uint16_t bitRate, uint16_t devisor)
|
|
{
|
|
return (F_CPU /(8UL*devisor))/bitRate;
|
|
}
|