forked from davidzuhn/WiThrottleProtocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiThrottleProtocol.h
162 lines (120 loc) · 4.38 KB
/
WiThrottleProtocol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* -*- c++ -*-
*
* WiThrottleProtocolProtocol
*
* This package implements a WiThrottleProtocol protocol connection,
* allow a device to communicate with a JMRI server or other
* WiThrottleProtocol device (like the Digitrax LNWI).
*
* Copyright © 2018-2019, 2021 Blue Knobby Systems Inc.
*
* This work is licensed under the Creative Commons Attribution-ShareAlike
* 4.0 International License. To view a copy of this license, visit
* http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to
* Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
*
* Attribution — You must give appropriate credit, provide a link to the
* license, and indicate if changes were made. You may do so in any
* reasonable manner, but not in any way that suggests the licensor
* endorses you or your use.
*
* ShareAlike — If you remix, transform, or build upon the material, you
* must distribute your contributions under the same license as the
* original.
*
* All other rights reserved.
*
*/
#ifndef WITHROTTLE_H
#define WITHROTTLE_H
#include "Arduino.h"
#include "Chrono.h"
typedef enum Direction {
Reverse = 0,
Forward = 1
} Direction;
typedef enum TrackPower {
PowerOff = 0,
PowerOn = 1,
PowerUnknown = 2
} TrackPower;
class WiThrottleProtocolDelegate
{
public:
virtual void receivedVersion(String version) {}
virtual void fastTimeChanged(uint32_t time) { }
virtual void fastTimeRateChanged(double rate) { }
virtual void heartbeatConfig(int seconds) { }
virtual void receivedFunctionState(uint8_t func, bool state) { }
virtual void receivedSpeed(int speed) { } // Vnnn
virtual void receivedDirection(Direction dir) { } // R{0,1}
virtual void receivedSpeedSteps(int steps) { } // snn
virtual void receivedWebPort(int port) { } // PWnnnnn
virtual void receivedTrackPower(TrackPower state) { } // PPAn
virtual void addressAdded(String address, String entry) { } // MT+addr<;>roster entry
virtual void addressRemoved(String address, String command) { } // MT-addr<;>[dr]
virtual void addressStealNeeded(String address, String entry) { } // MTSaddr<;>addr
};
class WiThrottleProtocol
{
public:
WiThrottleProtocol(bool server = false);
void begin(Stream *console);
void connect(Stream *stream);
void disconnect();
void setDeviceName(String deviceName);
void setDeviceID(String deviceId);
bool check();
int fastTimeHours();
int fastTimeMinutes();
float fastTimeRate();
bool clockChanged;
void requireHeartbeat(bool needed=true);
bool heartbeatChanged;
bool addLocomotive(String address); // address is [S|L]nnnn (where n is 0-10000)
bool stealLocomotive(String address); // address is [S|L]nnnn (where n is 0-10000)
bool releaseLocomotive(String address = "*");
void setFunction(int funcnum, bool pressed);
bool setSpeed(int speed);
int getSpeed();
bool setDirection(Direction direction);
Direction getDirection();
void emergencyStop();
WiThrottleProtocolDelegate *delegate = NULL;
private:
bool server;
Stream *stream;
Stream *console;
bool processCommand(char *c, int len);
bool processLocomotiveAction(char *c, int len);
bool processFastTime(char *c, int len);
bool processHeartbeat(char *c, int len);
void processProtocolVersion(char *c, int len);
void processWebPort(char *c, int len);
void processTrackPower(char *c, int len);
void processFunctionState(const String& functionData);
void processSpeedSteps(const String& speedStepData);
void processDirection(const String& speedStepData);
void processSpeed(const String& speedData);
void processAddRemove(char *c, int len);
void processStealNeeded(char *c, int len);
bool checkFastTime();
bool checkHeartbeat();
void sendCommand(String cmd);
void setCurrentFastTime(const String& s);
char inputbuffer[1024];
ssize_t nextChar; // where the next character to be read goes in the buffer
Chrono heartbeatTimer;
int heartbeatPeriod;
Chrono fastTimeTimer;
double currentFastTime;
float currentFastTimeRate;
void resetChangeFlags();
void init();
bool locomotiveSelected = false;
String currentAddress;
int currentSpeed;
int speedSteps; // 1=128, 2=28, 4=27, 8=14, 16=28Mot
Direction currentDirection;
};
#endif // WITHROTTLE_H