Compare commits

...

10 Commits

Author SHA1 Message Date
8fbbc167af improve data reciver 2024-06-10 20:15:41 +02:00
99ab8cf36d code formating changes 2023-04-02 23:55:00 +02:00
25b7258458 disable waitForReciveIdle 2022-04-12 00:39:05 +02:00
4aca3c3a11 reformat in uvostyle 2022-04-12 00:36:58 +02:00
00ca1d6f03 fix item name printing 2022-04-12 00:35:42 +02:00
0883a1c9d5 small fixes 2020-05-27 17:34:38 +02:00
bcd835aca6 Support multi backend items 2020-05-05 22:20:48 +02:00
14432ae200 fix embarsing load action bug 2020-04-20 21:17:01 +02:00
5e5f7aac7f removed unesscary file from git 2020-04-20 21:06:23 +02:00
c0ca4c81d0 Make saveing and loading to epprop a user controlled operation 2020-04-20 21:05:08 +02:00
24 changed files with 1357 additions and 1657 deletions

View File

@ -1,377 +0,0 @@
# This is the CMakeCache file.
# For build in directory: /home/philipp/Programming/avr/RGBcontroller
# It was generated by CMake: /usr/bin/cmake
# You can edit this file to change values found and used by cmake.
# If you do not want to change any of the values, simply exit the editor.
# If you do want to change a value, simply edit, save, and exit the editor.
# The syntax for the file is as follows:
# KEY:TYPE=VALUE
# KEY is the name of a variable in the cache.
# TYPE is a hint to GUIs for the type of VALUE, DO NOT EDIT TYPE!.
# VALUE is the current value for the KEY.
########################
# EXTERNAL cache entries
########################
//Path to a program.
AR_AVRDUDE:FILEPATH=/usr/bin/avrdude
//Path to a program.
AR_AVRSIZE:FILEPATH=/usr/bin/avr-size
//Path to a program.
CMAKE_AR:FILEPATH=/bin/ar
//Choose the type of build, options are: None Debug Release RelWithDebInfo
// MinSizeRel ...
CMAKE_BUILD_TYPE:STRING=
//Enable/Disable color output during build.
CMAKE_COLOR_MAKEFILE:BOOL=ON
//CXX compiler
CMAKE_CXX_COMPILER:FILEPATH=/bin/c++
//A wrapper around 'ar' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_CXX_COMPILER_AR:FILEPATH=/bin/gcc-ar
//A wrapper around 'ranlib' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_CXX_COMPILER_RANLIB:FILEPATH=/bin/gcc-ranlib
//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=
//Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
//Flags used by the CXX compiler during MINSIZEREL builds.
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
//Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
//Flags used by the CXX compiler during RELWITHDEBINFO builds.
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
//C compiler
CMAKE_C_COMPILER:FILEPATH=/bin/cc
//A wrapper around 'ar' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_C_COMPILER_AR:FILEPATH=/bin/gcc-ar
//A wrapper around 'ranlib' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_C_COMPILER_RANLIB:FILEPATH=/bin/gcc-ranlib
//Flags used by the C compiler during all build types.
CMAKE_C_FLAGS:STRING=
//Flags used by the C compiler during DEBUG builds.
CMAKE_C_FLAGS_DEBUG:STRING=-g
//Flags used by the C compiler during MINSIZEREL builds.
CMAKE_C_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
//Flags used by the C compiler during RELEASE builds.
CMAKE_C_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
//Flags used by the C compiler during RELWITHDEBINFO builds.
CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
//Flags used by the linker during all build types.
CMAKE_EXE_LINKER_FLAGS:STRING=
//Flags used by the linker during DEBUG builds.
CMAKE_EXE_LINKER_FLAGS_DEBUG:STRING=
//Flags used by the linker during MINSIZEREL builds.
CMAKE_EXE_LINKER_FLAGS_MINSIZEREL:STRING=
//Flags used by the linker during RELEASE builds.
CMAKE_EXE_LINKER_FLAGS_RELEASE:STRING=
//Flags used by the linker during RELWITHDEBINFO builds.
CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO:STRING=
//Enable/Disable output of compile commands during generation.
CMAKE_EXPORT_COMPILE_COMMANDS:BOOL=OFF
//Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local
//Path to a program.
CMAKE_LINKER:FILEPATH=/bin/ld
//Path to a program.
CMAKE_MAKE_PROGRAM:FILEPATH=/bin/make
//Flags used by the linker during the creation of modules during
// all build types.
CMAKE_MODULE_LINKER_FLAGS:STRING=
//Flags used by the linker during the creation of modules during
// DEBUG builds.
CMAKE_MODULE_LINKER_FLAGS_DEBUG:STRING=
//Flags used by the linker during the creation of modules during
// MINSIZEREL builds.
CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL:STRING=
//Flags used by the linker during the creation of modules during
// RELEASE builds.
CMAKE_MODULE_LINKER_FLAGS_RELEASE:STRING=
//Flags used by the linker during the creation of modules during
// RELWITHDEBINFO builds.
CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO:STRING=
//Path to a program.
CMAKE_NM:FILEPATH=/bin/nm
//Path to a program.
CMAKE_OBJCOPY:FILEPATH=/bin/objcopy
//Path to a program.
CMAKE_OBJDUMP:FILEPATH=/bin/objdump
//Value Computed by CMake
CMAKE_PROJECT_DESCRIPTION:STATIC=
//Value Computed by CMake
CMAKE_PROJECT_HOMEPAGE_URL:STATIC=
//Value Computed by CMake
CMAKE_PROJECT_NAME:STATIC=rgbcontroller
//Path to a program.
CMAKE_RANLIB:FILEPATH=/bin/ranlib
//Flags used by the linker during the creation of shared libraries
// during all build types.
CMAKE_SHARED_LINKER_FLAGS:STRING=
//Flags used by the linker during the creation of shared libraries
// during DEBUG builds.
CMAKE_SHARED_LINKER_FLAGS_DEBUG:STRING=
//Flags used by the linker during the creation of shared libraries
// during MINSIZEREL builds.
CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL:STRING=
//Flags used by the linker during the creation of shared libraries
// during RELEASE builds.
CMAKE_SHARED_LINKER_FLAGS_RELEASE:STRING=
//Flags used by the linker during the creation of shared libraries
// during RELWITHDEBINFO builds.
CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO:STRING=
//If set, runtime paths are not added when installing shared libraries,
// but are added when building.
CMAKE_SKIP_INSTALL_RPATH:BOOL=NO
//If set, runtime paths are not added when using shared libraries.
CMAKE_SKIP_RPATH:BOOL=NO
//Flags used by the linker during the creation of static libraries
// during all build types.
CMAKE_STATIC_LINKER_FLAGS:STRING=
//Flags used by the linker during the creation of static libraries
// during DEBUG builds.
CMAKE_STATIC_LINKER_FLAGS_DEBUG:STRING=
//Flags used by the linker during the creation of static libraries
// during MINSIZEREL builds.
CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL:STRING=
//Flags used by the linker during the creation of static libraries
// during RELEASE builds.
CMAKE_STATIC_LINKER_FLAGS_RELEASE:STRING=
//Flags used by the linker during the creation of static libraries
// during RELWITHDEBINFO builds.
CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO:STRING=
//Path to a program.
CMAKE_STRIP:FILEPATH=/bin/strip
//If this value is on, makefiles will be generated without the
// .SILENT directive, and all commands will be echoed to the console
// during the make. This is useful for debugging only. With Visual
// Studio IDE projects all commands are done without /nologo.
CMAKE_VERBOSE_MAKEFILE:BOOL=FALSE
//Additional Compiler Flags
COMPILE_FLAGS:STRING=
//Speed of the CPU
CPU_SPEED:STRING=16000000
//Processor Type
MCU:STRING=atmega328p
//USB Port
PORT:STRING=/dev/ttyUSB0
//Serial Port Speed
PORT_SPEED:STRING=57600
//Programmer Type
PROGRAMMER:STRING=stk500v1
//Value Computed by CMake
rgbcontroller_BINARY_DIR:STATIC=/home/philipp/Programming/avr/RGBcontroller
//Value Computed by CMake
rgbcontroller_SOURCE_DIR:STATIC=/home/philipp/Programming/avr/RGBcontroller
########################
# INTERNAL cache entries
########################
//ADVANCED property for variable: CMAKE_AR
CMAKE_AR-ADVANCED:INTERNAL=1
//This is the directory where this CMakeCache.txt was created
CMAKE_CACHEFILE_DIR:INTERNAL=/home/philipp/Programming/avr/RGBcontroller
//Major version of cmake used to create the current loaded cache
CMAKE_CACHE_MAJOR_VERSION:INTERNAL=3
//Minor version of cmake used to create the current loaded cache
CMAKE_CACHE_MINOR_VERSION:INTERNAL=15
//Patch version of cmake used to create the current loaded cache
CMAKE_CACHE_PATCH_VERSION:INTERNAL=5
//ADVANCED property for variable: CMAKE_COLOR_MAKEFILE
CMAKE_COLOR_MAKEFILE-ADVANCED:INTERNAL=1
//Path to CMake executable.
CMAKE_COMMAND:INTERNAL=/usr/bin/cmake
//Path to cpack program executable.
CMAKE_CPACK_COMMAND:INTERNAL=/usr/bin/cpack
//Path to ctest program executable.
CMAKE_CTEST_COMMAND:INTERNAL=/usr/bin/ctest
//ADVANCED property for variable: CMAKE_CXX_COMPILER
CMAKE_CXX_COMPILER-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_COMPILER_AR
CMAKE_CXX_COMPILER_AR-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_COMPILER_RANLIB
CMAKE_CXX_COMPILER_RANLIB-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_DEBUG-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_MINSIZEREL-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELEASE-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_RELWITHDEBINFO-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_COMPILER
CMAKE_C_COMPILER-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_COMPILER_AR
CMAKE_C_COMPILER_AR-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_COMPILER_RANLIB
CMAKE_C_COMPILER_RANLIB-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_FLAGS
CMAKE_C_FLAGS-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_DEBUG-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_MINSIZEREL-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELEASE-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_RELWITHDEBINFO-ADVANCED:INTERNAL=1
//Path to cache edit program executable.
CMAKE_EDIT_COMMAND:INTERNAL=/usr/bin/ccmake
//Executable file format
CMAKE_EXECUTABLE_FORMAT:INTERNAL=ELF
//ADVANCED property for variable: CMAKE_EXE_LINKER_FLAGS
CMAKE_EXE_LINKER_FLAGS-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_EXE_LINKER_FLAGS_DEBUG
CMAKE_EXE_LINKER_FLAGS_DEBUG-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_EXE_LINKER_FLAGS_MINSIZEREL
CMAKE_EXE_LINKER_FLAGS_MINSIZEREL-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_EXE_LINKER_FLAGS_RELEASE
CMAKE_EXE_LINKER_FLAGS_RELEASE-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO
CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_EXPORT_COMPILE_COMMANDS
CMAKE_EXPORT_COMPILE_COMMANDS-ADVANCED:INTERNAL=1
//Name of external makefile project generator.
CMAKE_EXTRA_GENERATOR:INTERNAL=
//Name of generator.
CMAKE_GENERATOR:INTERNAL=Unix Makefiles
//Generator instance identifier.
CMAKE_GENERATOR_INSTANCE:INTERNAL=
//Name of generator platform.
CMAKE_GENERATOR_PLATFORM:INTERNAL=
//Name of generator toolset.
CMAKE_GENERATOR_TOOLSET:INTERNAL=
//Source directory with the top level CMakeLists.txt file for this
// project
CMAKE_HOME_DIRECTORY:INTERNAL=/home/philipp/Programming/avr/RGBcontroller
//Install .so files without execute permission.
CMAKE_INSTALL_SO_NO_EXE:INTERNAL=0
//ADVANCED property for variable: CMAKE_LINKER
CMAKE_LINKER-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_MAKE_PROGRAM
CMAKE_MAKE_PROGRAM-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_MODULE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_MODULE_LINKER_FLAGS_DEBUG
CMAKE_MODULE_LINKER_FLAGS_DEBUG-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL
CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_MODULE_LINKER_FLAGS_RELEASE
CMAKE_MODULE_LINKER_FLAGS_RELEASE-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO
CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_NM
CMAKE_NM-ADVANCED:INTERNAL=1
//number of local generators
CMAKE_NUMBER_OF_MAKEFILES:INTERNAL=1
//ADVANCED property for variable: CMAKE_OBJCOPY
CMAKE_OBJCOPY-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_OBJDUMP
CMAKE_OBJDUMP-ADVANCED:INTERNAL=1
//Platform information initialized
CMAKE_PLATFORM_INFO_INITIALIZED:INTERNAL=1
//ADVANCED property for variable: CMAKE_RANLIB
CMAKE_RANLIB-ADVANCED:INTERNAL=1
//Path to CMake installation.
CMAKE_ROOT:INTERNAL=/usr/share/cmake-3.15
//ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS_DEBUG
CMAKE_SHARED_LINKER_FLAGS_DEBUG-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL
CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS_RELEASE
CMAKE_SHARED_LINKER_FLAGS_RELEASE-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO
CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_SKIP_INSTALL_RPATH
CMAKE_SKIP_INSTALL_RPATH-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_SKIP_RPATH
CMAKE_SKIP_RPATH-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_STATIC_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_STATIC_LINKER_FLAGS_DEBUG
CMAKE_STATIC_LINKER_FLAGS_DEBUG-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL
CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_STATIC_LINKER_FLAGS_RELEASE
CMAKE_STATIC_LINKER_FLAGS_RELEASE-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO
CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_STRIP
CMAKE_STRIP-ADVANCED:INTERNAL=1
//uname command
CMAKE_UNAME:INTERNAL=/bin/uname
//ADVANCED property for variable: CMAKE_VERBOSE_MAKEFILE
CMAKE_VERBOSE_MAKEFILE-ADVANCED:INTERNAL=1

