reformat in uvostyle

This commit is contained in:
2022-04-12 00:36:58 +02:00
parent 00ca1d6f03
commit 4aca3c3a11
22 changed files with 1260 additions and 1234 deletions

View File

@ -8,112 +8,115 @@
W433DataReciver* W433DataReciver::instance = nullptr; 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, void (*errorCodeHandler)(uint8_t, void*) ): W433DataReciver::W433DataReciver(volatile unsigned char* const port, const unsigned char pin,
_port(port), _pin(pin), _timerRegister(timerRegister), _timerOverflowRegister(timerOverflowRegister), _packetCallback(packetCallback), _errorCodeHandler(errorCodeHandler), _userData(userData) 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; instance = this;
for(uint8_t i = 0; i < 33; i++) timesBuffer[i] = 0; for(uint8_t i = 0; i < 33; i++) timesBuffer[i] = 0;
} }
W433DataReciver::~W433DataReciver() W433DataReciver::~W433DataReciver()
{ {
instance = nullptr; instance = nullptr;
} }
void W433DataReciver::staticInterrupt() void W433DataReciver::staticInterrupt()
{ {
if(instance != nullptr) instance->interrupt(); if(instance != nullptr) instance->interrupt();
} }
int8_t W433DataReciver::reciveBit(uint8_t index) 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;
} }
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;
} }
else return -1; else return -1;
} }
void W433DataReciver::waitForReciveIdle(const uint16_t timeoutMs) void W433DataReciver::waitForReciveIdle(const uint16_t timeoutMs)
{ {
uint16_t counter = 0; uint16_t counter = 0;
while(true) while(true)
{ {
while(counter < timeoutMs && state != LOOKING_FOR_SYNC) while(counter < timeoutMs && state != LOOKING_FOR_SYNC)
{ {
_delay_ms(1); _delay_ms(1);
++counter; ++counter;
} }
_delay_ms(500); _delay_ms(500);
counter+=500; counter+=500;
if(state == LOOKING_FOR_SYNC || counter >= timeoutMs) break; if(state == LOOKING_FOR_SYNC || counter >= timeoutMs) break;
} }
} }
bool W433DataReciver::isTime(int16_t input, const uint16_t time, const bool state, const uint16_t tollerance) bool W433DataReciver::isTime(int16_t input, const uint16_t time, const bool state, const uint16_t tollerance)
{ {
if((state && input < 0) || (!state && input > 0)) return false; if((state && input < 0) || (!state && input > 0)) return false;
input = abs(input); input = abs(input);
return input < (int16_t)(time+tollerance) && input > (int16_t)(time-tollerance); return input < (int16_t)(time+tollerance) && input > (int16_t)(time-tollerance);
} }
bool W433DataReciver::reciveSync(const uint16_t elapsedTime) bool W433DataReciver::reciveSync(const uint16_t elapsedTime)
{ {
if(elapsedTime < SYNC_TIME+SYNC_TIME_TOLERANCE && elapsedTime > SYNC_TIME-SYNC_TIME_TOLERANCE) if(elapsedTime < SYNC_TIME+SYNC_TIME_TOLERANCE && elapsedTime > SYNC_TIME-SYNC_TIME_TOLERANCE)
{ {
++syncCount; ++syncCount;
} }
else else
{ {
if(syncCount > 4 && syncFailCount < 3) ++syncFailCount; if(syncCount > 4 && syncFailCount < 3) ++syncFailCount;
else else
{ {
//if(syncCount > 7) error(ERR_SYNC_FAIL); //if(syncCount > 7) error(ERR_SYNC_FAIL);
setState(LOOKING_FOR_SYNC); setState(LOOKING_FOR_SYNC);
} }
} }
if(syncCount > 10) return true; if(syncCount > 10) return true;
else return false; else return false;
} }
bool W433DataReciver::recivedByte(const uint16_t elapsedTime) 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); if(timesBufferIndex == 32) writePin(&PORTC, PC0, true);
return timesBufferIndex == 32; return timesBufferIndex == 32;
} }
uint8_t W433DataReciver::assmbleByte() uint8_t W433DataReciver::assmbleByte()
{ {
uint8_t byte = 0; uint8_t byte = 0;
for(uint8_t i = 0; i < 8; ++i) for(uint8_t i = 0; i < 8; ++i)
{ {
int8_t bit = reciveBit(i*4); int8_t bit = reciveBit(i*4);
if(bit >= 0) byte = byte | (bit << (7-i)); if(bit >= 0) byte = byte | (bit << (7-i));
else else
{ {
setState(LOOKING_FOR_SYNC); setState(LOOKING_FOR_SYNC);
error(ERR_BYTE_ASM); error(ERR_BYTE_ASM);
} }
} }
timesBufferIndex = 0; timesBufferIndex = 0;
return byte; return byte;
} }
void W433DataReciver::error(const uint8_t errorCode) void W433DataReciver::error(const uint8_t errorCode)
@ -123,18 +126,19 @@ void W433DataReciver::error(const uint8_t errorCode)
void W433DataReciver::setState(const uint8_t stateIn) void W433DataReciver::setState(const uint8_t stateIn)
{ {
state = stateIn; state = stateIn;
timesBufferIndex = 0; timesBufferIndex = 0;
packetIndex = 0; packetIndex = 0;
syncCount = 0; syncCount = 0;
syncFailCount = 0; syncFailCount = 0;
} }
void W433DataReciver::interrupt() void W433DataReciver::interrupt()
{ {
uint16_t elapsedTime = polarity*(((*_timerOverflowRegister & 0x01) ? *_timerRegister+(UINT16_MAX - previousTime) : *_timerRegister - previousTime)/TICKS_PER_US); uint16_t elapsedTime = polarity*(((*_timerOverflowRegister & 0x01) ? *_timerRegister+
(UINT16_MAX - previousTime) : *_timerRegister - previousTime)/TICKS_PER_US);
if(elapsedTime < DISCARD_TIME)
if(elapsedTime < DISCARD_TIME)
{ {
if(timesBufferIndex > 0 && elapsedTime + abs(timesBuffer[timesBufferIndex-1]) < LARGE_TIME+LARGE_TIME_TOLERANCE) if(timesBufferIndex > 0 && elapsedTime + abs(timesBuffer[timesBufferIndex-1]) < LARGE_TIME+LARGE_TIME_TOLERANCE)
{ {
@ -144,89 +148,90 @@ void W433DataReciver::interrupt()
} }
return; return;
} }
previousTime = *_timerRegister; previousTime = *_timerRegister;
*_timerOverflowRegister = *_timerOverflowRegister | 0x01; *_timerOverflowRegister = *_timerOverflowRegister | 0x01;
if(state == LOOKING_FOR_SYNC && reciveSync(elapsedTime)) if(state == LOOKING_FOR_SYNC && reciveSync(elapsedTime))
{ {
setState(LOOKING_FOR_SYNC_END); setState(LOOKING_FOR_SYNC_END);
} }
else if(state == LOOKING_FOR_SYNC_END) else if(state == LOOKING_FOR_SYNC_END)
{ {
if(elapsedTime > SYNC_TIME + SYNC_END_TIME_TOLERANCE) if(elapsedTime > SYNC_TIME + SYNC_END_TIME_TOLERANCE)
{ {
if(elapsedTime < LARGE_TIME - LARGE_TIME_TOLERANCE) if(elapsedTime < LARGE_TIME - LARGE_TIME_TOLERANCE)
{ {
setState(LOOKING_FOR_SYNC); setState(LOOKING_FOR_SYNC);
error(ERR_NO_SYNC_END); error(ERR_NO_SYNC_END);
} }
else else
{ {
timesBuffer[0] = -LARGE_TIME; timesBuffer[0] = -LARGE_TIME;
setState(LOOKING_FOR_SIGNATURE); setState(LOOKING_FOR_SIGNATURE);
++timesBufferIndex; ++timesBufferIndex;
} }
} }
} }
else if(state == LOOKING_FOR_SIGNATURE) else if(state == LOOKING_FOR_SIGNATURE)
{ {
if(recivedByte(elapsedTime)) if(recivedByte(elapsedTime))
{ {
uint8_t recivedSignature = assmbleByte(); uint8_t recivedSignature = assmbleByte();
if( recivedSignature == signature) setState(RECVING_PACKET); if( recivedSignature == signature) setState(RECVING_PACKET);
else else
{ {
error(ERR_WRONG_SIG); error(ERR_WRONG_SIG);
setState(LOOKING_FOR_SYNC); setState(LOOKING_FOR_SYNC);
} }
} }
} }
else if( state == RECVING_PACKET ) else if( state == RECVING_PACKET )
{ {
if(recivedByte(elapsedTime)) if(recivedByte(elapsedTime))
{ {
uint8_t packetByte = assmbleByte(); uint8_t packetByte = assmbleByte();
packet = packet | ((uint32_t)packetByte) << ((3-packetIndex)*8); packet = packet | ((uint32_t)packetByte) << ((3-packetIndex)*8);
++packetIndex; ++packetIndex;
if(packetIndex > 3) if(packetIndex > 3)
{ {
packetIndex = 0; packetIndex = 0;
timesBufferIndex = 0; timesBufferIndex = 0;
setState(RECVING_PACKET_CHECKSUM); setState(RECVING_PACKET_CHECKSUM);
} }
} }
} }
else if(state == RECVING_PACKET_CHECKSUM) else if(state == RECVING_PACKET_CHECKSUM)
{ {
if(recivedByte(elapsedTime)) if(recivedByte(elapsedTime))
{ {
uint8_t recivedChecksum = assmbleByte(); uint8_t recivedChecksum = assmbleByte();
volatile uint8_t* buffer = reinterpret_cast<volatile uint8_t*>(&packet); volatile uint8_t* buffer = reinterpret_cast<volatile uint8_t*>(&packet);
uint8_t computedChecksum = 0; uint8_t computedChecksum = 0;
for(uint8_t j = 0; j < sizeof(packet); j++) for(uint8_t i = 0; i < 8; i++) computedChecksum = computedChecksum + ((buffer[j] & ( 1 << (8 - i))) >> (8 - i)); for(uint8_t j = 0; j < sizeof(packet); j++) for(uint8_t i = 0; i < 8;
//for(uint8_t j = 0; j < sizeof(packet); j++) for(uint8_t i = 0; i < 8; i++) computedChecksum = computedChecksum + (buffer[j] & ( 1 << (8 - i))); i++) computedChecksum = computedChecksum + ((buffer[j] & ( 1 << (8 - i))) >> (8 - i));
//for(uint8_t j = 0; j < sizeof(packet); j++) for(uint8_t i = 0; i < 8; i++) computedChecksum = computedChecksum + (buffer[j] & ( 1 << (8 - i)));
if(computedChecksum == recivedChecksum)
{ if(computedChecksum == recivedChecksum)
#ifdef USE_RINGBUFFER {
_ringBuffer.write(const_cast<uint8_t*>(buffer), sizeof(packet)); #ifdef USE_RINGBUFFER
#endif _ringBuffer.write(const_cast<uint8_t*>(buffer), sizeof(packet));
if(_packetCallback != nullptr)(*_packetCallback)(packet, _userData); #endif
} if(_packetCallback != nullptr)(*_packetCallback)(packet, _userData);
else error(ERR_CHECKSUM); }
packet = 0; else error(ERR_CHECKSUM);
setState(LOOKING_FOR_SYNC); packet = 0;
} setState(LOOKING_FOR_SYNC);
} }
}
} }
#ifdef USE_RINGBUFFER #ifdef USE_RINGBUFFER
RingBuffer<W433DataReciver::RINGBUFFER_LENGTH, uint8_t>* W433DataReciver::getRingBuffer() RingBuffer<W433DataReciver::RINGBUFFER_LENGTH, uint8_t>* W433DataReciver::getRingBuffer()
{ {
return &_ringBuffer; return &_ringBuffer;
} }
#endif #endif

