improved serial handeling added aux state info

This commit is contained in:
IMback
2018-09-03 20:47:01 +02:00
parent 296d38e8b0
commit 20b4663887
9 changed files with 676 additions and 659 deletions

View File

@ -18,82 +18,80 @@
AR_AVRDUDE:FILEPATH=/usr/bin/avrdude
//Path to a program.
CMAKE_AR:FILEPATH=/usr/bin/ar
CMAKE_AR:FILEPATH=/bin/ar
//Choose the type of build, options are: None(CMAKE_CXX_FLAGS or
// CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.
//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=/usr/bin/c++
CMAKE_CXX_COMPILER:FILEPATH=/bin/c++
//A wrapper around 'ar' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_CXX_COMPILER_AR:FILEPATH=/usr/bin/gcc-ar
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=/usr/bin/gcc-ranlib
CMAKE_CXX_COMPILER_RANLIB:FILEPATH=/bin/gcc-ranlib
//Flags used by the compiler during all build types.
//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=
//Flags used by the compiler during debug builds.
//Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
//Flags used by the compiler during release builds for minimum
// size.
//Flags used by the CXX compiler during MINSIZEREL builds.
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
//Flags used by the compiler during release builds.
//Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
//Flags used by the compiler during release builds with debug info.
//Flags used by the CXX compiler during RELWITHDEBINFO builds.
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
//C compiler
CMAKE_C_COMPILER:FILEPATH=/usr/bin/cc
CMAKE_C_COMPILER:FILEPATH=/bin/cc
//A wrapper around 'ar' adding the appropriate '--plugin' option
// for the GCC compiler
CMAKE_C_COMPILER_AR:FILEPATH=/usr/bin/gcc-ar
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=/usr/bin/gcc-ranlib
CMAKE_C_COMPILER_RANLIB:FILEPATH=/bin/gcc-ranlib
//Flags used by the compiler during all build types.
//Flags used by the C compiler during all build types.
CMAKE_C_FLAGS:STRING=
//Flags used by the compiler during debug builds.
//Flags used by the C compiler during DEBUG builds.
CMAKE_C_FLAGS_DEBUG:STRING=-g
//Flags used by the compiler during release builds for minimum
// size.
//Flags used by the C compiler during MINSIZEREL builds.
CMAKE_C_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
//Flags used by the compiler during release builds.
//Flags used by the C compiler during RELEASE builds.
CMAKE_C_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
//Flags used by the compiler during release builds with debug info.
//Flags used by the C compiler during RELWITHDEBINFO builds.
CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
//Flags used by the linker.
//Flags used by the linker during all build types.
CMAKE_EXE_LINKER_FLAGS:STRING=
//Flags used by the linker during debug builds.
//Flags used by the linker during DEBUG builds.
CMAKE_EXE_LINKER_FLAGS_DEBUG:STRING=
//Flags used by the linker during release minsize builds.
//Flags used by the linker during MINSIZEREL builds.
CMAKE_EXE_LINKER_FLAGS_MINSIZEREL:STRING=
//Flags used by the linker during release builds.
//Flags used by the linker during RELEASE builds.
CMAKE_EXE_LINKER_FLAGS_RELEASE:STRING=
//Flags used by the linker during Release with Debug Info builds.
//Flags used by the linker during RELWITHDEBINFO builds.
CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO:STRING=
//Enable/Disable output of compile commands during generation.
@ -103,54 +101,64 @@ CMAKE_EXPORT_COMPILE_COMMANDS:BOOL=OFF
CMAKE_INSTALL_PREFIX:PATH=/usr/local
//Path to a program.
CMAKE_LINKER:FILEPATH=/usr/bin/ld
CMAKE_LINKER:FILEPATH=/bin/ld
//Path to a program.
CMAKE_MAKE_PROGRAM:FILEPATH=/usr/bin/make
CMAKE_MAKE_PROGRAM:FILEPATH=/bin/make
//Flags used by the linker during the creation of modules.
//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 debug builds.
//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 release minsize builds.
//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 release builds.
//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 Release with Debug Info builds.
//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=/usr/bin/nm
CMAKE_NM:FILEPATH=/bin/nm
//Path to a program.
CMAKE_OBJCOPY:FILEPATH=/usr/bin/objcopy
CMAKE_OBJCOPY:FILEPATH=/bin/objcopy
//Path to a program.
CMAKE_OBJDUMP:FILEPATH=/usr/bin/objdump
CMAKE_OBJDUMP:FILEPATH=/bin/objdump
//Value Computed by CMake
CMAKE_PROJECT_NAME:STATIC=rgbcontroller
//Path to a program.
CMAKE_RANLIB:FILEPATH=/usr/bin/ranlib
CMAKE_RANLIB:FILEPATH=/bin/ranlib
//Flags used by the linker during the creation of dll's.
//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 debug builds.
//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 release minsize builds.
//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 release builds.
//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 Release with Debug Info builds.
//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,
@ -160,23 +168,28 @@ 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.
//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 debug builds.
//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 release minsize builds.
//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 release builds.
//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 Release with Debug Info builds.
//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=/usr/bin/strip
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
@ -349,7 +362,7 @@ CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO-ADVANCED:INTERNAL=1
//ADVANCED property for variable: CMAKE_STRIP
CMAKE_STRIP-ADVANCED:INTERNAL=1
//uname command
CMAKE_UNAME:INTERNAL=/usr/bin/uname
CMAKE_UNAME:INTERNAL=/bin/uname
//ADVANCED property for variable: CMAKE_VERBOSE_MAKEFILE
CMAKE_VERBOSE_MAKEFILE-ADVANCED:INTERNAL=1

