move to tab indentaion

This commit is contained in:
2022-01-31 21:13:27 +01:00
parent 8f5431fbc3
commit 074c832b3a
9 changed files with 311 additions and 311 deletions

View File

@ -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",
};

View File

@ -1,40 +1,40 @@
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);
}
}

56
item.h
View File

@ -7,41 +7,41 @@
class Item
{
public:
static constexpr uint8_t HIGH = 2;
static constexpr uint8_t LOW = 1;
static constexpr uint8_t OFF = 0;
static constexpr uint8_t HIGH = 2;
static constexpr uint8_t LOW = 1;
static constexpr uint8_t OFF = 0;
private:
static constexpr unsigned char _pinHighA = PD5;
static constexpr unsigned char _pinLowA = PD4;
static constexpr unsigned char _pinHighB = PD2;
static constexpr unsigned char _pinLowB = PD3;
static constexpr uint8_t SEND_COUNT = 2;
static constexpr unsigned char _pinHighA = PD5;
static constexpr unsigned char _pinLowA = PD4;
static constexpr unsigned char _pinHighB = PD2;
static constexpr unsigned char _pinLowB = PD3;
static constexpr uint8_t SEND_COUNT = 2;
uint8_t _address;
uint8_t _address;
protected:
inline static void off();
static void sendBit(const bool bit);
static void sendAddress(uint8_t address);
uint16_t packetAddSpeed();
uint16_t packetAddDirection();
uint16_t packetAddFunction(const uint8_t function);
uint16_t assemblePacket();
inline static void off();
static void sendBit(const bool bit);
static void sendAddress(uint8_t address);
uint16_t packetAddSpeed();
uint16_t packetAddDirection();
uint16_t packetAddFunction(const uint8_t function);
uint16_t assemblePacket();
public:
static void setOutput(const uint8_t state);
Item(const uint8_t address);
void sendRaw(const uint16_t data);
static void sendRawAddr(const uint8_t address, const uint16_t data);
static void setOutput(const uint8_t state);
Item(const uint8_t address);
void sendRaw(const uint16_t data);
static void sendRawAddr(const uint8_t address, const uint16_t data);
void setAddress(const uint8_t address);
uint8_t getAddress();
};

View File

@ -18,111 +18,111 @@ ISR(USART_RX_vect) //I have seen worse interrupt sintax
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;}

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,85 +4,85 @@
template<typename T, size_t size> 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;
}
};

140
train.cpp
View File

@ -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();
}

62
train.h
View File

@ -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);
};

View File

@ -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));}