Added reciver debugging
This commit is contained in:
@ -8,8 +8,8 @@
|
||||
|
||||
W433DataReciver* W433DataReciver::instance = nullptr;
|
||||
|
||||
W433DataReciver::W433DataReciver(volatile unsigned char* const port , const unsigned char pin, volatile uint16_t * timerRegister, volatile uint8_t* const timerOverflowRegister, void (* const packetCallback)(uint32_t, void*), void* const userData ):
|
||||
_port(port), _pin(pin), _timerRegister(timerRegister), _timerOverflowRegister(timerOverflowRegister), _packetCallback(packetCallback), _userData(userData)
|
||||
W433DataReciver::W433DataReciver(volatile unsigned char* const port , const unsigned char pin, volatile uint16_t * timerRegister, volatile uint8_t* const timerOverflowRegister, void (* const packetCallback)(uint32_t, void*), void* const userData, void (*errorCodeHandler)(uint8_t, void*) ):
|
||||
_port(port), _pin(pin), _timerRegister(timerRegister), _timerOverflowRegister(timerOverflowRegister), _packetCallback(packetCallback), _errorCodeHandler(errorCodeHandler), _userData(userData)
|
||||
{
|
||||
instance = this;
|
||||
for(uint8_t i = 0; i < 33; i++) timesBuffer[i] = 0;
|
||||
@ -62,7 +62,11 @@ bool W433DataReciver::reciveSync(const uint16_t elapsedTime)
|
||||
{
|
||||
++syncCount;
|
||||
}
|
||||
else syncCount = 0;
|
||||
else
|
||||
{
|
||||
if(syncCount > 5) error(ERR_SYNC_FAIL);
|
||||
syncCount = 0;
|
||||
}
|
||||
if(syncCount > 10) return true;
|
||||
else return false;
|
||||
}
|
||||
@ -81,12 +85,21 @@ uint8_t W433DataReciver::assmbleByte()
|
||||
{
|
||||
int8_t bit = reciveBit(i*4);
|
||||
if(bit >= 0) byte = byte | (bit << (7-i));
|
||||
else setState(LOOKING_FOR_SYNC);
|
||||
else
|
||||
{
|
||||
setState(LOOKING_FOR_SYNC);
|
||||
error(ERR_BYTE_ASM);
|
||||
}
|
||||
}
|
||||
timesBufferIndex = 0;
|
||||
return byte;
|
||||
}
|
||||
|
||||
void W433DataReciver::error(const uint8_t errorCode)
|
||||
{
|
||||
if(_errorCodeHandler != nullptr) (*_errorCodeHandler)(errorCode, _userData);
|
||||
}
|
||||
|
||||
void W433DataReciver::setState(const uint8_t stateIn)
|
||||
{
|
||||
state = stateIn;
|
||||
@ -119,7 +132,11 @@ void W433DataReciver::interrupt()
|
||||
{
|
||||
if(elapsedTime > SYNC_TIME + 50)
|
||||
{
|
||||
if(elapsedTime < LARGE_TIME + 50) setState(LOOKING_FOR_SYNC);
|
||||
if(elapsedTime < LARGE_TIME + 50)
|
||||
{
|
||||
setState(LOOKING_FOR_SYNC);
|
||||
error(ERR_NO_SYNC_END);
|
||||
}
|
||||
else
|
||||
{
|
||||
timesBuffer[0] = -LARGE_TIME;
|
||||
@ -134,7 +151,11 @@ void W433DataReciver::interrupt()
|
||||
{
|
||||
uint8_t recivedSignature = assmbleByte();
|
||||
if( recivedSignature == signature) setState(RECVING_PACKET);
|
||||
else setState(LOOKING_FOR_SYNC);
|
||||
else
|
||||
{
|
||||
error(ERR_WRONG_SIG);
|
||||
setState(LOOKING_FOR_SYNC);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if( state == RECVING_PACKET )
|
||||
@ -170,6 +191,7 @@ void W433DataReciver::interrupt()
|
||||
_ringBuffer.write(const_cast<uint8_t*>(buffer), sizeof(packet));
|
||||
if(_packetCallback != nullptr)(*_packetCallback)(packet, _userData);
|
||||
}
|
||||
else error(ERR_CHECKSUM);
|
||||
packet = 0;
|
||||
setState(LOOKING_FOR_SYNC);
|
||||
}
|
||||
|
@ -7,6 +7,13 @@ 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:
|
||||
|
||||
@ -31,9 +38,8 @@ private:
|
||||
static constexpr uint8_t LOOKING_FOR_SIGNATURE = 2;
|
||||
static constexpr uint8_t RECVING_PACKET = 3;
|
||||
static constexpr uint8_t RECVING_PACKET_CHECKSUM = 4;
|
||||
|
||||
//variables
|
||||
|
||||
//variables
|
||||
volatile unsigned char *_port;
|
||||
unsigned char _pin;
|
||||
|
||||
@ -49,7 +55,8 @@ private:
|
||||
volatile uint8_t packetIndex = 0;
|
||||
volatile uint32_t packet = 0;
|
||||
|
||||
void (* const _packetCallback)(uint32_t, void*) = nullptr;
|
||||
void (* const _packetCallback)(uint32_t, void*);
|
||||
void (* const _errorCodeHandler)(uint8_t, void*);
|
||||
void* const _userData;
|
||||
|
||||
volatile uint8_t syncCount = 0;
|
||||
@ -62,12 +69,14 @@ private:
|
||||
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 );
|
||||
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();
|
||||
|
13
main.cpp
13
main.cpp
@ -372,6 +372,17 @@ void serialDispatch(Serial* serial, SVector<WirelessRelay, MAX_RELAYS>* relays,
|
||||
}
|
||||
}
|
||||
|
||||
void reciverError(uint8_t code, void* userData)
|
||||
{
|
||||
if(!sensorsPaused)
|
||||
{
|
||||
Serial* serial = reinterpret_cast<Serial*>(userData);
|
||||
serial->write_p(PSTR("ERROR CODE: "));
|
||||
serial->write(code);
|
||||
serial->putChar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
void sensorPacketRecived(uint32_t data, void* userData)
|
||||
{
|
||||
if(!sensorsPaused)
|
||||
@ -424,7 +435,7 @@ int main()
|
||||
|
||||
setBit(&PCICR, PCIE1, true);
|
||||
setBit(&PCMSK1, PCINT8, true);
|
||||
W433DataReciver reciver(&PINC, PC0, &TCNT1, &TIFR1, &sensorPacketRecived, reinterpret_cast<void*>(&serial));
|
||||
W433DataReciver reciver(&PINC, PC0, &TCNT1, &TIFR1, &sensorPacketRecived, reinterpret_cast<void*>(&serial), &reciverError);
|
||||
|
||||
serial.write_p(PSTR("RGBController v1.0 starting\n"));
|
||||
|
||||
|
@ -109,9 +109,8 @@ void RgbLed::setFade(bool fade)
|
||||
uint8_t RgbLed::applyCal(uint16_t value, const uint16_t* cal)
|
||||
{
|
||||
uint16_t calValue;
|
||||
if(value < 128) calValue = cal[0] + ((cal[1] - cal[0])*(value/8))/16;
|
||||
else calValue = cal[1] + ((cal[2] - cal[1])*((value-128)/8))/16;
|
||||
|
||||
if(value < 128) calValue = cal[0] + ((cal[1] - cal[0])*((uint32_t)value))/128;
|
||||
else calValue = cal[1] + ((cal[2] - cal[1])*((uint32_t)(value-128)))/128;
|
||||
return (value*calValue)/1000;
|
||||
}
|
||||
|
||||
|
4
rgbled.h
4
rgbled.h
@ -7,9 +7,9 @@ private:
|
||||
Pwm8b* _pwmA;
|
||||
Pwm8b* _pwmB;
|
||||
|
||||
static constexpr uint16_t calRed[] = {650, 650, 650};
|
||||
static constexpr uint16_t calRed[] = {1000, 1000, 1000};
|
||||
static constexpr uint16_t calGreen[] = {1000, 1000, 1000};
|
||||
static constexpr uint16_t calBlue[] = {200, 250, 300};
|
||||
static constexpr uint16_t calBlue[] = {400, 500, 500};
|
||||
|
||||
|
||||
|
||||
|
125
ringbuffer.h
Executable file
125
ringbuffer.h
Executable file
@ -0,0 +1,125 @@
|
||||
/*UVOS*/
|
||||
|
||||
/* This file is part of TelemetrySystem.
|
||||
*
|
||||
* TelemetrySystem is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License (LGPL) version 3 as published by
|
||||
* the Free Software Foundation.
|
||||
*
|
||||
* TelemetrySystem is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with TelemetrySystem. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
template < int BUFFER_SIZE, typename T = uint8_t >
|
||||
class RingBuffer
|
||||
{
|
||||
private:
|
||||
|
||||
volatile uint_fast16_t _headIndex = 0;
|
||||
volatile uint_fast16_t _tailIndex = 0;
|
||||
volatile bool _overrun = false;
|
||||
volatile T _buffer[BUFFER_SIZE];
|
||||
|
||||
public:
|
||||
|
||||
RingBuffer()
|
||||
{
|
||||
flush();
|
||||
}
|
||||
|
||||
uint_fast16_t remaining() const volatile
|
||||
{
|
||||
return (_headIndex-_tailIndex);
|
||||
}
|
||||
|
||||
uint_fast16_t remainingCapacity() const volatile
|
||||
{
|
||||
return BUFFER_SIZE - (_headIndex-_tailIndex);
|
||||
}
|
||||
|
||||
bool isOverun() volatile
|
||||
{
|
||||
bool returnVal = _overrun;
|
||||
_overrun = false;
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
bool isEmpty() const volatile
|
||||
{
|
||||
return _tailIndex >= _headIndex;
|
||||
}
|
||||
|
||||
T read() volatile
|
||||
{
|
||||
if(!isEmpty())
|
||||
{
|
||||
_tailIndex++;
|
||||
return _buffer[(_tailIndex - 1) % BUFFER_SIZE];
|
||||
}
|
||||
else return '\0';
|
||||
}
|
||||
|
||||
unsigned int read( T* buffer, unsigned int length ) volatile
|
||||
{
|
||||
unsigned int i = 0;
|
||||
for(; i < length && !isEmpty(); i++)
|
||||
{
|
||||
buffer[i] = read();
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
void write( T in ) volatile
|
||||
{
|
||||
if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0)
|
||||
{
|
||||
_headIndex -= BUFFER_SIZE;
|
||||
_tailIndex -= BUFFER_SIZE;
|
||||
}
|
||||
_buffer[_headIndex % BUFFER_SIZE] = in;
|
||||
_headIndex++;
|
||||
if(remaining() > BUFFER_SIZE)
|
||||
{
|
||||
_overrun = true;
|
||||
_tailIndex = _headIndex - BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
void write( T* buffer, const unsigned int length ) volatile
|
||||
{
|
||||
for(unsigned int i = 0; i < length; i++) write(buffer[i]);
|
||||
}
|
||||
|
||||
void flush(T flushCharacter = ' ') volatile
|
||||
{
|
||||
_headIndex = 0;
|
||||
_tailIndex = 0;
|
||||
for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = flushCharacter;
|
||||
}
|
||||
|
||||
unsigned int getString(T terminator, T* buffer, const unsigned int bufferLength) volatile
|
||||
{
|
||||
unsigned int i = 0;
|
||||
for(; i <= remaining() && i <= BUFFER_SIZE && _buffer[(_tailIndex+i) % BUFFER_SIZE] != terminator; i++);
|
||||
|
||||
if( i < remaining() && i > 0)
|
||||
{
|
||||
if(i > bufferLength-1) i = bufferLength-1;
|
||||
read(buffer, i);
|
||||
buffer[i]='\0';
|
||||
_tailIndex++;
|
||||
}
|
||||
else if(i == 0) _tailIndex++;
|
||||
else i = 0;
|
||||
|
||||
return i;
|
||||
}
|
||||
};
|
33
watchdog.h
Normal file
33
watchdog.h
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include <avr/wdt.h>
|
||||
|
||||
#define wdt_set(value) \
|
||||
__asm__ __volatile__ ( \
|
||||
"in __tmp_reg__,__SREG__" "\n\t" \
|
||||
"cli" "\n\t" \
|
||||
"wdr" "\n\t" \
|
||||
"sts %0,%1" "\n\t" \
|
||||
"out __SREG__,__tmp_reg__" "\n\t" \
|
||||
"sts %0,%2" \
|
||||
: /* no outputs */ \
|
||||
: "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
|
||||
"r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
|
||||
"r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
|
||||
_BV(WDIE) | (value & 0x07)) ) \
|
||||
: "r0" \
|
||||
)
|
||||
|
||||
|
||||
#define wdt_off(x) \
|
||||
__asm__ __volatile__ ( \
|
||||
"in __tmp_reg__,__SREG__" "\n\t" \
|
||||
"cli" "\n\t" \
|
||||
"wdr" "\n\t" \
|
||||
"sts %0,%1" "\n\t" \
|
||||
"sts %0,%2" \
|
||||
: /* no outputs */ \
|
||||
: "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
|
||||
"r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
|
||||
"r" ((uint8_t) (0x00)) \
|
||||
: "r0" \
|
||||
)
|
Reference in New Issue
Block a user