View File

@ -7,88 +7,90 @@
class W433DataReciver class W433DataReciver
{ {
public: public:
static constexpr uint8_t RINGBUFFER_LENGTH = 32; static constexpr uint8_t RINGBUFFER_LENGTH = 32;
//errors //errors
static constexpr uint8_t ERR_SYNC_FAIL = 1; static constexpr uint8_t ERR_SYNC_FAIL = 1;
static constexpr uint8_t ERR_NO_SYNC_END = 2; static constexpr uint8_t ERR_NO_SYNC_END = 2;
static constexpr uint8_t ERR_BYTE_ASM = 3; static constexpr uint8_t ERR_BYTE_ASM = 3;
static constexpr uint8_t ERR_WRONG_SIG = 4; static constexpr uint8_t ERR_WRONG_SIG = 4;
static constexpr uint8_t ERR_CHECKSUM = 5; static constexpr uint8_t ERR_CHECKSUM = 5;
private: private:
static W433DataReciver* instance; static W433DataReciver* instance;
//constants //constants
static constexpr uint8_t CLOCK_DEVIDER = 1; static constexpr uint8_t CLOCK_DEVIDER = 1;
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 uint16_t SYNC_TIME = 800; static constexpr uint16_t SYNC_TIME = 800;
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.50; 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 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 uint16_t DISCARD_TIME = SMALL_TIME*0.6; 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;
static constexpr int8_t polarity = 1; static constexpr int8_t polarity = 1;
static constexpr uint8_t LOOKING_FOR_SYNC = 0; static constexpr uint8_t LOOKING_FOR_SYNC = 0;
static constexpr uint8_t LOOKING_FOR_SYNC_END = 1; static constexpr uint8_t LOOKING_FOR_SYNC_END = 1;
static constexpr uint8_t LOOKING_FOR_SIGNATURE = 2; static constexpr uint8_t LOOKING_FOR_SIGNATURE = 2;
static constexpr uint8_t RECVING_PACKET = 3; static constexpr uint8_t RECVING_PACKET = 3;
static constexpr uint8_t RECVING_PACKET_CHECKSUM = 4; static constexpr uint8_t RECVING_PACKET_CHECKSUM = 4;
//variables //variables
volatile unsigned char *_port; volatile unsigned char *_port;
unsigned char _pin; unsigned char _pin;
volatile uint16_t *_timerRegister; volatile uint16_t *_timerRegister;
volatile uint8_t *_timerOverflowRegister; volatile uint8_t *_timerOverflowRegister;
#ifdef USE_RINGBUFFER #ifdef USE_RINGBUFFER
RingBuffer<RINGBUFFER_LENGTH, uint8_t> _ringBuffer; RingBuffer<RINGBUFFER_LENGTH, uint8_t> _ringBuffer;
#endif #endif
volatile uint16_t previousTime = 0; volatile uint16_t previousTime = 0;
volatile uint8_t timesBufferIndex = 0; volatile uint8_t timesBufferIndex = 0;
volatile int16_t timesBuffer[33]; volatile int16_t timesBuffer[33];
volatile uint8_t packetIndex = 0; volatile uint8_t packetIndex = 0;
volatile uint32_t packet = 0; volatile uint32_t packet = 0;
void (* const _packetCallback)(uint32_t, void*); void (* const _packetCallback)(uint32_t, void*);
void (* const _errorCodeHandler)(uint8_t, void*); void (* const _errorCodeHandler)(uint8_t, void*);
void* const _userData; void* const _userData;
volatile uint8_t syncCount = 0; volatile uint8_t syncCount = 0;
volatile uint8_t syncFailCount = 0; volatile uint8_t syncFailCount = 0;
volatile uint8_t state = 0; volatile uint8_t state = 0;
//private functions //private functions
int8_t reciveBit(uint8_t index); int8_t reciveBit(uint8_t index);
inline uint8_t assmbleByte(); inline uint8_t assmbleByte();
inline void setState(const uint8_t stateIn); inline void setState(const uint8_t stateIn);
inline bool recivedByte(const uint16_t elapsedTime); inline bool recivedByte(const uint16_t elapsedTime);
inline bool reciveSync(const uint16_t elapsedTime); inline bool reciveSync(const uint16_t elapsedTime);
inline void error(const uint8_t errorCode); 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 inline bool isTime(int16_t input, const uint16_t time, const bool state = true, const uint16_t tollerance = 100);
public: 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(volatile unsigned char* const port, const unsigned char pin, volatile uint16_t * timerRegister,
~W433DataReciver(); volatile uint8_t* const timerOverflowRegister, void (*packetCallback)(uint32_t, void*) = nullptr,
static void initTimer(); void* userData = nullptr, void (*errorCodeHandler)(uint8_t, void*) = nullptr );
static void staticInterrupt(); ~W433DataReciver();
void waitForReciveIdle(const uint16_t timeoutMs = 10000); static void initTimer();
void interrupt(); static void staticInterrupt();
void waitForReciveIdle(const uint16_t timeoutMs = 10000);
void interrupt();
#ifdef USE_RINGBUFFER #ifdef USE_RINGBUFFER
RingBuffer<RINGBUFFER_LENGTH, uint8_t>* getRingBuffer(); RingBuffer<RINGBUFFER_LENGTH, uint8_t>* getRingBuffer();
#endif #endif
}; };

View File

