forked from Hamhackin/espBode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp_fy6900.cpp
195 lines (164 loc) · 6.32 KB
/
esp_fy6900.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
#include "esp_config.h"
#if AWG == FY6900
#warning Compiling for FY6900
#include "esp_fy6900.h"
volatile SDeviceState gDeviceState;
bool writeCommandToSerial(char* data, uint8_t len)
{
uint32_t timeout = 0;
// debug output
String dataStr(data);
DEBUG("[\n" + dataStr + "]");
// write command
Serial.write((uint8_t*)data, len);
// wait for device acknowledge response
while(!Serial.available())
{
delay(1);
if(timeout++>1000) return false;
}
bool ok = false;
ok = (Serial.read() == 0x0a); // 0x0a == \n
if(!ok){
DEBUG("Invalid response for command");
}
return ok;
}
bool sendCommand(const char* cmdPattern, uint32_t param1, uint32_t param2)
{
size_t cmdLen = snprintf(NULL, 0, cmdPattern, param1, param2);
char* command = (char*) malloc(cmdLen+1); //reserve 1 additional byte for snpritf 0 termination character
snprintf(command, cmdLen+1, cmdPattern, param1, param2);
bool ok = writeCommandToSerial(command, cmdLen);
free(command);
return ok;
}
bool sendCommand(const char* cmdPattern, uint32_t param1)
{
// this kind of cmdPatterns must not contain exactly 1 place holder!
// todo: add a check (search for exactly 1 '%')
return sendCommand(cmdPattern, param1, 0);
}
bool sendCommand(const char* cmdPattern)
{
// this kind of cmdPatterns must not contain place holders!
// todo: add a check (search for unexpected '%')
return sendCommand(cmdPattern, 0, 0);
}
/*
>>> before the refactoring of send commands:
Executable segment sizes:
IROM : 251124 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 27324 / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs...)
DATA : 1252 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 1372 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 25248 ) - zeroed variables (global, static) in RAM/HEAP
Sketch uses 281072 bytes (29%) of program storage space. Maximum is 958448 bytes.
Global variables use 27872 bytes (34%) of dynamic memory, leaving 54048 bytes for local variables. Maximum is 81920 bytes.
>>> after the refactoring of send commands:
Executable segment sizes:
IROM : 250900 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 27324 / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs...)
DATA : 1252 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 1256 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 25240 ) - zeroed variables (global, static) in RAM/HEAP
Sketch uses 280732 bytes (29%) of program storage space. Maximum is 958448 bytes.
Global variables use 27748 bytes (33%) of dynamic memory, leaving 54172 bytes for local variables. Maximum is 81920 bytes.
Executable segment sizes:
IROM : 250900 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 27324 / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs...)
DATA : 1252 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 1232 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 25248 ) - zeroed variables (global, static) in RAM/HEAP
Sketch uses 280708 bytes (29%) of program storage space. Maximum is 958448 bytes.
Global variables use 27732 bytes (33%) of dynamic memory, leaving 54188 bytes for local variables. Maximum is 81920 bytes.
Using library ESP8266WiFi at version 1.0 in folder: C:\Users\Steffen\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266WiFi
Using library ESP Telnet at version 2.0.0 in folder: C:\Users\Steffen\OneDrive\Documents\Arduino\libraries\ESP_Telnet
Using library ESP8266WebServer at version 1.0 in folder: C:\Users\Steffen\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266WebServer
*/
bool setCh1Wave(EWaveType wave)
{
gDeviceState.ch1Wave = wave;
return sendCommand("WMW%02u\n", wave);
}
bool setCh2Wave(EWaveType wave)
{
gDeviceState.ch2Wave = wave;
return sendCommand("WFW%02u\n", wave);
}
bool setCh1Output(uint32_t output)
{
gDeviceState.ch1Output = output;
return sendCommand(output ? "WMN1\n" : "WMN0\n");
}
bool setCh2Output(uint32_t output)
{
gDeviceState.ch2Output = output;
return sendCommand(output ? "WFN1\n" : "WFN0\n");
}
/* Set frequency in Hz */
bool setCh1Freq(uint32_t frequency)
{
gDeviceState.ch1Freq = frequency;
// todo: check if the FY6900-100Mhz version has a frquency digit more and if so if it is backward compatible with the 14 digit commands
return sendCommand("WMF%08u000000\n", frequency);
}
/* Set frequency in Hz */
bool setCh2Freq(uint32_t frequency)
{
gDeviceState.ch2Freq = frequency;
// todo: check if the FY6900-100Mhz version has a frequency digit more and if so if it is backward compatible with the 14 digit commands
return sendCommand("WFF%08u000000\n", frequency);
}
/* Ampl is in mV: 12.345V = 12345 */
bool setCh1Ampl(uint32_t ampl)
{
gDeviceState.ch1Ampl = ampl;
return sendCommand("WMA%02u.%03u\n", ampl/1000, ampl%1000);
}
bool setCh2Ampl(uint32_t ampl)
{
gDeviceState.ch2Ampl = ampl;
return sendCommand("WFA%02u.%03u\n", ampl/1000, ampl%1000);
}
/* Phase is in 0.1deg: 12.5deg = 125 */
bool setCh1Phase(uint32_t phase)
{
gDeviceState.ch1Phase = phase;
return sendCommand("WMP%03u.%03u\n", phase/1000, (phase%1000)/100);
}
bool setCh2Phase(uint32_t phase)
{
gDeviceState.ch2Phase = phase;
return sendCommand("WFP%03u.%03u\n", phase/1000, (phase%1000)/100);
}
bool setCh1Offset(int32_t offset)
{
gDeviceState.ch1Offset = offset;
return (offset>=0)
? sendCommand("WMO%02u.%02u\n", offset/1000, offset%1000)
: sendCommand("WMO-%02u.%02u\n", -offset/1000, -offset%1000);
}
bool setCh2Offset(int32_t offset)
{
gDeviceState.ch2Offset = offset;
return (offset>=0)
? sendCommand("WFO%02u.%02u\n", offset/1000, offset%1000)
: sendCommand("WFO-%02u.%02u\n", -offset/1000, -offset%1000);
}
bool initDevice(void)
{
Serial.write((uint8_t*)"\n", 1);
return
setCh1Output(0)
&& setCh1Wave(EWaveType_Sine)
&& setCh1Freq(1000)
&& setCh1Ampl(1000)
&& setCh1Offset(0)
&& setCh2Output(0)
&& setCh2Wave(EWaveType_Sine)
&& setCh2Freq(1000)
&& setCh2Ampl(1000)
&& setCh2Offset(0);
}
#endif