-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.c
231 lines (191 loc) · 4.79 KB
/
cmd.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "cmd.h"
#include "serial.h"
#include "stream.h"
#include "train.h"
#include "track.h"
#include "reset.h"
#include "util.h"
#include "version.h"
#include <ctype.h>
#include <stdint.h>
#include <string.h>
int cmd_emergency_stop( const char *cmd );
int cmd_track_power( const char *cmd );
int cmd_train_process( uint16_t addr, const char *cmd );
int cmd_train_dcc_mode( uint16_t addr, const char *cmd );
int cmd_train_activate( uint16_t addr, const char *cmd );
int cmd_train_speed_and_dir( uint16_t addr, const char *cmd );
int cmd_train_function( uint16_t addr, const char *cmd );
int cmd_process( const char *cmd )
{
uint16_t addr;
if ( *cmd == 0 ) {
return 0;
}
if ( *cmd == 'H' ) { // NOTHALT
return cmd_emergency_stop( cmd + 1 );
}
else if ( *cmd == 'R' ) {
return system_reset();
}
else if (*cmd == 'S' ) {
trains_reset_speed_and_functions();
return 1;
}
else if ( *cmd == 'P' ) { // TRACK POWER
return cmd_track_power( cmd + 1 );
}
else if ( isdigit( *cmd ) ) { // Kommando für Zugadresse
addr = str_to_uint16( &cmd );
if ( addr > 10239 ) {
return 0;
}
return cmd_train_process( addr, cmd );
}
else if ( *cmd == 'I' ) { // initialize and send version over serial
serial_puts( version_string );
serial_puts( "\r\n" );
return 1;
}
return 0;
}
int cmd_emergency_stop( const char *cmd )
{
if ( *cmd == '+' ) {
activate_emergency_stop( 1 );
}
else if ( *cmd == '-' ) {
activate_emergency_stop( 0 );
}
else {
return 0;
}
return 1;
}
int cmd_track_power( const char *cmd )
{
if ( *cmd == '+' ) {
track_set_power( main_track, 1 );
stream_dcc_reset(); // DCC Controller nach power-on zurücksetzen
}
else if ( *cmd == '-' ) {
track_set_power( main_track, 0 );
}
else {
return 0;
}
return 1;
}
int cmd_train_process( uint16_t addr, const char *cmd )
{
if ( *cmd == 0 ) {
return 0;
}
switch ( *cmd ) {
case 'A': // DCC Signal für Zug (de)aktivieren
return cmd_train_activate( addr, cmd );
case 'C': // DCC Modus setzen
return cmd_train_dcc_mode( addr, cmd );
case 'V': // Zug vorwärts-Geschw. setzen
case 'R': // Zug rückwärts-Geschw. setzen
return cmd_train_speed_and_dir( addr, cmd );
case 'F': // Funktion ein-/ausschalten
return cmd_train_function( addr, cmd );
default:
break;
}
return 0;
}
int cmd_train_dcc_mode( uint16_t addr, const char *cmd )
{
cmd++;
if ( *cmd == 0 ) {
return 0;
}
if ( *cmd == '+' || *cmd == '0' ) { // 14 Fahrstufen
Train *train = train_by_addr( addr );
train_set_dcc_mode( train, DCC_MODE_14 );
}
else if ( *cmd == '-' || *cmd == '1' ) { // 28 Fahrstufen
Train *train = train_by_addr( addr );
train_set_dcc_mode( train, DCC_MODE_28 );
}
else if ( *cmd == '2' ) {
Train *train = train_by_addr( addr );
train_set_dcc_mode( train, DCC_MODE_128 );
}
else {
return 0;
}
return 1;
}
int cmd_train_activate( uint16_t addr, const char *cmd )
{
cmd++;
if ( *cmd == 0 ) {
return 0;
}
if ( *cmd == '+' ) { // Zug aktivieren
Train *train = train_by_addr( addr );
train_activate( train );
}
else if ( *cmd == '-' ) { // Zug deaktivieren
Train *train = train_by_addr( addr );
train_deactivate( train );
}
else {
return 0;
}
return 1;
}
int cmd_train_speed_and_dir( uint16_t addr, const char *cmd )
{
char dir = *cmd++;
if ( !isdigit( *cmd ) ) {
return 0;
}
uint8_t speed = str_to_uint8( &cmd );
Train *train = train_by_addr( addr );
if ( train->dcc_mode == DCC_MODE_14 && speed > 14 ) {
return 0;
}
else if ( train->dcc_mode == DCC_MODE_28 && speed > 28 ) {
return 0;
}
else if ( speed > 126 ) {
return 0;
}
if ( addr > 0 ) {
if ( dir == 'V' ) {
train_set_speed_and_dir( train, speed, TRAIN_FORWARD );
}
else {
train_set_speed_and_dir( train, speed, TRAIN_BACKWARD );
}
return 1;
}
else {
// set speed for all trains
//trains_set_speed((uint8_t), speed);
}
return 0;
}
int cmd_train_function( uint16_t addr, const char *cmd )
{
cmd++;
if ( !isdigit( *cmd ) ) {
return 0;
}
uint8_t f = str_to_uint8( &cmd );
Train *train = train_by_addr( addr );
if ( *cmd == '+' ) {
train_enable_function( train, f );
}
else if ( *cmd == '-' ) {
train_disable_function( train, f );
}
else {
return 0;
}
return 1;
}