-
Notifications
You must be signed in to change notification settings - Fork 0
/
coolASSM.cpp
609 lines (536 loc) · 19.5 KB
/
coolASSM.cpp
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/**
* coolASSM - cool arduino serial state machine
*
* Christian
* graetz23@gmail.com
* created 20190511
* version 20200404
*
* MIT License
*
* Copyright (c) 2019-2024 coolASSM Christian (graetz23@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <Arduino.h>
// #include <Servo.h>
#include "./coolASSM.h" // base class header file
ASSM::ASSM( void ) {
if( ASSM_LED_ACTV ) {
pinMode( ASSM_LED_PIN, OUTPUT ); // arduino's built-in LED for flashing
} // if
_state = ASSM_STATE_IDLNG; // initial STATE is IDLE due to not reacting
_command = ASSM_CMD_NULL; // set COMMAND to NO (NULL) COMMAND
_helper = new ASSM_HELPER( ); // use internal helper ..
} // method
ASSM::~ASSM( void ) {
delete _helper;
} // method
void ASSM::setup( void ) {
Serial.begin( SERIAL_BAUD ); // set up serial
delay(10); // 10 ms
} // method
ASSM_HELPER* ASSM::get_ASSM_HELPER( void ) {
return _helper; // if one needs the helper outside cool ASSM ..
} // method
void ASSM::display( String s ) {
// String sstate = _helper->state_to_String( _state );
// const char* cstate = sstate.c_str();
// String scmmnd = _helper->command_to_String( _command );
// const char* ccmmnd = scmmnd.c_str();
// you can drive some display here
// e.g. an liquid crystal 20x2 lcd
} // method
void ASSM::welcome( ) {
// print some welcome message to display here
delay(250); // 500 ms
} // method
void ASSM::ready( ) {
// print some ready messsafe to display here
delay(750); // 500 ms
} // method
void ASSM::writeData( String tag, bool data ) {
String head = _helper->mark_as_Data_starting( tag );
String foot = _helper->mark_as_Data_stopping( tag );
String data_str = String(data);
String send = head + data_str + foot;
const char* cstr = send.c_str( );
Serial.write( cstr );
} // method
void ASSM::writeData( String tag, int data ) {
String head = _helper->mark_as_Data_starting( tag );
String foot = _helper->mark_as_Data_stopping( tag );
String data_str = String(data);
String send = head + data_str + foot;
const char* cstr = send.c_str( );
Serial.write( cstr );
} // method
void ASSM::writeData( String tag, float data, int digits, int precision ) {
String head = _helper->mark_as_Data_starting( tag );
String foot = _helper->mark_as_Data_stopping( tag );
String data_str = _helper->toStr( data, digits, precision );
String send = head + data_str + foot;
const char* cstr = send.c_str( );
Serial.write( cstr );
} // method
void ASSM::writeData( String tag, double data, int digits, int precision ) {
String head = _helper->mark_as_Data_starting( tag );
String foot = _helper->mark_as_Data_stopping( tag );
String data_str = _helper->toStr( data, digits, precision );
String send = head + data_str + foot;
const char* cstr = send.c_str( );
Serial.write( cstr );
} // method
void ASSM::writeData( String tag, String data ) {
String head = _helper->mark_as_Data_starting( tag );
String foot = _helper->mark_as_Data_stopping( tag );
String send = head + data + foot;
const char* cstr = send.c_str( );
Serial.write( cstr );
} // method
void ASSM::writeCommand( uint8_t command ) {
writeCommand( _helper->command_to_String( command ) );
} // method
void ASSM::writeCommand( String command ) {
String str = _helper->mark_as_State_or_Command( command );
const char* cstr = str.c_str( );
Serial.write( cstr );
} // method
void ASSM::writeState( uint8_t state ) {
writeState( _helper->state_to_String( state ) );
} // method
void ASSM::writeState( String state ) {
String str = _helper->mark_as_State_or_Command( state );
const char* cstr = str.c_str( );
Serial.write( cstr );
} // method
// read the received State from serial
uint8_t ASSM::readCommand( ) {
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
static byte ndx = 0;
static boolean isReceiving = false;
char rc;
uint8_t cmd = ASSM_CMD_NULL;
if( Serial.available( ) > 0 ) {
while( Serial.available( ) > 0 ) {
rc = Serial.read( );
if( isReceiving == true ) {
if( rc != _markerFoot ) {
receivedChars[ ndx ] = rc;
ndx++;
if( ndx >= numChars ) {
ndx = numChars - 1;
} // if
} else {
receivedChars[ ndx ] = '\0'; // terminate the string
isReceiving = false;
ndx = 0;
} // if
} // if
else if( rc == _markerHead )
{ // found a beginning
isReceiving = true;
} // if
} // loop
cmd = a2i( receivedChars );
} // if
return cmd;
} // method
/**
* running the recevied command
*/
void ASSM::loop( ) {
uint8_t command = readCommand( ); // read and filter a command from serial
uint8_t command_internal = command;
bool isProcessingInternally = true;
while( isProcessingInternally ) {
uint8_t state = process_command( command_internal );
command_internal = process_state( state );
if( command_internal == ASSM_CMD_NULL ) {
isProcessingInternally = false;
} // if
} // loop
} // method
uint8_t ASSM::process_command( uint8_t command ) {
_command = command; // copy to internal member variable
uint8_t state = ASSM_STATE_ERROR; // initial state is always last state
switch( _command ) {
// process the COMMAND SNA - State Not Available; we are in ERROR state ..
case ASSM_CMD_SNA:
state = ASSM_STATE_ERROR; // keep ERROR state as fundamental STATE .. ;-)
break;
// process the COMMAND PING
case ASSM_CMD_PING:
if( _state == ASSM_STATE_ERROR ) {
state = ASSM_STATE_IDLNG;
} else {
state = _state; // keep same STATE ..
}// if
writeCommand( ASSM_CMD_PONG ); // ALWAYS answer a PING with a PONG
break;
// process the COMMAND PONG
case ASSM_CMD_PONG:
if( _state == ASSM_STATE_ERROR ) {
state = ASSM_STATE_IDLNG;
} else {
state = _state; // keep same STATE ..
}// if
writeCommand( ASSM_CMD_PING ); // ALWAYS answer a PONG with a PING
break;
// process the COMMAND ACKNOWLEDGE; not really something to do here ..
case ASSM_CMD_AKNW:
state = _state;
break;
// process the COMMAND RUN; now some working task should be processed
case ASSM_CMD_RUN:
if( _state == ASSM_STATE_MODE1 ) {
state = _state; // keep same STATE ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE2 ) {
state = _state; // keep same STATE ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE3 ) {
state = _state; // keep same STATE ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE4 ) {
state = _state; // keep same STATE ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE5 ) {
state = _state; // keep same STATE ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE6 ) {
state = _state; // keep same STATE ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE7 ) {
state = _state; // keep same STATE ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else {
state = _state; // keep same STATE ..
} // if
break;
// process the COMMAND STOP; leave some working task and return to IDLE
case ASSM_CMD_STOP:
if( _state == ASSM_STATE_MODE1 ) {
state = ASSM_STATE_IDLNG;
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE2 ) {
state = ASSM_STATE_IDLNG;
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE3 ) {
state = ASSM_STATE_IDLNG;
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE4 ) {
state = ASSM_STATE_IDLNG;
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE5 ) {
state = ASSM_STATE_IDLNG;
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE6 ) {
state = ASSM_STATE_IDLNG;
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else if( _state == ASSM_STATE_MODE7 ) {
state = ASSM_STATE_IDLNG;
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else {
state = _state; // keep same STATE ..
}// if
break;
// process the COMMAND WAIT; may be stop some processing task ..
case ASSM_CMD_WAIT:
state = _state; // keep same STATE ..
break;
// process the COMMAND EVNT; may be do something while processing ..
case ASSM_CMD_EVNT:
state = _state; // keep same STATE ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
break;
// process the COMMAND STAT
case ASSM_CMD_STAT:
state = _state; // keep same STATE ..
writeState( _state ); // ALWAYS tell about the current STATE
break;
// process the COMMAND run MODEs
// process COMMAND run MODE 1
case ASSM_CMD_RMD1:
if(_state != ASSM_STATE_ERROR ) {
state = ASSM_STATE_MODE1; // only if we are not in ERROR ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else {
state = _state; // keep same STATE ..
}// if
break;
// process COMMAND run MODE 2
case ASSM_CMD_RMD2:
if(_state != ASSM_STATE_ERROR ) {
state = ASSM_STATE_MODE2; // only if we are not in ERROR ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else {
state = _state; // keep same STATE ..
}// if
break;
// process COMMAND run MODE 3
case ASSM_CMD_RMD3:
if(_state != ASSM_STATE_ERROR ) {
state = ASSM_STATE_MODE3; // only if we are not in ERROR ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else {
state = _state; // keep same STATE ..
}// if
break;
// process COMMAND run MODE 4
case ASSM_CMD_RMD4:
if(_state != ASSM_STATE_ERROR ) {
state = ASSM_STATE_MODE4; // only if we are not in ERROR ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else {
state = _state; // keep same STATE ..
}// if
break;
// process COMMAND run MODE 5
case ASSM_CMD_RMD5:
if(_state != ASSM_STATE_ERROR ) {
state = ASSM_STATE_MODE5; // only if we are not in ERROR ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else {
state = _state; // keep same STATE ..
}// if
break;
// process COMMAND run MODE 6
case ASSM_CMD_RMD6:
if(_state != ASSM_STATE_ERROR ) {
state = ASSM_STATE_MODE6; // only if we are not in ERROR ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else {
state = _state; // keep same STATE ..
}// if
break;
// process COMMAND run MODE 7
case ASSM_CMD_RMD7:
if(_state != ASSM_STATE_ERROR ) {
state = ASSM_STATE_MODE7; // only if we are not in ERROR ..
writeCommand( ASSM_CMD_AKNW ); // answer with a ACKNOWLEDGE
} else {
state = _state; // keep same STATE ..
}// if
break;
// process the COMMAND CNCT
case ASSM_CMD_CNCT: // obviously useless
// may be one want to CONNECT and DISCONNECT while IDLE / RUNNING ..
state = _state; // keep same STATE ..
break;
// process the COMMAND DCNT
case ASSM_CMD_DCNT: // obviously useless
// may be one want to CONNECT and DISCONNECT while IDLE / RUNNING ..
state = _state; // keep same STATE ..
break;
// process no (NULL) or an unknown COMMAND
default: // NULL command
state = _state; // keep same STATE ..
break;
} // switch
return state; // hand back next STATE by processed COMMAND
} // method
uint8_t ASSM::process_state( uint8_t state ) {
_state = state; // copy to internal member variable
uint8_t next_command = ASSM_CMD_NULL; // return no command
switch( _state ) {
case ASSM_STATE_ERROR: // SNA - State Not Available
next_command = ASSM_CMD_NULL;
if( ASSM_LED_ACTV ) {
led_on( ); // switch STATE indicator permanently on ..
} // if
next_command = error( _command );
break;
case ASSM_STATE_IDLNG: // IDLE around, and around, and arountthe world
next_command = ASSM_CMD_NULL;
if( ASSM_LED_ACTV ) {
led_heartBeat( 100 ); // 60 bpm heartbeat; 200 blink + 800 wait
} // if
next_command = idle( _command );
break;
case ASSM_STATE_MODE1: // run MODE 1
next_command = ASSM_CMD_NULL;
if( ASSM_LED_ACTV ) {
led_blink( 1 ); // let flash 20 milliseonds as load indicator ..
} // if
next_command = runMODE1( _command );
break;
case ASSM_STATE_MODE2: // run MODE 2
next_command = ASSM_CMD_NULL;
if( ASSM_LED_ACTV ) {
led_blink( 1 ); // let flash 20 milliseonds as load indicator ..
} // if
next_command = runMODE2( _command );
break;
case ASSM_STATE_MODE3: // run MODE 3
next_command = ASSM_CMD_NULL;
if( ASSM_LED_ACTV ) {
led_blink( 1 ); // let flash 20 milliseonds as load indicator ..
} // if
next_command = runMODE3( _command );
break;
case ASSM_STATE_MODE4: // run MODE 4
next_command = ASSM_CMD_NULL;
if( ASSM_LED_ACTV ) {
led_blink( 1 ); // let flash 20 milliseonds as load indicator ..
} // if
next_command = runMODE4( _command );
break;
case ASSM_STATE_MODE5: // run MODE 5
next_command = ASSM_CMD_NULL;
if( ASSM_LED_ACTV ) {
led_blink( 1 ); // let flash 20 milliseonds as load indicator ..
} // if
next_command = runMODE5( _command );
break;
case ASSM_STATE_MODE6: // run MODE 6
next_command = ASSM_CMD_NULL;
if( ASSM_LED_ACTV ) {
led_blink( 1 ); // let flash 20 milliseonds as load indicator ..
} // if
next_command = runMODE6( _command );
break;
case ASSM_STATE_MODE7: // run MODE 7
next_command = ASSM_CMD_NULL;
if( ASSM_LED_ACTV ) {
led_blink( 1 ); // let flash 20 milliseonds as load indicator ..
} // if
next_command = runMODE7( _command );
break;
default:
next_command = ASSM_CMD_NULL;
break;
} // switch
return next_command;
} // method
void ASSM::led_on( ) {
digitalWrite( ASSM_LED_PIN, HIGH ); // sets the digital pin LED on
} // method
void ASSM::led_off( ) {
digitalWrite( ASSM_LED_PIN, LOW ); // sets the digital pin LED on
} // method
void ASSM::led_blink( int duration ) {
if( duration < 1 ) {
duration = 1;
} // if
led_on( ); // sets the digital pin LED on
delay( duration ); // waits for a moment in ECU time
led_off( ); // sets the digital pin LED off
delay( duration ); // waits for a moment in ECU time
} // method
void ASSM::led_heartBeat( int interval ) {
if( interval < 100 ) {
interval = 100; // arduino on steroids; 200 ms equals 300 bpm ..
} // if
led_blink( 25 );
delay( 50 );
led_blink( 25 );
delay( interval );
} // method
int ASSM::a2i(const char *s)
{
int sign=1;
if(*s == '-'){
sign = -1;
s++;
} // if
int num=0;
while(*s){
num=((*s)-'0')+num*10;
s++;
} // loop
return num*sign;
} // method
// here you can add your code or
// overload the class and next the methods
uint8_t ASSM::error( uint8_t command ) {
uint8_t next_command = ASSM_CMD_NULL; // in general KEEP this STATE
// TODO Place your code for ERROR STATE or EXTEND CLASS and OVERLOAD method
return next_command;
} // method
uint8_t ASSM::idle( uint8_t command ) {
uint8_t next_command = ASSM_CMD_NULL; // in general KEEP this STATE
// TODO Place your code for IDLE STATE or EXTEND CLASS and OVERLOAD method
return next_command;
} // method
uint8_t ASSM::runMODE1( uint8_t command ) {
uint8_t next_command = ASSM_CMD_NULL; // in general KEEP this STATE
// TODO your code for run MODE 1 STATE or EXTEND CLASS and OVERLOAD method
return next_command;
} // method
uint8_t ASSM::runMODE2( uint8_t command ) {
uint8_t next_command = ASSM_CMD_NULL; // in general KEEP this STATE
// TODO your code for run MODE 2 STATE or EXTEND CLASS and OVERLOAD method
return next_command;
} // method
uint8_t ASSM::runMODE3( uint8_t command ) {
uint8_t next_command = ASSM_CMD_NULL; // in general KEEP this STATE
// TODO your code for run MODE 3 STATE or EXTEND CLASS and OVERLOAD method
return next_command;
} // method
uint8_t ASSM::runMODE4( uint8_t command ) {
uint8_t next_command = ASSM_CMD_NULL; // in general KEEP this STATE
// TODO your code for run MODE 4 STATE or EXTEND CLASS and OVERLOAD method
return next_command;
} // method
uint8_t ASSM::runMODE5( uint8_t command ) {
uint8_t next_command = ASSM_CMD_NULL; // in general KEEP this STATE
// TODO your code for run MODE 5 STATE or EXTEND CLASS and OVERLOAD method
return next_command;
} // method
uint8_t ASSM::runMODE6( uint8_t command ) {
uint8_t next_command = ASSM_CMD_NULL; // in general KEEP this STATE
// TODO your code for run MODE 6 STATE or EXTEND CLASS and OVERLOAD method
return next_command;
} // method
uint8_t ASSM::runMODE7( uint8_t command ) {
uint8_t next_command = ASSM_CMD_NULL; // in general KEEP this STATE
// TODO Replace code for eun MODE7 or EXTEND CLASS and OVERLOAD method
/// some easy example for a fake sensor / data Processing
if( ASSM_DEBUG_SHOW_RUNNING_INTERNAL ) {
display( "RNMD 7 ->" );
delay(ASSM_DEBUG_DISPLAY_SHOW);
display( _helper->state_to_String( _state ) );
delay(ASSM_DEBUG_DISPLAY_SHOW);
display( _helper->command_to_String( _command ) );
delay(ASSM_DEBUG_DISPLAY_SHOW);
display( "" );
delay(ASSM_DEBUG_DISPLAY_BLANK);
} // if
// if arduino is in runnig state and client sends
// EVNT as command, arduino responds with three
// WAIT commands to simulate a sensor requesting
if( command == ASSM_CMD_EVNT ) {
int cnt = 0;
while( cnt < 3 ) {
// write a <WAIT> command
writeCommand( ASSM_CMD_WAIT );
cnt++;
delay( 1000 ); // wait a second
} // loop
// afterwards arduino responds with an UNIQUE
// command, writes the data back to the client by:
// <DATA>1;2;3;4;5;6;7;8;9;0</DATA>
writeData( "DATA", "1.2;3.4;5.6" );
// at the end arduino sends a DONE command
writeCommand( ASSM_CMD_DONE );
} // if
return next_command;
} // method