Browse Source

Complexity of the cc measurement function reduced by using an early return.

klaute 2 years ago
parent
commit
d12c75fa00
1 changed files with 72 additions and 74 deletions
  1. 72
    74
      firmware/command_ctrl.ino

+ 72
- 74
firmware/command_ctrl.ino View File

@@ -188,97 +188,95 @@ void cc_startMeasurement()
188 188
   //    MSG_TYPE_CONFIG
189 189
   cc_getConfig();
190 190
 
191
+  if (start_freq == 0 || start_freq > 150000000 ||
192
+      end_freq   == 0 || end_freq   > 150000000 ||
193
+      step_freq  == 0 || step_freq  > 150000000 ||
194
+      start_freq >= end_freq ||
195
+      intervall  == 0)
196
+  {
197
+    // on error
198
+
199
+    sendSOM();
200
+    Serial.write(MSG_TYPE_ANSWER_NOK);
201
+    sendEOM();
202
+
203
+    return;
204
+  }
205
+
191 206
   // 2. start a for loop from the frequence to start to the end frequence
192
-  if (start_freq > 0 && start_freq <= 150000000 &&
193
-      end_freq > 0 && end_freq <= 150000000 &&
194
-      start_freq < end_freq &&
195
-      intervall > 0 &&
196
-      step_freq > 0)
207
+  si5351.drive_strength(SI5351_CLK0, drive_str); // 2 4 6 8ma
208
+
209
+  uint8_t t = 0;
210
+  for (t = 0; t < 100; t++)
197 211
   {
198
-    si5351.drive_strength(SI5351_CLK0, drive_str); // 2 4 6 8ma
212
+    uint16_t tmp = analogRead(A0);
213
+             tmp = analogRead(A1);
214
+  }
199 215
 