View File

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

View File

@ -1,221 +1,144 @@
#include "W433DataReciver.h"
#include <util/crc16.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, void (*errorCodeHandler)(uint8_t, void*) ):
_port(port), _pin(pin), _timerRegister(timerRegister), _timerOverflowRegister(timerOverflowRegister), _packetCallback(packetCallback), _errorCodeHandler(errorCodeHandler), _userData(userData)
W433DataReciver::W433DataReciver(volatile unsigned char* const portIn, const unsigned char pinIn,
void (*packetCallbackIn)(uint32_t, void*), void* userDataIn,
void (*errorCodeHandlerIn)(uint8_t, void*)):
port(portIn), pin(pinIn),
packetCallback(packetCallbackIn), errorCodeHandler(errorCodeHandlerIn), userData(userDataIn)
{
instance = this;
for(uint8_t i = 0; i < 33; i++) timesBuffer[i] = 0;
instance = this;
}
W433DataReciver::~W433DataReciver()
{
instance = nullptr;
instance = nullptr;
}
void W433DataReciver::staticInterrupt()
{
if(instance != nullptr) instance->interrupt();
if(instance != nullptr)
instance->interrupt();
}
int8_t W433DataReciver::reciveBit(uint8_t index)
uint16_t W433DataReciver::calcCrc(uint32_t data)
{
if(
timesBuffer[index] < 0 &&
isTime(timesBuffer[index+1], SMALL_TIME, true, SMALL_TIME_TOLERANCE) &&
isTime(timesBuffer[index+2], LARGE_TIME, false, LARGE_TIME_TOLERANCE) //&&
//isTime(timesBuffer[index+3], SMALL_TIME, true, SMALL_TIME_TOLERANCE)
)
{
return 1;
}
else if(
timesBuffer[index] < 0 &&
isTime(timesBuffer[index+1], LARGE_TIME, true, LARGE_TIME_TOLERANCE) &&
isTime(timesBuffer[index+2], SMALL_TIME, false, SMALL_TIME_TOLERANCE) //&&
//isTime(timesBuffer[index+3], SMALL_TIME, true, SMALL_TIME_TOLERANCE)
)
{
return 0;
}
else return -1;
uint16_t crc = 0xffff;
uint8_t* ptr = reinterpret_cast<uint8_t*>(&data);
for(size_t i = 0; i < sizeof(data); ++i)
crc = _crc_ccitt_update(crc, ptr[i]);
return crc;
}
void W433DataReciver::waitForReciveIdle(const uint16_t timeoutMs)
uint8_t W433DataReciver::symbolDecode(uint8_t symbol)
{
for(uint16_t i = 0; i < timeoutMs && state != LOOKING_FOR_SYNC; ++i ) _delay_ms(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+SYNC_TIME_TOLERANCE && elapsedTime > SYNC_TIME-SYNC_TIME_TOLERANCE)
{
++syncCount;
}
else
switch(symbol)
{
if(syncCount > 4 && syncFailCount < 3) ++syncFailCount;
else
case 0xe:
return 1;
case 0x13:
return 2;
case 0x15:
return 3;
case 0x16:
return 4;
case 0x19:
return 5;
case 0x1a:
return 6;
case 0x1c:
return 7;
case 0x23:
return 8;
case 0x25:
return 9;
case 0x26:
return 10;
case 0x29:
return 11;
case 0x2a:
return 12;
case 0x2c:
return 13;
case 0x32:
return 14;
case 0x34:
return 15;
case 0xd:
default:
return 0;
}
}
void W433DataReciver::pll(bool sample)
{
// Integrate each sample
if(sample)
rxIntegrator++;
if (sample != prevSample)
{
pllRamp += pllRamp < PLL_RAMP_TRANSITION ? PLL_RAMP_INC_RETARD : PLL_RAMP_INC_ADVANCE;
prevSample = sample;
}
else
{
pllRamp += PLL_RAMP_INC_STEP;
}
if (pllRamp >= PLL_RAMP_LEN)
{
rxBits >>= 1;
if (rxIntegrator >= 5)
rxBits |= 0x800;
pllRamp -= PLL_RAMP_LEN;
rxIntegrator = 0;
if(mode == MODE_RECEIVING)
{
//if(syncCount > 7) error(ERR_SYNC_FAIL);
setState(LOOKING_FOR_SYNC);
if (++rxCount >= 12)
{
uint8_t currentByte = (symbolDecode(rxBits & 0x3f)) | symbolDecode(rxBits >> 6) << 4;
rxBuf[PACKET_LENGTH-1-(rxLen++)] = currentByte;
if (rxLen >= PACKET_LENGTH)
{
mode = MODE_SEARCHING;
uint32_t* packet = reinterpret_cast<uint32_t*>(rxBuf+2);
uint16_t crc = rxBuf[0] << 8 | rxBuf[1];
uint16_t crcC = calcCrc(*packet);
if(crc != crcC)
{
if(errorCodeHandler)
errorCodeHandler(ERROR_CRC, userData);
}
else
{
packetCallback(*packet, userData);
}
}
rxCount = 0;
}
}
else if (rxBits == 0xb38)
{
mode = MODE_RECEIVING;
rxCount = 0;
rxLen = 0;
}
}
if(syncCount > 10) return true;
else return false;
}
bool W433DataReciver::recivedByte(const uint16_t elapsedTime)
{
timesBuffer[timesBufferIndex] = readPin(_port, _pin) ? 0-elapsedTime : elapsedTime;
++timesBufferIndex;
if(timesBufferIndex == 32) writePin(&PORTC, PC0, true);
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);
error(ERR_BYTE_ASM);
}
}
timesBufferIndex = 0;
return byte;
}
void W433DataReciver::error(const uint8_t errorCode)
{
if(_errorCodeHandler != nullptr) (*_errorCodeHandler)(errorCode, _userData);
}
void W433DataReciver::setState(const uint8_t stateIn)
{
state = stateIn;
timesBufferIndex = 0;
packetIndex = 0;
syncCount = 0;
syncFailCount = 0;
}
void W433DataReciver::interrupt()
{
uint16_t elapsedTime = polarity*(((*_timerOverflowRegister & 0x01) ? *_timerRegister+(UINT16_MAX - previousTime) : *_timerRegister - previousTime)/TICKS_PER_US);
if(elapsedTime < DISCARD_TIME)
{
if(timesBufferIndex > 0 && elapsedTime + abs(timesBuffer[timesBufferIndex-1]) < LARGE_TIME+LARGE_TIME_TOLERANCE)
{
writePin(&PORTC, PC2, true);
previousTime = *_timerRegister - elapsedTime - abs(timesBuffer[timesBufferIndex-1]);
timesBufferIndex-=1;
}
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 + SYNC_END_TIME_TOLERANCE)
{
if(elapsedTime < LARGE_TIME - LARGE_TIME_TOLERANCE)
{
setState(LOOKING_FOR_SYNC);
error(ERR_NO_SYNC_END);
}
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
{
error(ERR_WRONG_SIG);
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))) >> (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)
{
#ifdef USE_RINGBUFFER
_ringBuffer.write(const_cast<uint8_t*>(buffer), sizeof(packet));
#endif
if(_packetCallback != nullptr)(*_packetCallback)(packet, _userData);
}
else error(ERR_CHECKSUM);
packet = 0;
setState(LOOKING_FOR_SYNC);
}
}
pll(readPin(port, pin));
}
#ifdef USE_RINGBUFFER
RingBuffer<W433DataReciver::RINGBUFFER_LENGTH, uint8_t>* W433DataReciver::getRingBuffer()
uint16_t W433DataReciver::calculateOverflowRegister(uint16_t bitRate, uint16_t devisor)
{
return &_ringBuffer;
return (F_CPU /(8UL*devisor))/bitRate;
}
#endif

