-
Notifications
You must be signed in to change notification settings - Fork 17
/
esp_parser.cpp
138 lines (121 loc) · 3.22 KB
/
esp_parser.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
#include "esp_parser.h"
#include "esp_fy6800.h"
volatile char *gReadBuffer = NULL;
/* Function parses number from the msg string expects natural a number. */
static uint32_t parseNumber(char *msg)
{
uint32_t number = 0;
while(0<=(msg[0]-'0') && (msg[0]-'0')<=9)
{
number*=10;
number+=msg[0]-'0';
msg++;
}
return number;
}
/* Similar to parseNumber, but handles also decimal '.' and '-' sign.0
Return value is multiplied *1000 to include the decimal part:
123.345 -> 123456
-1.2 -> -1200 */
static int32_t parseDecimal(char *msg)
{
uint8_t dot = 0;
int32_t multiplier = 1000;
int32_t number = 0;
if(msg[0] == '-')
{
multiplier*=-1;
msg++;
}
while((0<=(msg[0]-'0') && (msg[0]-'0')<=9) || msg[0] == '.')
{
if(msg[0] == '.')
{
dot = 1;
msg++;
continue;
}
if(dot) multiplier/=10;
number*=10;
number+=msg[0]-'0';
msg++;
}
return number*multiplier;
}
void handleWriteMsg(char *msg, uint8_t len)
{
uint8_t selectedChannel = 1;
while(len>0)
{
/* ID request message, we preapare answer in gReadBuffer */
if(0 == strncmp(msg, "IDN-SGLT-PRI?", 13))
{
if(gReadBuffer != NULL) free((void*)gReadBuffer); /* Prevent memory leaks */
gReadBuffer = (char*)malloc(strlen(ID)+1);
strcpy((char*)gReadBuffer, ID);
break;
}
if(0 == strncmp(msg, "C1:", 3))
{
selectedChannel = 1;
msg+=3;
len-=3;
}
if(0 == strncmp(msg, "C2:", 3))
{
selectedChannel = 2;
msg+=3;
len-=3;
}
if(0 == strncmp(msg, "BSWV WVTP,", 10))
{
msg+=10;
len-=10;
if(0 == strncmp(msg, "SINE,",5))
{
msg+=5;
len-=5;
selectedChannel==1?setCh1Wave(EWaveType_Sine):setCh2Wave(EWaveType_Sine);
}
}
if(0 == strncmp(msg, "PHSE,", 5))
{
msg+=5;
len-=5;
selectedChannel==1?setCh1Phase(parseNumber(msg)):setCh2Phase(parseNumber(msg));
}
if(0 == strncmp(msg, "FRQ,", 4))
{
msg+=4;
len-=4;
selectedChannel==1?setCh1Freq(parseNumber(msg)):setCh2Freq(parseNumber(msg));
}
if(0 == strncmp(msg, "AMP,", 4))
{
msg+=4;
len-=4;
selectedChannel==1?setCh1Ampl(parseDecimal(msg)):setCh2Ampl(parseDecimal(msg));
}
if(0 == strncmp(msg, "OFST,", 5))
{
msg+=5;
len-=5;
selectedChannel==1?setCh1Offset(parseDecimal(msg)):setCh2Offset(parseDecimal(msg));
}
if(0 == strncmp(msg, "OUTP ON", 7))
{
msg+=7;
len-=7;
selectedChannel==1?setCh1Output(1):setCh2Output(1);
}
if(0 == strncmp(msg, "OUTP OFF", 8))
{
msg+=8;
len-=8;
selectedChannel==1?setCh1Output(0):setCh2Output(0);
}
/* Unknown string, skip one character */
msg++;
len--;
}
}