200
-    uint8_t t = 0;
201
-    for (t = 0; t < 100; t++)
216
+  uint32_t freq = 0;
217
+  for (freq = start_freq; freq <= end_freq; freq += step_freq)
218
+  {
219
+    if (freq > end_freq)
202 220
     {
203
-      uint16_t tmp = analogRead(A0);
204
-               tmp = analogRead(A1);
221
+      // prevent to step over the end frequency
222
+      // maybe the user is not allowed to send data in the frequency band
223
+      freq = end_freq;
205 224
     }
225
+    uint32_t a0_sum  = 0;
226
+    uint32_t a1_sum  = 0;
227
+    uint16_t i = 0;
206 228
 
207
-    uint32_t freq = 0;
208
-    for (freq = start_freq; freq <= end_freq; freq += step_freq)
229
+    si5351.set_freq((uint64_t)freq * 100, SI5351_PLL_FIXED, SI5351_CLK0);
230
+    si5351.output_enable(SI5351_CLK0, 1); // enable clock output 0
231
+    delay(1);
232
+
233
+    for (i = 0; i < intervall; i++)
209 234
     {
210
-      if (freq > end_freq)
235
+      // 3. on every loop read the analog input A0 and A1 for the in intervall (milliseconds)
236
+      //    and generate the average value, read the ADC value every milli second.
237
+      uint8_t t = 0;
238
+      uint16_t ta0 = 0;
239
+      uint16_t ta1 = 0;
240
+      for (t = 0; t < MEAS_LOOP_CNT; t++)
211 241
       {
212
-        // prevent to step over the end frequency
213
-        // maybe the user is not allowed to send data in the frequency band
214
-        freq = end_freq;
242
+        ta0 += analogRead(A0);
243
+        ta1 += analogRead(A1);
215 244
       }
216
-      uint32_t a0_sum  = 0;
217
-      uint32_t a1_sum  = 0;
218
-      uint16_t i = 0;
245
+      a0_sum += (ta0 / MEAS_LOOP_CNT);
246
+      a1_sum += (ta1 / MEAS_LOOP_CNT);
219 247
 
220
-      si5351.set_freq((uint64_t)freq * 100, SI5351_PLL_FIXED, SI5351_CLK0);
221
-      si5351.output_enable(SI5351_CLK0, 1); // enable clock output 0
222 248
       delay(1);
249
+    }
223 250
 
224
-      for (i = 0; i < intervall; i++)
225
-      {
226
-        // 3. on every loop read the analog input A0 and A1 for the in intervall (milliseconds)
227
-        //    and generate the average value, read the ADC value every milli second.
228
-        uint8_t t = 0;
229
-        uint16_t ta0 = 0;
230
-        uint16_t ta1 = 0;
231
-        for (t = 0; t < MEAS_LOOP_CNT; t++)
232
-        {
233
-          ta0 += analogRead(A0);
234
-          ta1 += analogRead(A1);
235
-        }
236
-        a0_sum += (ta0 / MEAS_LOOP_CNT);
237
-        a1_sum += (ta1 / MEAS_LOOP_CNT);
238
-
239
-        delay(1);
240
-      }
251
+    a0_sum = a0_sum / intervall;
252
+    a1_sum = a1_sum / intervall;
241 253
 
242
-      a0_sum = a0_sum / intervall;
243
-      a1_sum = a1_sum / intervall;
244
-
245
-      // 4. send the current output frequency, the drive strength and the measured ADC values from A0 and A1 to the host
246
-      //    MSG_TYPE_MEAS_FREQ_INFO
247
-      sendSOM();
248
-      Serial.write(MSG_TYPE_MEAS_FREQ_INFO);
249
-      Serial.write((uint8_t)((freq & 0xff000000) >> 24));
250
-      Serial.write((uint8_t)((freq & 0x00ff0000) >> 16));
251
-      Serial.write((uint8_t)((freq & 0x0000ff00) >>  8));
252
-      Serial.write((uint8_t) (freq & 0x000000ff));
253
-      Serial.write((uint8_t)((a0_sum & 0xff00) >> 8));
254
-      Serial.write((uint8_t) (a0_sum & 0x00ff));
255
-      Serial.write((uint8_t)((a1_sum & 0xff00) >> 8));
256
-      Serial.write((uint8_t) (a1_sum & 0x00ff));
257
-      sendEOM();
258
-
259
-      si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
260
-
261
-      if (freq >= end_freq)
262
-        break; // abort the loop because all is done
263
-    }
254
+    // 4. send the current output frequency, the drive strength and the measured ADC values from A0 and A1 to the host
255
+    //    MSG_TYPE_MEAS_FREQ_INFO
256
+    sendSOM();
257
+    Serial.write(MSG_TYPE_MEAS_FREQ_INFO);
258
+    Serial.write((uint8_t)((freq & 0xff000000) >> 24));
259
+    Serial.write((uint8_t)((freq & 0x00ff0000) >> 16));
260
+    Serial.write((uint8_t)((freq & 0x0000ff00) >>  8));
261
+    Serial.write((uint8_t) (freq & 0x000000ff));
262
+    Serial.write((uint8_t)((a0_sum & 0xff00) >> 8));
263
+    Serial.write((uint8_t) (a0_sum & 0x00ff));
264
+    Serial.write((uint8_t)((a1_sum & 0xff00) >> 8));
265
+    Serial.write((uint8_t) (a1_sum & 0x00ff));
266
+    sendEOM();
264 267
 
265
-    // 5. send a measurement end message to the host
266
-    //    MSG_TYPE_MEAS_END_INFO
267
-    char* tmp = "     ";
268
-    sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2,
269
-                               MSG_TYPE_MEAS_END_INFO,
270
-                               MSG_EOM1, MSG_EOM2);
271
-    Serial.write(tmp);
268
+    si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
272 269
 
273
-  } else {
274
-    // on error
275
-    char* tmp = "     ";
276
-    sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2,
277
-                               MSG_TYPE_ANSWER_NOK,
278
-                               MSG_EOM1, MSG_EOM2);
279
-    Serial.write(tmp);
270
+    if (freq >= end_freq)
271
+      break; // abort the loop because all is done
280 272
   }
281 273
 
274
+  // 5. send a measurement end message to the host
275
+  //    MSG_TYPE_MEAS_END_INFO
276
+  sendSOM();
277
+  Serial.write(MSG_TYPE_MEAS_END_INFO);
278
+  sendEOM();
279
+
282 280
   si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
283 281
 
284 282
 }