View File

@ -1,94 +1,63 @@
#pragma once
#include <stdint.h>
#include "ringbuffer.h"
//#define USE_RINGBUFFER
#include <stdlib.h>
#include <stdint.h>
class W433DataReciver
{
public:
static constexpr uint8_t RINGBUFFER_LENGTH = 32;
//errors
static constexpr uint8_t ERR_SYNC_FAIL = 1;
static constexpr uint8_t ERR_NO_SYNC_END = 2;
static constexpr uint8_t ERR_BYTE_ASM = 3;
static constexpr uint8_t ERR_WRONG_SIG = 4;
static constexpr uint8_t ERR_CHECKSUM = 5;
static constexpr int ERROR_CRC = 1;
static constexpr int SAMPLES_PER_BIT = 8;
static constexpr int PLL_RAMP_LEN = 160;
static constexpr int PLL_RAMP_INC_STEP = PLL_RAMP_LEN/SAMPLES_PER_BIT;
static constexpr int PLL_RAMP_TRANSITION = PLL_RAMP_LEN/2;
static constexpr int PLL_RAMP_INC_RETARD = PLL_RAMP_INC_STEP-9;
static constexpr int PLL_RAMP_INC_ADVANCE = PLL_RAMP_INC_STEP+9;
static constexpr int PLL_HEADER_LEN_BITS = 8;
static constexpr int PACKET_LENGTH = sizeof(uint32_t)+2;
private:
static W433DataReciver* instance;
//constants
static constexpr uint8_t CLOCK_DEVIDER = 1;
static constexpr uint16_t LARGE_TIME = 500;
static constexpr uint16_t SMALL_TIME = 125;
static constexpr uint16_t SYNC_TIME = SMALL_TIME;
static constexpr uint8_t SYNC_TIME_TOLERANCE = SYNC_TIME*0.20;
static constexpr uint16_t SYNC_END_TIME_TOLERANCE = SYNC_TIME*0.80;
static constexpr uint16_t LARGE_TIME_TOLERANCE = LARGE_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 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;
#ifdef USE_RINGBUFFER
RingBuffer<RINGBUFFER_LENGTH, uint8_t> _ringBuffer;
#endif
volatile uint16_t previousTime = 0;
volatile uint8_t timesBufferIndex = 0;
volatile int16_t timesBuffer[33];
volatile uint8_t packetIndex = 0;
volatile uint32_t packet = 0;
void (* const _packetCallback)(uint32_t, void*);
void (* const _errorCodeHandler)(uint8_t, void*);
void* const _userData;
volatile uint8_t syncCount = 0;
volatile uint8_t syncFailCount = 0;
volatile uint8_t state = 0;
//private functions
int8_t reciveBit(uint8_t index);
inline uint8_t assmbleByte();
inline void setState(const uint8_t stateIn);
inline bool recivedByte(const uint16_t elapsedTime);
inline bool reciveSync(const uint16_t elapsedTime);
inline void error(const uint8_t errorCode);
static inline bool isTime(int16_t input, const uint16_t time, const bool state = true, const uint16_t tollerance = 100);
static constexpr int MODE_SEARCHING = 0;
static constexpr int MODE_RECEIVING = 1;
static W433DataReciver* instance;
volatile unsigned char *port;
unsigned char pin;
bool prevSample = 0;
uint8_t pllRamp = 0;
uint8_t rxIntegrator = 0;
uint16_t rxBits = 0;
uint8_t mode = MODE_SEARCHING;
uint8_t rxBuf[PACKET_LENGTH];
uint8_t rxCount = 0;
volatile uint8_t rxLen = 0;
void (* const packetCallback)(uint32_t, void*);
void (* const errorCodeHandler)(uint8_t, void*);
void* const userData;
private:
static uint16_t calcCrc(uint32_t data);
static uint8_t symbolDecode(uint8_t symbol);
void pll(bool sample);
public:
W433DataReciver(volatile unsigned char* const port , const unsigned char pin, volatile uint16_t * timerRegister, volatile uint8_t* const timerOverflowRegister, void (*packetCallback)(uint32_t, void*) = nullptr, void* userData = nullptr, void (*errorCodeHandler)(uint8_t, void*) = nullptr );
~W433DataReciver();
static void initTimer();
static void staticInterrupt();
void waitForReciveIdle(const uint16_t timeoutMs = 2000);
void interrupt();
#ifdef USE_RINGBUFFER
RingBuffer<RINGBUFFER_LENGTH, uint8_t>* getRingBuffer();
#endif
W433DataReciver(volatile unsigned char* const portIn, const unsigned char pinIn, void (*packetCallbackIn)(uint32_t,
void*),
void* userDataIn = nullptr, void (*errorCodeHandlerIn)(uint8_t, void*) = nullptr );
~W433DataReciver();
static void staticInterrupt();
void interrupt();
static uint16_t calculateOverflowRegister(uint16_t bitRate, uint16_t devisor);
};

98
W433DataTransmitter.cpp Normal file
View File

@ -0,0 +1,98 @@
#include "W433DataTransmitter.h"
#include "writepin.h"
W433DataTransmitter::W433DataTransmitter(volatile unsigned char *port, const unsigned char pin): _port(port), _pin(pin)
{
}
void W433DataTransmitter::sendBit(const bool bit)
{
switch(bit)
{
case true:
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
break;
case false:
writePin(_port,_pin,true);
_delay_us(LARGE_TIME);
writePin(_port,_pin,false);
_delay_us(SMALL_TIME);
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
break;
}
}
void W433DataTransmitter::sendSyncpulse()
{
for(uint8_t i = 0; i < 25; ++i)
{
writePin(_port,_pin,true);
_delay_us(SYNC_TIME);
writePin(_port,_pin,false);
_delay_us(SYNC_TIME);
}
}
void W433DataTransmitter::sendEndPulse()
{
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
writePin(_port,_pin,true);
_delay_us(LARGE_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME*10);
}
void W433DataTransmitter::sendRawData(const uint8_t data)
{
for(uint8_t i = 0; i < 8; i++) sendBit(data & ( 1 << (7 - i)));
}
void W433DataTransmitter::sendPacket(const uint32_t data)
{
sendSyncpulse();
_delay_us(LARGE_TIME);
sendRawData(signature);
uint8_t checksum = 0;
for(uint8_t i = 0; i < 4; ++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))) >> (8 - i));
sendRawData( dataOctet );
}
sendRawData( checksum );
sendEndPulse();
}
void W433DataTransmitter::send(const uint8_t* const data, uint16_t length)
{
uint16_t packets = length/4;
if(length % 4 != 0) ++packets;
for(uint8_t j = 0; j < packets; j++)
{
uint32_t paketData = 0;
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];
sendPacket(paketData);
}
}
void W433DataTransmitter::send(const uint8_t data)
{
sendPacket(data);
}