@ -5,94 +5,94 @@ W433DataTransmitter::W433DataTransmitter(volatile unsigned char *port, const uns
{ {
} }
void W433DataTransmitter::sendBit(const bool bit) void W433DataTransmitter::sendBit(const bool bit)
{ {
switch(bit) switch(bit)
{ {
case true: case true:
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(SMALL_TIME); _delay_us(SMALL_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(SMALL_TIME); _delay_us(SMALL_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
break; break;
case false: case false:
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(SMALL_TIME); _delay_us(SMALL_TIME);
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(SMALL_TIME); _delay_us(SMALL_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
break; break;
} }
} }
void W433DataTransmitter::sendSyncpulse() void W433DataTransmitter::sendSyncpulse()
{ {
for(uint8_t i = 0; i < 25; ++i) for(uint8_t i = 0; i < 25; ++i)
{ {
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(SYNC_TIME); _delay_us(SYNC_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(SYNC_TIME); _delay_us(SYNC_TIME);
} }
} }
void W433DataTransmitter::sendEndPulse() void W433DataTransmitter::sendEndPulse()
{ {
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(LARGE_TIME*10); _delay_us(LARGE_TIME*10);
} }
void W433DataTransmitter::sendRawData(const uint8_t data) void W433DataTransmitter::sendRawData(const uint8_t data)
{ {
for(uint8_t i = 0; i < 8; i++) sendBit(data & ( 1 << (7 - i))); for(uint8_t i = 0; i < 8; i++) sendBit(data & ( 1 << (7 - i)));
} }
void W433DataTransmitter::sendPacket(const uint32_t data) void W433DataTransmitter::sendPacket(const uint32_t data)
{ {
sendSyncpulse(); sendSyncpulse();
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
sendRawData(signature); sendRawData(signature);
uint8_t checksum = 0; uint8_t checksum = 0;
for(uint8_t i = 0; i < 4; ++i) for(uint8_t i = 0; i < 4; ++i)
{ {
uint8_t dataOctet = (data & (0xFF000000 >> i*8 )) >> (24 - 8*i); uint8_t dataOctet = (data & (0xFF000000 >> i*8 )) >> (24 - 8*i);
//for(uint8_t i = 0; i < 8; i++) checksum = checksum + (dataOctet & ( 1 << (8 - i))); //for(uint8_t i = 0; i < 8; i++) checksum = checksum + (dataOctet & ( 1 << (8 - i)));
for(uint8_t i = 0; i < 8; i++) checksum = checksum + ((dataOctet & ( 1 << (8 - i))) >> (8 - i)); for(uint8_t i = 0; i < 8; i++) checksum = checksum + ((dataOctet & ( 1 << (8 - i))) >> (8 - i));
sendRawData( dataOctet ); sendRawData( dataOctet );
} }
sendRawData( checksum ); sendRawData( checksum );
sendEndPulse(); sendEndPulse();
} }
void W433DataTransmitter::send(const uint8_t* const data, uint16_t length) void W433DataTransmitter::send(const uint8_t* const data, uint16_t length)
{ {
uint16_t packets = length/4; uint16_t packets = length/4;
if(length % 4 != 0) ++packets; if(length % 4 != 0) ++packets;
for(uint8_t j = 0; j < packets; j++) for(uint8_t j = 0; j < packets; j++)
{ {
uint32_t paketData = 0; uint32_t paketData = 0;
uint8_t* paketDataPointer = reinterpret_cast<uint8_t*>(&paketData); uint8_t* paketDataPointer = reinterpret_cast<uint8_t*>(&paketData);
for(uint8_t i = 0; i < 4 && j*4+i < length; i++) paketDataPointer[3-i] = data[j*4+i]; for(uint8_t i = 0; i < 4 && j*4+i < length; i++) paketDataPointer[3-i] = data[j*4+i];
sendPacket(paketData); sendPacket(paketData);
} }
} }
void W433DataTransmitter::send(const uint8_t data) void W433DataTransmitter::send(const uint8_t data)
{ {
sendPacket(data); sendPacket(data);
} }

View File

@ -7,24 +7,24 @@
class W433DataTransmitter class W433DataTransmitter
{ {
private: 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 uint16_t SYNC_TIME = 800; static constexpr uint16_t SYNC_TIME = 800;
static constexpr uint8_t signature = 0xA5; static constexpr uint8_t signature = 0xA5;
volatile unsigned char * const _port; volatile unsigned char * const _port;
const unsigned char _pin; const unsigned char _pin;
void sendBit(const bool bit); void sendBit(const bool bit);
void sendSyncpulse(); void sendSyncpulse();
void sendRawData(const uint8_t data); void sendRawData(const uint8_t data);
void sendEndPulse(); void sendEndPulse();
public: public:
W433DataTransmitter(volatile unsigned char * const port, const unsigned char pin); W433DataTransmitter(volatile unsigned char * const port, const unsigned char pin);
void send(const uint8_t* const data, uint16_t length); void send(const uint8_t* const data, uint16_t length);
void send(const uint8_t data); void send(const uint8_t data);
void sendPacket(const uint32_t data); void sendPacket(const uint32_t data);
}; };

View File

@ -7,63 +7,63 @@ unsigned char _pin = PB5;
void WirelessRelay::sendId() void WirelessRelay::sendId()
{ {
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(SMALL_TIME); _delay_us(SMALL_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
for(short i = 0; i<10; i++) for(short i = 0; i<10; i++)
{ {
sendBit( id & 1 << (15 - i) ); sendBit( id & 1 << (15 - i) );
} }
} }
void WirelessRelay::sendBit(const bool in) void WirelessRelay::sendBit(const bool in)
{ {
switch(in) switch(in)
{ {
case true: case true:
//Der Code fuer '0' //Der Code fuer '0'
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(SMALL_TIME); _delay_us(SMALL_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(SMALL_TIME); _delay_us(SMALL_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
break; break;
case false: case false:
//Der Code fuer '1' //Der Code fuer '1'
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(SMALL_TIME); _delay_us(SMALL_TIME);
writePin(_port,_pin,true); writePin(_port,_pin,true);
_delay_us(SMALL_TIME); _delay_us(SMALL_TIME);
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(LARGE_TIME); _delay_us(LARGE_TIME);
break; break;
} }
} }
void WirelessRelay::sync() void WirelessRelay::sync()
{ {
writePin(_port,_pin,false); writePin(_port,_pin,false);
_delay_us(SMALL_TIME*31); _delay_us(SMALL_TIME*31);
} }
void WirelessRelay::setValue(const uint8_t value) void WirelessRelay::setValue(const uint8_t value)
{ {
lastValue = value; lastValue = value;
for(short z = 0; z<10; z++) for(short z = 0; z<10; z++)
{ {
sendId(); sendId();
sendBit(value); sendBit(value);
sendBit(!value); sendBit(!value);
sync(); sync();
} }
} }
void WirelessRelay::resend() void WirelessRelay::resend()
@ -73,13 +73,13 @@ void WirelessRelay::resend()
WirelessRelay::WirelessRelay(const uint16_t idIn, char nameIn[]) WirelessRelay::WirelessRelay(const uint16_t idIn, char nameIn[])
{ {
id = idIn; id = idIn;
setName(nameIn); setName(nameIn);
type = 0; type = 0;
} }
WirelessRelay::WirelessRelay(const Item& item) WirelessRelay::WirelessRelay(const Item& item)
{ {
Item::operator=(item); Item::operator=(item);
type = 0; type = 0;
} }

View File

@ -8,20 +8,20 @@
class WirelessRelay: public Item class WirelessRelay: public Item
{ {
public: public:
static constexpr uint16_t LARGE_TIME = 750; static constexpr uint16_t LARGE_TIME = 750;
static constexpr uint8_t SMALL_TIME = 250; static constexpr uint8_t SMALL_TIME = 250;
static constexpr uint16_t MAX_NAME_LENGTH = 16; static constexpr uint16_t MAX_NAME_LENGTH = 16;
private: private:
void sendBit(const bool i); void sendBit(const bool i);
void sync(); void sync();
void sendId(); void sendId();
public: public:
WirelessRelay(const uint16_t idIn, char nameIn[]); WirelessRelay(const uint16_t idIn, char nameIn[]);
WirelessRelay(const Item& item); WirelessRelay(const Item& item);
void setValue(const uint8_t value); void setValue(const uint8_t value);
void resend(); void resend();
}; };
#endif #endif

View File

@ -1,9 +1,9 @@
#pragma once #pragma once
const char *bit_rep[16] = const char *bit_rep[16] =
{ {
[ 0] = "0000", [ 1] = "0001", [ 2] = "0010", [ 3] = "0011", [ 0] = "0000", [ 1] = "0001", [ 2] = "0010", [ 3] = "0011",
[ 4] = "0100", [ 5] = "0101", [ 6] = "0110", [ 7] = "0111", [ 4] = "0100", [ 5] = "0101", [ 6] = "0110", [ 7] = "0111",
[ 8] = "1000", [ 9] = "1001", [10] = "1010", [11] = "1011", [ 8] = "1000", [ 9] = "1001", [10] = "1010", [11] = "1011",
[12] = "1100", [13] = "1101", [14] = "1110", [15] = "1111", [12] = "1100", [13] = "1101", [14] = "1110", [15] = "1111",
}; };

View File

@ -1,51 +1,51 @@
void EEPROM_write_char(uint16_t address, unsigned char data) void EEPROM_write_char(uint16_t address, unsigned char data)
{ {
//Wait for completion of previous write //Wait for completion of previous write
while(EECR & (1<<EEPE)); while(EECR & (1<<EEPE));
//Set up address and Data Registers //Set up address and Data Registers
EEAR = address; EEAR = address;
EEDR = data; EEDR = data;
//Write logical one to EEMPE //Write logical one to EEMPE
EECR |= (1<<EEMPE); EECR |= (1<<EEMPE);
//Start eeprom write by setting EEPE //Start eeprom write by setting EEPE
EECR |= (1<<EEPE); EECR |= (1<<EEPE);
} }
unsigned char EEPROM_read_char(uint16_t uiAddress) unsigned char EEPROM_read_char(uint16_t uiAddress)
{ {
// Wait for completion of previous write // Wait for completion of previous write
while(EECR & (1<<EEPE)); while(EECR & (1<<EEPE));
//Set up address register //Set up address register
EEAR = uiAddress; EEAR = uiAddress;
//Start eeprom read by writing EERE //Start eeprom read by writing EERE
EECR |= (1<<EERE); EECR |= (1<<EERE);
//Return data from Data Register //Return data from Data Register
return EEDR; return EEDR;
} }
void EEPROM_write_string(uint16_t address, char* buffer, uint16_t length) void EEPROM_write_string(uint16_t address, char* buffer, uint16_t length)
{ {
for(uint16_t i = 0; i < length; i++) EEPROM_write_char( address+i, buffer[i] ); for(uint16_t i = 0; i < length; i++) EEPROM_write_char( address+i, buffer[i] );
} }
void EEPROM_read_string(uint16_t address, char* buffer, uint16_t length) void EEPROM_read_string(uint16_t address, char* buffer, uint16_t length)
{ {
for(uint16_t i = 0; i < length; i++) buffer[i] = EEPROM_read_char( address+i); for(uint16_t i = 0; i < length; i++) buffer[i] = EEPROM_read_char( address+i);
} }
template <class T> void EEPROM_write_class(uint16_t address, T& in) template <class T> void EEPROM_write_class(uint16_t address, T& in)
{ {
EEPROM_write_string( address, reinterpret_cast<char*>(&in), sizeof(in)); EEPROM_write_string( address, reinterpret_cast<char*>(&in), sizeof(in));
} }
template <class T> T EEPROM_read_class(uint16_t address) template <class T> T EEPROM_read_class(uint16_t address)
{ {
char data[sizeof(T)]; char data[sizeof(T)];
EEPROM_read_string( address, data, sizeof(T) ); EEPROM_read_string( address, data, sizeof(T) );
return *reinterpret_cast<T*>(data); return *reinterpret_cast<T*>(data);
} }
template <class T> void EEPROM_read_class(uint16_t address, T* in) template <class T> void EEPROM_read_class(uint16_t address, T* in)
{ {
EEPROM_read_string( address, reinterpret_cast<char*>(in), sizeof(T) ); EEPROM_read_string( address, reinterpret_cast<char*>(in), sizeof(T) );
} }

View File

@ -3,7 +3,7 @@
void Item::setName(const char * const nameN) void Item::setName(const char * const nameN)
{ {
size_t len = strlen(nameN); size_t len = strlen(nameN);
if(len < MAX_NAME_LENGTH)memcpy(name, nameN, len+1); if(len < MAX_NAME_LENGTH)memcpy(name, nameN, len+1);
} }

14
item.h
View File

@ -4,11 +4,11 @@
class Item class Item
{ {
public: public:
static constexpr uint16_t MAX_NAME_LENGTH = 16; static constexpr uint16_t MAX_NAME_LENGTH = 16;
bool lastValue = 0; bool lastValue = 0;
uint16_t id; uint16_t id;
char name[MAX_NAME_LENGTH]=""; char name[MAX_NAME_LENGTH]="";
uint8_t type = 0; uint8_t type = 0;
void setName(const char * const name); void setName(const char * const name);
}; };

634
main.cpp
View File

@ -33,13 +33,13 @@ static bool resendEnabled = false;
ISR(PCINT1_vect) ISR(PCINT1_vect)
{ {
W433DataReciver::staticInterrupt(); W433DataReciver::staticInterrupt();
} }
ISR(WDT_vect) ISR(WDT_vect)
{ {
if(++resendCounter > 225) if(++resendCounter > 225)
{ {
resendCounter = 0; resendCounter = 0;
if(resendEnabled)resendNow = true; if(resendEnabled)resendNow = true;
} }
@ -47,8 +47,8 @@ ISR(WDT_vect)
inline static void printHelp(Serial* serial) 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\
item add [id] [type] [name]: Add Wireless item. Save to make permant.\n\ item add [id] [type] [name]: Add Wireless item. Save to make permant.\n\
item delete [n] : Delete n'th item. Save to make permant.\n\ item delete [n] : Delete n'th item. Save to make permant.\n\
@ -82,7 +82,7 @@ int freeRAM()
void save() void save()
{ {
EEPROM_write_char(4, resendEnabled); EEPROM_write_char(4, resendEnabled);
EEPROM_write_class< SVector<Item, MAX_ITEMS> > (ITEM_VECTOR_EEPROM_ADDR, items); EEPROM_write_class< SVector<Item, MAX_ITEMS> > (ITEM_VECTOR_EEPROM_ADDR, items);
} }
void loadRGB(RgbLed* rgbled) void loadRGB(RgbLed* rgbled)
@ -93,47 +93,48 @@ void loadRGB(RgbLed* rgbled)
void load() void load()
{ {
resendEnabled = EEPROM_read_char(4); resendEnabled = EEPROM_read_char(4);
EEPROM_read_class< SVector<Item, MAX_ITEMS> > (ITEM_VECTOR_EEPROM_ADDR, &items); EEPROM_read_class< SVector<Item, MAX_ITEMS> > (ITEM_VECTOR_EEPROM_ADDR, &items);
} }
void writeItemState(Serial* serial, Item* relay, uint8_t number) void writeItemState(Serial* serial, Item* relay, uint8_t number)
{ {
const uint16_t id = relay->id; const uint16_t id = relay->id;
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "ITEM NUMBER: %u ID: %s%s%s%s TYPE: %u STATE: %u NAME: %s\n", number, snprintf(buffer, SNPRINTF_BUFFER_SIZE, "ITEM NUMBER: %u ID: %s%s%s%s TYPE: %u STATE: %u NAME: %s\n", number,
bit_rep[ id >> 12], bit_rep[ id >> 12],
bit_rep[(id & 0x0F00) >> 8 ], bit_rep[(id & 0x0F00) >> 8 ],
bit_rep[(id & 0x00F0) >> 4 ], bit_rep[(id & 0x00F0) >> 4 ],
bit_rep[(id & 0x000F)], bit_rep[(id & 0x000F)],
relay->type, relay->type,
relay->lastValue, relay->lastValue,
relay->name relay->name
); );
serial->write(buffer, SNPRINTF_BUFFER_SIZE); serial->write(buffer, SNPRINTF_BUFFER_SIZE);
} }
void itemDispatch(SVector<Item, MAX_ITEMS>* items, Pwm16b* auxPwm, char* token, Serial* serial) void itemDispatch(SVector<Item, MAX_ITEMS>* items, Pwm16b* auxPwm, char* token, Serial* serial)
{ {
if( strcmp(token, "add") == 0 ) if( strcmp(token, "add") == 0 )
{ {
token = strtok(NULL, " \n"); token = strtok(NULL, " \n");
uint16_t id = strtol(token, nullptr, 2 ); uint16_t id = strtol(token, nullptr, 2 );
token = strtok(NULL, " \n"); token = strtok(NULL, " \n");
uint8_t type = strtol(token, nullptr, 10 ); uint8_t type = strtol(token, nullptr, 10 );
if(id != 0 && (type == 0 || type == 1) && items->remainingCapacity() > 0) if(id != 0 && (type == 0 || type == 1) && items->remainingCapacity() > 0)
{ {
token = strtok(NULL, "\n\0"); token = strtok(NULL, "\n\0");
Item item; Item item;
item.id = id; item.id = id;
item.type = type; item.type = type;
if( token != NULL ) item.setName(token); if( token != NULL ) item.setName(token);
items->push_back(item); items->push_back(item);
writeItemState(serial, &items->back(), items->count()-1); writeItemState(serial, &items->back(), items->count()-1);
} }
else if(items->remainingCapacity() == 0) serial->write_p(PSTR("Relay storage full.\n")); else if(items->remainingCapacity() == 0) serial->write_p(PSTR("Relay storage full.\n"));
else serial->write_p(PSTR("Usage: item add [id] [type] [name]\n [id] being a 16bit binary nummber and [name] an optional string\n")); else serial->write_p(
} PSTR("Usage: item add [id] [type] [name]\n [id] being a 16bit binary nummber and [name] an optional string\n"));
else if( strcmp(token, "delete") == 0 ) }
{ else if( strcmp(token, "delete") == 0 )
{
token = strtok(NULL, " \n"); token = strtok(NULL, " \n");
if(items->count() > 0) if(items->count() > 0)
{ {
@ -143,248 +144,250 @@ void itemDispatch(SVector<Item, MAX_ITEMS>* items, Pwm16b* auxPwm, char* token,
serial->write(buffer, SNPRINTF_BUFFER_SIZE); serial->write(buffer, SNPRINTF_BUFFER_SIZE);
items->erase(index); items->erase(index);
} }
} }
else if( strcmp(token, "on") == 0 ) else if( strcmp(token, "on") == 0 )
{ {
char* token = strtok(NULL, " \n"); char* token = strtok(NULL, " \n");
if( token != NULL) if( token != NULL)
{ {
uint8_t selected = strtol(token, nullptr, 10); uint8_t selected = strtol(token, nullptr, 10);
if (selected < items->count()) if (selected < items->count())
{ {
items->at(selected).lastValue = true; items->at(selected).lastValue = true;
if(items->at(selected).type == 0)WirelessRelay(items->at(selected)).setValue(true); if(items->at(selected).type == 0)WirelessRelay(items->at(selected)).setValue(true);
else UvosItem(items->at(selected)).setValue(true); else UvosItem(items->at(selected)).setValue(true);
writeItemState(serial, &items->at(selected), selected); writeItemState(serial, &items->at(selected), selected);
} }
else serial->write_p(PSTR("No sutch item\n")); else serial->write_p(PSTR("No sutch item\n"));
} }
else serial->write_p(PSTR("Usage: item on [nn]\n")); else serial->write_p(PSTR("Usage: item on [nn]\n"));
} }
else if( strcmp(token, "off") == 0 ) else if( strcmp(token, "off") == 0 )
{ {
char* token = strtok(NULL, " \n"); char* token = strtok(NULL, " \n");
if( token != NULL) if( token != NULL)
{ {
uint8_t selected = strtol(token, nullptr, 10); uint8_t selected = strtol(token, nullptr, 10);
if (selected < items->count()) if (selected < items->count())
{ {
items->at(selected).lastValue = false; items->at(selected).lastValue = false;
if(items->at(selected).type == 0)WirelessRelay(items->at(selected)).setValue(false); if(items->at(selected).type == 0)WirelessRelay(items->at(selected)).setValue(false);
else UvosItem(items->at(selected)).setValue(false); else UvosItem(items->at(selected)).setValue(false);
writeItemState(serial, &items->at(selected), selected); writeItemState(serial, &items->at(selected), selected);
} }
else serial->write_p(PSTR("No sutch item\n")); else serial->write_p(PSTR("No sutch item\n"));
} }
else serial->write_p(PSTR("Usage: item off [nn]\n")); else serial->write_p(PSTR("Usage: item off [nn]\n"));
} }
else if( strcmp(token, "resend") == 0 ) else if( strcmp(token, "resend") == 0 )
{ {
char* token = strtok(NULL, " \n"); char* token = strtok(NULL, " \n");
serial->write_p(PSTR("Resend every 30 min is ")); serial->write_p(PSTR("Resend every 30 min is "));
if( token != NULL ) if( token != NULL )
{ {
serial->write_p(PSTR("now ")); serial->write_p(PSTR("now "));
if(strcmp(token, "on") == 0) resendEnabled = true; if(strcmp(token, "on") == 0) resendEnabled = true;
else resendEnabled = false; else resendEnabled = false;
} }
resendEnabled ? serial->write_p(PSTR("enabled.\n")) : serial->write_p(PSTR("disabled.\n")) ; resendEnabled ? serial->write_p(PSTR("enabled.\n")) : serial->write_p(PSTR("disabled.\n")) ;
} }
else else
{ {
serial->write(token); serial->write(token);
serial->write_p(PSTR(" is not a valid subcommand: item [add/delete/on/off]\n")); serial->write_p(PSTR(" is not a valid subcommand: item [add/delete/on/off]\n"));
} }
} }
void rgbDispatch(RgbLed* rgbled, char* token, Serial* serial) void rgbDispatch(RgbLed* rgbled, char* token, Serial* serial)
{ {
if( strcmp(token, "on") == 0 ) if( strcmp(token, "on") == 0 )
{ {
rgbled->on(); rgbled->on();
serial->write_p(PSTR("RGB lights on\n")); serial->write_p(PSTR("RGB lights on\n"));
} }
else if( strcmp(token, "off") == 0 ) else if( strcmp(token, "off") == 0 )
{ {
rgbled->off(); rgbled->off();
serial->write_p(PSTR("RGB lights off\n")); serial->write_p(PSTR("RGB lights off\n"));
} }
else if( strcmp(token, "print") == 0 ) else if( strcmp(token, "print") == 0 )
{ {
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "Current RGB values:\nR: %u G: %u B: %u\n", rgbled->getR(), rgbled->getG(), rgbled->getB()); snprintf(buffer, SNPRINTF_BUFFER_SIZE, "Current RGB values:\nR: %u G: %u B: %u\n", rgbled->getR(), rgbled->getG(),
serial->write(buffer, SNPRINTF_BUFFER_SIZE); rgbled->getB());
} serial->write(buffer, SNPRINTF_BUFFER_SIZE);
else if( strcmp(token, "set") == 0 ) }
{ else if( strcmp(token, "set") == 0 )
char* rToken = strtok(NULL, " \n"); {
char* gToken = strtok(NULL, " \n"); char* rToken = strtok(NULL, " \n");
char* bToken = strtok(NULL, " \n"); char* gToken = strtok(NULL, " \n");
if(rToken != NULL && gToken != NULL && bToken != NULL) char* bToken = strtok(NULL, " \n");
{ if(rToken != NULL && gToken != NULL && bToken != NULL)
{
uint8_t r = atoi(rToken); uint8_t r = atoi(rToken);
uint8_t g = atoi(gToken); uint8_t g = atoi(gToken);
uint8_t b = atoi(bToken); uint8_t b = atoi(bToken);
rgbled->setSolidColor(r,g,b); rgbled->setSolidColor(r,g,b);
EEPROM_write_char(1, r); EEPROM_write_char(1, r);
EEPROM_write_char(2, g); EEPROM_write_char(2, g);
EEPROM_write_char(3, b); EEPROM_write_char(3, b);
serial->write_p(PSTR("Set RGB values\n")); serial->write_p(PSTR("Set RGB values\n"));
} }
else serial->write_p(PSTR("Usage: rgb set [RRR] [GGG] [BBB]\n")); else serial->write_p(PSTR("Usage: rgb set [RRR] [GGG] [BBB]\n"));
} }
else if( strcmp(token, "pattern") == 0 ) else if( strcmp(token, "pattern") == 0 )
{ {
token = strtok(NULL, " \n"); token = strtok(NULL, " \n");
if( token != NULL ) if( token != NULL )
{ {
rgbled->setPattern(atoi(token)); rgbled->setPattern(atoi(token));
serial->write_p(PSTR("Set Pattern\n")); serial->write_p(PSTR("Set Pattern\n"));
} }
else serial->write_p(PSTR("Usage: rgb pattern [id]\n")); else serial->write_p(PSTR("Usage: rgb pattern [id]\n"));
} }
else if( strcmp(token, "preset") == 0 ) else if( strcmp(token, "preset") == 0 )
{ {
token = strtok(NULL, " \n"); token = strtok(NULL, " \n");
if( token != NULL ) if( token != NULL )
{ {
rgbled->setPreset(atoi(token)); rgbled->setPreset(atoi(token));
serial->write_p(PSTR("Set Preset\n")); serial->write_p(PSTR("Set Preset\n"));
} }
else serial->write_p(PSTR("Usage: rgb preset [ID]\n")); else serial->write_p(PSTR("Usage: rgb preset [ID]\n"));
} }
else if( strcmp(token, "fade") == 0 ) else if( strcmp(token, "fade") == 0 )
{ {
token = strtok(NULL, " \n"); token = strtok(NULL, " \n");
if( token != NULL ) if( token != NULL )
{ {
if( strcmp(token, "on") == 0 ) if( strcmp(token, "on") == 0 )
{ {
rgbled->setFade(true); rgbled->setFade(true);
serial->write_p(PSTR("Turned on Fade\n")); serial->write_p(PSTR("Turned on Fade\n"));
} }
else else
{ {
rgbled->setFade(false); rgbled->setFade(false);
serial->write_p(PSTR("Turned off Fade\n")); serial->write_p(PSTR("Turned off Fade\n"));
} }
} }
else serial->write_p(PSTR("Usage: rgb fade [on/off]\n")); else serial->write_p(PSTR("Usage: rgb fade [on/off]\n"));
} }
else else
{ {
serial->write(token); serial->write(token);
serial->write_p(PSTR(" is not a valid subcommand: rgb [on/off/print/set/pattern/preset/fade]\n")); serial->write_p(PSTR(" is not a valid subcommand: rgb [on/off/print/set/pattern/preset/fade]\n"));
} }
} }
void auxDispatch(Pwm16b* auxPwm, char* token, Serial* serial) void auxDispatch(Pwm16b* auxPwm, char* token, Serial* serial)
{ {
if(strcmp(token, "set") == 0 ) if(strcmp(token, "set") == 0 )
{ {
token = strtok(NULL, " \n"); token = strtok(NULL, " \n");
if(token != NULL) if(token != NULL)
{ {
if(atoi(token) == 0) auxPwm->off(); if(atoi(token) == 0) auxPwm->off();
else auxPwm->on(); else auxPwm->on();
auxPwm->setDutyA(atoi(token) << 8); auxPwm->setDutyA(atoi(token) << 8);
serial->write_p(PSTR("Set PWM value\n")); serial->write_p(PSTR("Set PWM value\n"));
} }
else serial->write_p(PSTR("Usage: aux set [VALUE]\n")); else serial->write_p(PSTR("Usage: aux set [VALUE]\n"));
} }
else else
{ {
serial->write(token, COMMAND_BUFFER_SIZE-4); serial->write(token, COMMAND_BUFFER_SIZE-4);
serial->write_p(PSTR(" is not a valid subcommand: aux set [value]\n")); serial->write_p(PSTR(" is not a valid subcommand: aux set [value]\n"));
} }
} }
void serialDispatch(Serial* serial, SVector<Item, MAX_ITEMS>* items, RgbLed* rgbled, Pwm16b* auxPwm, W433DataReciver* reciver) void serialDispatch(Serial* serial, SVector<Item, MAX_ITEMS>* items, RgbLed* rgbled, Pwm16b* auxPwm,
W433DataReciver* reciver)
{ {
if(serial->dataIsWaiting()) if(serial->dataIsWaiting())
{ {
char buffer[COMMAND_BUFFER_SIZE]; char buffer[COMMAND_BUFFER_SIZE];
unsigned int length = serial->getString(buffer, COMMAND_BUFFER_SIZE); unsigned int length = serial->getString(buffer, COMMAND_BUFFER_SIZE);
if(length > 2) if(length > 2)
{ {
setBit(&PCICR, PCIE1, false); setBit(&PCICR, PCIE1, false);
char* token = strtok(buffer, " \n"); char* token = strtok(buffer, " \n");
if(strcmp(token, "item") == 0) if(strcmp(token, "item") == 0)
{ {
reciver->waitForReciveIdle(); reciver->waitForReciveIdle();
itemDispatch(items, auxPwm, strtok(NULL, " \n"), serial); itemDispatch(items, auxPwm, strtok(NULL, " \n"), serial);
} }
else if(strcmp(token, "rgb") == 0) else if(strcmp(token, "rgb") == 0)
{ {
rgbDispatch(rgbled, strtok(NULL, " \n"), serial); rgbDispatch(rgbled, strtok(NULL, " \n"), serial);
} }
else if(strcmp(token, "aux") == 0) else if(strcmp(token, "aux") == 0)
{ {
auxDispatch(auxPwm, strtok(NULL, " \n"), serial); auxDispatch(auxPwm, strtok(NULL, " \n"), serial);
} }
else if(strcmp(token, "pause") == 0) else if(strcmp(token, "pause") == 0)
{ {
sensorsPaused = true; sensorsPaused = true;
serial->write_p(PSTR("Sensors paused\n")); serial->write_p(PSTR("Sensors paused\n"));
} }
else if(strcmp(token, "resume") == 0) else if(strcmp(token, "resume") == 0)
{ {
sensorsPaused = false; sensorsPaused = false;
serial->write_p(PSTR("Sensors resumed\n")); serial->write_p(PSTR("Sensors resumed\n"));
} }
else if(strcmp(token, "state") == 0) else if(strcmp(token, "state") == 0)
{ {
serial->write_p(PSTR("Items:\n")); serial->write_p(PSTR("Items:\n"));
for(uint8_t i = 0; i < items->count(); i++) for(uint8_t i = 0; i < items->count(); i++)
{ {
writeItemState(serial, &items->at(i), i); writeItemState(serial, &items->at(i), i);
} }
serial->write_p(PSTR("EOL\n")); serial->write_p(PSTR("EOL\n"));
} }
else if(strcmp(token, "save") == 0) else if(strcmp(token, "save") == 0)
{ {
save(); save();
serial->write_p(PSTR("State saved to EEPROM.\n")); serial->write_p(PSTR("State saved to EEPROM.\n"));
} }
else if(strcmp(token, "load") == 0) else if(strcmp(token, "load") == 0)
{ {
load(); load();
serial->write_p(PSTR("Loaded state from EEPROM.\n")); serial->write_p(PSTR("Loaded state from EEPROM.\n"));
} }
else if(strcmp(token, "erase") == 0) else if(strcmp(token, "erase") == 0)
{ {
for(uint16_t i = 0; i < 1024; i++) EEPROM_write_char(i, 0); for(uint16_t i = 0; i < 1024; i++) EEPROM_write_char(i, 0);
serial->write_p(PSTR("EEPROM erased\n")); serial->write_p(PSTR("EEPROM erased\n"));
load(); load();
} }
else if(strcmp(token, "dump") == 0) else if(strcmp(token, "dump") == 0)
{ {
for(uint16_t i = 0; i < 1024; i++) for(uint16_t i = 0; i < 1024; i++)
{ {
if(i != 0) serial->putChar(','); if(i != 0) serial->putChar(',');
serial->write((uint16_t)EEPROM_read_char(i)); serial->write((uint16_t)EEPROM_read_char(i));
} }
serial->putChar('\n'); serial->putChar('\n');
} }
else if(strcmp(token, "free") == 0) else if(strcmp(token, "free") == 0)
{ {
serial->write_p(PSTR("Free Ram: ")); serial->write_p(PSTR("Free Ram: "));
serial->write(freeRAM()); serial->write(freeRAM());
serial->write_p(PSTR(" Bytes.\n")); serial->write_p(PSTR(" Bytes.\n"));
} }
else if(strcmp(token, "help") == 0) else if(strcmp(token, "help") == 0)
{ {
printHelp(serial); printHelp(serial);
} }
else else
{ {
serial->write_p(PSTR("Not a valid command\n")); serial->write_p(PSTR("Not a valid command\n"));
} }
setBit(&PCICR, PCIE1, true); setBit(&PCICR, PCIE1, true);
} }
} }
} }
void reciverError(uint8_t code, void* userData) void reciverError(uint8_t code, void* userData)
@ -403,72 +406,73 @@ void sensorPacketRecived(uint32_t data, void* userData)
if(!sensorsPaused) if(!sensorsPaused)
{ {
Serial* serial = reinterpret_cast<Serial*>(userData); Serial* serial = reinterpret_cast<Serial*>(userData);
uint16_t field = data & 0x0000FFFF; uint16_t field = data & 0x0000FFFF;
serial->write_p(PSTR("SENSOR TYPE: ")); serial->write_p(PSTR("SENSOR TYPE: "));
serial->write(data >> 24); serial->write(data >> 24);
serial->write_p(PSTR(" ID: ")); serial->write_p(PSTR(" ID: "));
serial->write((data & 0x00FF0000) >> 16); serial->write((data & 0x00FF0000) >> 16);
if(data >> 24 == 1) serial->write_p(PSTR(" TEMPERATURE: ")); if(data >> 24 == 1) serial->write_p(PSTR(" TEMPERATURE: "));
else if(data >> 24 == 2) serial->write_p(PSTR(" HUMIDITY: ")); else if(data >> 24 == 2) serial->write_p(PSTR(" HUMIDITY: "));
else serial->write_p(PSTR(" FIELD: ")); else serial->write_p(PSTR(" FIELD: "));
serial->write(field); serial->write(field);
serial->putChar('\n'); serial->putChar('\n');
} }
} }
int main() int main()
{ {
wdt_set(WDTO_8S); wdt_set(WDTO_8S);
DDRB = (1 << PB5) | ( 1 << PB1); DDRB = (1 << PB5) | ( 1 << PB1);
DDRD = (1 << PD3) | (1 << PD5)| (1 << PD6); DDRD = (1 << PD3) | (1 << PD5)| (1 << PD6);
//door watcher //door watcher
PORTB = (1<< PB3) | (1<< PB4); //Enable pull up on door watcher pins; PORTB = (1<< PB3) | (1<< PB4); //Enable pull up on door watcher pins;
bool doorOne = readPin(&PINB, PB3); bool doorOne = readPin(&PINB, PB3);
bool doorTow = readPin(&PINB, PB4); bool doorTow = readPin(&PINB, PB4);
sei(); sei();
Serial serial; Serial serial;
Pwm8b pwmTc0( &TCCR0A, &TCCR0B, &OCR0A, &OCR0B, 0b00000011, true, true ); Pwm8b pwmTc0( &TCCR0A, &TCCR0B, &OCR0A, &OCR0B, 0b00000011, true, true );
Pwm8b pwmTc2( &TCCR2A, &TCCR2B, &OCR2A, &OCR2B, 0b00000101, false, true ); Pwm8b pwmTc2( &TCCR2A, &TCCR2B, &OCR2A, &OCR2B, 0b00000101, false, true );
pwmTc0.off(); pwmTc0.off();
pwmTc2.off(); pwmTc2.off();
RgbLed rgbled( &pwmTc0, &pwmTc2 ); RgbLed rgbled( &pwmTc0, &pwmTc2 );
loadRGB(&rgbled); loadRGB(&rgbled);
Pwm16b pwmTc1 ( &TCCR1A, &TCCR1B, &OCR1A, &OCR1B, &ICR1, 0b00000001, true, false); Pwm16b pwmTc1 ( &TCCR1A, &TCCR1B, &OCR1A, &OCR1B, &ICR1, 0b00000001, true, false);
setBit(&PCICR, PCIE1, true); setBit(&PCICR, PCIE1, true);
setBit(&PCMSK1, PCINT8, true); setBit(&PCMSK1, PCINT8, true);
W433DataReciver reciver(&PINC, PC0, &TCNT1, &TIFR1, &sensorPacketRecived, reinterpret_cast<void*>(&serial), &reciverError); W433DataReciver reciver(&PINC, PC0, &TCNT1, &TIFR1, &sensorPacketRecived, reinterpret_cast<void*>(&serial),
W433DataTransmitter transmitter(&PORTB, PB5); &reciverError);
UvosItem::transmitter=&transmitter; W433DataTransmitter transmitter(&PORTB, PB5);
UvosItem::transmitter=&transmitter;
serial.write_p(PSTR("RGBController v1.5 starting\n"));
serial.write_p(PSTR("RGBController v1.5 starting\n"));
load();
load();
while(true)
{ while(true)
serialDispatch(&serial, &items, &rgbled, &pwmTc1, &reciver); {
rgbled.logic(); serialDispatch(&serial, &items, &rgbled, &pwmTc1, &reciver);
rgbled.logic();
if(doorOne != readPin(&PINB, PB3) && !sensorsPaused)
{ if(doorOne != readPin(&PINB, PB3) && !sensorsPaused)
_delay_ms(10); {
if(doorOne != readPin(&PINB, PB3)) _delay_ms(10);
{ if(doorOne != readPin(&PINB, PB3))
{
doorOne = readPin(&PINB, PB3); doorOne = readPin(&PINB, PB3);
serial.write_p(PSTR("SENSOR TYPE: ")); serial.write_p(PSTR("SENSOR TYPE: "));
serial.putChar('0'); serial.putChar('0');
@ -477,15 +481,15 @@ int main()
serial.write_p(PSTR(" STATE: ")); serial.write_p(PSTR(" STATE: "));
serial.write(doorOne); serial.write(doorOne);
serial.putChar('\n'); serial.putChar('\n');
} }
} }
if(doorTow != readPin(&PINB, PB4) && !sensorsPaused) if(doorTow != readPin(&PINB, PB4) && !sensorsPaused)
{ {
_delay_ms(10); _delay_ms(10);
if(doorTow != readPin(&PINB, PB4)) if(doorTow != readPin(&PINB, PB4))
{ {
doorTow = readPin(&PINB, PB4); doorTow = readPin(&PINB, PB4);
serial.write_p(PSTR("SENSOR TYPE: ")); serial.write_p(PSTR("SENSOR TYPE: "));
serial.putChar('0'); serial.putChar('0');
@ -494,22 +498,22 @@ int main()
serial.write_p(PSTR(" STATE: ")); serial.write_p(PSTR(" STATE: "));
serial.write(doorTow); serial.write(doorTow);
serial.putChar('\n'); serial.putChar('\n');
} }
} }
if(resendNow) if(resendNow)
{ {
for(uint16_t i = 0; i < items.count(); i++) for(uint16_t i = 0; i < items.count(); i++)
{ {
reciver.waitForReciveIdle(); reciver.waitForReciveIdle();
items[i].type == 0 ? WirelessRelay(items[i]).resend() : UvosItem(items[i]).resend(); items[i].type == 0 ? WirelessRelay(items[i]).resend() : UvosItem(items[i]).resend();
_delay_ms(100); _delay_ms(100);
} }
resendNow = false; resendNow = false;
} }
_delay_ms(2); _delay_ms(2);
} }
return 0; return 0;
} }

124
pwm.cpp
View File

@ -2,136 +2,140 @@
//16bit //16bit
Pwm16b::Pwm16b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB, volatile uint16_t *compareRegisterA, volatile uint16_t *compareRegisterB, volatile uint16_t *inputCaptureRegister, const uint8_t speed, const bool enableA, const bool enableB) Pwm16b::Pwm16b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB,
volatile uint16_t *compareRegisterA, volatile uint16_t *compareRegisterB, volatile uint16_t *inputCaptureRegister,
const uint8_t speed, const bool enableA, const bool enableB)
{ {
_timerControlRegisterA = timerControlRegisterA; _timerControlRegisterA = timerControlRegisterA;
_compareRegisterA = compareRegisterA; _compareRegisterA = compareRegisterA;
_compareRegisterB = compareRegisterB; _compareRegisterB = compareRegisterB;
_enableA =enableA; _enableA =enableA;
_enableB =enableB; _enableB =enableB;
*_timerControlRegisterA = 0x00; *_timerControlRegisterA = 0x00;
*timerControlRegisterB = 0x00; *timerControlRegisterB = 0x00;
*inputCaptureRegister = 0xFFFF; *inputCaptureRegister = 0xFFFF;
*timerControlRegisterB |= (1<<WGM13) | (1<<WGM12); *timerControlRegisterB |= (1<<WGM13) | (1<<WGM12);
*timerControlRegisterB |= 0b00000111 & speed; *timerControlRegisterB |= 0b00000111 & speed;
*_timerControlRegisterA|= (1<<WGM11); *_timerControlRegisterA|= (1<<WGM11);
*_compareRegisterA = 0; *_compareRegisterA = 0;
*_compareRegisterB = 0; *_compareRegisterB = 0;
} }
bool Pwm16b::isOn() bool Pwm16b::isOn()
{ {
return *_timerControlRegisterA != (1<<WGM11); return *_timerControlRegisterA != (1<<WGM11);
} }
void Pwm16b::off() void Pwm16b::off()
{ {
*_timerControlRegisterA = 0x00; *_timerControlRegisterA = 0x00;
*_timerControlRegisterA |= (1<<WGM11); *_timerControlRegisterA |= (1<<WGM11);
} }
void Pwm16b::on() void Pwm16b::on()
{ {
off(); off();
if(_enableA) *_timerControlRegisterA|= (1<<COM1A1); if(_enableA) *_timerControlRegisterA|= (1<<COM1A1);
if(_enableB) *_timerControlRegisterA|= (1<<COM1B1); if(_enableB) *_timerControlRegisterA|= (1<<COM1B1);
} }
uint16_t Pwm16b::getValueA() uint16_t Pwm16b::getValueA()
{ {
return *_compareRegisterA; return *_compareRegisterA;
} }
uint16_t Pwm16b::getValueB() uint16_t Pwm16b::getValueB()
{ {
return *_compareRegisterB; return *_compareRegisterB;
} }
void Pwm16b::setDutyA(const uint16_t duty) void Pwm16b::setDutyA(const uint16_t duty)
{ {
*_compareRegisterA = duty; *_compareRegisterA = duty;
} }
void Pwm16b::setDutyB(const uint16_t duty) void Pwm16b::setDutyB(const uint16_t duty)
{ {
*_compareRegisterB = duty; *_compareRegisterB = duty;
} }
Pwm16b::~Pwm16b() Pwm16b::~Pwm16b()
{ {
off(); off();
} }
//8bit //8bit
Pwm8b::Pwm8b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB, volatile unsigned char *compareRegisterA, volatile unsigned char *compareRegisterB, const uint8_t speed, const bool enableA, const bool enableB) Pwm8b::Pwm8b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB,
volatile unsigned char *compareRegisterA, volatile unsigned char *compareRegisterB, const uint8_t speed,
const bool enableA, const bool enableB)
{ {
_timerControlRegisterA = timerControlRegisterA; _timerControlRegisterA = timerControlRegisterA;
_compareRegisterA = compareRegisterA; _compareRegisterA = compareRegisterA;
_compareRegisterB = compareRegisterB; _compareRegisterB = compareRegisterB;
_enableA =enableA; _enableA =enableA;
_enableB =enableB; _enableB =enableB;
*_timerControlRegisterA = 0x00; *_timerControlRegisterA = 0x00;
*timerControlRegisterB = 0x00; *timerControlRegisterB = 0x00;
//fast 8 bit PWM pwm A //fast 8 bit PWM pwm A
if(_enableA) *_timerControlRegisterA|= (1<<COM0A1); if(_enableA) *_timerControlRegisterA|= (1<<COM0A1);
if(_enableB) *_timerControlRegisterA|= (1<<COM0B1); if(_enableB) *_timerControlRegisterA|= (1<<COM0B1);
*_timerControlRegisterA|= (1<<WGM01) | (1<<WGM00); *_timerControlRegisterA|= (1<<WGM01) | (1<<WGM00);
*timerControlRegisterB |= 0b00000111 & speed; *timerControlRegisterB |= 0b00000111 & speed;
*_compareRegisterA = 0; //0% pwm to start0 *_compareRegisterA = 0; //0% pwm to start0
*_compareRegisterB = 0; //0% pwm to start0 *_compareRegisterB = 0; //0% pwm to start0
} }
bool Pwm8b::isOn() bool Pwm8b::isOn()
{ {
return (*_timerControlRegisterA & 0x11111100) != 0; return (*_timerControlRegisterA & 0x11111100) != 0;
} }
void Pwm8b::off() void Pwm8b::off()
{ {
*_timerControlRegisterA &= 0b00000011; *_timerControlRegisterA &= 0b00000011;
} }
void Pwm8b::on() void Pwm8b::on()
{ {
off(); off();
if(_enableA) *_timerControlRegisterA|= (1<<COM0A1); if(_enableA) *_timerControlRegisterA|= (1<<COM0A1);
if(_enableB) *_timerControlRegisterA|= (1<<COM0B1); if(_enableB) *_timerControlRegisterA|= (1<<COM0B1);
} }
uint8_t Pwm8b::getValueA() uint8_t Pwm8b::getValueA()
{ {
return *_compareRegisterA; return *_compareRegisterA;
} }
uint8_t Pwm8b::getValueB() uint8_t Pwm8b::getValueB()
{ {
return *_compareRegisterB; return *_compareRegisterB;
} }
void Pwm8b::setDutyA(const uint8_t duty) void Pwm8b::setDutyA(const uint8_t duty)
{ {
*_compareRegisterA = duty; *_compareRegisterA = duty;
} }
void Pwm8b::setDutyB(const uint8_t duty) void Pwm8b::setDutyB(const uint8_t duty)
{ {
*_compareRegisterB = duty; *_compareRegisterB = duty;
} }
Pwm8b::~Pwm8b() Pwm8b::~Pwm8b()
{ {
off(); off();
} }

66
pwm.h
View File

@ -6,44 +6,48 @@
class Pwm16b //TC1 pwm on PB1 & PB2 class Pwm16b //TC1 pwm on PB1 & PB2
{ {
private: private:
volatile unsigned char *_timerControlRegisterA; //TCCRxA volatile unsigned char *_timerControlRegisterA; //TCCRxA
volatile uint16_t *_compareRegisterA; //OCRxA volatile uint16_t *_compareRegisterA; //OCRxA
volatile uint16_t *_compareRegisterB; //OCRxB volatile uint16_t *_compareRegisterB; //OCRxB
bool _enableA; bool _enableA;
bool _enableB; bool _enableB;
public: public:
Pwm16b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB, volatile uint16_t *compareRegisterA, volatile uint16_t *compareRegisterB, volatile uint16_t *inputCaptureRegister, const uint8_t speed = 0b00000011, const bool enableA = true, const bool enableB = true); Pwm16b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB,
~Pwm16b(); volatile uint16_t *compareRegisterA, volatile uint16_t *compareRegisterB, volatile uint16_t *inputCaptureRegister,
void setDutyA(const uint16_t duty); const uint8_t speed = 0b00000011, const bool enableA = true, const bool enableB = true);
void setDutyB(const uint16_t duty); ~Pwm16b();
uint16_t getValueA(); void setDutyA(const uint16_t duty);
uint16_t getValueB(); void setDutyB(const uint16_t duty);
bool isOn(); uint16_t getValueA();
void off(); uint16_t getValueB();
void on(); bool isOn();
void off();
void on();
}; };
class Pwm8b class Pwm8b
{ {
private: private:
volatile unsigned char *_timerControlRegisterA; //TCCRxA volatile unsigned char *_timerControlRegisterA; //TCCRxA
volatile unsigned char *_compareRegisterA; //OCRxA volatile unsigned char *_compareRegisterA; //OCRxA
volatile unsigned char *_compareRegisterB; //OCRxB volatile unsigned char *_compareRegisterB; //OCRxB
bool _enableA; bool _enableA;
bool _enableB; bool _enableB;
public: public:
Pwm8b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB, volatile unsigned char *compareRegisterA, volatile unsigned char *compareRegisterB, const uint8_t speed = 0b00000011, const bool enableA = true, const bool enableB = true); Pwm8b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB,
~Pwm8b(); volatile unsigned char *compareRegisterA, volatile unsigned char *compareRegisterB, const uint8_t speed = 0b00000011,
void setDutyA(const uint8_t duty); const bool enableA = true, const bool enableB = true);
void setDutyB(const uint8_t duty); ~Pwm8b();
uint8_t getValueA(); void setDutyA(const uint8_t duty);
uint8_t getValueB(); void setDutyB(const uint8_t duty);
bool isOn(); uint8_t getValueA();
void off(); uint8_t getValueB();
void on(); bool isOn();
void off();
void on();
}; };
#endif #endif

