Browse Source

Some more duplicat code refactorized / reduced.

klaute 1 year ago
parent
commit
075c570cae
1 changed files with 71 additions and 94 deletions
  1. 71
    94
      firmware/command_ctrl.ino

+ 71
- 94
firmware/command_ctrl.ino View File

@@ -101,82 +101,64 @@ uint8_t cc_cmd_data_read_cnt = 0;
101 101
 
102 102
 /*****************************************************************************/
103 103
 
104
+uint32_t read32BitDataFromBuffer(uint8_t pos)
105
+{
106
+  uint32_t tmp  = (uint32_t)cc_read_data[pos    ] << 24;
107
+           tmp += (uint32_t)cc_read_data[pos + 1] << 16;
108
+           tmp += (uint32_t)cc_read_data[pos + 2] <<  8;
109
+           tmp += (uint32_t)cc_read_data[pos + 3];
110
+  return tmp;
111
+}
112
+
113
+uint16_t read16BitDataFromBuffer(uint8_t pos)
114
+{
115
+  uint16_t tmp  = (uint16_t)cc_read_data[pos    ] << 8;
116
+           tmp += (uint16_t)cc_read_data[pos + 1];
117
+  return tmp;
118
+}
119
+
120
+/*****************************************************************************/
121
+
104 122
 void cc_setStartFreq()
105 123
 {
106
-  uint32_t tmp_start_freq  = (uint32_t)cc_read_data[0] << 24;
107
-           tmp_start_freq += (uint32_t)cc_read_data[1] << 16;
108
-           tmp_start_freq += (uint32_t)cc_read_data[2] <<  8;
109
-           tmp_start_freq += (uint32_t)cc_read_data[3];
110
-  if (tmp_start_freq < 1)
111
-    tmp_start_freq = 1;
112
-  if (tmp_start_freq > 150000000)
113
-    tmp_start_freq = 150000000;
124
+  uint32_t tmp_start_freq = read32BitDataFromBuffer(0);
114 125
 
115
-  start_freq = tmp_start_freq;
126
+  start_freq = keepFreqRange(tmp_start_freq);
116 127
 
117
-  char* tmp = "     ";
118
-  sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2,
119
-                             MSG_TYPE_ANSWER_OK,
120
-                             MSG_EOM1, MSG_EOM2);
121
-  Serial.write(tmp);
128
+  sendSOM();
129
+  Serial.write(MSG_TYPE_ANSWER_OK);
130
+  sendEOM();
122 131
 }
123 132
 
124 133
 void cc_setEndFreq()
125 134
 {
126
-  uint32_t tmp_end_freq  = (uint32_t)cc_read_data[0] << 24;
127
-           tmp_end_freq += (uint32_t)cc_read_data[1] << 16;
128
-           tmp_end_freq += (uint32_t)cc_read_data[2] <<  8;
129
-           tmp_end_freq += (uint32_t)cc_read_data[3];
130
-  if (tmp_end_freq < 1)
131
-    tmp_end_freq = 1;
132
-  if (tmp_end_freq > 150000000)
133
-    tmp_end_freq = 150000000;
135
+  uint32_t tmp_end_freq = read32BitDataFromBuffer(0);
134 136
 
135
-  end_freq = tmp_end_freq;
137
+  end_freq = keepFreqRange(tmp_end_freq);
136 138
 
137
-  char* tmp = "     ";
138
-  sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2,
139
-                             MSG_TYPE_ANSWER_OK,
140
-                             MSG_EOM1, MSG_EOM2);
141
-  Serial.write(tmp);
139
+  sendSOM();
140
+  Serial.write(MSG_TYPE_ANSWER_OK);
141
+  sendEOM();
142 142
 }
143 143
 
144 144
 void cc_setFreqStep()