30
W433DataTransmitter.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <stdint.h>
#include <util/delay.h>
class W433DataTransmitter
{
private:
static constexpr uint16_t LARGE_TIME = 2000;
static constexpr uint16_t SMALL_TIME = 500;
static constexpr uint16_t SYNC_TIME = 800;
static constexpr uint8_t signature = 0xA5;
volatile unsigned char * const _port;
const unsigned char _pin;
void sendBit(const bool bit);
void sendSyncpulse();
void sendRawData(const uint8_t data);
void sendEndPulse();
public:
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 data);
void sendPacket(const uint32_t data);
};

View File

@ -7,111 +7,79 @@ unsigned char _pin = PB5;
void WirelessRelay::sendId()
{
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
for(short i = 0; i<10; i++)
{
sendBit( _id & 1 << (15 - i) );
}
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
for(short i = 0; i<10; i++)
{
sendBit( id & 1 << (15 - i) );
}
}
void WirelessRelay::sendBit(const bool in)
{
switch(in)
{
case true:
//Der Code fuer '0'
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
break;
void WirelessRelay::sendBit(const bool in)
{
switch(in)
{
case true:
//Der Code fuer '0'
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
break;
case false:
//Der Code fuer '1'
writePin(_port,_pin,true);
_delay_us(LARGE_TIME);
writePin(_port,_pin,false);
_delay_us(SMALL_TIME);
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
break;
}
case false:
//Der Code fuer '1'
writePin(_port,_pin,true);
_delay_us(LARGE_TIME);
writePin(_port,_pin,false);
_delay_us(SMALL_TIME);
writePin(_port,_pin,true);
_delay_us(SMALL_TIME);
writePin(_port,_pin,false);
_delay_us(LARGE_TIME);
break;
}
}
void WirelessRelay::sync()
{
writePin(_port,_pin,false);
_delay_us(SMALL_TIME*31);
writePin(_port,_pin,false);
_delay_us(SMALL_TIME*31);
}
void WirelessRelay::on()
void WirelessRelay::setValue(const uint8_t value)
{
_state = true;
for(short z = 0; z<10; z++)
{
sendId();
sendBit(true);
sendBit(false);
sync();
}
}
void WirelessRelay::off()
{
_state = false;
for(short z = 0; z<10; z++)
{
sendId();
sendBit(false);
sendBit(true);
sync();
}
}
uint16_t WirelessRelay::getId()
{
return _id;
}
bool WirelessRelay::getExpectedState()
{
return _state;
}
char* WirelessRelay::getName()
{
return _name;
}
void WirelessRelay::setName(char name[])
{
memcpy(_name, name, strlen(name)+1);
}
void WirelessRelay::init( const uint16_t id, char nameIn[])
{
setName(nameIn);
_id=id;
lastValue = value;
for(short z = 0; z<10; z++)
{
sendId();
sendBit(value);
sendBit(!value);
sync();
}
}
void WirelessRelay::resend()
{
_state ? on() : off();
setValue(lastValue);
}
WirelessRelay::WirelessRelay(const uint16_t id, char nameIn[])
WirelessRelay::WirelessRelay(const uint16_t idIn, char nameIn[])
{
init(id, nameIn);
id = idIn;
setName(nameIn);
type = 0;
}
WirelessRelay::WirelessRelay(){}
WirelessRelay::WirelessRelay(const Item& item)
{
Item::operator=(item);
type = 0;
}

View File

@ -3,33 +3,25 @@
#include<util/delay.h>
#include"writepin.h"
#include "item.h"
class WirelessRelay
class WirelessRelay: public Item
{
public:
static constexpr uint16_t LARGE_TIME = 750;
static constexpr uint8_t SMALL_TIME = 250;
static constexpr uint16_t MAX_NAME_LENGTH = 16;
static constexpr uint16_t LARGE_TIME = 750;
static constexpr uint8_t SMALL_TIME = 250;
static constexpr uint16_t MAX_NAME_LENGTH = 16;
private:
bool _state = false;
uint16_t _id;
char _name[MAX_NAME_LENGTH];
void sendBit(const bool i);
void sync();
void sendId();
void sendBit(const bool i);
void sync();
void sendId();
public:
WirelessRelay(const uint16_t id, char nameIn[]);
WirelessRelay();
void init(const uint16_t id, char nameIn[]);
void on();
void off();
char* getName();
void setName(char* name);
uint16_t getId();
bool getExpectedState();
WirelessRelay(const uint16_t idIn, char nameIn[]);
WirelessRelay(const Item& item);
void setValue(const uint8_t value);
void resend();
};
#endif

View File

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

View File

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

9
item.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "item.h"
#include "string.h"
void Item::setName(const char * const nameN)
{
size_t len = strlen(nameN);
if(len < MAX_NAME_LENGTH)memcpy(name, nameN, len+1);
}

14
item.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
class Item
{
public:
static constexpr uint16_t MAX_NAME_LENGTH = 16;
bool lastValue = 0;
uint16_t id;
char name[MAX_NAME_LENGTH]="";
uint8_t type = 0;
void setName(const char * const name);
};

677
main.cpp
View File

@ -12,16 +12,18 @@
#include "watchdog.h"
#include "staticvector.h"
#include "W433DataReciver.h"
#include "W433DataTransmitter.h"
#include "uvositem.h"
#define COMMAND_BUFFER_SIZE 64
#define SNPRINTF_BUFFER_SIZE 96
#define MAX_RELAYS 32
#define RELAY_VECTOR_EEPROM_ADDR 32
#define MAX_ITEMS 24
#define ITEM_VECTOR_EEPROM_ADDR 32
char buffer[SNPRINTF_BUFFER_SIZE];
SVector<WirelessRelay, MAX_RELAYS> relays;
SVector<Item, MAX_ITEMS> items;
bool sensorsPaused = false;
@ -29,15 +31,15 @@ static volatile bool resendNow = false;
static volatile uint8_t resendCounter = 0;
static bool resendEnabled = false;
ISR(PCINT1_vect)
ISR(TIMER1_COMPB_vect)
{
W433DataReciver::staticInterrupt();
W433DataReciver::staticInterrupt();
}
ISR(WDT_vect)
{
if(++resendCounter > 225)
{
{
resendCounter = 0;
if(resendEnabled)resendNow = true;
}
@ -45,13 +47,15 @@ ISR(WDT_vect)
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\
relay add [id] [name] : Add Wireless Relay.\n\
relay delete [n] : Delete n'th Relay.\n\
relay [on/off] [nn] : Turn on/off nth relay.\n\
relay resend [on/off] : Turn on/off periodic auto resend.\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 [on/off] [nn] : Turn on/off nth relay.\n\
item resend [on/off] : Turn on/off periodic auto resend. Save to make permant.\n\
save : Save current state as startup state.\n\
load : load startup state.\n\
state : Get machine readable state.\n\
erase : Erase epprom.\n\
dump : Dump epprom.\n\
@ -78,7 +82,7 @@ int freeRAM()
void save()
{
EEPROM_write_char(4, resendEnabled);
EEPROM_write_class< SVector<WirelessRelay, MAX_RELAYS> > (RELAY_VECTOR_EEPROM_ADDR, relays);
EEPROM_write_class< SVector<Item, MAX_ITEMS> > (ITEM_VECTOR_EEPROM_ADDR, items);
}
void loadRGB(RgbLed* rgbled)
@ -89,291 +93,302 @@ void loadRGB(RgbLed* rgbled)
void load()
{
resendEnabled = EEPROM_read_char(4);
EEPROM_read_class< SVector<WirelessRelay, MAX_RELAYS> > (RELAY_VECTOR_EEPROM_ADDR, &relays);
EEPROM_read_class< SVector<Item, MAX_ITEMS> > (ITEM_VECTOR_EEPROM_ADDR, &items);
}
void writeRelayState(Serial* serial, WirelessRelay* relay, uint8_t number)
void writeItemState(Serial* serial, Item* relay, uint8_t number)
{
uint16_t id = relay->getId();
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "RELAY NUMBER: %u ID: %s%s%s STATE: %u NAME: %s\n", number,
bit_rep[ id >> 12],
bit_rep[(id & 0x0F00) >> 8 ],
bit_rep[(id & 0x00F0) >> 4 ],
relay->getExpectedState(),
relay->getName()
);
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,
bit_rep[ id >> 12],
bit_rep[(id & 0x0F00) >> 8 ],
bit_rep[(id & 0x00F0) >> 4 ],
bit_rep[(id & 0x000F)],
relay->type,
relay->lastValue,
relay->name
);
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
}
void relayDispatch(SVector<WirelessRelay, MAX_RELAYS>* relays, Pwm16b* auxPwm, char* token, Serial* serial)
void itemDispatch(SVector<Item, MAX_ITEMS>* items, Pwm16b* auxPwm, char* token, Serial* serial)
{
if( strcmp(token, "add") == 0 )
{
token = strtok(NULL, " \n");
uint16_t id = strtol(token, nullptr, 2 );
if(id != 0 && relays->remainingCapacity() > 0)
{
id = id << 4;
token = strtok(NULL, "\0");
if( token == NULL )
{
char name[] = "";
WirelessRelay relay(id, name);
relays->push_back(relay);
}
else
{
WirelessRelay relay(id, token);
relays->push_back(relay);
}
writeRelayState(serial, &relays->back(), relays->count()-1);
save();
}
else if(relays->remainingCapacity() == 0) serial->write_p(PSTR("Relay storage full.\n"));
else serial->write_p(PSTR("Usage: relay add [id] [name]\n [id] being a 16bit binary nummber and [name] an optional string\n"));
}
else if( strcmp(token, "delete") == 0 )
{
if(strcmp(token, "add") == 0)
{
token = strtok(NULL, " \n");
if(relays->count() > 0)
uint16_t id = strtol(token, nullptr, 2 );
token = strtok(NULL, " \n");
uint8_t type = strtol(token, nullptr, 10 );
if(id != 0 && (type == 0 || type == 1) && items->remainingCapacity() > 0)
{
uint16_t index = relays->count();
if( token != NULL) index = atoi(token);
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "Deleting relay NUMBER: %u NAME: %s\n", index, relays->at(index).getName());
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
relays->erase(index);
save();
token = strtok(NULL, "\n\0");
Item item;
item.id = id;
item.type = type;
if( token != NULL )
item.setName(token);
items->push_back(item);
writeItemState(serial, &items->back(), items->count()-1);
}
}
else if( strcmp(token, "on") == 0 )
{
char* token = strtok(NULL, " \n");
if( token != NULL)
{
uint8_t selected = strtol(token, nullptr, 10);
if (selected < relays->count())
{
relays->at(selected).on();
writeRelayState(serial, &relays->at(selected), selected);
}
else serial->write_p(PSTR("No sutch Relay\n"));
}
else serial->write_p(PSTR("Usage: relay on [nn]\n"));
}
else if( strcmp(token, "off") == 0 )
{
char* token = strtok(NULL, " \n");
if( token != NULL)
{
uint8_t selected = strtol(token, nullptr, 10);
if (selected < relays->count())
{
relays->at(selected).off();
writeRelayState(serial, &relays->at(selected), selected);
}
else serial->write_p(PSTR("No sutch Relay\n"));
}
else serial->write_p(PSTR("Usage: relay off [nn]\n"));
}
else if( strcmp(token, "resend") == 0 )
{
char* token = strtok(NULL, " \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 if(strcmp(token, "delete") == 0)
{
token = strtok(NULL, " \n");
if(items->count() > 0)
{
uint16_t index = items->count();
if( token != NULL) index = atoi(token);
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "Deleting item NUMBER: %u NAME: %s\n", index, items->at(index).name);
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
items->erase(index);
}
}
else if(strcmp(token, "on") == 0)
{
char* token = strtok(NULL, " \n");
if(token != NULL)
{
uint8_t selected = strtol(token, nullptr, 10);
if (selected < items->count())
{
items->at(selected).lastValue = true;
if(items->at(selected).type == 0)WirelessRelay(items->at(selected)).setValue(true);
else UvosItem(items->at(selected)).setValue(true);
writeItemState(serial, &items->at(selected), selected);
}
else serial->write_p(PSTR("No sutch item\n"));
}
else serial->write_p(PSTR("Usage: item on [nn]\n"));
}
else if(strcmp(token, "off") == 0)
{
char* token = strtok(NULL, " \n");
if( token != NULL)
{
uint8_t selected = strtol(token, nullptr, 10);
if (selected < items->count())
{
items->at(selected).lastValue = false;
if(items->at(selected).type == 0)WirelessRelay(items->at(selected)).setValue(false);
else UvosItem(items->at(selected)).setValue(false);
writeItemState(serial, &items->at(selected), selected);
}
else serial->write_p(PSTR("No sutch item\n"));
}
else serial->write_p(PSTR("Usage: item off [nn]\n"));
}
else if(strcmp(token, "resend") == 0)
{
char* token = strtok(NULL, " \n");
serial->write_p(PSTR("Resend every 30 min is "));
if( token != NULL )
{
if( token != NULL )
{
serial->write_p(PSTR("now "));
if(strcmp(token, "on") == 0) resendEnabled = true;
if(strcmp(token, "on") == 0) resendEnabled = true;
else resendEnabled = false;
save();
}
resendEnabled ? serial->write_p(PSTR("enabled.\n")) : serial->write_p(PSTR("disabled.\n")) ;
}
else
{
serial->write(token);
serial->write_p(PSTR(" is not a valid subcommand: relay [add/delete/on/off]\n"));
}
}
resendEnabled ? serial->write_p(PSTR("enabled.\n")) : serial->write_p(PSTR("disabled.\n")) ;
}
else
{
serial->write(token);
serial->write_p(PSTR(" is not a valid subcommand: item [add/delete/on/off]\n"));
}
}
void rgbDispatch(RgbLed* rgbled, char* token, Serial* serial)
{
if( strcmp(token, "on") == 0 )
{
rgbled->on();
serial->write_p(PSTR("RGB lights on\n"));
}
else if( strcmp(token, "off") == 0 )
{
rgbled->off();
serial->write_p(PSTR("RGB lights off\n"));
}
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());
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
}
else if( strcmp(token, "set") == 0 )
{
char* rToken = strtok(NULL, " \n");
char* gToken = strtok(NULL, " \n");
char* bToken = strtok(NULL, " \n");
if(rToken != NULL && gToken != NULL && bToken != NULL)
{
if( strcmp(token, "on") == 0 )
{
rgbled->on();
serial->write_p(PSTR("RGB lights on\n"));
}
else if( strcmp(token, "off") == 0 )
{
rgbled->off();
serial->write_p(PSTR("RGB lights off\n"));
}
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());
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
}
else if( strcmp(token, "set") == 0 )
{
char* rToken = strtok(NULL, " \n");
char* gToken = strtok(NULL, " \n");
char* bToken = strtok(NULL, " \n");
if(rToken != NULL && gToken != NULL && bToken != NULL)
{
uint8_t r = atoi(rToken);
uint8_t g = atoi(gToken);
uint8_t b = atoi(bToken);
rgbled->setSolidColor(r,g,b);
rgbled->setSolidColor(r,g,b);
EEPROM_write_char(1, r);
EEPROM_write_char(2, g);
EEPROM_write_char(3, b);
serial->write_p(PSTR("Set RGB values\n"));
}
else serial->write_p(PSTR("Usage: rgb set [RRR] [GGG] [BBB]\n"));
}
else if( strcmp(token, "pattern") == 0 )
{
token = strtok(NULL, " \n");
if( token != NULL )
{
rgbled->setPattern(atoi(token));
serial->write_p(PSTR("Set Pattern\n"));
}
else serial->write_p(PSTR("Usage: rgb pattern [id]\n"));
}
else if( strcmp(token, "preset") == 0 )
{
token = strtok(NULL, " \n");
if( token != NULL )
{
rgbled->setPreset(atoi(token));
serial->write_p(PSTR("Set Preset\n"));
}
else serial->write_p(PSTR("Usage: rgb preset [ID]\n"));
}
else if( strcmp(token, "fade") == 0 )
{
token = strtok(NULL, " \n");
if( token != NULL )
{
if( strcmp(token, "on") == 0 )
{
rgbled->setFade(true);
serial->write_p(PSTR("Turned on Fade\n"));
}
else
{
rgbled->setFade(false);
serial->write_p(PSTR("Turned off Fade\n"));
}
}
else serial->write_p(PSTR("Usage: rgb fade [on/off]\n"));
}
else
{
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("Set RGB values\n"));
}
else serial->write_p(PSTR("Usage: rgb set [RRR] [GGG] [BBB]\n"));
}
else if( strcmp(token, "pattern") == 0 )
{
token = strtok(NULL, " \n");
if( token != NULL )
{
rgbled->setPattern(atoi(token));
serial->write_p(PSTR("Set Pattern\n"));
}
else serial->write_p(PSTR("Usage: rgb pattern [id]\n"));
}
else if( strcmp(token, "preset") == 0 )
{
token = strtok(NULL, " \n");
if( token != NULL )
{
rgbled->setPreset(atoi(token));
serial->write_p(PSTR("Set Preset\n"));
}
else serial->write_p(PSTR("Usage: rgb preset [ID]\n"));
}
else if( strcmp(token, "fade") == 0 )
{
token = strtok(NULL, " \n");
if( token != NULL )
{
if( strcmp(token, "on") == 0 )
{
rgbled->setFade(true);
serial->write_p(PSTR("Turned on Fade\n"));
}
else
{
rgbled->setFade(false);
serial->write_p(PSTR("Turned off Fade\n"));
}
}
else serial->write_p(PSTR("Usage: rgb fade [on/off]\n"));
}
else
{
serial->write(token);
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)
{
if(strcmp(token, "set") == 0 )
{
token = strtok(NULL, " \n");
if(token != NULL)
{
{
token = strtok(NULL, " \n");
if(token != NULL)
{
if(atoi(token) == 0) auxPwm->off();
else auxPwm->on();
auxPwm->setDutyA(atoi(token) << 8);
serial->write_p(PSTR("Set PWM value\n"));
}
else serial->write_p(PSTR("Usage: aux set [VALUE]\n"));
}
else
{
serial->write(token, COMMAND_BUFFER_SIZE-4);
serial->write_p(PSTR(" is not a valid subcommand: aux set [value]\n"));
}
auxPwm->setDutyA(atoi(token) << 8);
serial->write_p(PSTR("Set PWM value\n"));
}
else serial->write_p(PSTR("Usage: aux set [VALUE]\n"));
}
else
{
serial->write(token, COMMAND_BUFFER_SIZE-4);
serial->write_p(PSTR(" is not a valid subcommand: aux set [value]\n"));
}
}
void serialDispatch(Serial* serial, SVector<WirelessRelay, MAX_RELAYS>* relays, RgbLed* rgbled, Pwm16b* auxPwm, W433DataReciver* reciver)
void serialDispatch(Serial* serial, SVector<Item, MAX_ITEMS>* items, RgbLed* rgbled, Pwm16b* auxPwm,
W433DataReciver* reciver)
{
if(serial->dataIsWaiting())
{
char buffer[COMMAND_BUFFER_SIZE];
unsigned int length = serial->getString(buffer, COMMAND_BUFFER_SIZE);
if(length > 2)
{
if(serial->dataIsWaiting())
{
char buffer[COMMAND_BUFFER_SIZE];
unsigned int length = serial->getString(buffer, COMMAND_BUFFER_SIZE);
if(length > 2)
{
setBit(&PCICR, PCIE1, false);
char* token = strtok(buffer, " \n");
if(strcmp(token, "relay") == 0)
{
relayDispatch(relays, auxPwm, strtok(NULL, " \n"), serial);
}
else if(strcmp(token, "rgb") == 0)
{
reciver->waitForReciveIdle();
rgbDispatch(rgbled, strtok(NULL, " \n"), serial);
}
else if(strcmp(token, "aux") == 0)
{
auxDispatch(auxPwm, strtok(NULL, " \n"), serial);
}
else if(strcmp(token, "pause") == 0)
{
sensorsPaused = true;
char* token = strtok(buffer, " \n");
if(strcmp(token, "item") == 0)
{
//reciver->waitForReciveIdle();
itemDispatch(items, auxPwm, strtok(NULL, " \n"), serial);
}
else if(strcmp(token, "rgb") == 0)
{
rgbDispatch(rgbled, strtok(NULL, " \n"), serial);
}
else if(strcmp(token, "aux") == 0)
{
auxDispatch(auxPwm, strtok(NULL, " \n"), serial);
}
else if(strcmp(token, "pause") == 0)
{
sensorsPaused = true;
serial->write_p(PSTR("Sensors paused\n"));
}
else if(strcmp(token, "resume") == 0)
{
sensorsPaused = false;
}
else if(strcmp(token, "resume") == 0)
{
sensorsPaused = false;
serial->write_p(PSTR("Sensors resumed\n"));
}
else if(strcmp(token, "state") == 0)
{
serial->write_p(PSTR("Relays:\n"));
for(uint8_t i = 0; i < relays->count(); i++)
{
writeRelayState(serial, &relays->at(i), i);
//serial->putChar('\n');
}
serial->write_p(PSTR("EOL\n"));
}
else if(strcmp(token, "erase") == 0)
{
for(uint16_t i = 0; i < 1024; i++) EEPROM_write_char(i, 0);
serial->write_p(PSTR("EEPROM erased\n"));
load();
}
else if(strcmp(token, "dump") == 0)
{
for(uint16_t i = 0; i < 1024; i++)
{
if(i != 0) serial->putChar(',');
serial->write((uint16_t)EEPROM_read_char(i));
}
serial->putChar('\n');
}
else if(strcmp(token, "free") == 0)
{
serial->write_p(PSTR("Free Ram: "));
}
else if(strcmp(token, "state") == 0)
{
serial->write_p(PSTR("Items:\n"));
for(uint8_t i = 0; i < items->count(); i++)
{
writeItemState(serial, &items->at(i), i);
}
serial->write_p(PSTR("EOL\n"));
}
else if(strcmp(token, "save") == 0)
{
save();
serial->write_p(PSTR("State saved to EEPROM.\n"));
}
else if(strcmp(token, "load") == 0)
{
load();
serial->write_p(PSTR("Loaded state from EEPROM.\n"));
}
else if(strcmp(token, "erase") == 0)
{
for(uint16_t i = 0; i < 1024; i++) EEPROM_write_char(i, 0);
serial->write_p(PSTR("EEPROM erased\n"));
load();
}
else if(strcmp(token, "dump") == 0)
{
for(uint16_t i = 0; i < 1024; i++)
{
if(i != 0) serial->putChar(',');
serial->write((uint16_t)EEPROM_read_char(i));
}
serial->putChar('\n');
}
else if(strcmp(token, "free") == 0)
{
serial->write_p(PSTR("Free Ram: "));
serial->write(freeRAM());
serial->write_p(PSTR(" Bytes.\n"));
}
else if(strcmp(token, "help") == 0)
{
printHelp(serial);
}
else
{
serial->write_p(PSTR("Not a valid command\n"));
}
setBit(&PCICR, PCIE1, true);
}
}
}
else if(strcmp(token, "help") == 0)
{
printHelp(serial);
}
else
{
serial->write_p(PSTR("Not a valid command\n"));
}
setBit(&PCICR, PCIE1, true);
}
}
}
void reciverError(uint8_t code, void* userData)
@ -381,7 +396,7 @@ void reciverError(uint8_t code, void* userData)
if(!sensorsPaused)
{
Serial* serial = reinterpret_cast<Serial*>(userData);
serial->write_p(PSTR("ERROR CODE: "));
serial->write_p(PSTR("RECV ERROR CODE: "));
serial->write(code);
serial->putChar('\n');
}
@ -392,69 +407,73 @@ void sensorPacketRecived(uint32_t data, void* userData)
if(!sensorsPaused)
{
Serial* serial = reinterpret_cast<Serial*>(userData);
uint16_t field = data & 0x0000FFFF;
serial->write_p(PSTR("SENSOR TYPE: "));
serial->write(data >> 24);
serial->write_p(PSTR(" ID: "));
serial->write((data & 0x00FF0000) >> 16);
if(data >> 24 == 1) serial->write_p(PSTR(" TEMPERATURE: "));
else if(data >> 24 == 2) serial->write_p(PSTR(" HUMIDITY: "));
else serial->write_p(PSTR(" FIELD: "));
if(data >> 24 == 1) serial->write_p(PSTR(" TEMPERATURE: "));
else if(data >> 24 == 2) serial->write_p(PSTR(" HUMIDITY: "));
else serial->write_p(PSTR(" FIELD: "));
serial->write(field);
serial->putChar('\n');
}
}
int main()
int main()
{
wdt_set(WDTO_8S);
DDRB = (1 << PB5) | ( 1 << PB1);
DDRD = (1 << PD3) | (1 << PD5)| (1 << PD6);
//door watcher
PORTB = (1<< PB3) | (1<< PB4); //Enable pull up on door watcher pins;
bool doorOne = readPin(&PINB, PB3);
bool doorTow = readPin(&PINB, PB4);
sei();
Serial serial;
Pwm8b pwmTc0( &TCCR0A, &TCCR0B, &OCR0A, &OCR0B, 0b00000011, true, true );
Pwm8b pwmTc2( &TCCR2A, &TCCR2B, &OCR2A, &OCR2B, 0b00000101, false, true );
pwmTc0.off();
pwmTc2.off();
RgbLed rgbled( &pwmTc0, &pwmTc2 );
DDRB = (1 << PB5) | ( 1 << PB1);
DDRD = (1 << PD3) | (1 << PD5)| (1 << PD6);
//door watcher
PORTB = (1<< PB3) | (1<< PB4); //Enable pull up on door watcher pins;
bool doorOne = readPin(&PINB, PB3);
bool doorTow = readPin(&PINB, PB4);
sei();
Serial serial;
Pwm8b pwmTc0( &TCCR0A, &TCCR0B, &OCR0A, &OCR0B, 0b00000011, true, true );
Pwm8b pwmTc2( &TCCR2A, &TCCR2B, &OCR2A, &OCR2B, 0b00000101, false, true );
pwmTc0.off();
pwmTc2.off();
RgbLed rgbled( &pwmTc0, &pwmTc2 );
loadRGB(&rgbled);
Pwm16b pwmTc1 ( &TCCR1A, &TCCR1B, &OCR1A, &OCR1B, &ICR1, 0b00000001, true, false);
setBit(&PCICR, PCIE1, true);
setBit(&PCMSK1, PCINT8, true);
W433DataReciver reciver(&PINC, PC0, &TCNT1, &TIFR1, &sensorPacketRecived, reinterpret_cast<void*>(&serial), &reciverError);
serial.write_p(PSTR("RGBController v1.1 starting\n"));
load();
while(true)
{
serialDispatch(&serial, &relays, &rgbled, &pwmTc1, &reciver);
rgbled.logic();
if(doorOne != readPin(&PINB, PB3) && !sensorsPaused)
{
_delay_ms(10);
if(doorOne != readPin(&PINB, PB3))
{
Pwm16b pwmTc1(&TCCR1A, &TCCR1B, &OCR1A, &OCR1B, &ICR1, 0b00000001, true, false);
setBit(&TIMSK1, OCIE1B, true);
setBit(&TCCR1B, CS10, true);
ICR1 = W433DataReciver::calculateOverflowRegister(2000, 1);
OCR1B = ICR1-1;
W433DataReciver reciver(&PINC, PC0, &sensorPacketRecived, reinterpret_cast<void*>(&serial), &reciverError);
W433DataTransmitter transmitter(&PORTB, PB5);
UvosItem::transmitter = &transmitter;
serial.write_p(PSTR("RGBController v1.5 starting\n"));
load();
while(true)
{
serialDispatch(&serial, &items, &rgbled, &pwmTc1, &reciver);
rgbled.logic();
if(doorOne != readPin(&PINB, PB3) && !sensorsPaused)
{
_delay_ms(10);
if(doorOne != readPin(&PINB, PB3))
{
doorOne = readPin(&PINB, PB3);
serial.write_p(PSTR("SENSOR TYPE: "));
serial.putChar('0');
@ -463,15 +482,15 @@ int main()
serial.write_p(PSTR(" STATE: "));
serial.write(doorOne);
serial.putChar('\n');
}
}
if(doorTow != readPin(&PINB, PB4) && !sensorsPaused)
{
_delay_ms(10);
if(doorTow != readPin(&PINB, PB4))
{
}
}
if(doorTow != readPin(&PINB, PB4) && !sensorsPaused)
{
_delay_ms(10);
if(doorTow != readPin(&PINB, PB4))
{
doorTow = readPin(&PINB, PB4);
serial.write_p(PSTR("SENSOR TYPE: "));
serial.putChar('0');
@ -480,22 +499,22 @@ int main()
serial.write_p(PSTR(" STATE: "));
serial.write(doorTow);
serial.putChar('\n');
}
}
if(resendNow)
}
}
if(resendNow)
{
for(uint16_t i = 0; i < relays.count(); i++)
for(uint16_t i = 0; i < items.count(); i++)
{
reciver.waitForReciveIdle();
relays[i].resend();
//reciver.waitForReciveIdle();
items[i].type == 0 ? WirelessRelay(items[i]).resend() : UvosItem(items[i]).resend();
_delay_ms(100);
}
resendNow = false;
}
_delay_ms(2);
}
_delay_ms(2);
}
return 0;
return 0;
}

