Changed Sensor Output formating

This commit is contained in:
IMback
2018-10-29 18:21:42 +01:00
parent 2fa8104164
commit 5dac2e5ff9
7 changed files with 318 additions and 12 deletions

View File

@ -238,7 +238,7 @@ CMAKE_CACHE_MAJOR_VERSION:INTERNAL=3
//Minor version of cmake used to create the current loaded cache //Minor version of cmake used to create the current loaded cache
CMAKE_CACHE_MINOR_VERSION:INTERNAL=12 CMAKE_CACHE_MINOR_VERSION:INTERNAL=12
//Patch version of cmake used to create the current loaded cache //Patch version of cmake used to create the current loaded cache
CMAKE_CACHE_PATCH_VERSION:INTERNAL=2 CMAKE_CACHE_PATCH_VERSION:INTERNAL=3
//ADVANCED property for variable: CMAKE_COLOR_MAKEFILE //ADVANCED property for variable: CMAKE_COLOR_MAKEFILE
CMAKE_COLOR_MAKEFILE-ADVANCED:INTERNAL=1 CMAKE_COLOR_MAKEFILE-ADVANCED:INTERNAL=1
//Path to CMake executable. //Path to CMake executable.

View File

@ -19,7 +19,7 @@ set(COMPILE_FLAGS "" CACHE STRING "Additional Compiler Flags")
# Set own source files # Set own source files
# Simply list all your C / C++ source (not header!) files here # Simply list all your C / C++ source (not header!) files here
set(SRC_FILES main.cpp serial.cpp WirelessRelay.cpp pwm.cpp rgbled.cpp) set(SRC_FILES main.cpp serial.cpp WirelessRelay.cpp pwm.cpp rgbled.cpp W433DataReciver.cpp)
# Compiler suite specification # Compiler suite specification
set(CMAKE_C_COMPILER /usr/bin/avr-gcc) set(CMAKE_C_COMPILER /usr/bin/avr-gcc)

174
W433DataReciver.cpp Normal file
View File

@ -0,0 +1,174 @@
#include "W433DataReciver.h"
#include "writepin.h"
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "serial.h"
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)
{
instance = this;
for(uint8_t i = 0; i < 33; i++) timesBuffer[i] = 0;
}
W433DataReciver::~W433DataReciver()
{
instance = nullptr;
}
void W433DataReciver::staticInterrupt()
{
if(instance != nullptr) instance->interrupt();
}
int8_t W433DataReciver::reciveBit(uint8_t index)
{
if(
timesBuffer[index] < 0 &&
isTime(timesBuffer[index+1], SMALL_TIME, true) &&
isTime(timesBuffer[index+2], LARGE_TIME, false) &&
isTime(timesBuffer[index+3], SMALL_TIME, true)
)
{
return 1;
}
else if(
timesBuffer[index] < 0 &&
isTime(timesBuffer[index+1], LARGE_TIME, true) &&
isTime(timesBuffer[index+2], SMALL_TIME, false) &&
isTime(timesBuffer[index+3], SMALL_TIME, true)
)
{
return 0;
}
else return -1;
}
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;
input = abs(input);
return input < (int16_t)(time+tollerance) && input > (int16_t)(time-tollerance);
}
bool W433DataReciver::reciveSync(const uint16_t elapsedTime)
{
if(elapsedTime < SYNC_TIME+50 && elapsedTime > SYNC_TIME-50)
{
++syncCount;
}
else syncCount = 0;
if(syncCount > 10) return true;
else return false;
}
bool W433DataReciver::recivedByte(const uint16_t elapsedTime)
{
timesBuffer[timesBufferIndex] = readPin(_port, _pin) ? 0-elapsedTime : elapsedTime;
++timesBufferIndex;
return timesBufferIndex == 32;
}
uint8_t W433DataReciver::assmbleByte()
{
uint8_t byte = 0;
for(uint8_t i = 0; i < 8; ++i)
{
int8_t bit = reciveBit(i*4);
if(bit >= 0) byte = byte | (bit << (7-i));
else setState(LOOKING_FOR_SYNC);
}
timesBufferIndex = 0;
return byte;
}
void W433DataReciver::setState(const uint8_t stateIn)
{
state = stateIn;
timesBufferIndex = 0;
packetIndex = 0;
}
void W433DataReciver::interrupt()
{
uint16_t elapsedTime = polarity*(((*_timerOverflowRegister & 0x01) ? *_timerRegister+(UINT16_MAX - previousTime) : *_timerRegister - previousTime)/TICKS_PER_US);
if(elapsedTime < SMALL_TIME/4) return;
previousTime = *_timerRegister;
*_timerOverflowRegister = *_timerOverflowRegister | 0x01;
if(state == LOOKING_FOR_SYNC && reciveSync(elapsedTime))
{
setState(LOOKING_FOR_SYNC_END);
}
else if(state == LOOKING_FOR_SYNC_END)
{
if(elapsedTime > SYNC_TIME + 50)
{
if(elapsedTime < LARGE_TIME + 50) setState(LOOKING_FOR_SYNC);
else
{
timesBuffer[0] = -LARGE_TIME;
setState(LOOKING_FOR_SIGNATURE);
++timesBufferIndex;
}
}
}
else if(state == LOOKING_FOR_SIGNATURE)
{
if(recivedByte(elapsedTime))
{
uint8_t recivedSignature = assmbleByte();
if( recivedSignature == signature) setState(RECVING_PACKET);
else setState(LOOKING_FOR_SYNC);
}
}
else if( state == RECVING_PACKET )
{
if(recivedByte(elapsedTime))
{
uint8_t packetByte = assmbleByte();
packet = packet | ((uint32_t)packetByte) << ((3-packetIndex)*8);
++packetIndex;
if(packetIndex > 3)
{
packetIndex = 0;
timesBufferIndex = 0;
setState(RECVING_PACKET_CHECKSUM);
}
}
}
else if(state == RECVING_PACKET_CHECKSUM)
{
if(recivedByte(elapsedTime))
{
uint8_t recivedChecksum = assmbleByte();
volatile uint8_t* buffer = reinterpret_cast<volatile uint8_t*>(&packet);
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)));
if(computedChecksum == recivedChecksum)
{
_ringBuffer.write(const_cast<uint8_t*>(buffer), sizeof(packet));
if(_packetCallback != nullptr)(*_packetCallback)(packet, _userData);
}
packet = 0;
setState(LOOKING_FOR_SYNC);
}
}
}
RingBuffer<W433DataReciver::RINGBUFFER_LENGTH, uint8_t>* W433DataReciver::getRingBuffer()
{
return &_ringBuffer;
}