View File

@ -19,6 +19,7 @@ private:
public:
WirelessRelay(const uint16_t id);
WirelessRelay();
void setTimeout(uint64_t timeout);
void init(const uint16_t id);
void on();
void off();

View File

@ -9,7 +9,7 @@
#include "rgbled.h"
#include "eeprom.h"
#define MAX_RELAYS 32
#define MAX_RELAYS 16
#define COMMAND_BUFFER_SIZE 32
#define SNPRINTF_BUFFER_SIZE 64
@ -35,7 +35,7 @@ inline static void printHelp(Serial* serial)
relay list : Print list of saved Wireless Relays.\n\
relay machine [on/off] : Get machine readable relay state when state changes.\n\
relay [on/off] [nn] : Turn on/off nth relay.\n\
relay state : Get machine readable relay state.\n\
state : Get machine readable state.\n\
erase : Erase epprom.\n\
rgb [on/off] : Turn on/off RGB leds at current value.\n\
rgb print : Print current RGB value.\n\
@ -50,7 +50,7 @@ inline static void printHelp(Serial* serial)
void writeState( WirelessRelayStore* relays, Pwm16b* auxPwm, Serial* serial )
{
serial->write("ST");
serial->write(auxPwm.isOn() ? auxPwm.getValueA() : 0);
serial->write(auxPwm->isOn() ? auxPwm->getValueA() >> 8 : 0);
serial->putChar(',');
serial->write(relays->count);
for(uint8_t i = 0; i < relays->count; i++)
@ -61,7 +61,7 @@ void writeState( WirelessRelayStore* relays, Pwm16b* auxPwm, Serial* serial )
serial->putChar('\n');
}
void relayDispatch(WirelessRelayStore* relays, char* inBuffer, Serial* serial)
void relayDispatch(WirelessRelayStore* relays, Pwm16b* auxPwm, char* inBuffer, Serial* serial)
{
if( strncmp(inBuffer, "add", 3) == 0 && inBuffer[3] != '\0')
{
@ -106,7 +106,7 @@ void relayDispatch(WirelessRelayStore* relays, char* inBuffer, Serial* serial)
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "RELAY: %u turned on\n", selected);
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
if(relays->printState) writeState(relays, serial);
if(relays->printState) writeState(relays, auxPwm, serial);
}
else serial->write("No sutch Relay\n");
}
@ -119,7 +119,7 @@ void relayDispatch(WirelessRelayStore* relays, char* inBuffer, Serial* serial)
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "RELAY: %u turned off\n", selected);
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
if(relays->printState) writeState(relays, serial);
if(relays->printState) writeState(relays, auxPwm, serial);
}
else serial->write("No sutch Relay\n");
}
@ -240,7 +240,7 @@ void serialDispatch(Serial* serial , WirelessRelayStore* relays, RgbLed* rgbled,
{
if(length > 4 && strncmp(buffer, "relay", 5) == 0)
{
relayDispatch(relays, buffer+6, serial);
relayDispatch(relays, auxPwm, buffer+6, serial);
}
else if(length > 2 && strncmp(buffer, "rgb", 3) == 0)
{
@ -348,7 +348,7 @@ int main()
if(doorTow) openCount++;
if(openCount > 60000)
{
serial.write("Door Open Warning\n");
serial.write_p(PSTR("Door Open Warning\n"));
openCount = 0;
}

View File

@ -25,7 +25,7 @@ Pwm16b::Pwm16b( volatile unsigned char *timerControlRegisterA, volatile unsigned
bool Pwm16b::isOn()
{
return (*_timerControlRegisterA != (1<<WGM11));
return *_timerControlRegisterA != (1<<WGM11);
}
void Pwm16b::off()
@ -94,7 +94,7 @@ Pwm8b::Pwm8b( volatile unsigned char *timerControlRegisterA, volatile unsigned c
bool Pwm8b::isOn()
{
return (*_timerControlRegisterA & 0x11111100) != 0);
return (*_timerControlRegisterA & 0x11111100) != 0;
}
void Pwm8b::off()

3
pwm.h
View File

@ -17,7 +17,8 @@ public:
~Pwm16b();
void setDutyA(const uint16_t duty);
void setDutyB(const uint16_t duty);
uint16_t getValue();
uint16_t getValueA();
uint16_t getValueB();
bool isOn();
void off();
void on();

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,19 @@
#include "serial.h"
#include "ringbuffer.h"
char rxBuffer[BUFFER_SIZE];
volatile uint16_t interruptIndex = 0;
RingBuffer<SERIAL_BUFFER_SIZE> rxBuffer;
bool stopped = false;
ISR (USART_RX_vect) //I have seen worse interrupt sintax
{
rxBuffer[interruptIndex % BUFFER_SIZE] = UDR0;
interruptIndex++;
rxBuffer.write(UDR0);
if(serialFlowControl && !stopped && rxBuffer.isOverun(32))
{
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = 0x13;
stopped = true;
}
}
Serial::Serial()
@ -32,6 +39,17 @@ void Serial::write(const char* in, const unsigned int length)
}
}
void Serial::write_p(const char in[])
{
char ch = pgm_read_byte(in);
while (ch != '\0')
{
putChar(ch);
in++;
ch = pgm_read_byte(in);
}
}
void Serial::write(const char in[])
{
for(unsigned int i = 0; i < strlen(in); i++)
@ -40,14 +58,6 @@ void Serial::write(const char in[])
}
}
void Serial::write_p(const char *in)
{
while (pgm_read_byte(in) != '\0')
{
putChar(pgm_read_byte(in++));
}
}
void Serial::write(int32_t in)
{
@ -77,54 +87,39 @@ void Serial::write(int32_t in)
}
}
bool Serial::dataIsWaiting()
{
return (interruptIndex > _rxIndex);
return !rxBuffer.isEmpty();
}
char Serial::getChar()
{
if( _rxIndex >= (32768) - 2*BUFFER_SIZE ) flush(); //may explode only occasionaly
if(dataIsWaiting())
if(!rxBuffer.isEmpty())
{
_rxIndex++;
return rxBuffer[(_rxIndex -1) % BUFFER_SIZE];
if(serialFlowControl && stopped && !rxBuffer.isOverun(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)
{
int i = 0;
for(; i <= (interruptIndex-_rxIndex) && i <= BUFFER_SIZE && rxBuffer[(_rxIndex+i) % BUFFER_SIZE] != _terminator; i++);
return rxBuffer.getString(_terminator, buffer, bufferLength);
}
if( i < (interruptIndex-_rxIndex) && i > 0)
{
int j = 0;
for(; j < i && j < bufferLength-1 ; j++)
{
buffer[j] = getChar();
}
buffer[j+1]='\0';
_rxIndex++;
}
else
{
i = 0;
if( _rxIndex >= (32768) - 2*BUFFER_SIZE ) flush();
}
if (rxBuffer[(_rxIndex+i) % BUFFER_SIZE] == _terminator) _rxIndex++;
return i;
unsigned int Serial::read(char* buffer, const unsigned int length )
{
return rxBuffer.read((uint8_t*)buffer, length);
}
void Serial::flush()
{
_rxIndex = 0;
interruptIndex = 0;
for(int i = 0; i < BUFFER_SIZE; i++) rxBuffer[i] = ' ';
rxBuffer.flush();
}
void Serial::setTerminator(char terminator){_terminator = terminator;}

View File

@ -2,7 +2,7 @@
#define SERIAL_H
#define BAUD 38400
#define BUFFER_SIZE 128
#define SERIAL_BUFFER_SIZE 128
#include <util/setbaud.h>
#include <avr/io.h>
@ -11,12 +11,12 @@
#include <stdlib.h>
#include <avr/pgmspace.h>
const bool serialFlowControl = false;
class Serial
{
private:
char _terminator = '\n';
uint16_t _rxIndex=0;
public:
Serial();
@ -25,6 +25,7 @@ public:
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);