|
5 | 5 | void processATCommand(Stream &stream)
|
6 | 6 | {
|
7 | 7 | String command = stream.readStringUntil('\n');
|
| 8 | + String response; |
8 | 9 |
|
9 |
| - if (command.startsWith("AT+NUMPHASES=")) |
| 10 | + if (command.startsWith("AT+")) |
10 | 11 | {
|
11 |
| - ctx.config.numPhases = command.substring(13).toInt(); |
12 |
| - stream.println("OK"); |
13 |
| - } |
14 |
| - else if (command == "AT+NUMPHASES?") |
15 |
| - { |
16 |
| - stream.println(String("+NUMPHASES: ") + ctx.config.numPhases); |
17 |
| - } |
18 |
| - else if (command.startsWith("AT+SIGNALFREQ=")) |
19 |
| - { |
20 |
| - ctx.config.signalFrequency = command.substring(14).toInt(); |
21 |
| - stream.println("OK"); |
22 |
| - } |
23 |
| - else if (command == "AT+SIGNALFREQ?") |
24 |
| - { |
25 |
| - stream.println(String("+SIGNALFREQ: ") + ctx.config.signalFrequency); |
26 |
| - } |
27 |
| - else if (command.startsWith("AT+SAMPLINGFREQ=")) |
28 |
| - { |
29 |
| - ctx.config.samplingFrequency = command.substring(16).toInt(); |
30 |
| - stream.println("OK"); |
31 |
| - } |
32 |
| - else if (command == "AT+SAMPLINGFREQ?") |
33 |
| - { |
34 |
| - stream.println(String("+SAMPLINGFREQ: ") + ctx.config.samplingFrequency); |
35 |
| - } |
36 |
| - else if (command.startsWith("AT+AMPLITUDE=")) |
37 |
| - { |
38 |
| - ctx.config.amplitude = command.substring(13).toInt(); |
39 |
| - stream.println("OK"); |
40 |
| - } |
41 |
| - else if (command == "AT+AMPLITUDE?") |
42 |
| - { |
43 |
| - stream.println(String("+AMPLITUDE: ") + ctx.config.amplitude); |
44 |
| - } |
45 |
| - else if (command.startsWith("AT+OFFSET=")) |
46 |
| - { |
47 |
| - ctx.config.offset = command.substring(10).toInt(); |
48 |
| - stream.println("OK"); |
49 |
| - } |
50 |
| - else if (command == "AT+OFFSET?") |
51 |
| - { |
52 |
| - stream.println(String("+OFFSET: ") + ctx.config.offset); |
53 |
| - } |
54 |
| - else if (command.startsWith("AT+ERRORPERCENT=")) |
55 |
| - { |
56 |
| - ctx.config.errorPercentage = command.substring(16).toFloat(); |
57 |
| - stream.println("OK"); |
58 |
| - } |
59 |
| - else if (command == "AT+ERRORPERCENT?") |
60 |
| - { |
61 |
| - stream.println(String("+ERRORPERCENT: ") + ctx.config.errorPercentage); |
62 |
| - } |
63 |
| - else if (command == "AT+ALL?") |
64 |
| - { |
65 |
| - stream.println(String("+NUMPHASES: ") + ctx.config.numPhases); |
66 |
| - stream.println(String("+SIGNALFREQ: ") + ctx.config.signalFrequency); |
67 |
| - stream.println(String("+SAMPLINGFREQ: ") + ctx.config.samplingFrequency); |
68 |
| - stream.println(String("+AMPLITUDE: ") + ctx.config.amplitude); |
69 |
| - stream.println(String("+OFFSET: ") + ctx.config.offset); |
70 |
| - stream.println(String("+ERRORPERCENT: ") + ctx.config.errorPercentage); |
71 |
| - stream.println(String("+POINTSPERWAVE: ") + ctx.config.getPointsPerWave()); |
72 |
| - stream.println(String("+SAMPLINGINTERVAL: ") + ctx.config.getSamplingInterval()); |
| 12 | + // Extract the command and optional argument |
| 13 | + int separatorIndex = command.indexOf('='); |
| 14 | + String cmdKey = separatorIndex > 0 ? command.substring(0, separatorIndex) : command; |
| 15 | + String cmdValue = separatorIndex > 0 ? command.substring(separatorIndex + 1) : ""; |
| 16 | + |
| 17 | + // Process commands using a switch-case equivalent |
| 18 | + if (cmdKey == "AT+NUMPHASES") |
| 19 | + { |
| 20 | + int numPhases = cmdValue.toInt(); |
| 21 | + if (numPhases <= 0) |
| 22 | + { |
| 23 | + response = "ERROR: NUMPHASES must be positive."; |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + ctx.config.numPhases = numPhases; |
| 28 | + response = "OK"; |
| 29 | + } |
| 30 | + } |
| 31 | + else if (cmdKey == "AT+SIGNALFREQ") |
| 32 | + { |
| 33 | + int signalFreq = cmdValue.toInt(); |
| 34 | + if (signalFreq <= 0) |
| 35 | + { |
| 36 | + response = "ERROR: SIGNALFREQ must be positive."; |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + ctx.config.signalFrequency = signalFreq; |
| 41 | + response = "OK"; |
| 42 | + } |
| 43 | + } |
| 44 | + else if (cmdKey == "AT+SAMPLINGFREQ") |
| 45 | + { |
| 46 | + int samplingFreq = cmdValue.toInt(); |
| 47 | + if (samplingFreq <= 0) |
| 48 | + { |
| 49 | + response = "ERROR: SAMPLINGFREQ must be positive."; |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + ctx.config.samplingFrequency = samplingFreq; |
| 54 | + response = "OK"; |
| 55 | + } |
| 56 | + } |
| 57 | + else if (cmdKey == "AT+AMPLITUDE") |
| 58 | + { |
| 59 | + int amplitude = cmdValue.toInt(); |
| 60 | + if (amplitude < 0) |
| 61 | + { |
| 62 | + response = "ERROR: AMPLITUDE must be zero or positive."; |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + ctx.config.amplitude = amplitude; |
| 67 | + response = "OK"; |
| 68 | + } |
| 69 | + } |
| 70 | + else if (cmdKey == "AT+OFFSET") |
| 71 | + { |
| 72 | + int offset = cmdValue.toInt(); |
| 73 | + if (offset < -10000 || offset > 10000) |
| 74 | + { |
| 75 | + response = "ERROR: OFFSET must be between -10000 and 10000."; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + ctx.config.offset = offset; |
| 80 | + response = "OK"; |
| 81 | + } |
| 82 | + } |
| 83 | + else if (cmdKey == "AT+ERRORPERCENT") |
| 84 | + { |
| 85 | + float errorPercent = cmdValue.toFloat(); |
| 86 | + if (errorPercent < 0.0 || errorPercent > 100.0) |
| 87 | + { |
| 88 | + response = "ERROR: ERRORPERCENT must be between 0 and 100."; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + ctx.config.errorPercentage = errorPercent; |
| 93 | + response = "OK"; |
| 94 | + } |
| 95 | + } |
| 96 | + else if (cmdKey == "AT+NUMPHASES?") |
| 97 | + { |
| 98 | + response = String("+NUMPHASES: ") + ctx.config.numPhases; |
| 99 | + } |
| 100 | + else if (cmdKey == "AT+SIGNALFREQ?") |
| 101 | + { |
| 102 | + response = String("+SIGNALFREQ: ") + ctx.config.signalFrequency; |
| 103 | + } |
| 104 | + else if (cmdKey == "AT+SAMPLINGFREQ?") |
| 105 | + { |
| 106 | + response = String("+SAMPLINGFREQ: ") + ctx.config.samplingFrequency; |
| 107 | + } |
| 108 | + else if (cmdKey == "AT+AMPLITUDE?") |
| 109 | + { |
| 110 | + response = String("+AMPLITUDE: ") + ctx.config.amplitude; |
| 111 | + } |
| 112 | + else if (cmdKey == "AT+OFFSET?") |
| 113 | + { |
| 114 | + response = String("+OFFSET: ") + ctx.config.offset; |
| 115 | + } |
| 116 | + else if (cmdKey == "AT+ERRORPERCENT?") |
| 117 | + { |
| 118 | + response = String("+ERRORPERCENT: ") + ctx.config.errorPercentage; |
| 119 | + } |
| 120 | + else if (cmdKey == "AT+ALL?") |
| 121 | + { |
| 122 | + response = String("+NUMPHASES: ") + ctx.config.numPhases + "\n" + |
| 123 | + String("+SIGNALFREQ: ") + ctx.config.signalFrequency + "\n" + |
| 124 | + String("+SAMPLINGFREQ: ") + ctx.config.samplingFrequency + "\n" + |
| 125 | + String("+AMPLITUDE: ") + ctx.config.amplitude + "\n" + |
| 126 | + String("+OFFSET: ") + ctx.config.offset + "\n" + |
| 127 | + String("+ERRORPERCENT: ") + ctx.config.errorPercentage + "\n" + |
| 128 | + String("+POINTSPERWAVE: ") + ctx.config.getPointsPerWave() + "\n" + |
| 129 | + String("+SAMPLINGINTERVAL: ") + ctx.config.getSamplingInterval(); |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + response = "ERROR: Unknown command."; |
| 134 | + } |
73 | 135 | }
|
74 | 136 | else
|
75 | 137 | {
|
76 |
| - stream.println("ERROR"); |
| 138 | + response = "ERROR: Invalid AT command format."; |
77 | 139 | }
|
| 140 | + |
| 141 | + // Send the response back to the stream |
| 142 | + stream.println(response); |
78 | 143 | }
|
0 commit comments