124
pwm.cpp
View File

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

66
pwm.h
View File

@ -6,44 +6,48 @@
class Pwm16b //TC1 pwm on PB1 & PB2
{
private:
volatile unsigned char *_timerControlRegisterA; //TCCRxA
volatile uint16_t *_compareRegisterA; //OCRxA
volatile uint16_t *_compareRegisterB; //OCRxB
bool _enableA;
bool _enableB;
volatile unsigned char *_timerControlRegisterA; //TCCRxA
volatile uint16_t *_compareRegisterA; //OCRxA
volatile uint16_t *_compareRegisterB; //OCRxB
bool _enableA;
bool _enableB;
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();
void setDutyA(const uint16_t duty);
void setDutyB(const uint16_t duty);
uint16_t getValueA();
uint16_t getValueB();
bool isOn();
void off();
void on();
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();
void setDutyA(const uint16_t duty);
void setDutyB(const uint16_t duty);
uint16_t getValueA();
uint16_t getValueB();
bool isOn();
void off();
void on();
};
class Pwm8b
{
private:
volatile unsigned char *_timerControlRegisterA; //TCCRxA
volatile unsigned char *_compareRegisterA; //OCRxA
volatile unsigned char *_compareRegisterB; //OCRxB
bool _enableA;
bool _enableB;
volatile unsigned char *_timerControlRegisterA; //TCCRxA
volatile unsigned char *_compareRegisterA; //OCRxA
volatile unsigned char *_compareRegisterB; //OCRxB
bool _enableA;
bool _enableB;
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();
void setDutyA(const uint8_t duty);
void setDutyB(const uint8_t duty);
uint8_t getValueA();
uint8_t getValueB();
bool isOn();
void off();
void on();
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();
void setDutyA(const uint8_t duty);
void setDutyB(const uint8_t duty);
uint8_t getValueA();
uint8_t getValueB();
bool isOn();
void off();
void on();
};
#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)
{
_pattern=0;
_targetR = applyCal(r, calRed);
_targetG = applyCal(g, calGreen);
_targetB = applyCal(b, calBlue);
_pattern=0;
_targetR = applyCal(r, calRed);
_targetG = applyCal(g, calGreen);
_targetB = applyCal(b, calBlue);
}
void RgbLed::setPreset( const uint8_t preset)
{
switch (preset)
{
//whites
case 1:
setSolidColor( 160,255,80 ); //neutral white
break;
case 2:
setSolidColor( 200,255,20 ); //Warm white
break;
case 3:
setSolidColor( 180,255,140 ); //cold white
break;
//reds
case 4:
setSolidColor( 255,0,0 ); //red
break;
case 5:
setSolidColor( 255,60,10 ); //ruby
break;
case 6:
setSolidColor( 255,30,30 ); //pink
break;
case 7:
setSolidColor( 255,155,0 ); //orange
break;
//greens
case 8:
setSolidColor( 0,255,0 ); //green
break;
case 9:
setSolidColor( 55,255,10 ); //poison
break;
case 10:
setSolidColor( 0,255,0 ); //green
break;
case 11:
setSolidColor( 8,80,7 ); //mint
break;
//blues
case 12:
setSolidColor( 0,0,255 ); //blue
break;
case 13:
setSolidColor( 50,255,255 ); //sky
break;
case 14:
setSolidColor( 10,80,150 ); //ocean
break;
case 15:
setSolidColor( 0,255,220 ); //turqouse
break;
//strange yellow color color
case 16:
setSolidColor( 200,255,0 );
break;
default:
setSolidColor( 255,055,20 );
}
switch (preset)
{
//whites
case 1:
setSolidColor( 160,255,80 ); //neutral white
break;
case 2:
setSolidColor( 200,255,20 ); //Warm white
break;
case 3:
setSolidColor( 180,255,140 ); //cold white
break;
//reds
case 4:
setSolidColor( 255,0,0 ); //red
break;
case 5:
setSolidColor( 255,60,10 ); //ruby
break;
case 6:
setSolidColor( 255,30,30 ); //pink
break;
case 7:
setSolidColor( 255,155,0 ); //orange
break;
//greens
case 8:
setSolidColor( 0,255,0 ); //green
break;
case 9:
setSolidColor( 55,255,10 ); //poison
break;
case 10:
setSolidColor( 0,255,0 ); //green
break;
case 11:
setSolidColor( 8,80,7 ); //mint
break;
//blues
case 12:
setSolidColor( 0,0,255 ); //blue
break;
case 13:
setSolidColor( 50,255,255 ); //sky
break;
case 14:
setSolidColor( 10,80,150 ); //ocean
break;
case 15:
setSolidColor( 0,255,220 ); //turqouse
break;
//strange yellow color color
case 16:
setSolidColor( 200,255,0 );
break;
default:
setSolidColor( 255,055,20 );
}
}
void RgbLed::setPattern(const uint8_t id)
{
_pattern=id;
if( id != 0 )
{
_pwmA->setDutyB(0);
_pwmA->setDutyA(0);
_pwmB->setDutyB(0);
on();
_stroke = false;
_counter = 0;
}
_pattern=id;
if( id != 0 )
{
_pwmA->setDutyB(0);
_pwmA->setDutyA(0);
_pwmB->setDutyB(0);
on();
_stroke = false;
_counter = 0;
}
}
void RgbLed::on()
{
_powerd = true;
_pwmA->on();
_pwmB->on();
_powerd = true;
_pwmA->on();
_pwmB->on();
}
void RgbLed::off()
{
_powerd = false;
_pwmA->off();
_pwmB->off();
_powerd = false;
_pwmA->off();
_pwmB->off();
}
void RgbLed::setFade(bool fade)
{
_fade=fade;
_fade=fade;
}
uint8_t RgbLed::applyCal(uint16_t value, const uint16_t* cal)
@ -114,101 +114,102 @@ uint8_t RgbLed::applyCal(uint16_t value, const uint16_t* cal)
return (value*calValue)/1000;
}
//unfinished
void RgbLed::adjustHeadroom(uint8_t& r, uint8_t& g, uint8_t& b, const uint8_t lumina)
{
uint8_t postCalLumina = ((uint16_t)r+g+b)/3;
while(postCalLumina < lumina && r < 255 && g < 255 && b < 255)
{
if(r > 255);
if(r > 255) break;
}
}
void RgbLed::patternStep()
{
if(_pattern == 1)
{
if(!_stroke)_counter++;
else _counter --;
if(_counter == 255) _stroke = true;
else if(_counter == 0) _stroke = false;
_pwmA->setDutyB(_counter);
_pwmA->setDutyA(255-_counter);
_pwmB->setDutyB(_counter-64);
}
else if(_pattern == 2) //Alarm!
{
if(!_stroke)_counter++;
else _counter --;
if(_counter == 255 << 1) _stroke = true;
else if(_counter == 0) _stroke = false;
_pwmA->setDutyB(_counter >> 1);
_pwmA->setDutyA(0);
_pwmB->setDutyB(0);
}
else if(_pattern == 3)
{
if(!_stroke)_counter++;
else _counter --;
if(_counter == (uint8_t) 255 << 8) _stroke = true;
else if(_counter == 0) _stroke = false;
_pwmA->setDutyB(_counter >> 6);
_pwmA->setDutyA(_counter >> 8);
_pwmB->setDutyB(_counter >> 3);
}
else if(_pattern == 4)
{
( _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 > 8192 ) _pwmB->setDutyB(_counter >> 9);
if(_counter<65530) _counter++;
else _pwmB->setDutyB(140);
_delay_ms(18);
}
if(_pattern == 1)
{
if(!_stroke)_counter++;
else _counter --;
if(_counter == 255) _stroke = true;
else if(_counter == 0) _stroke = false;
_pwmA->setDutyB(_counter);
_pwmA->setDutyA(255-_counter);
_pwmB->setDutyB(_counter-64);
}
else if(_pattern == 2) //Alarm!
{
if(!_stroke)_counter++;
else _counter --;
if(_counter == 255 << 1) _stroke = true;
else if(_counter == 0) _stroke = false;
_pwmA->setDutyB(_counter >> 1);
_pwmA->setDutyA(0);
_pwmB->setDutyB(0);
}
else if(_pattern == 3)
{
if(!_stroke)_counter++;
else _counter --;
if(_counter == (uint8_t) 255 << 8) _stroke = true;
else if(_counter == 0) _stroke = false;
_pwmA->setDutyB(_counter >> 6);
_pwmA->setDutyA(_counter >> 8);
_pwmB->setDutyB(_counter >> 3);
}
else if(_pattern == 4)
{
( _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 > 8192 ) _pwmB->setDutyB(_counter >> 9);
if(_counter<65530) _counter++;
else _pwmB->setDutyB(140);
_delay_ms(18);
}
}
void RgbLed::logic()
{
patternStep();
if(_pattern == 0 && _fade)
{
_counter++;
if( uint8_t(_counter << _fadeSpeed) == 0)
{
if( getR() != _targetR)
{
_pwmA->setDutyB(getR() - sgn(getR() - _targetR));
}
if( getG() != _targetG)
{
_pwmA->setDutyA(getG() - sgn(getG() - _targetG));
}
if( getB() != _targetB)
{
_pwmB->setDutyB(getB() - sgn(getB() - _targetB));
}
}
}
else if(_pattern == 0)
{
_pwmA->setDutyA(_targetG);
_pwmA->setDutyB(_targetR);
_pwmB->setDutyB(_targetB);
}
patternStep();
if(_pattern == 0 && _fade)
{
_counter++;
if( uint8_t(_counter << _fadeSpeed) == 0)
{
if( getR() != _targetR)
{
_pwmA->setDutyB(getR() - sgn(getR() - _targetR));
}
if( getG() != _targetG)
{
_pwmA->setDutyA(getG() - sgn(getG() - _targetG));
}
if( getB() != _targetB)
{
_pwmB->setDutyB(getB() - sgn(getB() - _targetB));
}
}
}
else if(_pattern == 0)
{
_pwmA->setDutyA(_targetG);
_pwmA->setDutyB(_targetR);
_pwmB->setDutyB(_targetB);
}
}
uint8_t RgbLed::getR()
{
return _pwmA->getValueB();
return _pwmA->getValueB();
}
uint8_t RgbLed::getB()
{
return _pwmB->getValueB();
return _pwmB->getValueB();
}
uint8_t RgbLed::getG()
{
return _pwmA->getValueA();
return _pwmA->getValueA();
}