145 145
 {
146
-  uint32_t tmp_step_freq  = (uint32_t)cc_read_data[0] << 24;
147
-           tmp_step_freq += (uint32_t)cc_read_data[1] << 16;
148
-           tmp_step_freq += (uint32_t)cc_read_data[2] <<  8;
149
-           tmp_step_freq += (uint32_t)cc_read_data[3];
150
-  if (tmp_step_freq < 1)
151
-    tmp_step_freq = 1;
152
-  if (tmp_step_freq > 150000000)
153
-    tmp_step_freq = 150000000;
146
+  uint32_t tmp_step_freq = read32BitDataFromBuffer(0);
154 147
 
155
-  step_freq = tmp_step_freq;
148
+  step_freq = keepFreqRange(tmp_step_freq);
156 149
 
157
-  char* tmp = "     ";
158
-  sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2,
159
-                             MSG_TYPE_ANSWER_OK,
160
-                             MSG_EOM1, MSG_EOM2);
161
-  Serial.write(tmp);
150
+  sendSOM();
151
+  Serial.write(MSG_TYPE_ANSWER_OK);
152
+  sendEOM();
162 153
 }
163 154
 
164 155
 void cc_setIntervall()
165 156
 {
166
-  uint16_t tmp_intervall  = (uint16_t)cc_read_data[0] << 8;
167
-           tmp_intervall += (uint16_t)cc_read_data[1];
168
-  if (tmp_intervall < 1)
169
-    tmp_intervall = 1;
170
-  if (tmp_intervall > 150000000)
171
-    tmp_intervall = 150000000;
157
+  intervall = read16BitDataFromBuffer(0);
172 158
 
173
-  intervall = tmp_intervall;
174
-
175
-  char* tmp = "     ";
176
-  sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2,
177
-                             MSG_TYPE_ANSWER_OK,
178
-                             MSG_EOM1, MSG_EOM2);
179
-  Serial.write(tmp);
159
+  sendSOM();
160
+  Serial.write(MSG_TYPE_ANSWER_OK);
161
+  sendEOM();
180 162
 }
181 163
 
182 164
 void cc_setDriveStrength()
@@ -189,18 +171,13 @@ void cc_setDriveStrength()
189 171
   {
190 172
     drive_str = tmp_ds;
191 173
 
192
-    char* tmp = "     ";
193
-    sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2,
194
-                               MSG_TYPE_ANSWER_OK,
195
-                               MSG_EOM1, MSG_EOM2);
196
-    Serial.write(tmp);
174
+    sendSOM();
175
+    Serial.write(MSG_TYPE_ANSWER_OK);
176
+    sendEOM();
197 177
   } else {
198
-
199
-    char* tmp = "     ";
200
-    sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2,
201
-                               MSG_TYPE_ANSWER_NOK,
202
-                               MSG_EOM1, MSG_EOM2);
203
-    Serial.write(tmp);
178
+    sendSOM();
179
+    Serial.write(MSG_TYPE_ANSWER_NOK);
180
+    sendEOM();
204 181
   }
205 182
 }
206 183
 
@@ -267,8 +244,7 @@ void cc_startMeasurement()
267 244
 
268 245
       // 4. send the current output frequency, the drive strength and the measured ADC values from A0 and A1 to the host
269 246
       //    MSG_TYPE_MEAS_FREQ_INFO
270
-      Serial.write(MSG_SOM1);
271
-      Serial.write(MSG_SOM2);
247
+      sendSOM();
272 248
       Serial.write(MSG_TYPE_MEAS_FREQ_INFO);
273 249
       Serial.write((uint8_t)((freq & 0xff000000) >> 24));
274 250
       Serial.write((uint8_t)((freq & 0x00ff0000) >> 16));
@@ -278,8 +254,7 @@ void cc_startMeasurement()
278 254
       Serial.write((uint8_t) (a0_sum & 0x00ff));
279 255
       Serial.write((uint8_t)((a1_sum & 0xff00) >> 8));
280 256
       Serial.write((uint8_t) (a1_sum & 0x00ff));
281
-      Serial.write(MSG_EOM1);
282
-      Serial.write(MSG_EOM2);
257
+      sendEOM();
283 258
 
284 259
       si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
285 260
 
@@ -310,8 +285,7 @@ void cc_startMeasurement()
310 285
 
311 286
 void cc_getConfig()
312 287
 {
313
-  Serial.write(MSG_SOM1);
314
-  Serial.write(MSG_SOM2);
288
+  sendSOM();
315 289
   Serial.write(MSG_TYPE_CONFIG);
316 290
   Serial.write((uint8_t)((start_freq & 0xff000000) >> 24));
317 291
   Serial.write((uint8_t)((start_freq & 0x00ff0000) >> 16));
@@ -328,16 +302,14 @@ void cc_getConfig()
328 302
   Serial.write((uint8_t)((intervall & 0xff00) >> 8));
329 303
   Serial.write((uint8_t) (intervall & 0x00ff));
330 304
   Serial.write((uint8_t) drive_str);
331
-  Serial.write(MSG_EOM1);
332
-  Serial.write(MSG_EOM2);
305
+  sendEOM();
333 306
 }