View File

@ -4,106 +4,106 @@ RgbLed::RgbLed( Pwm8b* pwmA, Pwm8b* pwmB ): _pwmA(pwmA), _pwmB(pwmB) {}
void RgbLed::setSolidColor( const uint8_t r, const uint8_t g, const uint8_t b) void RgbLed::setSolidColor( const uint8_t r, const uint8_t g, const uint8_t b)
{ {
_pattern=0; _pattern=0;
_targetR = applyCal(r, calRed); _targetR = applyCal(r, calRed);
_targetG = applyCal(g, calGreen); _targetG = applyCal(g, calGreen);
_targetB = applyCal(b, calBlue); _targetB = applyCal(b, calBlue);
} }
void RgbLed::setPreset( const uint8_t preset) void RgbLed::setPreset( const uint8_t preset)
{ {
switch (preset) switch (preset)
{ {
//whites //whites
case 1: case 1:
setSolidColor( 160,255,80 ); //neutral white setSolidColor( 160,255,80 ); //neutral white
break; break;
case 2: case 2:
setSolidColor( 200,255,20 ); //Warm white setSolidColor( 200,255,20 ); //Warm white
break; break;
case 3: case 3:
setSolidColor( 180,255,140 ); //cold white setSolidColor( 180,255,140 ); //cold white
break; break;
//reds //reds
case 4: case 4:
setSolidColor( 255,0,0 ); //red setSolidColor( 255,0,0 ); //red
break; break;
case 5: case 5:
setSolidColor( 255,60,10 ); //ruby setSolidColor( 255,60,10 ); //ruby
break; break;
case 6: case 6:
setSolidColor( 255,30,30 ); //pink setSolidColor( 255,30,30 ); //pink
break; break;
case 7: case 7:
setSolidColor( 255,155,0 ); //orange setSolidColor( 255,155,0 ); //orange
break; break;
//greens //greens
case 8: case 8:
setSolidColor( 0,255,0 ); //green setSolidColor( 0,255,0 ); //green
break; break;
case 9: case 9:
setSolidColor( 55,255,10 ); //poison setSolidColor( 55,255,10 ); //poison
break; break;
case 10: case 10:
setSolidColor( 0,255,0 ); //green setSolidColor( 0,255,0 ); //green
break; break;
case 11: case 11:
setSolidColor( 8,80,7 ); //mint setSolidColor( 8,80,7 ); //mint
break; break;
//blues //blues
case 12: case 12:
setSolidColor( 0,0,255 ); //blue setSolidColor( 0,0,255 ); //blue
break; break;
case 13: case 13:
setSolidColor( 50,255,255 ); //sky setSolidColor( 50,255,255 ); //sky
break; break;
case 14: case 14:
setSolidColor( 10,80,150 ); //ocean setSolidColor( 10,80,150 ); //ocean
break; break;
case 15: case 15:
setSolidColor( 0,255,220 ); //turqouse setSolidColor( 0,255,220 ); //turqouse
break; break;
//strange yellow color color //strange yellow color color
case 16: case 16:
setSolidColor( 200,255,0 ); setSolidColor( 200,255,0 );
break; break;
default: default:
setSolidColor( 255,055,20 ); setSolidColor( 255,055,20 );
} }
} }
void RgbLed::setPattern(const uint8_t id) void RgbLed::setPattern(const uint8_t id)
{ {
_pattern=id; _pattern=id;
if( id != 0 ) if( id != 0 )
{ {
_pwmA->setDutyB(0); _pwmA->setDutyB(0);
_pwmA->setDutyA(0); _pwmA->setDutyA(0);
_pwmB->setDutyB(0); _pwmB->setDutyB(0);
on(); on();
_stroke = false; _stroke = false;
_counter = 0; _counter = 0;
} }
} }
void RgbLed::on() void RgbLed::on()
{ {
_powerd = true; _powerd = true;
_pwmA->on(); _pwmA->on();
_pwmB->on(); _pwmB->on();
} }
void RgbLed::off() void RgbLed::off()
{ {
_powerd = false; _powerd = false;
_pwmA->off(); _pwmA->off();
_pwmB->off(); _pwmB->off();
} }
void RgbLed::setFade(bool fade) void RgbLed::setFade(bool fade)
{ {
_fade=fade; _fade=fade;
} }
uint8_t RgbLed::applyCal(uint16_t value, const uint16_t* cal) uint8_t RgbLed::applyCal(uint16_t value, const uint16_t* cal)
@ -126,89 +126,90 @@ void RgbLed::adjustHeadroom(uint8_t& r, uint8_t& g, uint8_t& b, const uint8_t lu
void RgbLed::patternStep() void RgbLed::patternStep()
{ {
if(_pattern == 1) if(_pattern == 1)
{ {
if(!_stroke)_counter++; if(!_stroke)_counter++;
else _counter --; else _counter --;
if(_counter == 255) _stroke = true; if(_counter == 255) _stroke = true;
else if(_counter == 0) _stroke = false; else if(_counter == 0) _stroke = false;
_pwmA->setDutyB(_counter); _pwmA->setDutyB(_counter);
_pwmA->setDutyA(255-_counter); _pwmA->setDutyA(255-_counter);
_pwmB->setDutyB(_counter-64); _pwmB->setDutyB(_counter-64);
} }
else if(_pattern == 2) //Alarm! else if(_pattern == 2) //Alarm!
{ {
if(!_stroke)_counter++; if(!_stroke)_counter++;
else _counter --; else _counter --;
if(_counter == 255 << 1) _stroke = true; if(_counter == 255 << 1) _stroke = true;
else if(_counter == 0) _stroke = false; else if(_counter == 0) _stroke = false;
_pwmA->setDutyB(_counter >> 1); _pwmA->setDutyB(_counter >> 1);
_pwmA->setDutyA(0); _pwmA->setDutyA(0);
_pwmB->setDutyB(0); _pwmB->setDutyB(0);
} }
else if(_pattern == 3) else if(_pattern == 3)
{ {
if(!_stroke)_counter++; if(!_stroke)_counter++;
else _counter --; else _counter --;
if(_counter == (uint8_t) 255 << 8) _stroke = true; if(_counter == (uint8_t) 255 << 8) _stroke = true;
else if(_counter == 0) _stroke = false; else if(_counter == 0) _stroke = false;
_pwmA->setDutyB(_counter >> 6); _pwmA->setDutyB(_counter >> 6);
_pwmA->setDutyA(_counter >> 8); _pwmA->setDutyA(_counter >> 8);
_pwmB->setDutyB(_counter >> 3); _pwmB->setDutyB(_counter >> 3);
} }
else if(_pattern == 4) else if(_pattern == 4)
{ {
( _counter < 8192 ) ? _pwmA->setDutyB(_counter >> 6) : _pwmA->setDutyB( 128 + (_counter >> 11)); ( _counter < 8192 ) ? _pwmA->setDutyB(_counter >> 6) : _pwmA->setDutyB( 128 + (_counter >> 11));
if( _counter > 1024 ) ( 8192 < _counter && _counter < 16384 ) ? _pwmA->setDutyA((_counter-8192) >> 6) : _pwmA->setDutyA( 128 + (_counter >> 9 )); if( _counter > 1024 ) ( 8192 < _counter
if( _counter > 8192 ) _pwmB->setDutyB(_counter >> 9); && _counter < 16384 ) ? _pwmA->setDutyA((_counter-8192) >> 6) : _pwmA->setDutyA( 128 + (_counter >> 9 ));
if( _counter > 8192 ) _pwmB->setDutyB(_counter >> 9);
if(_counter<65530) _counter++;
else _pwmB->setDutyB(140); if(_counter<65530) _counter++;
else _pwmB->setDutyB(140);
_delay_ms(18);
} _delay_ms(18);
}
} }
void RgbLed::logic() void RgbLed::logic()
{ {
patternStep(); patternStep();
if(_pattern == 0 && _fade) if(_pattern == 0 && _fade)
{ {
_counter++; _counter++;
if( uint8_t(_counter << _fadeSpeed) == 0) if( uint8_t(_counter << _fadeSpeed) == 0)
{ {
if( getR() != _targetR) if( getR() != _targetR)
{ {
_pwmA->setDutyB(getR() - sgn(getR() - _targetR)); _pwmA->setDutyB(getR() - sgn(getR() - _targetR));
} }
if( getG() != _targetG) if( getG() != _targetG)
{ {
_pwmA->setDutyA(getG() - sgn(getG() - _targetG)); _pwmA->setDutyA(getG() - sgn(getG() - _targetG));
} }
if( getB() != _targetB) if( getB() != _targetB)
{ {
_pwmB->setDutyB(getB() - sgn(getB() - _targetB)); _pwmB->setDutyB(getB() - sgn(getB() - _targetB));
} }
} }
} }
else if(_pattern == 0) else if(_pattern == 0)
{ {
_pwmA->setDutyA(_targetG); _pwmA->setDutyA(_targetG);
_pwmA->setDutyB(_targetR); _pwmA->setDutyB(_targetR);
_pwmB->setDutyB(_targetB); _pwmB->setDutyB(_targetB);
} }
} }
uint8_t RgbLed::getR() uint8_t RgbLed::getR()
{ {
return _pwmA->getValueB(); return _pwmA->getValueB();
} }
uint8_t RgbLed::getB() uint8_t RgbLed::getB()
{ {
return _pwmB->getValueB(); return _pwmB->getValueB();
} }
uint8_t RgbLed::getG() uint8_t RgbLed::getG()
{ {
return _pwmA->getValueA(); return _pwmA->getValueA();
} }

View File

@ -4,54 +4,54 @@
class RgbLed class RgbLed
{ {
private: private:
Pwm8b* _pwmA; Pwm8b* _pwmA;
Pwm8b* _pwmB; Pwm8b* _pwmB;
static constexpr uint16_t calRed[] = {1000, 1000, 1000}; static constexpr uint16_t calRed[] = {1000, 1000, 1000};
static constexpr uint16_t calGreen[] = {1000, 1000, 1000}; static constexpr uint16_t calGreen[] = {1000, 1000, 1000};
static constexpr uint16_t calBlue[] = {400, 500, 500}; static constexpr uint16_t calBlue[] = {400, 500, 500};
uint8_t _pattern = 0; uint8_t _pattern = 0;
uint16_t _counter = 0; uint16_t _counter = 0;
bool _stroke = false; bool _stroke = false;
uint8_t _targetR = 0; uint8_t _targetR = 0;
uint8_t _targetG = 0; uint8_t _targetG = 0;
uint8_t _targetB = 0; uint8_t _targetB = 0;
bool _fade = true; bool _fade = true;
uint8_t _fadeSpeed = 7; uint8_t _fadeSpeed = 7;
bool _powerd = false; bool _powerd = false;
void patternStep(); void patternStep();
uint16_t getCalValue(); uint16_t getCalValue();
uint8_t applyCal(uint16_t value, const uint16_t* cal); uint8_t applyCal(uint16_t value, const uint16_t* cal);
void adjustHeadroom(uint8_t& r, uint8_t& g, uint8_t& b, const uint8_t lumina); void adjustHeadroom(uint8_t& r, uint8_t& g, uint8_t& b, const uint8_t lumina);
public: public:
RgbLed( Pwm8b* pwmA, Pwm8b* pwmB ); RgbLed( Pwm8b* pwmA, Pwm8b* pwmB );
void setSolidColor( const uint8_t r, const uint8_t g, const uint8_t b); void setSolidColor( const uint8_t r, const uint8_t g, const uint8_t b);
void setPattern(const uint8_t id); void setPattern(const uint8_t id);
void setPreset( const uint8_t preset); void setPreset( const uint8_t preset);
void on(); void on();
void off(); void off();
void setFade(bool fade = true); void setFade(bool fade = true);
void logic(); void logic();
uint8_t getR(); uint8_t getR();
uint8_t getB(); uint8_t getB();
uint8_t getG(); uint8_t getG();
bool isPowerd(); bool isPowerd();
uint8_t getPattern(); uint8_t getPattern();
}; };
template <typename T> T sgn(T val) template <typename T> T sgn(T val)
{ {
return (T(0) < val) - (val < T(0)); return (T(0) < val) - (val < T(0));
} }

188
ringbuffer.h Executable file → Normal file
View File

@ -22,104 +22,104 @@ template < int BUFFER_SIZE, typename T = uint8_t >
class RingBuffer class RingBuffer
{ {
private: private:
volatile uint_fast16_t _headIndex = 0; volatile uint_fast16_t _headIndex = 0;
volatile uint_fast16_t _tailIndex = 0; volatile uint_fast16_t _tailIndex = 0;
volatile bool _overrun = false; volatile bool _overrun = false;
volatile T _buffer[BUFFER_SIZE]; volatile T _buffer[BUFFER_SIZE];
public: public:
RingBuffer() RingBuffer()
{ {
flush(); flush();
} }
uint_fast16_t remaining() const volatile uint_fast16_t remaining() const volatile
{ {
return (_headIndex-_tailIndex); return (_headIndex-_tailIndex);
} }
uint_fast16_t remainingCapacity() const volatile uint_fast16_t remainingCapacity() const volatile
{ {
return BUFFER_SIZE - (_headIndex-_tailIndex); return BUFFER_SIZE - (_headIndex-_tailIndex);
} }
bool isOverun() volatile bool isOverun() volatile
{ {
bool returnVal = _overrun; bool returnVal = _overrun;
_overrun = false; _overrun = false;
return returnVal; return returnVal;
} }
bool isEmpty() const volatile bool isEmpty() const volatile
{ {
return _tailIndex >= _headIndex; return _tailIndex >= _headIndex;
} }
T read() volatile T read() volatile
{ {
if(!isEmpty()) if(!isEmpty())
{ {
_tailIndex++; _tailIndex++;
return _buffer[(_tailIndex - 1) % BUFFER_SIZE]; return _buffer[(_tailIndex - 1) % BUFFER_SIZE];
} }
else return '\0'; else return '\0';
} }
unsigned int read( T* buffer, unsigned int length ) volatile unsigned int read( T* buffer, unsigned int length ) volatile
{ {
unsigned int i = 0; unsigned int i = 0;
for(; i < length && !isEmpty(); i++) for(; i < length && !isEmpty(); i++)
{ {
buffer[i] = read(); buffer[i] = read();
} }
return i; return i;
} }
void write( T in ) volatile void write( T in ) volatile
{ {
if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0) if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0)
{ {
_headIndex -= BUFFER_SIZE; _headIndex -= BUFFER_SIZE;
_tailIndex -= BUFFER_SIZE; _tailIndex -= BUFFER_SIZE;
} }
_buffer[_headIndex % BUFFER_SIZE] = in; _buffer[_headIndex % BUFFER_SIZE] = in;
_headIndex++; _headIndex++;
if(remaining() > BUFFER_SIZE) if(remaining() > BUFFER_SIZE)
{ {
_overrun = true; _overrun = true;
_tailIndex = _headIndex - BUFFER_SIZE; _tailIndex = _headIndex - BUFFER_SIZE;
} }
} }
void write( T* buffer, const unsigned int length ) volatile void write( T* buffer, const unsigned int length ) volatile
{ {
for(unsigned int i = 0; i < length; i++) write(buffer[i]); for(unsigned int i = 0; i < length; i++) write(buffer[i]);
} }
void flush(T flushCharacter = ' ') volatile void flush(T flushCharacter = ' ') volatile
{ {
_headIndex = 0; _headIndex = 0;
_tailIndex = 0; _tailIndex = 0;
for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = flushCharacter; 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 getString(T terminator, T* buffer, const unsigned int bufferLength) volatile
{ {
unsigned int i = 0; unsigned int i = 0;
for(; i <= remaining() && i <= BUFFER_SIZE && _buffer[(_tailIndex+i) % BUFFER_SIZE] != terminator; i++); for(; i <= remaining() && i <= BUFFER_SIZE && _buffer[(_tailIndex+i) % BUFFER_SIZE] != terminator; i++);
if( i < remaining() && i > 0) if( i < remaining() && i > 0)
{ {
if(i > bufferLength-1) i = bufferLength-1; if(i > bufferLength-1) i = bufferLength-1;
read(buffer, i); read(buffer, i);
buffer[i]='\0'; buffer[i]='\0';
_tailIndex++; _tailIndex++;
} }
else if(i == 0) _tailIndex++; else if(i == 0) _tailIndex++;
else i = 0; else i = 0;
return i; return i;
} }
}; };

View File

@ -16,113 +16,116 @@ ISR(USART_RX_vect) //I have seen worse interrupt sintax
} }
} }
Serial::Serial() Serial::Serial()
{ {
UBRR0H = UBRRH_VALUE; UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE; UBRR0L = UBRRL_VALUE;
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
UCSR0B = _BV(RXEN0) | _BV(TXEN0); //Enable RX and TX UCSR0B = _BV(RXEN0) | _BV(TXEN0); //Enable RX and TX
UCSR0B |= (1 << RXCIE0); //Enable Rx interuppt UCSR0B |= (1 << RXCIE0); //Enable Rx interuppt
sei(); sei();
} }
void Serial::putChar(const char c) void Serial::putChar(const char c)
{ {
loop_until_bit_is_set(UCSR0A, UDRE0); loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c; UDR0 = c;
} }
void Serial::write(const char* in, const unsigned int length) void Serial::write(const char* in, const unsigned int length)
{ {
for(unsigned int i = 0; i < length && in[i] != '\0'; i++) for(unsigned int i = 0; i < length && in[i] != '\0'; i++)
{ {
putChar(in[i]); putChar(in[i]);
} }
} }
void Serial::write_p(const char in[]) void Serial::write_p(const char in[])
{ {
cli(); cli();
char ch = pgm_read_byte(in); char ch = pgm_read_byte(in);
while (ch != '\0') while (ch != '\0')
{ {
putChar(ch); putChar(ch);
in++; in++;
ch = pgm_read_byte(in); ch = pgm_read_byte(in);
} }
sei(); sei();
} }
void Serial::write(const char in[]) void Serial::write(const char in[])
{ {
for(unsigned int i = 0; i < strlen(in); i++) for(unsigned int i = 0; i < strlen(in); i++)
{ {
putChar(in[i]); putChar(in[i]);
} }
} }
void Serial::write(int32_t in) void Serial::write(int32_t in)
{ {
if(in == 0) if(in == 0)
{ {
putChar('0'); putChar('0');
} }
else else
{ {
bool flag = false; bool flag = false;
char str[64] = { 0 }; char str[64] = { 0 };
int16_t i = 62; int16_t i = 62;
if (in < 0) if (in < 0)
{ {
flag = true; flag = true;
in = abs(in); in = abs(in);
} }
while (in != 0 && i > 0) while (in != 0 && i > 0)
{ {
str[i--] = (in % 10) + '0'; str[i--] = (in % 10) + '0';
in /= 10; in /= 10;
} }
if (flag) str[i--] = '-'; if (flag) str[i--] = '-';
write(str + i + 1, 64-(i+1)); write(str + i + 1, 64-(i+1));
} }
} }
bool Serial::dataIsWaiting() bool Serial::dataIsWaiting()
{ {
return !rxBuffer.isEmpty(); return !rxBuffer.isEmpty();
} }
char Serial::getChar() char Serial::getChar()
{ {
if(!rxBuffer.isEmpty()) if(!rxBuffer.isEmpty())
{ {
if(serialFlowControl && stopped && rxBuffer.remainingCapacity() > 32 ) if(serialFlowControl && stopped && rxBuffer.remainingCapacity() > 32 )
{ {
loop_until_bit_is_set(UCSR0A, UDRE0); loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = 0x11; UDR0 = 0x11;
stopped = false; stopped = false;
} }
return rxBuffer.read(); return rxBuffer.read();
} }
else return '\0'; else return '\0';
} }
unsigned int Serial::getString(char* buffer, const int bufferLength) unsigned int Serial::getString(char* buffer, const int bufferLength)
{ {
return rxBuffer.getString(_terminator, (uint8_t*)buffer, bufferLength); return rxBuffer.getString(_terminator, (uint8_t*)buffer, bufferLength);
} }
unsigned int Serial::read(char* buffer, const unsigned int length ) unsigned int Serial::read(char* buffer, const unsigned int length )
{ {
return rxBuffer.read((uint8_t*)buffer, length); return rxBuffer.read((uint8_t*)buffer, length);
} }
void Serial::flush() void Serial::flush()
{ {
rxBuffer.flush(); rxBuffer.flush();
} }
void Serial::setTerminator(char terminator){_terminator = terminator;} void Serial::setTerminator(char terminator)
{
_terminator = terminator;
}