72
W433DataReciver.h Normal file
View File

@ -0,0 +1,72 @@
#pragma once
#include <stdint.h>
#include "ringbuffer.h"
class W433DataReciver
{
public:
static constexpr uint8_t RINGBUFFER_LENGTH = 32;
private:
static W433DataReciver* instance;
//constants
static constexpr uint8_t CLOCK_DEVIDER = 1;
static constexpr uint16_t SYNC_TIME = 800;
static constexpr uint16_t LARGE_TIME = 2000;
static constexpr uint16_t SMALL_TIME = 500;
static constexpr uint16_t TICKS_PER_US = (F_CPU) / (1000000*CLOCK_DEVIDER) ;
static constexpr uint8_t signature = 0xA5;
static constexpr int8_t polarity = 1;
static constexpr uint8_t LOOKING_FOR_SYNC = 0;
static constexpr uint8_t LOOKING_FOR_SYNC_END = 1;
static constexpr uint8_t LOOKING_FOR_SIGNATURE = 2;
static constexpr uint8_t RECVING_PACKET = 3;
static constexpr uint8_t RECVING_PACKET_CHECKSUM = 4;
//variables
volatile unsigned char *_port;
unsigned char _pin;
volatile uint16_t *_timerRegister;
volatile uint8_t *_timerOverflowRegister;
RingBuffer<RINGBUFFER_LENGTH, uint8_t> _ringBuffer;
volatile uint16_t previousTime = 0;
volatile uint8_t timesBufferIndex = 0;
volatile int16_t timesBuffer[33];
volatile uint8_t packetIndex = 0;
volatile uint32_t packet = 0;
void (* const _packetCallback)(uint32_t, void*) = nullptr;
void* const _userData;
volatile uint8_t syncCount = 0;
volatile uint8_t state = 0;
//private functions
int8_t reciveBit(uint8_t index);
inline uint8_t assmbleByte();
inline void setState(const uint8_t stateIn);
inline bool recivedByte(const uint16_t elapsedTime);
inline bool reciveSync(const uint16_t elapsedTime);
static inline bool isTime(int16_t input, const uint16_t time, const bool state = true, const uint16_t tollerance = 50);
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();
static void initTimer();
static void staticInterrupt();
void interrupt();
RingBuffer<RINGBUFFER_LENGTH, uint8_t>* getRingBuffer();
};

View File