View File

@ -4,56 +4,54 @@
class RgbLed
{
private:
Pwm8b* _pwmA;
Pwm8b* _pwmB;
Pwm8b* _pwmA;
Pwm8b* _pwmB;
static constexpr uint16_t calRed[] = {1000, 1000, 1000};
static constexpr uint16_t calGreen[] = {1000, 1000, 1000};
static constexpr uint16_t calBlue[] = {400, 500, 500};
uint8_t _pattern = 0;
uint16_t _counter = 0;
bool _stroke = false;
uint8_t _targetR = 0;
uint8_t _targetG = 0;
uint8_t _targetB = 0;
bool _fade = true;
uint8_t _fadeSpeed = 7;
bool _powerd = false;
void patternStep();
uint8_t _pattern = 0;
uint16_t _counter = 0;
bool _stroke = false;
uint8_t _targetR = 0;
uint8_t _targetG = 0;
uint8_t _targetB = 0;
bool _fade = true;
uint8_t _fadeSpeed = 7;
bool _powerd = false;
void patternStep();
uint16_t getCalValue();
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);
public:
RgbLed( Pwm8b* pwmA, Pwm8b* pwmB );
void setSolidColor( const uint8_t r, const uint8_t g, const uint8_t b);
void setPattern(const uint8_t id);
void setPreset( const uint8_t preset);
void on();
void off();
void setFade(bool fade = true);
void logic();
uint8_t getR();
uint8_t getB();
uint8_t getG();
bool isPowerd();
uint8_t getPattern();
RgbLed( Pwm8b* pwmA, Pwm8b* pwmB );
void setSolidColor( const uint8_t r, const uint8_t g, const uint8_t b);
void setPattern(const uint8_t id);
void setPreset( const uint8_t preset);
void on();
void off();
void setFade(bool fade = true);
void logic();
uint8_t getR();
uint8_t getB();
uint8_t getG();
bool isPowerd();
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
{
private:
volatile uint_fast16_t _headIndex = 0;
volatile uint_fast16_t _tailIndex = 0;
volatile uint_fast16_t _headIndex = 0;
volatile uint_fast16_t _tailIndex = 0;
volatile bool _overrun = false;
volatile T _buffer[BUFFER_SIZE];
volatile T _buffer[BUFFER_SIZE];
public:
RingBuffer()
{
flush();
}
uint_fast16_t remaining() const volatile
{
return (_headIndex-_tailIndex);
}
uint_fast16_t remainingCapacity() const volatile
{
return BUFFER_SIZE - (_headIndex-_tailIndex);
}
bool isOverun() volatile
{
bool returnVal = _overrun;
_overrun = false;
return returnVal;
}
bool isEmpty() const volatile
{
return _tailIndex >= _headIndex;
}
T read() volatile
{
if(!isEmpty())
{
_tailIndex++;
return _buffer[(_tailIndex - 1) % BUFFER_SIZE];
}
else return '\0';
}
unsigned int read( T* buffer, unsigned int length ) volatile
{
unsigned int i = 0;
for(; i < length && !isEmpty(); i++)
{
buffer[i] = read();
}
return i;
}
void write( T in ) volatile
{
if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0)
{
_headIndex -= BUFFER_SIZE;
_tailIndex -= BUFFER_SIZE;
}
_buffer[_headIndex % BUFFER_SIZE] = in;
_headIndex++;
if(remaining() > BUFFER_SIZE)
RingBuffer()
{
flush();
}
uint_fast16_t remaining() const volatile
{
return (_headIndex-_tailIndex);
}
uint_fast16_t remainingCapacity() const volatile
{
return BUFFER_SIZE - (_headIndex-_tailIndex);
}
bool isOverun() volatile
{
bool returnVal = _overrun;
_overrun = false;
return returnVal;
}
bool isEmpty() const volatile
{
return _tailIndex >= _headIndex;
}
T read() volatile
{
if(!isEmpty())
{
_tailIndex++;
return _buffer[(_tailIndex - 1) % BUFFER_SIZE];
}
else return '\0';
}
unsigned int read( T* buffer, unsigned int length ) volatile
{
unsigned int i = 0;
for(; i < length && !isEmpty(); i++)
{
buffer[i] = read();
}
return i;
}
void write( T in ) volatile
{
if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0)
{
_headIndex -= BUFFER_SIZE;
_tailIndex -= BUFFER_SIZE;
}
_buffer[_headIndex % BUFFER_SIZE] = in;
_headIndex++;
if(remaining() > BUFFER_SIZE)
{
_overrun = true;
_tailIndex = _headIndex - BUFFER_SIZE;
}
}
void write( T* buffer, const unsigned int length ) volatile
{
for(unsigned int i = 0; i < length; i++) write(buffer[i]);
}
void flush(T flushCharacter = ' ') volatile
{
_headIndex = 0;
_tailIndex = 0;
for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = flushCharacter;
}
unsigned int getString(T terminator, T* buffer, const unsigned int bufferLength) volatile
{
unsigned int i = 0;
for(; i <= remaining() && i <= BUFFER_SIZE && _buffer[(_tailIndex+i) % BUFFER_SIZE] != terminator; i++);
if( i < remaining() && i > 0)
{
if(i > bufferLength-1) i = bufferLength-1;
read(buffer, i);
buffer[i]='\0';
_tailIndex++;
}
else if(i == 0) _tailIndex++;
else i = 0;
return i;
}
}
void write( T* buffer, const unsigned int length ) volatile
{
for(unsigned int i = 0; i < length; i++) write(buffer[i]);
}
void flush(T flushCharacter = ' ') volatile
{
_headIndex = 0;
_tailIndex = 0;
for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = flushCharacter;
}
unsigned int getString(T terminator, T* buffer, const unsigned int bufferLength) volatile
{
unsigned int i = 0;
for(; i <= remaining() && i <= BUFFER_SIZE && _buffer[(_tailIndex+i) % BUFFER_SIZE] != terminator; i++);
if( i < remaining() && i > 0)
{
if(i > bufferLength-1) i = bufferLength-1;
read(buffer, i);
buffer[i]='\0';
_tailIndex++;
}
else if(i == 0) _tailIndex++;
else i = 0;
return i;
}
};

View File

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

View File

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

25
uvositem.cpp Normal file
View File

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

16
uvositem.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include "item.h"
#include "W433DataTransmitter.h"
class UvosItem: public Item
{
public:
inline static W433DataTransmitter* transmitter = nullptr;
public:
UvosItem(const uint8_t idIn, char nameIn[]);
UvosItem(const Item& item);
void setValue(const uint8_t value);
void resend();
};

View File

@ -2,25 +2,29 @@
#define WRITEPIN_H
#include <avr/io.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
inline void writePin(volatile unsigned char *port, const unsigned char pin, const bool state) //waste 2 cycles
{
*port &= ~(1 << pin);
if(state) *port |= (1 << pin);
*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);
writePin(reg, bit, value);
}
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));
}
#pragma GCC diagnostic pop
#endif