-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSWCommand.cpp
84 lines (68 loc) · 1.56 KB
/
SWCommand.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
#include <Arduino.h>
#include "SWCommand.h"
SWCommand::SWCommand()
{
_done = false;
_type = SWCommandTypeNone;
_index = 0;
}
bool SWCommand::available()
{
if ( Serial.available() > 0 )
{
char chr = (char)Serial.read();
if ( chr == '\n' )
{
_done = _parse();
_index = 0;
if ( !_done ) Serial.println( "Syntax Error" );
else if ( _type == SWCommandTypeSet ) print( _value );
return true; // ja ho tenim tot tornem que vale
}
else if ( _index < SWCommandBuffLength )
{
_buff[_index++] = chr;
_buff[_index] = '\0';
}
}
return false; // no ho tenim encara
}
void SWCommand::print( long value )
{
Serial.print( _var );
Serial.print( " = " );
Serial.println( value );
}
bool SWCommand::_parse()
{
// inicialització
int i = 0;
char ch = '\0'; ;
// saltem espais
while ( _buff[i] == ' ' ) i++;
// determinem la variable
if ( isalpha( _buff[i] ) )
_var = _buff[i++];
else
return false;
#ifdef SWCOMMAND_USESTRING
while ( isalnum( _buff[i]) )
_var.concat( _buff[i++] );
#endif
// saltem espais
while ( _buff[i] == ' ' ) i++;
// busquem operador '=' i determinem la comanda
_type = SWCommandTypeGet;
if ( _buff[i] == '=' ) _type = SWCommandTypeSet, i++;
// determinarem el valor si cal
if ( _type == SWCommandTypeSet )
{
char *endPtr;
_value = strtol( _buff+i, &endPtr, 0 );
i = endPtr - _buff;
}
// saltem espais
while ( _buff[i] == ' ' ) i++;
// nomes tornem amb exit si estem al final
return ( _buff[i] == '\0' );
}