@ -10,6 +10,7 @@
#include "eeprom.h" #include "eeprom.h"
#include "bitrep.h" #include "bitrep.h"
#include "staticvector.h" #include "staticvector.h"
#include "W433DataReciver.h"
#define COMMAND_BUFFER_SIZE 64 #define COMMAND_BUFFER_SIZE 64
#define SNPRINTF_BUFFER_SIZE 64 #define SNPRINTF_BUFFER_SIZE 64
@ -21,6 +22,13 @@ char buffer[SNPRINTF_BUFFER_SIZE];
SVector<WirelessRelay, MAX_RELAYS> relays; SVector<WirelessRelay, MAX_RELAYS> relays;
bool flag = false;
ISR(PCINT1_vect)
{
W433DataReciver::staticInterrupt();
}
inline static void printHelp(Serial* serial) inline static void printHelp(Serial* serial)
{ {
@ -270,6 +278,7 @@ void serialDispatch(Serial* serial, SVector<WirelessRelay, MAX_RELAYS>* relays,
unsigned int length = serial->getString(buffer, COMMAND_BUFFER_SIZE); unsigned int length = serial->getString(buffer, COMMAND_BUFFER_SIZE);
if(length > 2) if(length > 2)
{ {
cli();
char* token = strtok(buffer, " \n"); char* token = strtok(buffer, " \n");
if(strcmp(token, "relay") == 0) if(strcmp(token, "relay") == 0)
{ {
@ -311,10 +320,28 @@ void serialDispatch(Serial* serial, SVector<WirelessRelay, MAX_RELAYS>* relays,
{ {
serial->write_p(PSTR("Not a valid command\n")); serial->write_p(PSTR("Not a valid command\n"));
} }
sei();
} }
} }
} }
void temperaturePacketRecived(uint32_t data, void* userData)
{
Serial* serial = reinterpret_cast<Serial*>(userData);
if(data >> 24 == 1)
{
serial->write_p(PSTR("SENSOR TYPE: "));
serial->write(data >> 24);
serial->write_p(PSTR(" ID: "));
serial->write((data & 0x00FF0000) >> 16);
serial->write_p(PSTR(" TEMPERATURE: "));
serial->write((data & 0x0000FF00) >> 8);
serial->write_p(PSTR(" HUMIDITY: "));
serial->write(data & 0x000000FF);
serial->putChar('\n');
}
}
int main() int main()
{ {
@ -340,8 +367,12 @@ int main()
RgbLed rgbled( &pwmTc0, &pwmTc2 ); RgbLed rgbled( &pwmTc0, &pwmTc2 );
Pwm16b pwmTc1 ( &TCCR1A, &TCCR1B, &OCR1A, &OCR1B, &ICR1, 0b00000001, true, false); Pwm16b pwmTc1 ( &TCCR1A, &TCCR1B, &OCR1A, &OCR1B, &ICR1, 0b00000001, true, false);
serial.write_p(PSTR("RGBController v0.7 starting\n")); setBit(&PCICR, PCIE1, true);
setBit(&PCMSK1, PCINT8, true);
W433DataReciver reciver(&PINC, PC0, &TCNT1, &TIFR1, &temperaturePacketRecived, reinterpret_cast<void*>(&serial));
serial.write_p(PSTR("RGBController v0.8 starting\n"));
load(); load();
@ -355,9 +386,14 @@ int main()
_delay_ms(10); _delay_ms(10);
if(doorOne != readPin(&PINB, PB3)) if(doorOne != readPin(&PINB, PB3))
{ {
doorOne = readPin(&PINB, PB3); doorOne = readPin(&PINB, PB3);
serial.write("D1"); serial->write_p(PSTR("SENSOR TYPE: "));
doorOne ? serial.write("O\n") : serial.write("C\n"); serial->putChar('0');
serial->write_p(PSTR(" ID: "));
serial->putChar('0');
serial->write_p(PSTR(" STATE: "));
serial->write(doorOne);
serial->putChar('\n');
} }
} }
@ -366,9 +402,14 @@ int main()
_delay_ms(10); _delay_ms(10);
if(doorTow != readPin(&PINB, PB4)) if(doorTow != readPin(&PINB, PB4))
{ {
doorTow = readPin(&PINB, PB4); doorTow = readPin(&PINB, PB3);
serial.write("D2"); serial->write_p(PSTR("SENSOR TYPE: "));
doorTow ? serial.write("O\n") : serial.write("C\n"); serial->putChar('0');
serial->write_p(PSTR(" ID: "));
serial->putChar('1');
serial->write_p(PSTR(" STATE: "));
serial->write(doorTow);
serial->putChar('\n');
} }
} }
if(doorTow) openCount++; if(doorTow) openCount++;
@ -378,6 +419,12 @@ int main()
openCount = 0; openCount = 0;
} }
if(flag)
{
flag = false;
serial.write("flag\n");
}
_delay_ms(2); _delay_ms(2);
} }

View File

@ -1,7 +1,7 @@
#include "serial.h" #include "serial.h"
#include "ringbuffer.h" #include "ringbuffer.h"
RingBuffer<SERIAL_BUFFER_SIZE> rxBuffer; RingBuffer<SERIAL_BUFFER_SIZE, uint8_t> rxBuffer;
bool stopped = false; bool stopped = false;
@ -111,7 +111,7 @@ char Serial::getChar()
unsigned int Serial::getString(char* buffer, const int bufferLength) unsigned int Serial::getString(char* buffer, const int bufferLength)
{ {
return rxBuffer.getString(_terminator, 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 )

View File

@ -2,12 +2,25 @@
#define WRITEPIN_H #define WRITEPIN_H
#include <avr/io.h> #include <avr/io.h>
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 )
{
writePin(reg, bit, value);
}
inline void setDirection( volatile unsigned char *portDirReg, const unsigned char pin, bool 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