View File

@ -16,21 +16,21 @@ const bool serialFlowControl = false;
class Serial class Serial
{ {
private: private:
char _terminator = '\n'; char _terminator = '\n';
public: public:
Serial(); Serial();
void putChar(const char c); void putChar(const char c);
void write(const char* in, const unsigned int length); void write(const char* in, const unsigned int length);
void write(const char in[]); void write(const char in[]);
void write_p(const char in[]); //for flash space strigns void write_p(const char in[]); //for flash space strigns
void write(const int32_t in); void write(const int32_t in);
unsigned int read( char* buffer, const unsigned int length ); unsigned int read( char* buffer, const unsigned int length );
bool dataIsWaiting(); bool dataIsWaiting();
char getChar(); char getChar();
unsigned int getString(char* buffer, const int bufferLength); unsigned int getString(char* buffer, const int bufferLength);
void flush(); void flush();
void setTerminator(const char terminator); void setTerminator(const char terminator);
}; };
#endif #endif

View File

@ -4,80 +4,80 @@
template<typename T, size_t size> class SVector template<typename T, size_t size> class SVector
{ {
private: private:
size_t stored = 0; size_t stored = 0;
T array[size]; T array[size];
public: public:
T* data() T* data()
{ {
return array; return array;
} }
T& operator[](size_t i) T& operator[](size_t i)
{ {
return array[i]; return array[i];
} }
T& at(size_t i) T& at(size_t i)
{ {
return array[i]; return array[i];
} }
T& front() T& front()
{ {
return array[0]; return array[0];
} }
T& back() T& back()
{ {
return array[stored-1]; return array[stored-1];
} }
bool empty() const bool empty() const
{ {
return stored == 0 ? true : false; return stored == 0 ? true : false;
} }
size_t count() const size_t count() const
{ {
return stored; return stored;
} }
constexpr size_t maxSize() const constexpr size_t maxSize() const
{ {
return size; return size;
} }
size_t remainingCapacity() const size_t remainingCapacity() const
{ {
return size - stored; return size - stored;
} }
bool push_back(const T in) bool push_back(const T in)
{ {
if( remainingCapacity() != 0) if( remainingCapacity() != 0)
{ {
array[stored] = in; array[stored] = in;
++stored; ++stored;
return true; return true;
} }
else return false; else return false;
} }
bool erase(size_t position) bool erase(size_t position)
{ {
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++ ) memcpy(&array[i], &array[i+1], sizeof(T)); for( size_t i = position; i < stored; i++ ) memcpy(&array[i], &array[i+1], sizeof(T));
return true; return true;
} }
void clear() void clear()
{ {
for( size_t i = 0; i < stored; i++ ) array[i].~T(); for( size_t i = 0; i < stored; i++ ) array[i].~T();
stored = 0; stored = 0;
} }
}; };

