working with delta only
This commit is contained in:
56
main.cpp
56
main.cpp
@ -7,14 +7,7 @@
|
|||||||
#include "writepin.h"
|
#include "writepin.h"
|
||||||
#include "train.h"
|
#include "train.h"
|
||||||
#include "eeprom.h"
|
#include "eeprom.h"
|
||||||
|
#include "bitrep.h"
|
||||||
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",
|
|
||||||
};
|
|
||||||
|
|
||||||
#define MAX_TRAINS 16
|
#define MAX_TRAINS 16
|
||||||
#define COMMAND_BUFFER_SIZE 64
|
#define COMMAND_BUFFER_SIZE 64
|
||||||
@ -110,6 +103,7 @@ inline static void printHelp(Serial* serial)
|
|||||||
|
|
||||||
void trainDispatch(char* inBuffer, Serial* serial)
|
void trainDispatch(char* inBuffer, Serial* serial)
|
||||||
{
|
{
|
||||||
|
cli();
|
||||||
if(powerIsOn == false)
|
if(powerIsOn == false)
|
||||||
{
|
{
|
||||||
powerIsOn = true;
|
powerIsOn = true;
|
||||||
@ -149,28 +143,50 @@ void trainDispatch(char* inBuffer, Serial* serial)
|
|||||||
}
|
}
|
||||||
serial->putChar('\n');
|
serial->putChar('\n');
|
||||||
}
|
}
|
||||||
else\
|
else
|
||||||
{
|
{
|
||||||
uint8_t id = strtol(inBuffer, nullptr, 10);
|
uint8_t id = strtol(inBuffer, nullptr, 10);
|
||||||
if(id < storedTrains )
|
if(id < storedTrains )
|
||||||
{
|
{
|
||||||
char* token = strtok(NULL, " ");
|
char* token = strtok(NULL, " ");
|
||||||
if( strcmp(token, "speed") == 0 )
|
if( token != NULL && strcmp(token, "speed") == 0 )
|
||||||
{
|
{
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
trains[id].setSpeed(atoi(token));
|
trains[id].setSpeed(atoi(token));
|
||||||
|
serial->write_p(PSTR("Set Train speed\n"));
|
||||||
}
|
}
|
||||||
else if( strcmp(token, "function") == 0 )
|
else if(token != NULL && strcmp(token, "function") == 0 )
|
||||||
{
|
{
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
trains[id].sendFunction(atoi(token));
|
if(token != NULL)
|
||||||
|
{
|
||||||
|
for(uint16_t j = 0; j < 8; j++)
|
||||||
|
{
|
||||||
|
uint16_t i = strtol(token, nullptr, 16 );
|
||||||
|
trains[id].sendFunction(i);
|
||||||
|
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "TRYING: %x\n", i);
|
||||||
|
serial->write(buffer, strlen(buffer));
|
||||||
|
sei();
|
||||||
|
_delay_ms(250);
|
||||||
|
cli();
|
||||||
|
|
||||||
}
|
}
|
||||||
else if( strcmp(token, "reverse") == 0 ) trains[id].reverse();
|
}
|
||||||
else if( strcmp(token, "stop") )trains[id].setSpeed(0);
|
/*
|
||||||
|
trains[id].sendFunction(atoi(token));
|
||||||
|
serial->write_p(PSTR("Set Train function\n"));*/
|
||||||
|
}
|
||||||
|
else if( token != NULL && strcmp(token, "reverse") == 0 )
|
||||||
|
{
|
||||||
|
trains[id].reverse();
|
||||||
|
serial->write_p(PSTR("Reversed Train\n"));
|
||||||
|
}
|
||||||
|
else if( token != NULL && strcmp(token, "stop") )trains[id].setSpeed(0);
|
||||||
else serial->write_p(PSTR("Not a valid command\n"));
|
else serial->write_p(PSTR("Not a valid command\n"));
|
||||||
}
|
}
|
||||||
else serial->write_p(PSTR("Id out of range.\n"));
|
else serial->write_p(PSTR("Id out of range.\n"));
|
||||||
}
|
}
|
||||||
|
sei();
|
||||||
}
|
}
|
||||||
|
|
||||||
void powerDispatch(char* token, Serial* serial)
|
void powerDispatch(char* token, Serial* serial)
|
||||||
@ -195,13 +211,13 @@ void powerDispatch(char* token, Serial* serial)
|
|||||||
else if(strcmp(token, "auto") == 0)
|
else if(strcmp(token, "auto") == 0)
|
||||||
{
|
{
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
if(strcmp(token, "on") == 0)
|
if(token != NULL && strcmp(token, "on") == 0)
|
||||||
{
|
{
|
||||||
autoff = true;
|
autoff = true;
|
||||||
serial->write_p(PSTR("auto power off turned on.\n"));
|
serial->write_p(PSTR("auto power off turned on.\n"));
|
||||||
save_state();
|
save_state();
|
||||||
}
|
}
|
||||||
else if(strcmp(token, "off") == 0)
|
else if(token != NULL && strcmp(token, "off") == 0)
|
||||||
{
|
{
|
||||||
autoff = false;
|
autoff = false;
|
||||||
serial->write_p(PSTR("auto power off turned off.\n"));
|
serial->write_p(PSTR("auto power off turned off.\n"));
|
||||||
@ -232,7 +248,7 @@ void serialDispatch(Serial* serial)
|
|||||||
if(length > 4 && strcmp(token, "train") == 0)
|
if(length > 4 && strcmp(token, "train") == 0)
|
||||||
{
|
{
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
trainDispatch(token, serial);
|
if(token != NULL)trainDispatch(token, serial);
|
||||||
}
|
}
|
||||||
else if(length > 4 && strncmp(token, "erase", 4) == 0)
|
else if(length > 4 && strncmp(token, "erase", 4) == 0)
|
||||||
{
|
{
|
||||||
@ -253,13 +269,15 @@ void serialDispatch(Serial* serial)
|
|||||||
{
|
{
|
||||||
for(uint16_t i = 0; i < storedTrains; i++)
|
for(uint16_t i = 0; i < storedTrains; i++)
|
||||||
{
|
{
|
||||||
|
cli();
|
||||||
trains[i].setSpeed(0);
|
trains[i].setSpeed(0);
|
||||||
|
sei();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(length > 3 && strcmp(token, "power") == 0)
|
else if(length > 3 && strcmp(token, "power") == 0)
|
||||||
{
|
{
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
powerDispatch(token, serial);
|
if(token != NULL)powerDispatch(token, serial);
|
||||||
}
|
}
|
||||||
else if(length > 3 && strcmp(token, "help") == 0)
|
else if(length > 3 && strcmp(token, "help") == 0)
|
||||||
{
|
{
|
||||||
@ -282,7 +300,7 @@ int main()
|
|||||||
TIMSK0 = 0b00000001; //turn on timer0 interupt on OCIEB
|
TIMSK0 = 0b00000001; //turn on timer0 interupt on OCIEB
|
||||||
TCCR0B = (1<<CS02) /*| (1<<CS00)*/; // run timer0 with /1024 scaler
|
TCCR0B = (1<<CS02) /*| (1<<CS00)*/; // run timer0 with /1024 scaler
|
||||||
|
|
||||||
DDRD = (1 << PD2) | (1 << PD3);
|
DDRD = (1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5);
|
||||||
|
|
||||||
restore_state();
|
restore_state();
|
||||||
|
|
||||||
|
36
train.cpp
36
train.cpp
@ -8,8 +8,6 @@ Train::Train(const uint8_t address): _address(address)
|
|||||||
|
|
||||||
Train::Train()
|
Train::Train()
|
||||||
{
|
{
|
||||||
writePin(_port, _pinHigh, false);
|
|
||||||
writePin(_port, _pinLow, true);
|
|
||||||
_address = 0;
|
_address = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,24 +21,33 @@ uint8_t Train::getAddress()
|
|||||||
return _address;
|
return _address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Train::off()
|
||||||
|
{
|
||||||
|
writePin(_port, _pinHighA, false);
|
||||||
|
writePin(_port, _pinHighB, false);
|
||||||
|
writePin(_port, _pinLowA, true);
|
||||||
|
writePin(_port, _pinLowB, true);
|
||||||
|
}
|
||||||
|
|
||||||
void Train::setOutput(const uint8_t state)
|
void Train::setOutput(const uint8_t state)
|
||||||
{
|
{
|
||||||
if(state == HIGH)
|
if(state == HIGH)
|
||||||
{
|
{
|
||||||
writePin(_port, _pinLow, true);
|
off();
|
||||||
_delay_us(5);
|
_delay_us(5);
|
||||||
writePin(_port, _pinHigh, true);
|
writePin(_port, _pinHighA, true);
|
||||||
|
writePin(_port, _pinLowB, false);
|
||||||
}
|
}
|
||||||
else if (state == LOW)
|
else if (state == LOW)
|
||||||
{
|
{
|
||||||
writePin(_port, _pinHigh, false);
|
off();
|
||||||
_delay_us(5);
|
_delay_us(5);
|
||||||
writePin(_port, _pinLow, false);
|
writePin(_port, _pinHighB, true);
|
||||||
|
writePin(_port, _pinLowA, false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
writePin(_port, _pinHigh, false);
|
off();
|
||||||
writePin(_port, _pinLow, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +77,7 @@ void Train::sendAddress()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Train::sendData(const uint8_t data)
|
void Train::sendRaw(const uint8_t data)
|
||||||
{
|
{
|
||||||
for(uint8_t j = 0; j < SEND_COUNT; j++)
|
for(uint8_t j = 0; j < SEND_COUNT; j++)
|
||||||
{
|
{
|
||||||
@ -78,7 +85,6 @@ void Train::sendData(const uint8_t data)
|
|||||||
for(uint8_t i = 0; i < 5; i++)
|
for(uint8_t i = 0; i < 5; i++)
|
||||||
{
|
{
|
||||||
sendBit(data & (1 << i));
|
sendBit(data & (1 << i));
|
||||||
sendBit(data & (1 << i));
|
|
||||||
}
|
}
|
||||||
_delay_ms(2);
|
_delay_ms(2);
|
||||||
}
|
}
|
||||||
@ -90,14 +96,14 @@ void Train::setSpeed(uint8_t speed)
|
|||||||
if(speed <= 15)
|
if(speed <= 15)
|
||||||
{
|
{
|
||||||
lastSpeed = speed;
|
lastSpeed = speed;
|
||||||
sendData(speed << 1);
|
sendData((speed << 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Train::resendSpeed()
|
void Train::resendSpeed()
|
||||||
{
|
{
|
||||||
uint8_t data = lastSpeed;
|
uint8_t data = lastSpeed;
|
||||||
sendData(data << 1);
|
sendData((data << 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Train::getSpeed()
|
uint8_t Train::getSpeed()
|
||||||
@ -107,10 +113,10 @@ uint8_t Train::getSpeed()
|
|||||||
|
|
||||||
void Train::reverse()
|
void Train::reverse()
|
||||||
{
|
{
|
||||||
sendData(1 << 1);
|
sendData((1 << 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Train::sendFunction(const uint8_t function)
|
void Train::sendFunction(const uint16_t function)
|
||||||
{
|
{
|
||||||
sendData((function << 1) | 1);
|
sendDataUndoubled(function);
|
||||||
}
|
}
|
||||||
|
48
train.h
48
train.h
@ -7,26 +7,34 @@
|
|||||||
|
|
||||||
class Train
|
class Train
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
uint8_t _address;
|
|
||||||
|
|
||||||
static const unsigned char _pinHigh = PD3;
|
|
||||||
static const unsigned char _pinLow = PD2;
|
|
||||||
|
|
||||||
static const uint8_t SEND_COUNT = 4;
|
|
||||||
|
|
||||||
uint8_t lastSpeed = 0;
|
|
||||||
|
|
||||||
void sendBit(const bool bit);
|
|
||||||
void sendAddress();
|
|
||||||
void sendData(const uint8_t data);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static const uint8_t M_DELTA = 0;
|
||||||
|
static const uint8_t M_DIGITAL = 1;
|
||||||
|
|
||||||
static const uint8_t HIGH = 2;
|
static const uint8_t HIGH = 2;
|
||||||
static const uint8_t LOW = 1;
|
static const uint8_t LOW = 1;
|
||||||
static const uint8_t OFF = 0;
|
static const uint8_t OFF = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t _address;
|
||||||
|
|
||||||
|
static const unsigned char _pinHighA = PD5;
|
||||||
|
static const unsigned char _pinLowA = PD4;
|
||||||
|
static const unsigned char _pinHighB = PD2;
|
||||||
|
static const unsigned char _pinLowB = PD3;
|
||||||
|
|
||||||
|
static const uint8_t SEND_COUNT = 4;
|
||||||
|
|
||||||
|
uint8_t _protocol = M_DIGITAL;
|
||||||
|
|
||||||
|
uint16_t lastdatapacket = 0;
|
||||||
|
|
||||||
|
inline static void off();
|
||||||
|
void sendBit(const bool bit);
|
||||||
|
void sendAddress();
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
static void setOutput(const uint8_t state);
|
static void setOutput(const uint8_t state);
|
||||||
|
|
||||||
Train(const uint8_t address);
|
Train(const uint8_t address);
|
||||||
@ -34,16 +42,22 @@ public:
|
|||||||
|
|
||||||
void resendSpeed();
|
void resendSpeed();
|
||||||
|
|
||||||
void setSpeed(uint8_t speed);
|
|
||||||
|
|
||||||
void reverse();
|
void reverse();
|
||||||
|
|
||||||
uint8_t getAddress();
|
uint8_t getAddress();
|
||||||
|
|
||||||
uint8_t getSpeed();
|
uint8_t getSpeed();
|
||||||
|
|
||||||
|
uint8_t getProtocol();
|
||||||
|
|
||||||
|
void setSpeed(uint8_t speed);
|
||||||
|
|
||||||
|
void setProtocol(const uint8_t protocol);
|
||||||
|
|
||||||
void setAddress(const uint8_t address);
|
void setAddress(const uint8_t address);
|
||||||
|
|
||||||
void sendFunction(const uint8_t function);
|
void sendFunction(const uint16_t function);
|
||||||
|
|
||||||
|
void sendRaw(const uint16_t data);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user