move to tab indentaion
This commit is contained in:
8
bitrep.h
8
bitrep.h
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
const char *bit_rep[16] =
|
const char *bit_rep[16] =
|
||||||
{
|
{
|
||||||
[ 0] = "0000", [ 1] = "0001", [ 2] = "0010", [ 3] = "0011",
|
[ 0] = "0000", [ 1] = "0001", [ 2] = "0010", [ 3] = "0011",
|
||||||
[ 4] = "0100", [ 5] = "0101", [ 6] = "0110", [ 7] = "0111",
|
[ 4] = "0100", [ 5] = "0101", [ 6] = "0110", [ 7] = "0111",
|
||||||
[ 8] = "1000", [ 9] = "1001", [10] = "1010", [11] = "1011",
|
[ 8] = "1000", [ 9] = "1001", [10] = "1010", [11] = "1011",
|
||||||
[12] = "1100", [13] = "1101", [14] = "1110", [15] = "1111",
|
[12] = "1100", [13] = "1101", [14] = "1110", [15] = "1111",
|
||||||
};
|
};
|
||||||
|
50
eeprom.h
50
eeprom.h
@ -1,40 +1,40 @@
|
|||||||
void EEPROM_write_char(uint16_t address, unsigned char data)
|
void EEPROM_write_char(uint16_t address, unsigned char data)
|
||||||
{
|
{
|
||||||
/* Wait for completion of previous write */
|
/* Wait for completion of previous write */
|
||||||
while(EECR & (1<<EEPE));
|
while(EECR & (1<<EEPE));
|
||||||
/* Set up address and Data Registers */
|
/* Set up address and Data Registers */
|
||||||
EEAR = address;
|
EEAR = address;
|
||||||
EEDR = data;
|
EEDR = data;
|
||||||
/* Write logical one to EEMPE */
|
/* Write logical one to EEMPE */
|
||||||
EECR |= (1<<EEMPE);
|
EECR |= (1<<EEMPE);
|
||||||
/* Start eeprom write by setting EEPE */
|
/* Start eeprom write by setting EEPE */
|
||||||
EECR |= (1<<EEPE);
|
EECR |= (1<<EEPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char EEPROM_read_char(uint16_t uiAddress)
|
unsigned char EEPROM_read_char(uint16_t uiAddress)
|
||||||
{
|
{
|
||||||
/* Wait for completion of previous write */
|
/* Wait for completion of previous write */
|
||||||
while(EECR & (1<<EEPE));
|
while(EECR & (1<<EEPE));
|
||||||
/* Set up address register */
|
/* Set up address register */
|
||||||
EEAR = uiAddress;
|
EEAR = uiAddress;
|
||||||
/* Start eeprom read by writing EERE */
|
/* Start eeprom read by writing EERE */
|
||||||
EECR |= (1<<EERE);
|
EECR |= (1<<EERE);
|
||||||
/* Return data from Data Register */
|
/* Return data from Data Register */
|
||||||
return EEDR;
|
return EEDR;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EEPROM_write_string(uint16_t address, char* buffer, uint16_t length)
|
void EEPROM_write_string(uint16_t address, char* buffer, uint16_t length)
|
||||||
{
|
{
|
||||||
for(uint16_t i = 0; i < length; i++)
|
for(uint16_t i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
EEPROM_write_char( address+i, buffer[i] );
|
EEPROM_write_char( address+i, buffer[i] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EEPROM_read_string(uint16_t address, char* buffer, uint16_t length)
|
void EEPROM_read_string(uint16_t address, char* buffer, uint16_t length)
|
||||||
{
|
{
|
||||||
for(uint16_t i = 0; i < length; i++)
|
for(uint16_t i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
buffer[i] = EEPROM_read_char( address+i);
|
buffer[i] = EEPROM_read_char( address+i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
40
item.h
40
item.h
@ -8,38 +8,38 @@ class Item
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
static constexpr uint8_t HIGH = 2;
|
static constexpr uint8_t HIGH = 2;
|
||||||
static constexpr uint8_t LOW = 1;
|
static constexpr uint8_t LOW = 1;
|
||||||
static constexpr uint8_t OFF = 0;
|
static constexpr uint8_t OFF = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
static constexpr unsigned char _pinHighA = PD5;
|
static constexpr unsigned char _pinHighA = PD5;
|
||||||
static constexpr unsigned char _pinLowA = PD4;
|
static constexpr unsigned char _pinLowA = PD4;
|
||||||
static constexpr unsigned char _pinHighB = PD2;
|
static constexpr unsigned char _pinHighB = PD2;
|
||||||
static constexpr unsigned char _pinLowB = PD3;
|
static constexpr unsigned char _pinLowB = PD3;
|
||||||
static constexpr uint8_t SEND_COUNT = 2;
|
static constexpr uint8_t SEND_COUNT = 2;
|
||||||
|
|
||||||
uint8_t _address;
|
uint8_t _address;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
inline static void off();
|
inline static void off();
|
||||||
static void sendBit(const bool bit);
|
static void sendBit(const bool bit);
|
||||||
static void sendAddress(uint8_t address);
|
static void sendAddress(uint8_t address);
|
||||||
uint16_t packetAddSpeed();
|
uint16_t packetAddSpeed();
|
||||||
uint16_t packetAddDirection();
|
uint16_t packetAddDirection();
|
||||||
uint16_t packetAddFunction(const uint8_t function);
|
uint16_t packetAddFunction(const uint8_t function);
|
||||||
uint16_t assemblePacket();
|
uint16_t assemblePacket();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
static void setOutput(const uint8_t state);
|
static void setOutput(const uint8_t state);
|
||||||
|
|
||||||
Item(const uint8_t address);
|
Item(const uint8_t address);
|
||||||
|
|
||||||
void sendRaw(const uint16_t data);
|
void sendRaw(const uint16_t data);
|
||||||
static void sendRawAddr(const uint8_t address, const uint16_t data);
|
static void sendRawAddr(const uint8_t address, const uint16_t data);
|
||||||
|
|
||||||
void setAddress(const uint8_t address);
|
void setAddress(const uint8_t address);
|
||||||
uint8_t getAddress();
|
uint8_t getAddress();
|
||||||
|
122
serial.cpp
122
serial.cpp
@ -18,111 +18,111 @@ ISR(USART_RX_vect) //I have seen worse interrupt sintax
|
|||||||
|
|
||||||
Serial::Serial()
|
Serial::Serial()
|
||||||
{
|
{
|
||||||
UBRR0H = UBRRH_VALUE;
|
UBRR0H = UBRRH_VALUE;
|
||||||
UBRR0L = UBRRL_VALUE;
|
UBRR0L = UBRRL_VALUE;
|
||||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
||||||
UCSR0B = _BV(RXEN0) | _BV(TXEN0); //Enable RX and TX
|
UCSR0B = _BV(RXEN0) | _BV(TXEN0); //Enable RX and TX
|
||||||
UCSR0B |= (1 << RXCIE0); //Enable Rx interuppt
|
UCSR0B |= (1 << RXCIE0); //Enable Rx interuppt
|
||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Serial::putChar(const char c)
|
void Serial::putChar(const char c)
|
||||||
{
|
{
|
||||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||||
UDR0 = c;
|
UDR0 = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Serial::write(const char* in, const unsigned int length)
|
void Serial::write(const char* in, const unsigned int length)
|
||||||
{
|
{
|
||||||
for(unsigned int i = 0; i < length && in[i] != '\0'; i++)
|
for(unsigned int i = 0; i < length && in[i] != '\0'; i++)
|
||||||
{
|
{
|
||||||
putChar(in[i]);
|
putChar(in[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Serial::write_p(const char in[])
|
void Serial::write_p(const char in[])
|
||||||
{
|
{
|
||||||
cli();
|
cli();
|
||||||
char ch = pgm_read_byte(in);
|
char ch = pgm_read_byte(in);
|
||||||
while (ch != '\0')
|
while (ch != '\0')
|
||||||
{
|
{
|
||||||
putChar(ch);
|
putChar(ch);
|
||||||
in++;
|
in++;
|
||||||
ch = pgm_read_byte(in);
|
ch = pgm_read_byte(in);
|
||||||
}
|
}
|
||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Serial::write(const char in[])
|
void Serial::write(const char in[])
|
||||||
{
|
{
|
||||||
for(unsigned int i = 0; i < strlen(in); i++)
|
for(unsigned int i = 0; i < strlen(in); i++)
|
||||||
{
|
{
|
||||||
putChar(in[i]);
|
putChar(in[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Serial::write(int32_t in)
|
void Serial::write(int32_t in)
|
||||||
{
|
{
|
||||||
if(in == 0)
|
if(in == 0)
|
||||||
{
|
{
|
||||||
putChar('0');
|
putChar('0');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bool flag = false;
|
bool flag = false;
|
||||||
char str[64] = { 0 };
|
char str[64] = { 0 };
|
||||||
int16_t i = 62;
|
int16_t i = 62;
|
||||||
if (in < 0)
|
if (in < 0)
|
||||||
{
|
{
|
||||||
flag = true;
|
flag = true;
|
||||||
in = abs(in);
|
in = abs(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (in != 0 && i > 0)
|
while (in != 0 && i > 0)
|
||||||
{
|
{
|
||||||
str[i--] = (in % 10) + '0';
|
str[i--] = (in % 10) + '0';
|
||||||
in /= 10;
|
in /= 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flag) str[i--] = '-';
|
if (flag) str[i--] = '-';
|
||||||
write(str + i + 1, 64-(i+1));
|
write(str + i + 1, 64-(i+1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Serial::dataIsWaiting()
|
bool Serial::dataIsWaiting()
|
||||||
{
|
{
|
||||||
return !rxBuffer.isEmpty();
|
return !rxBuffer.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
char Serial::getChar()
|
char Serial::getChar()
|
||||||
{
|
{
|
||||||
if(!rxBuffer.isEmpty())
|
if(!rxBuffer.isEmpty())
|
||||||
{
|
{
|
||||||
if(serialFlowControl && stopped && rxBuffer.remainingCapacity() > 32 )
|
if(serialFlowControl && stopped && rxBuffer.remainingCapacity() > 32 )
|
||||||
{
|
{
|
||||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||||
UDR0 = 0x11;
|
UDR0 = 0x11;
|
||||||
stopped = false;
|
stopped = false;
|
||||||
}
|
}
|
||||||
return rxBuffer.read();
|
return rxBuffer.read();
|
||||||
}
|
}
|
||||||
else return '\0';
|
else return '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Serial::getString(char* buffer, const int bufferLength)
|
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 )
|
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()
|
void Serial::flush()
|
||||||
{
|
{
|
||||||
rxBuffer.flush();
|
rxBuffer.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Serial::setTerminator(char terminator){_terminator = terminator;}
|
void Serial::setTerminator(char terminator){_terminator = terminator;}
|
||||||
|
26
serial.h
26
serial.h
@ -16,21 +16,21 @@ const bool serialFlowControl = false;
|
|||||||
class Serial
|
class Serial
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
char _terminator = '\n';
|
char _terminator = '\n';
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Serial();
|
Serial();
|
||||||
void putChar(const char c);
|
void putChar(const char c);
|
||||||
void write(const char* in, const unsigned int length);
|
void write(const char* in, const unsigned int length);
|
||||||
void write(const char in[]);
|
void write(const char in[]);
|
||||||
void write_p(const char in[]); //for flash space strigns
|
void write_p(const char in[]); //for flash space strigns
|
||||||
void write(const int32_t in);
|
void write(const int32_t in);
|
||||||
unsigned int read( char* buffer, const unsigned int length );
|
unsigned int read( char* buffer, const unsigned int length );
|
||||||
bool dataIsWaiting();
|
bool dataIsWaiting();
|
||||||
char getChar();
|
char getChar();
|
||||||
unsigned int getString(char* buffer, const int bufferLength);
|
unsigned int getString(char* buffer, const int bufferLength);
|
||||||
void flush();
|
void flush();
|
||||||
void setTerminator(const char terminator);
|
void setTerminator(const char terminator);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
122
staticvector.h
122
staticvector.h
@ -4,7 +4,7 @@
|
|||||||
template<typename T, size_t size> class SVector
|
template<typename T, size_t size> class SVector
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
size_t stored = 0;
|
size_t stored = 0;
|
||||||
|
|
||||||
|
|
||||||
char buff[sizeof(T)*size];
|
char buff[sizeof(T)*size];
|
||||||
@ -12,77 +12,77 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
T* data()
|
T* data()
|
||||||
{
|
{
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
T& operator[](size_t i)
|
T& operator[](size_t i)
|
||||||
{
|
{
|
||||||
return array[i];
|
return array[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
T& at(size_t i)
|
T& at(size_t i)
|
||||||
{
|
{
|
||||||
return array[i];
|
return array[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
T& front()
|
T& front()
|
||||||
{
|
{
|
||||||
return array[0];
|
return array[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
T& back()
|
T& back()
|
||||||
{
|
{
|
||||||
return array[stored-1];
|
return array[stored-1];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool empty() const
|
bool empty() const
|
||||||
{
|
{
|
||||||
return stored == 0 ? true : false;
|
return stored == 0 ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t count() const
|
size_t count() const
|
||||||
{
|
{
|
||||||
return stored;
|
return stored;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr size_t maxSize() const
|
constexpr size_t maxSize() const
|
||||||
{
|
{
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t remainingCapacity() const
|
size_t remainingCapacity() const
|
||||||
{
|
{
|
||||||
return size - stored;
|
return size - stored;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool push_back(const T in)
|
bool push_back(const T in)
|
||||||
{
|
{
|
||||||
if( remainingCapacity() != 0)
|
if( remainingCapacity() != 0)
|
||||||
{
|
{
|
||||||
array[stored] = in;
|
array[stored] = in;
|
||||||
++stored;
|
++stored;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool erase(size_t position)
|
bool erase(size_t position)
|
||||||
{
|
{
|
||||||
if(position > stored)
|
if(position > stored)
|
||||||
return false;
|
return false;
|
||||||
array[position].~T();
|
array[position].~T();
|
||||||
--stored;
|
--stored;
|
||||||
for( size_t i = position; i < stored; i++ )
|
for( size_t i = position; i < stored; i++ )
|
||||||
memcpy(&array[i], &array[i+1], sizeof(T));
|
memcpy(&array[i], &array[i+1], sizeof(T));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
for( size_t i = 0; i < stored; i++ )
|
for( size_t i = 0; i < stored; i++ )
|
||||||
array[i].~T();
|
array[i].~T();
|
||||||
stored = 0;
|
stored = 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
138
train.cpp
138
train.cpp
@ -7,98 +7,98 @@ Train::Train(const uint8_t address, uint8_t functionmask): Item(address), _func
|
|||||||
|
|
||||||
uint16_t Train::packetAddSpeed()
|
uint16_t Train::packetAddSpeed()
|
||||||
{
|
{
|
||||||
uint16_t packet = 0;
|
uint16_t packet = 0;
|
||||||
if(_speed > 14)
|
if(_speed > 14)
|
||||||
_speed = 14;
|
_speed = 14;
|
||||||
for(uint8_t i = 0; i < 4; i++)
|
for(uint8_t i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
packet |= ((bool)((_speed+1) & (1 << i))) << (i*2+2);
|
packet |= ((bool)((_speed+1) & (1 << i))) << (i*2+2);
|
||||||
}
|
}
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Train::packetAddDirection()
|
uint16_t Train::packetAddDirection()
|
||||||
{
|
{
|
||||||
uint16_t packet = 0;
|
uint16_t packet = 0;
|
||||||
|
|
||||||
uint8_t data = 0;
|
uint8_t data = 0;
|
||||||
if(!_direction)
|
if(!_direction)
|
||||||
{
|
{
|
||||||
if(_speed > 6)
|
if(_speed > 6)
|
||||||
data = 0b0100;
|
data = 0b0100;
|
||||||
else if(_speed > 0)
|
else if(_speed > 0)
|
||||||
data = 0b0101;
|
data = 0b0101;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(_speed > 0)
|
if(_speed > 0)
|
||||||
data = 0b1011;
|
data = 0b1011;
|
||||||
else if (_speed > 6)
|
else if (_speed > 6)
|
||||||
data = 0b1010;
|
data = 0b1010;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_speed == 0)
|
if(_speed == 0)
|
||||||
data = !_direction ? 0b0101 : 0b1011;
|
data = !_direction ? 0b0101 : 0b1011;
|
||||||
|
|
||||||
for(uint8_t i = 0; i < 4; i++)
|
for(uint8_t i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
uint8_t bit = (data & (1 << (3-i))) >> (3-i);
|
uint8_t bit = (data & (1 << (3-i))) >> (3-i);
|
||||||
packet |= bit << ((i*2)+3);
|
packet |= bit << ((i*2)+3);
|
||||||
}
|
}
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Train::packetAddFunction(const uint8_t function)
|
uint16_t Train::packetAddFunction(const uint8_t function)
|
||||||
{
|
{
|
||||||
uint16_t packet = 0;
|
uint16_t packet = 0;
|
||||||
bool enabled = _function & (1 << function);
|
bool enabled = _function & (1 << function);
|
||||||
if(function == 0)
|
if(function == 0)
|
||||||
{
|
{
|
||||||
packet |= enabled ? 0b00000011 : 0b00000000;
|
packet |= enabled ? 0b00000011 : 0b00000000;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(enabled)
|
if(enabled)
|
||||||
packet |= 0b1000000000;
|
packet |= 0b1000000000;
|
||||||
for(uint8_t i = 0; i < 4; ++i )
|
for(uint8_t i = 0; i < 4; ++i )
|
||||||
{
|
{
|
||||||
if(function > i)
|
if(function > i)
|
||||||
packet = packet | (1 << (7-i*2));
|
packet = packet | (1 << (7-i*2));
|
||||||
else
|
else
|
||||||
packet = packet & ~(1 << (7-i*2));
|
packet = packet & ~(1 << (7-i*2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Train::setSpeed(uint8_t speed)
|
void Train::setSpeed(uint8_t speed)
|
||||||
{
|
{
|
||||||
_speed = speed;
|
_speed = speed;
|
||||||
sendData();
|
sendData();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Train::getSpeed()
|
uint8_t Train::getSpeed()
|
||||||
{
|
{
|
||||||
return _speed;
|
return _speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Train::stop()
|
void Train::stop()
|
||||||
{
|
{
|
||||||
_speed = 0;
|
_speed = 0;
|
||||||
_function = 0;
|
_function = 0;
|
||||||
sendData();
|
sendData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint16_t Train::assembleSpeedPacket()
|
uint16_t Train::assembleSpeedPacket()
|
||||||
{
|
{
|
||||||
uint16_t packet = packetAddSpeed() | packetAddFunction(0) | packetAddDirection();
|
uint16_t packet = packetAddSpeed() | packetAddFunction(0) | packetAddDirection();
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Train::sendData()
|
void Train::sendData()
|
||||||
{
|
{
|
||||||
sendRaw(assembleSpeedPacket());
|
sendRaw(assembleSpeedPacket());
|
||||||
if(_functionmask)
|
if(_functionmask)
|
||||||
{
|
{
|
||||||
uint8_t functionToResend = (_function & 0xF0) >> 4;
|
uint8_t functionToResend = (_function & 0xF0) >> 4;
|
||||||
@ -117,30 +117,30 @@ void Train::sendData()
|
|||||||
|
|
||||||
uint16_t Train::getLastPacket()
|
uint16_t Train::getLastPacket()
|
||||||
{
|
{
|
||||||
return assembleSpeedPacket();
|
return assembleSpeedPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Train::reverse()
|
void Train::reverse()
|
||||||
{
|
{
|
||||||
_direction = !_direction;
|
_direction = !_direction;
|
||||||
sendData();
|
sendData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint8_t Train::getFunctions()
|
uint8_t Train::getFunctions()
|
||||||
{
|
{
|
||||||
return _function & 0x0F;
|
return _function & 0x0F;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Train::getFunctionMask()
|
uint8_t Train::getFunctionMask()
|
||||||
{
|
{
|
||||||
return _functionmask;
|
return _functionmask;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Train::sendFunction(const uint8_t function, bool enable)
|
void Train::sendFunction(const uint8_t function, bool enable)
|
||||||
{
|
{
|
||||||
if(function > 3)
|
if(function > 3)
|
||||||
return;
|
return;
|
||||||
_function = (_function & ~(1 << function)) | (enable << function);
|
_function = (_function & ~(1 << function)) | (enable << function);
|
||||||
sendData();
|
sendData();
|
||||||
}
|
}
|
||||||
|
38
train.h
38
train.h
@ -6,38 +6,38 @@ class Train: public Item
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
uint8_t _function = 0;
|
uint8_t _function = 0;
|
||||||
uint8_t _functionmask;
|
uint8_t _functionmask;
|
||||||
uint8_t _speed = 0;
|
uint8_t _speed = 0;
|
||||||
bool _direction = false;
|
bool _direction = false;
|
||||||
|
|
||||||
uint16_t packetAddSpeed();
|
uint16_t packetAddSpeed();
|
||||||
uint16_t packetAddDirection();
|
uint16_t packetAddDirection();
|
||||||
uint16_t packetAddFunction(const uint8_t function);
|
uint16_t packetAddFunction(const uint8_t function);
|
||||||
uint16_t assembleSpeedPacket();
|
uint16_t assembleSpeedPacket();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Train(const uint8_t address, uint8_t functionmask = 0b0000);
|
Train(const uint8_t address, uint8_t functionmask = 0b0000);
|
||||||
|
|
||||||
void sendData();
|
void sendData();
|
||||||
|
|
||||||
void reverse();
|
void reverse();
|
||||||
|
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
bool isActive() {return getSpeed() || getFunctions();}
|
bool isActive() {return getSpeed() || getFunctions();}
|
||||||
|
|
||||||
uint16_t getLastPacket();
|
uint16_t getLastPacket();
|
||||||
|
|
||||||
void setSpeed(uint8_t speed);
|
void setSpeed(uint8_t speed);
|
||||||
|
|
||||||
uint8_t getSpeed();
|
uint8_t getSpeed();
|
||||||
|
|
||||||
void setFunctionMask(uint8_t functionmask) {_functionmask = functionmask;}
|
void setFunctionMask(uint8_t functionmask) {_functionmask = functionmask;}
|
||||||
|
|
||||||
uint8_t getFunctions();
|
uint8_t getFunctions();
|
||||||
|
|
||||||
uint8_t getFunctionMask();
|
uint8_t getFunctionMask();
|
||||||
|
|
||||||
void sendFunction(const uint8_t function, bool enable = true);
|
void sendFunction(const uint8_t function, bool enable = true);
|
||||||
};
|
};
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
inline void writePin(volatile unsigned char *port, const unsigned char pin, const bool state) //waste 2 cycles
|
inline void writePin(volatile unsigned char *port, const unsigned char pin, const bool state) //waste 2 cycles
|
||||||
{
|
{
|
||||||
*port &= ~(1 << pin);
|
*port &= ~(1 << pin);
|
||||||
if(state) *port |= (1 << pin);
|
if(state) *port |= (1 << pin);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline 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));}
|
||||||
|
Reference in New Issue
Block a user