334 307
 
335 308
 /*****************************************************************************/
336 309
 
337 310
 void cc_enableClk(void)
338 311
 {
339
-  Serial.write(MSG_SOM1);
340
-  Serial.write(MSG_SOM2);
312
+  sendSOM();
341 313
   if (cc_read_data[0] == SI5351_CLK0)
342 314
   {
343 315
     si5351.set_freq((uint64_t)start_freq * 100, SI5351_PLL_FIXED, SI5351_CLK0);
@@ -358,16 +330,14 @@ void cc_enableClk(void)
358 330
   } else {
359 331
     Serial.write(MSG_TYPE_ANSWER_NOK);
360 332
   }
361
-  Serial.write(MSG_EOM1);
362
-  Serial.write(MSG_EOM2);
333
+  sendEOM();
363 334
 }
364 335
 
365 336
 /*****************************************************************************/
366 337
 
367 338
 void cc_disableClk(void)
368 339
 {
369
-  Serial.write(MSG_SOM1);
370
-  Serial.write(MSG_SOM2);
340
+  sendSOM();
371 341
   if (cc_read_data[0] == SI5351_CLK0)
372 342
   {
373 343
     si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0
@@ -385,8 +355,7 @@ void cc_disableClk(void)
385 355
   } else {
386 356
     Serial.write(MSG_TYPE_ANSWER_NOK);
387 357
   }
388
-  Serial.write(MSG_EOM1);
389
-  Serial.write(MSG_EOM2);
358
+  sendEOM();
390 359
 }
391 360
 
392 361
 /*****************************************************************************/
@@ -396,11 +365,9 @@ void cc_saveDefaults(void)
396 365
 
397 366
   writeEEPROMConfig();
398 367
 
399
-  Serial.write(MSG_SOM1);
400
-  Serial.write(MSG_SOM2);
368
+  sendSOM();
401 369
   Serial.write(MSG_TYPE_ANSWER_OK);
402
-  Serial.write(MSG_EOM1);
403
-  Serial.write(MSG_EOM2);
370
+  sendEOM();
404 371
 }
405 372
 
406 373
 /*****************************************************************************/
@@ -414,25 +381,21 @@ void cc_setClkCorrection(void)
414 381
 
415 382
   si5351.set_correction(tmp_corr);
416 383
 
417
-  Serial.write(MSG_SOM1);
418
-  Serial.write(MSG_SOM2);
384
+  sendSOM();
419 385
   Serial.write(MSG_TYPE_ANSWER_OK);
420
-  Serial.write(MSG_EOM1);
421
-  Serial.write(MSG_EOM2);
386
+  sendEOM();
422 387
 }
423 388
 
424 389
 void cc_getClkCorrection(void)
425 390
 {
426 391
   uint32_t tmp_corr = si5351.get_correction();
427 392
 
428
-  Serial.write(MSG_SOM1);
429
-  Serial.write(MSG_SOM2);
393
+  sendSOM();
430 394
   Serial.write((uint8_t)((tmp_corr & 0xff000000) >> 24));
431 395
   Serial.write((uint8_t)((tmp_corr & 0x00ff0000) >> 16));
432 396
   Serial.write((uint8_t)((tmp_corr & 0x0000ff00) >>  8));
433 397
   Serial.write((uint8_t) (tmp_corr & 0x000000ff));
434
-  Serial.write(MSG_EOM1);
435
-  Serial.write(MSG_EOM2);
398
+  sendEOM();
436 399
 }
437 400
 
438 401
 /*****************************************************************************/
@@ -560,3 +523,17 @@ void cc_clearReadDataBuffer()
560 523
 
561 524
 /*****************************************************************************/
562 525
 
526
+void sendSOM()
527
+{
528
+  Serial.write(MSG_SOM1);
529
+  Serial.write(MSG_SOM2);
530
+}
531
+
532
+void sendEOM()
533
+{
534
+  Serial.write(MSG_EOM1);
535
+  Serial.write(MSG_EOM2);
536
+}
537
+
538
+/*****************************************************************************/
539
+