From d12c75fa008ba58948b2c7634191af5e13661f19 Mon Sep 17 00:00:00 2001 From: klaute Date: Fri, 14 Oct 2016 10:08:56 +0200 Subject: [PATCH] Complexity of the cc measurement function reduced by using an early return. --- firmware/command_ctrl.ino | 172 +++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 87 deletions(-) diff --git a/firmware/command_ctrl.ino b/firmware/command_ctrl.ino index a83fa53..12a348e 100644 --- a/firmware/command_ctrl.ino +++ b/firmware/command_ctrl.ino @@ -188,97 +188,95 @@ void cc_startMeasurement() // MSG_TYPE_CONFIG cc_getConfig(); - // 2. start a for loop from the frequence to start to the end frequence - if (start_freq > 0 && start_freq <= 150000000 && - end_freq > 0 && end_freq <= 150000000 && - start_freq < end_freq && - intervall > 0 && - step_freq > 0) + if (start_freq == 0 || start_freq > 150000000 || + end_freq == 0 || end_freq > 150000000 || + step_freq == 0 || step_freq > 150000000 || + start_freq >= end_freq || + intervall == 0) { - si5351.drive_strength(SI5351_CLK0, drive_str); // 2 4 6 8ma - - uint8_t t = 0; - for (t = 0; t < 100; t++) - { - uint16_t tmp = analogRead(A0); - tmp = analogRead(A1); - } - - uint32_t freq = 0; - for (freq = start_freq; freq <= end_freq; freq += step_freq) - { - if (freq > end_freq) - { - // prevent to step over the end frequency - // maybe the user is not allowed to send data in the frequency band - freq = end_freq; - } - uint32_t a0_sum = 0; - uint32_t a1_sum = 0; - uint16_t i = 0; - - si5351.set_freq((uint64_t)freq * 100, SI5351_PLL_FIXED, SI5351_CLK0); - si5351.output_enable(SI5351_CLK0, 1); // enable clock output 0 - delay(1); - - for (i = 0; i < intervall; i++) - { - // 3. on every loop read the analog input A0 and A1 for the in intervall (milliseconds) - // and generate the average value, read the ADC value every milli second. - uint8_t t = 0; - uint16_t ta0 = 0; - uint16_t ta1 = 0; - for (t = 0; t < MEAS_LOOP_CNT; t++) - { - ta0 += analogRead(A0); - ta1 += analogRead(A1); - } - a0_sum += (ta0 / MEAS_LOOP_CNT); - a1_sum += (ta1 / MEAS_LOOP_CNT); - - delay(1); - } - - a0_sum = a0_sum / intervall; - a1_sum = a1_sum / intervall; - - // 4. send the current output frequency, the drive strength and the measured ADC values from A0 and A1 to the host - // MSG_TYPE_MEAS_FREQ_INFO - sendSOM(); - Serial.write(MSG_TYPE_MEAS_FREQ_INFO); - Serial.write((uint8_t)((freq & 0xff000000) >> 24)); - Serial.write((uint8_t)((freq & 0x00ff0000) >> 16)); - Serial.write((uint8_t)((freq & 0x0000ff00) >> 8)); - Serial.write((uint8_t) (freq & 0x000000ff)); - Serial.write((uint8_t)((a0_sum & 0xff00) >> 8)); - Serial.write((uint8_t) (a0_sum & 0x00ff)); - Serial.write((uint8_t)((a1_sum & 0xff00) >> 8)); - Serial.write((uint8_t) (a1_sum & 0x00ff)); - sendEOM(); - - si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0 - - if (freq >= end_freq) - break; // abort the loop because all is done - } - - // 5. send a measurement end message to the host - // MSG_TYPE_MEAS_END_INFO - char* tmp = " "; - sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_MEAS_END_INFO, - MSG_EOM1, MSG_EOM2); - Serial.write(tmp); - - } else { // on error - char* tmp = " "; - sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_NOK, - MSG_EOM1, MSG_EOM2); - Serial.write(tmp); + + sendSOM(); + Serial.write(MSG_TYPE_ANSWER_NOK); + sendEOM(); + + return; } + // 2. start a for loop from the frequence to start to the end frequence + si5351.drive_strength(SI5351_CLK0, drive_str); // 2 4 6 8ma + + uint8_t t = 0; + for (t = 0; t < 100; t++) + { + uint16_t tmp = analogRead(A0); + tmp = analogRead(A1); + } + + uint32_t freq = 0; + for (freq = start_freq; freq <= end_freq; freq += step_freq) + { + if (freq > end_freq) + { + // prevent to step over the end frequency + // maybe the user is not allowed to send data in the frequency band + freq = end_freq; + } + uint32_t a0_sum = 0; + uint32_t a1_sum = 0; + uint16_t i = 0; + + si5351.set_freq((uint64_t)freq * 100, SI5351_PLL_FIXED, SI5351_CLK0); + si5351.output_enable(SI5351_CLK0, 1); // enable clock output 0 + delay(1); + + for (i = 0; i < intervall; i++) + { + // 3. on every loop read the analog input A0 and A1 for the in intervall (milliseconds) + // and generate the average value, read the ADC value every milli second. + uint8_t t = 0; + uint16_t ta0 = 0; + uint16_t ta1 = 0; + for (t = 0; t < MEAS_LOOP_CNT; t++) + { + ta0 += analogRead(A0); + ta1 += analogRead(A1); + } + a0_sum += (ta0 / MEAS_LOOP_CNT); + a1_sum += (ta1 / MEAS_LOOP_CNT); + + delay(1); + } + + a0_sum = a0_sum / intervall; + a1_sum = a1_sum / intervall; + + // 4. send the current output frequency, the drive strength and the measured ADC values from A0 and A1 to the host + // MSG_TYPE_MEAS_FREQ_INFO + sendSOM(); + Serial.write(MSG_TYPE_MEAS_FREQ_INFO); + Serial.write((uint8_t)((freq & 0xff000000) >> 24)); + Serial.write((uint8_t)((freq & 0x00ff0000) >> 16)); + Serial.write((uint8_t)((freq & 0x0000ff00) >> 8)); + Serial.write((uint8_t) (freq & 0x000000ff)); + Serial.write((uint8_t)((a0_sum & 0xff00) >> 8)); + Serial.write((uint8_t) (a0_sum & 0x00ff)); + Serial.write((uint8_t)((a1_sum & 0xff00) >> 8)); + Serial.write((uint8_t) (a1_sum & 0x00ff)); + sendEOM(); + + si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0 + + if (freq >= end_freq) + break; // abort the loop because all is done + } + + // 5. send a measurement end message to the host + // MSG_TYPE_MEAS_END_INFO + sendSOM(); + Serial.write(MSG_TYPE_MEAS_END_INFO); + sendEOM(); + si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0 }