From 074c832b3ab9321f2d6085b75eb1bcd9140a7b22 Mon Sep 17 00:00:00 2001 From: uvos Date: Mon, 31 Jan 2022 21:13:27 +0100 Subject: [PATCH] move to tab indentaion --- bitrep.h | 8 +-- eeprom.h | 50 ++++++++-------- item.h | 56 +++++++++--------- serial.cpp | 122 +++++++++++++++++++-------------------- serial.h | 28 ++++----- staticvector.h | 152 ++++++++++++++++++++++++------------------------- train.cpp | 140 ++++++++++++++++++++++----------------------- train.h | 62 ++++++++++---------- writepin.h | 4 +- 9 files changed, 311 insertions(+), 311 deletions(-) diff --git a/bitrep.h b/bitrep.h index 8cf1d25..92fe648 100644 --- a/bitrep.h +++ b/bitrep.h @@ -2,8 +2,8 @@ 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", }; diff --git a/eeprom.h b/eeprom.h index 46e0772..f9d2651 100644 --- a/eeprom.h +++ b/eeprom.h @@ -1,40 +1,40 @@ void EEPROM_write_char(uint16_t address, unsigned char data) { - /* Wait for completion of previous write */ - while(EECR & (1< 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;} diff --git a/serial.h b/serial.h index af85802..46cab6f 100644 --- a/serial.h +++ b/serial.h @@ -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 diff --git a/staticvector.h b/staticvector.h index 2747528..217817d 100644 --- a/staticvector.h +++ b/staticvector.h @@ -4,85 +4,85 @@ template class SVector { private: - size_t stored = 0; - - + size_t stored = 0; + + char buff[sizeof(T)*size]; T *array = (T*)buff; - + 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; + } }; diff --git a/train.cpp b/train.cpp index fe9b3cf..2e81525 100644 --- a/train.cpp +++ b/train.cpp @@ -7,98 +7,98 @@ Train::Train(const uint8_t address, uint8_t functionmask): Item(address), _func uint16_t Train::packetAddSpeed() { - uint16_t packet = 0; - if(_speed > 14) - _speed = 14; - for(uint8_t i = 0; i < 4; i++) - { - packet |= ((bool)((_speed+1) & (1 << i))) << (i*2+2); - } - return packet; + uint16_t packet = 0; + if(_speed > 14) + _speed = 14; + for(uint8_t i = 0; i < 4; i++) + { + packet |= ((bool)((_speed+1) & (1 << i))) << (i*2+2); + } + return packet; } uint16_t Train::packetAddDirection() { - uint16_t packet = 0; + uint16_t packet = 0; - uint8_t data = 0; - if(!_direction) - { - if(_speed > 6) - data = 0b0100; - else if(_speed > 0) - data = 0b0101; - } - else - { - if(_speed > 0) - data = 0b1011; - else if (_speed > 6) - data = 0b1010; - } + uint8_t data = 0; + if(!_direction) + { + if(_speed > 6) + data = 0b0100; + else if(_speed > 0) + data = 0b0101; + } + else + { + if(_speed > 0) + data = 0b1011; + else if (_speed > 6) + data = 0b1010; + } - if(_speed == 0) - data = !_direction ? 0b0101 : 0b1011; + if(_speed == 0) + data = !_direction ? 0b0101 : 0b1011; - for(uint8_t i = 0; i < 4; i++) - { - uint8_t bit = (data & (1 << (3-i))) >> (3-i); - packet |= bit << ((i*2)+3); - } - return packet; + for(uint8_t i = 0; i < 4; i++) + { + uint8_t bit = (data & (1 << (3-i))) >> (3-i); + packet |= bit << ((i*2)+3); + } + return packet; } uint16_t Train::packetAddFunction(const uint8_t function) { - uint16_t packet = 0; - bool enabled = _function & (1 << function); - if(function == 0) - { - packet |= enabled ? 0b00000011 : 0b00000000; - } - else - { - if(enabled) - packet |= 0b1000000000; - for(uint8_t i = 0; i < 4; ++i ) - { - if(function > i) - packet = packet | (1 << (7-i*2)); - else - packet = packet & ~(1 << (7-i*2)); - } - } - return packet; + uint16_t packet = 0; + bool enabled = _function & (1 << function); + if(function == 0) + { + packet |= enabled ? 0b00000011 : 0b00000000; + } + else + { + if(enabled) + packet |= 0b1000000000; + for(uint8_t i = 0; i < 4; ++i ) + { + if(function > i) + packet = packet | (1 << (7-i*2)); + else + packet = packet & ~(1 << (7-i*2)); + } + } + return packet; } void Train::setSpeed(uint8_t speed) { - _speed = speed; - sendData(); + _speed = speed; + sendData(); } uint8_t Train::getSpeed() { - return _speed; + return _speed; } void Train::stop() { - _speed = 0; - _function = 0; - sendData(); + _speed = 0; + _function = 0; + sendData(); } uint16_t Train::assembleSpeedPacket() { - uint16_t packet = packetAddSpeed() | packetAddFunction(0) | packetAddDirection(); - return packet; + uint16_t packet = packetAddSpeed() | packetAddFunction(0) | packetAddDirection(); + return packet; } void Train::sendData() { - sendRaw(assembleSpeedPacket()); + sendRaw(assembleSpeedPacket()); if(_functionmask) { uint8_t functionToResend = (_function & 0xF0) >> 4; @@ -117,30 +117,30 @@ void Train::sendData() uint16_t Train::getLastPacket() { - return assembleSpeedPacket(); + return assembleSpeedPacket(); } void Train::reverse() { - _direction = !_direction; - sendData(); + _direction = !_direction; + sendData(); } uint8_t Train::getFunctions() { - return _function & 0x0F; + return _function & 0x0F; } - + uint8_t Train::getFunctionMask() { - return _functionmask; + return _functionmask; } void Train::sendFunction(const uint8_t function, bool enable) { - if(function > 3) - return; - _function = (_function & ~(1 << function)) | (enable << function); - sendData(); + if(function > 3) + return; + _function = (_function & ~(1 << function)) | (enable << function); + sendData(); } diff --git a/train.h b/train.h index 24e536e..e6e4303 100644 --- a/train.h +++ b/train.h @@ -6,38 +6,38 @@ class Train: public Item { private: uint8_t _function = 0; - uint8_t _functionmask; - uint8_t _speed = 0; - bool _direction = false; + uint8_t _functionmask; + uint8_t _speed = 0; + bool _direction = false; - uint16_t packetAddSpeed(); - uint16_t packetAddDirection(); - uint16_t packetAddFunction(const uint8_t function); - uint16_t assembleSpeedPacket(); - + uint16_t packetAddSpeed(); + uint16_t packetAddDirection(); + uint16_t packetAddFunction(const uint8_t function); + uint16_t assembleSpeedPacket(); + public: - - Train(const uint8_t address, uint8_t functionmask = 0b0000); + + Train(const uint8_t address, uint8_t functionmask = 0b0000); - void sendData(); - - void reverse(); - - void stop(); - - bool isActive() {return getSpeed() || getFunctions();} - - uint16_t getLastPacket(); - - void setSpeed(uint8_t speed); - - uint8_t getSpeed(); - - void setFunctionMask(uint8_t functionmask) {_functionmask = functionmask;} - - uint8_t getFunctions(); - - uint8_t getFunctionMask(); - - void sendFunction(const uint8_t function, bool enable = true); + void sendData(); + + void reverse(); + + void stop(); + + bool isActive() {return getSpeed() || getFunctions();} + + uint16_t getLastPacket(); + + void setSpeed(uint8_t speed); + + uint8_t getSpeed(); + + void setFunctionMask(uint8_t functionmask) {_functionmask = functionmask;} + + uint8_t getFunctions(); + + uint8_t getFunctionMask(); + + void sendFunction(const uint8_t function, bool enable = true); }; diff --git a/writepin.h b/writepin.h index 4ef214f..d379695 100644 --- a/writepin.h +++ b/writepin.h @@ -4,8 +4,8 @@ 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 bool readPin( volatile unsigned char *inPort, const unsigned char pin){ return (bool) (*inPort & (1 << pin));}