View File

@ -2,23 +2,23 @@
UvosItem::UvosItem(const uint8_t idIn, char nameIn[]) UvosItem::UvosItem(const uint8_t idIn, char nameIn[])
{ {
id = 129 << 8 + idIn; id = 129 << 8 + idIn;
type = 1; type = 1;
} }
UvosItem::UvosItem(const Item& item) UvosItem::UvosItem(const Item& item)
{ {
Item::operator=(item); Item::operator=(item);
type = 1; type = 1;
} }
void UvosItem::setValue(const uint8_t value) void UvosItem::setValue(const uint8_t value)
{ {
const uint8_t paket[4] = {id >> 8, id & 0x00FF, 0, value}; const uint8_t paket[4] = {id >> 8, id & 0x00FF, 0, value};
if(transmitter)transmitter->send(paket, 4); if(transmitter)transmitter->send(paket, 4);
} }
void UvosItem::resend() void UvosItem::resend()
{ {
setValue(lastValue); setValue(lastValue);
} }

View File

@ -5,12 +5,12 @@
class UvosItem: public Item class UvosItem: public Item
{ {
public:
inline static W433DataTransmitter* transmitter = nullptr;
public: public:
UvosItem(const uint8_t idIn, char nameIn[]); inline static W433DataTransmitter* transmitter = nullptr;
UvosItem(const Item& item);
void setValue(const uint8_t value); public:
UvosItem(const uint8_t idIn, char nameIn[]);
UvosItem(const Item& item);
void setValue(const uint8_t value);
void resend(); void resend();
}; };

View File

@ -5,22 +5,25 @@
inline void writePin(volatile unsigned char *port, const unsigned char pin, const bool state) //waste 2 cycles inline void writePin(volatile unsigned char *port, const unsigned char pin, const bool state) //waste 2 cycles
{ {
*port &= ~(1 << pin); *port &= ~(1 << pin);
if(state) *port |= (1 << pin); if(state) *port |= (1 << pin);
} }
inline void setBit( volatile unsigned char *reg, const unsigned char bit, bool value ) inline void setBit( volatile unsigned char *reg, const unsigned char bit, bool value )
{ {
writePin(reg, bit, value); writePin(reg, bit, value);
} }
inline void setDirection( volatile unsigned char *portDirReg, const unsigned char pin, bool makeOutput ) inline void setDirection( volatile unsigned char *portDirReg, const unsigned char pin, bool makeOutput )
{ {
writePin(portDirReg, pin, makeOutput); writePin(portDirReg, pin, makeOutput);
} }
inline bool readPin( volatile unsigned char *inPort, const unsigned char pin){ return (bool) (*inPort & (1 << pin));} inline bool readPin( volatile unsigned char *inPort, const unsigned char pin)
{
return (bool) (*inPort & (1 << pin));
}
#endif #endif