-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.c
311 lines (272 loc) · 8.46 KB
/
stream.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "stream.h"
#include "serial.h"
#include "track.h"
#include "train.h"
#include <stddef.h>
#include <util/atomic.h>
volatile uint8_t repeated_stream_count = 0; // Anzahl an Wiederholungen für aktuellen Stream
uint8_t repeated_stream_type = STREAM_NONE;
Train *repeated_stream_train = NULL;
volatile uint8_t num_addresses_active = 0;
int8_t estop = 0; // Nothalt
static Stream estop_stream = { .length = 3, .data = { 0x00, 0x41, 0x41 } };
static Stream estop128_stream = { .length = 4,
.data = { 0x00, 0x3f, 0x01, 0x3e } };
static Stream idle_stream = { .length = 3, .data = { 0xff, 0x00, 0xff } };
static Stream reset_stream = { .length = 3, .data = { 0x00, 0x00, 0x00 } };
void activate_emergency_stop( int8_t stop ) { estop = stop; }
void stream_dcc_reset( void )
{
ATOMIC_BLOCK( ATOMIC_RESTORESTATE )
{
repeated_stream_type = STREAM_RESET;
repeated_stream_count = 20;
}
}
static void gen_speed_stream( Train *train, Stream *stream );
static void gen_function_stream( Train *train, Stream *stream,
int functions );
void gen_train_stream( Train *train, Stream *stream )
{
if ( train->stream_type == SPEED_AND_DIR ) {
return gen_speed_stream( train, stream );
}
else {
return gen_function_stream( train, stream, train->stream_type );
}
}
static void plan_next_train_stream( Train *train )
{
uint8_t *stream = &train->stream_type;
uint8_t f_enabled = train->f_enabled;
*stream += 1;
switch ( *stream ) {
case SPEED_AND_DIR:
case FUNCTION_0_4:
case FUNCTION_5_8:
case FUNCTION_9_12:
return;
case FUNCTION_13_20:
if ( f_enabled & 0x01 ) {
return;
}
*stream += 1;
case FUNCTION_21_28:
if ( f_enabled & 0x02 ) {
return;
}
*stream += 1;
case FUNCTION_29_36:
if ( f_enabled & 0x04 ) {
return;
}
*stream += 1;
case FUNCTION_37_44:
if ( f_enabled & 0x08 ) {
return;
}
*stream += 1;
case FUNCTION_45_52:
if ( f_enabled & 0x10 ) {
return;
}
*stream += 1;
case FUNCTION_53_60:
if ( f_enabled & 0x20 ) {
return;
}
*stream += 1;
case FUNCTION_61_68:
if ( f_enabled & 0x40 ) {
return;
}
*stream = SPEED_AND_DIR;
default:
*stream = SPEED_AND_DIR;
}
}
static uint8_t stream_type = STREAM_IDLE;
static Train *stream_train = NULL;
static uint8_t stream_odd = 0;
// Läuft im Kontext der ISR
void plan_next_stream( void )
{
stream_odd = ~stream_odd; // markiert jedes zweite Datenpaket
// Nothalt hat Priorität
if ( estop && repeated_stream_type != STREAM_RESET && repeated_stream_type != STREAM_IDLE ) {
stream_type = ( stream_odd ) ? STREAM_ESTOP : STREAM_ESTOP128;
return;
}
// Aktueller Stream soll wiederholt werden
if ( repeated_stream_count > 0 ) {
stream_type = repeated_stream_type;
stream_train = repeated_stream_train;
repeated_stream_count--;
if ( repeated_stream_count == 0 ) {
if ( repeated_stream_type == STREAM_RESET ) {
// Dem Reset müssen laut Spezifikation 10 IDLE Pakete folgen.
repeated_stream_type = STREAM_IDLE;
repeated_stream_count = 10;
}
else {
repeated_stream_type = STREAM_NONE;
}
}
return;
}
// Idle Kommando senden wenn keine Züge aktiv ist. Weiterhin jedes zweite
// Paket als Idle Kommando senden wenn nur ein Zug aktiv ist, um 5ms
// Zeitintervall zwischen den Paketen an die gleiche Adresse einzuhalten.
if ( !num_addresses_active || ( num_addresses_active == 1 && stream_odd ) ) {
stream_type = STREAM_IDLE;
}
else {
stream_type = STREAM_TRAIN;
if ( stream_train == NULL ) {
stream_train = trains;
}
else {
do {
stream_train = stream_train->next;
} while ( !stream_train->active );
}
}
}
Stream *build_next_stream( void ) {
static Stream stream;
switch ( stream_type ) {
case STREAM_RESET:
return &reset_stream;
case STREAM_TRAIN:
gen_train_stream( stream_train, &stream );
plan_next_train_stream( stream_train );
return &stream;
case STREAM_ESTOP: return &estop_stream;
case STREAM_ESTOP128: return &estop128_stream;
default:
return &idle_stream;
}
}
static inline void update_checksum( Stream *stream )
{
uint8_t i = 0;
uint8_t checksum = 0;
for ( ; i < stream->length - 1; i++ ) {
checksum ^= stream->data[i];
}
stream->data[stream->length - 1] = checksum;
}
static void gen_speed_stream( Train *train, Stream *stream )
{
uint8_t *data = stream->data;
// Zugaddresse kodieren
if ( train->addr < 128 ) {
stream->length = 3;
*data++ = train->addr;
}
else {
stream->length = 4;
*data++ = 0xc0 | ( train->addr >> 8 );
*data++ = train->addr & 0xff;
}
// Geschwindigkeit und Richtung kodieren
if ( train->dcc_mode == DCC_MODE_128 ) {
*data++ = 0x3f;
*data = ( ( train->direction == TRAIN_FORWARD ) ? 0x80 : 0 ) |
( ( train->speed > 0 ) ? train->speed + 1 : 0 );
stream->length++; // ein extra-Byte für 128 Fahrstufen
}
else {
*data = 0x40 | ( ( train->direction == TRAIN_FORWARD ) ? 0x20 : 0 );
if ( train->dcc_mode == DCC_MODE_28 ) {
uint8_t spd = ( train->speed > 0 ) ? train->speed + 3 : 0;
*data |= ( ( spd & 1 ) << 4 ) | ( spd >> 1 );
}
else {
*data |= ( ( train->speed > 0 ) ? train->speed + 1 : 0 ) |
( ( train->functions[0] & 1 ) ? 0x10 : 0 );
}
}
update_checksum( stream );
}
static void gen_function_stream( Train *train, Stream *stream, int functions )
{
uint8_t *data = stream->data;
uint8_t fdata;
// Zugaddresse kodieren
if ( train->addr < 128 ) {
stream->length = 3;
*data++ = train->addr;
}
else {
stream->length = 4;
*data++ = 0xc0 | ( train->addr >> 8 );
*data++ = train->addr & 0xff;
}
switch ( functions ) {
case FUNCTION_0_4: // Funktionen 0-4 codieren
fdata = ( ( train->functions[0] >> 1 ) & 0x0f ) |
( ( train->functions[0] << 4 ) & 0x10 );
*data = 0x80 | fdata;
break;
case FUNCTION_5_8: // Funktionen 5-8 codieren
fdata = ( train->functions[0] >> 5 ) |
( ( train->functions[1] << 3 ) & 0x08 );
*data = 0xb0 | fdata;
break;
case FUNCTION_9_12: // Funktionen 9-12 codieren
fdata = ( train->functions[1] >> 1 ) & 0x0f;
*data = 0xa0 | fdata;
break;
case FUNCTION_13_20: // Funktionen 13-20 codieren
fdata = ( train->functions[1] >> 5 ) |
( ( train->functions[2] << 3 ) & 0xf8 );
*data++ = 0xde;
*data = fdata;
stream->length++;
break;
case FUNCTION_21_28: // Funktionen 21-28 codieren
fdata = ( train->functions[2] >> 5 ) |
( ( train->functions[3] << 3 ) & 0xf8 );
*data++ = 0xdf;
*data = fdata;
stream->length++;
break;
case FUNCTION_29_36:
fdata = ( train->functions[3] >> 5 ) |
( ( train->functions[4] << 3 ) & 0xf8 );
*data++ = 0xd8;
*data = fdata;
stream->length++;
break;
case FUNCTION_37_44:
fdata = ( train->functions[4] >> 5 ) |
( ( train->functions[5] << 3 ) & 0xf8 );
*data++ = 0xd9;
*data = fdata;
stream->length++;
break;
case FUNCTION_45_52:
fdata = ( train->functions[5] >> 5 ) |
( ( train->functions[6] << 3 ) & 0xf8 );
*data++ = 0xda;
*data = fdata;
stream->length++;
break;
case FUNCTION_53_60:
fdata = ( train->functions[6] >> 5 ) |
( ( train->functions[7] << 3 ) & 0xf8 );
*data++ = 0xdb;
*data = fdata;
stream->length++;
break;
case FUNCTION_61_68:
fdata = ( train->functions[7] >> 5 ) |
( ( train->functions[8] << 3 ) & 0xf8 );
*data++ = 0xdc;
*data = fdata;
stream->length++;
break;
}
update_checksum( stream );
}