improve it
This commit is contained in:
15
main.cpp
15
main.cpp
@ -8,6 +8,7 @@
|
|||||||
#include "train.h"
|
#include "train.h"
|
||||||
#include "eeprom.h"
|
#include "eeprom.h"
|
||||||
#include "bitrep.h"
|
#include "bitrep.h"
|
||||||
|
#include "ringbuffer.h"
|
||||||
|
|
||||||
#define MAX_TRAINS 16
|
#define MAX_TRAINS 16
|
||||||
#define COMMAND_BUFFER_SIZE 64
|
#define COMMAND_BUFFER_SIZE 64
|
||||||
@ -126,7 +127,7 @@ void trainDispatch(char* inBuffer, Serial* serial)
|
|||||||
serial->write_p(PSTR("Trains:\n"));
|
serial->write_p(PSTR("Trains:\n"));
|
||||||
for(uint8_t i = 0; i < storedTrains; i++)
|
for(uint8_t i = 0; i < storedTrains; i++)
|
||||||
{
|
{
|
||||||
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "NUMBER: %u ID: %s%s CURRENT PACKET: %x", i, bit_rep[trains[i].getAddress() >> 4], bit_rep[trains[i].getAddress() & 0x0F], trains[i].getLastPacket());
|
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "NUMBER: %u ID: %s%s CURRENT PACKET: %x SPEED: %u", i, bit_rep[trains[i].getAddress() >> 4], bit_rep[trains[i].getAddress() & 0x0F], trains[i].getLastPacket(), trains[i].getSpeed());
|
||||||
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
|
serial->write(buffer, SNPRINTF_BUFFER_SIZE);
|
||||||
serial->write_p(PSTR(" PROTOCOL: "));
|
serial->write_p(PSTR(" PROTOCOL: "));
|
||||||
if(trains[i].getProtocol() == Train::M_DELTA)serial->write_p(PSTR("delta\n"));
|
if(trains[i].getProtocol() == Train::M_DELTA)serial->write_p(PSTR("delta\n"));
|
||||||
@ -258,7 +259,7 @@ void serialDispatch(Serial* serial)
|
|||||||
{
|
{
|
||||||
char buffer[COMMAND_BUFFER_SIZE];
|
char buffer[COMMAND_BUFFER_SIZE];
|
||||||
unsigned int length = serial->getString(buffer, COMMAND_BUFFER_SIZE);
|
unsigned int length = serial->getString(buffer, COMMAND_BUFFER_SIZE);
|
||||||
if(length > 2)
|
if(length > 0)
|
||||||
{
|
{
|
||||||
serial->write_p(PSTR("Got: "));
|
serial->write_p(PSTR("Got: "));
|
||||||
serial->putChar('\"');
|
serial->putChar('\"');
|
||||||
@ -271,13 +272,13 @@ void serialDispatch(Serial* serial)
|
|||||||
if(token != NULL)
|
if(token != NULL)
|
||||||
trainDispatch(token, serial);
|
trainDispatch(token, serial);
|
||||||
}
|
}
|
||||||
else if(length > 4 && strncmp(token, "erase", 4) == 0)
|
else if(strncmp(token, "erase", 4) == 0)
|
||||||
{
|
{
|
||||||
for(uint16_t i = 0; i < EPPROM_SIZE; i++) EEPROM_write_char(i, 0);
|
for(uint16_t i = 0; i < EPPROM_SIZE; i++) EEPROM_write_char(i, 0);
|
||||||
serial->write_p(PSTR("EEPROM erased\n"));
|
serial->write_p(PSTR("EEPROM erased\n"));
|
||||||
storedTrains = 0;
|
storedTrains = 0;
|
||||||
}
|
}
|
||||||
else if(length > 3 && strcmp(token, "dump") == 0)
|
else if(strcmp(token, "dump") == 0)
|
||||||
{
|
{
|
||||||
for(uint16_t i = 0; i < EPPROM_SIZE; i++)
|
for(uint16_t i = 0; i < EPPROM_SIZE; i++)
|
||||||
{
|
{
|
||||||
@ -286,7 +287,7 @@ void serialDispatch(Serial* serial)
|
|||||||
}
|
}
|
||||||
serial->putChar('\n');
|
serial->putChar('\n');
|
||||||
}
|
}
|
||||||
else if(length > 3 && (strcmp(token, "stop") == 0 || strcmp(token, "s") == 0 ))
|
else if((strcmp(token, "stop") == 0 || strcmp(token, "s") == 0 ))
|
||||||
{
|
{
|
||||||
for(uint16_t i = 0; i < storedTrains; i++)
|
for(uint16_t i = 0; i < storedTrains; i++)
|
||||||
{
|
{
|
||||||
@ -295,12 +296,12 @@ void serialDispatch(Serial* serial)
|
|||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(length > 3 && strcmp(token, "power") == 0)
|
else if(strcmp(token, "power") == 0)
|
||||||
{
|
{
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
if(token != NULL)powerDispatch(token, serial);
|
if(token != NULL)powerDispatch(token, serial);
|
||||||
}
|
}
|
||||||
else if(length > 3 && strcmp(token, "help") == 0)
|
else if(strcmp(token, "help") == 0)
|
||||||
{
|
{
|
||||||
printHelp(serial);
|
printHelp(serial);
|
||||||
}
|
}
|
||||||
|
23
ringbuffer.h
23
ringbuffer.h
@ -1,18 +1,18 @@
|
|||||||
/*UVOS*/
|
/*UVOS*/
|
||||||
|
|
||||||
/* This file is part of TelemetrySystem.
|
/* This file is part of UsbLedController.
|
||||||
*
|
*
|
||||||
* TelemetrySystem is free software: you can redistribute it and/or modify
|
* UsbLedController is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Lesser General Public License (LGPL) version 3 as published by
|
* it under the terms of the GNU Lesser General Public License (LGPL) version 3 as published by
|
||||||
* the Free Software Foundation.
|
* the Free Software Foundation.
|
||||||
*
|
*
|
||||||
* TelemetrySystem is distributed in the hope that it will be useful,
|
* UsbLedController is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with TelemetrySystem. If not, see <http://www.gnu.org/licenses/>.
|
* along with UsbLedController. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
@ -45,10 +45,14 @@ public:
|
|||||||
return BUFFER_SIZE - (_headIndex-_tailIndex);
|
return BUFFER_SIZE - (_headIndex-_tailIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned capacity() const volatile
|
||||||
|
{
|
||||||
|
return BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
bool isOverun() volatile
|
bool isOverun() volatile
|
||||||
{
|
{
|
||||||
bool returnVal = _overrun;
|
bool returnVal = _overrun;
|
||||||
_overrun = false;
|
|
||||||
return returnVal;
|
return returnVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,8 +61,14 @@ public:
|
|||||||
return _tailIndex >= _headIndex;
|
return _tailIndex >= _headIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const T operator[](uint_fast16_t idx) const volatile
|
||||||
|
{
|
||||||
|
return _buffer[idx];
|
||||||
|
}
|
||||||
|
|
||||||
T read() volatile
|
T read() volatile
|
||||||
{
|
{
|
||||||
|
_overrun = false;
|
||||||
if(!isEmpty())
|
if(!isEmpty())
|
||||||
{
|
{
|
||||||
_tailIndex++;
|
_tailIndex++;
|
||||||
@ -79,7 +89,7 @@ public:
|
|||||||
|
|
||||||
void write( T in ) volatile
|
void write( T in ) volatile
|
||||||
{
|
{
|
||||||
if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0)
|
if ((int)_headIndex - BUFFER_SIZE > 0 && (int)_tailIndex - BUFFER_SIZE > 0)
|
||||||
{
|
{
|
||||||
_headIndex -= BUFFER_SIZE;
|
_headIndex -= BUFFER_SIZE;
|
||||||
_tailIndex -= BUFFER_SIZE;
|
_tailIndex -= BUFFER_SIZE;
|
||||||
@ -103,6 +113,7 @@ public:
|
|||||||
_headIndex = 0;
|
_headIndex = 0;
|
||||||
_tailIndex = 0;
|
_tailIndex = 0;
|
||||||
for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = flushCharacter;
|
for(int i = 0; i < BUFFER_SIZE; i++) _buffer[i] = flushCharacter;
|
||||||
|
_overrun = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int getString(T terminator, T* buffer, const unsigned int bufferLength) volatile
|
unsigned int getString(T terminator, T* buffer, const unsigned int bufferLength) volatile
|
||||||
|
53
train.cpp
53
train.cpp
@ -114,9 +114,26 @@ void Train::setSpeed(uint8_t speed)
|
|||||||
resendData();
|
resendData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t Train::getSpeed()
|
||||||
|
{
|
||||||
|
uint8_t speed = 0;
|
||||||
|
for(uint8_t i = 0; i < 4; i++)
|
||||||
|
speed |= ((bool)(lastDataPacket & (1 << (i+1)*2))) << i;
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
|
||||||
void Train::resendData()
|
void Train::resendData()
|
||||||
{
|
{
|
||||||
sendRaw(lastDataPacket);
|
sendRaw(lastDataPacket);
|
||||||
|
if(functionTimer > 0)
|
||||||
|
{
|
||||||
|
--functionTimer;
|
||||||
|
}
|
||||||
|
else if(functionTimer == 0)
|
||||||
|
{
|
||||||
|
functionClear();
|
||||||
|
--functionTimer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Train::getLastPacket()
|
uint16_t Train::getLastPacket()
|
||||||
@ -126,20 +143,48 @@ uint16_t Train::getLastPacket()
|
|||||||
|
|
||||||
void Train::reverse()
|
void Train::reverse()
|
||||||
{
|
{
|
||||||
|
functionClear();
|
||||||
|
resendData();
|
||||||
|
resendData();
|
||||||
|
if(getSpeed() > 0)
|
||||||
|
{
|
||||||
|
setSpeed(0);
|
||||||
|
resendData();
|
||||||
|
resendData();
|
||||||
|
_delay_ms(2000);
|
||||||
|
}
|
||||||
sendRaw(0b000001100);
|
sendRaw(0b000001100);
|
||||||
sendRaw(0b000001100);
|
sendRaw(0b000001100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Train::functionClear()
|
||||||
|
{
|
||||||
|
lastDataPacket = lastDataPacket & ~(0b10101010);
|
||||||
|
}
|
||||||
|
|
||||||
void Train::sendFunction(const uint8_t function, bool enable)
|
void Train::sendFunction(const uint8_t function, bool enable)
|
||||||
{
|
{
|
||||||
if(function == 0) lastDataPacket = (lastDataPacket & ~0b00000011) | (enable ? 0b00000011 : 0b00000000);
|
if(function == 0)
|
||||||
|
{
|
||||||
|
lastDataPacket = (lastDataPacket & ~0b00000001) | (enable ? 0b00000001 : 0b00000000);
|
||||||
|
//lastDataPacket = (lastDataPacket & ~0b1100000000) | (enable ? 0b1100000000 : 0);
|
||||||
|
}
|
||||||
else if(_protocol == M_DIGITAL && function <= 3)
|
else if(_protocol == M_DIGITAL && function <= 3)
|
||||||
{
|
{
|
||||||
lastDataPacket = lastDataPacket | 0b1000000000;
|
if(enable)
|
||||||
if(enable) lastDataPacket = lastDataPacket | (1 << (9-function*2));
|
lastDataPacket |= 0b1000000000;
|
||||||
else lastDataPacket = lastDataPacket & ~(1 << (9-function*2));
|
else
|
||||||
|
lastDataPacket &= ~0b1000000000;
|
||||||
|
for(uint8_t i = 0; i < 4; ++i )
|
||||||
|
{
|
||||||
|
if(function > i)
|
||||||
|
lastDataPacket = lastDataPacket | (1 << (7-i*2));
|
||||||
|
else
|
||||||
|
lastDataPacket = lastDataPacket & ~(1 << (7-i*2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
resendData();
|
resendData();
|
||||||
|
functionTimer = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Train::setProtocol(const uint8_t protocol)
|
void Train::setProtocol(const uint8_t protocol)
|
||||||
|
6
train.h
6
train.h
@ -17,6 +17,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t _address;
|
uint8_t _address;
|
||||||
|
uint8_t _function = 0;
|
||||||
|
uint8_t _speed = 0;
|
||||||
|
|
||||||
static const unsigned char _pinHighA = PD5;
|
static const unsigned char _pinHighA = PD5;
|
||||||
static const unsigned char _pinLowA = PD4;
|
static const unsigned char _pinLowA = PD4;
|
||||||
@ -28,10 +30,12 @@ private:
|
|||||||
uint8_t _protocol = M_DIGITAL;
|
uint8_t _protocol = M_DIGITAL;
|
||||||
|
|
||||||
uint16_t lastDataPacket = 0;
|
uint16_t lastDataPacket = 0;
|
||||||
|
int8_t functionTimer = -1;
|
||||||
|
|
||||||
inline static void off();
|
inline static void off();
|
||||||
void sendBit(const bool bit);
|
void sendBit(const bool bit);
|
||||||
void sendAddress();
|
void sendAddress();
|
||||||
|
void functionClear();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -56,6 +60,8 @@ public:
|
|||||||
|
|
||||||
void setSpeed(uint8_t speed);
|
void setSpeed(uint8_t speed);
|
||||||
|
|
||||||
|
uint8_t getSpeed();
|
||||||
|
|
||||||
void setProtocol(const uint8_t protocol);
|
void setProtocol(const uint8_t protocol);
|
||||||
|
|
||||||
void setAddress(const uint8_t address);
|
void setAddress(const uint8_t address);
|
||||||
|
Reference in New Issue
Block a user