From 075c570caea55c8a60199f159fdfa953cc3b1048 Mon Sep 17 00:00:00 2001 From: klaute Date: Fri, 14 Oct 2016 10:02:09 +0200 Subject: [PATCH] Some more duplicat code refactorized / reduced. --- firmware/command_ctrl.ino | 165 ++++++++++++++++---------------------- 1 file changed, 71 insertions(+), 94 deletions(-) diff --git a/firmware/command_ctrl.ino b/firmware/command_ctrl.ino index 69434ed..a83fa53 100644 --- a/firmware/command_ctrl.ino +++ b/firmware/command_ctrl.ino @@ -101,82 +101,64 @@ uint8_t cc_cmd_data_read_cnt = 0; /*****************************************************************************/ +uint32_t read32BitDataFromBuffer(uint8_t pos) +{ + uint32_t tmp = (uint32_t)cc_read_data[pos ] << 24; + tmp += (uint32_t)cc_read_data[pos + 1] << 16; + tmp += (uint32_t)cc_read_data[pos + 2] << 8; + tmp += (uint32_t)cc_read_data[pos + 3]; + return tmp; +} + +uint16_t read16BitDataFromBuffer(uint8_t pos) +{ + uint16_t tmp = (uint16_t)cc_read_data[pos ] << 8; + tmp += (uint16_t)cc_read_data[pos + 1]; + return tmp; +} + +/*****************************************************************************/ + void cc_setStartFreq() { - uint32_t tmp_start_freq = (uint32_t)cc_read_data[0] << 24; - tmp_start_freq += (uint32_t)cc_read_data[1] << 16; - tmp_start_freq += (uint32_t)cc_read_data[2] << 8; - tmp_start_freq += (uint32_t)cc_read_data[3]; - if (tmp_start_freq < 1) - tmp_start_freq = 1; - if (tmp_start_freq > 150000000) - tmp_start_freq = 150000000; + uint32_t tmp_start_freq = read32BitDataFromBuffer(0); - start_freq = tmp_start_freq; + start_freq = keepFreqRange(tmp_start_freq); - char* tmp = " "; - sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_OK, - MSG_EOM1, MSG_EOM2); - Serial.write(tmp); + sendSOM(); + Serial.write(MSG_TYPE_ANSWER_OK); + sendEOM(); } void cc_setEndFreq() { - uint32_t tmp_end_freq = (uint32_t)cc_read_data[0] << 24; - tmp_end_freq += (uint32_t)cc_read_data[1] << 16; - tmp_end_freq += (uint32_t)cc_read_data[2] << 8; - tmp_end_freq += (uint32_t)cc_read_data[3]; - if (tmp_end_freq < 1) - tmp_end_freq = 1; - if (tmp_end_freq > 150000000) - tmp_end_freq = 150000000; + uint32_t tmp_end_freq = read32BitDataFromBuffer(0); - end_freq = tmp_end_freq; + end_freq = keepFreqRange(tmp_end_freq); - char* tmp = " "; - sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_OK, - MSG_EOM1, MSG_EOM2); - Serial.write(tmp); + sendSOM(); + Serial.write(MSG_TYPE_ANSWER_OK); + sendEOM(); } void cc_setFreqStep() { - uint32_t tmp_step_freq = (uint32_t)cc_read_data[0] << 24; - tmp_step_freq += (uint32_t)cc_read_data[1] << 16; - tmp_step_freq += (uint32_t)cc_read_data[2] << 8; - tmp_step_freq += (uint32_t)cc_read_data[3]; - if (tmp_step_freq < 1) - tmp_step_freq = 1; - if (tmp_step_freq > 150000000) - tmp_step_freq = 150000000; + uint32_t tmp_step_freq = read32BitDataFromBuffer(0); - step_freq = tmp_step_freq; + step_freq = keepFreqRange(tmp_step_freq); - char* tmp = " "; - sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_OK, - MSG_EOM1, MSG_EOM2); - Serial.write(tmp); + sendSOM(); + Serial.write(MSG_TYPE_ANSWER_OK); + sendEOM(); } void cc_setIntervall() { - uint16_t tmp_intervall = (uint16_t)cc_read_data[0] << 8; - tmp_intervall += (uint16_t)cc_read_data[1]; - if (tmp_intervall < 1) - tmp_intervall = 1; - if (tmp_intervall > 150000000) - tmp_intervall = 150000000; + intervall = read16BitDataFromBuffer(0); - intervall = tmp_intervall; - - char* tmp = " "; - sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_OK, - MSG_EOM1, MSG_EOM2); - Serial.write(tmp); + sendSOM(); + Serial.write(MSG_TYPE_ANSWER_OK); + sendEOM(); } void cc_setDriveStrength() @@ -189,18 +171,13 @@ void cc_setDriveStrength() { drive_str = tmp_ds; - char* tmp = " "; - sprintf(tmp, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, - MSG_TYPE_ANSWER_OK, - MSG_EOM1, MSG_EOM2); - Serial.write(tmp); + sendSOM(); + Serial.write(MSG_TYPE_ANSWER_OK); + sendEOM(); } else { - - 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(); } } @@ -267,8 +244,7 @@ void cc_startMeasurement() // 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 - Serial.write(MSG_SOM1); - Serial.write(MSG_SOM2); + sendSOM(); Serial.write(MSG_TYPE_MEAS_FREQ_INFO); Serial.write((uint8_t)((freq & 0xff000000) >> 24)); Serial.write((uint8_t)((freq & 0x00ff0000) >> 16)); @@ -278,8 +254,7 @@ void cc_startMeasurement() Serial.write((uint8_t) (a0_sum & 0x00ff)); Serial.write((uint8_t)((a1_sum & 0xff00) >> 8)); Serial.write((uint8_t) (a1_sum & 0x00ff)); - Serial.write(MSG_EOM1); - Serial.write(MSG_EOM2); + sendEOM(); si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0 @@ -310,8 +285,7 @@ void cc_startMeasurement() void cc_getConfig() { - Serial.write(MSG_SOM1); - Serial.write(MSG_SOM2); + sendSOM(); Serial.write(MSG_TYPE_CONFIG); Serial.write((uint8_t)((start_freq & 0xff000000) >> 24)); Serial.write((uint8_t)((start_freq & 0x00ff0000) >> 16)); @@ -328,16 +302,14 @@ void cc_getConfig() Serial.write((uint8_t)((intervall & 0xff00) >> 8)); Serial.write((uint8_t) (intervall & 0x00ff)); Serial.write((uint8_t) drive_str); - Serial.write(MSG_EOM1); - Serial.write(MSG_EOM2); + sendEOM(); } /*****************************************************************************/ void cc_enableClk(void) { - Serial.write(MSG_SOM1); - Serial.write(MSG_SOM2); + sendSOM(); if (cc_read_data[0] == SI5351_CLK0) { si5351.set_freq((uint64_t)start_freq * 100, SI5351_PLL_FIXED, SI5351_CLK0); @@ -358,16 +330,14 @@ void cc_enableClk(void) } else { Serial.write(MSG_TYPE_ANSWER_NOK); } - Serial.write(MSG_EOM1); - Serial.write(MSG_EOM2); + sendEOM(); } /*****************************************************************************/ void cc_disableClk(void) { - Serial.write(MSG_SOM1); - Serial.write(MSG_SOM2); + sendSOM(); if (cc_read_data[0] == SI5351_CLK0) { si5351.output_enable(SI5351_CLK0, 0); // disable clock output 0 @@ -385,8 +355,7 @@ void cc_disableClk(void) } else { Serial.write(MSG_TYPE_ANSWER_NOK); } - Serial.write(MSG_EOM1); - Serial.write(MSG_EOM2); + sendEOM(); } /*****************************************************************************/ @@ -396,11 +365,9 @@ void cc_saveDefaults(void) writeEEPROMConfig(); - Serial.write(MSG_SOM1); - Serial.write(MSG_SOM2); + sendSOM(); Serial.write(MSG_TYPE_ANSWER_OK); - Serial.write(MSG_EOM1); - Serial.write(MSG_EOM2); + sendEOM(); } /*****************************************************************************/ @@ -414,25 +381,21 @@ void cc_setClkCorrection(void) si5351.set_correction(tmp_corr); - Serial.write(MSG_SOM1); - Serial.write(MSG_SOM2); + sendSOM(); Serial.write(MSG_TYPE_ANSWER_OK); - Serial.write(MSG_EOM1); - Serial.write(MSG_EOM2); + sendEOM(); } void cc_getClkCorrection(void) { uint32_t tmp_corr = si5351.get_correction(); - Serial.write(MSG_SOM1); - Serial.write(MSG_SOM2); + sendSOM(); Serial.write((uint8_t)((tmp_corr & 0xff000000) >> 24)); Serial.write((uint8_t)((tmp_corr & 0x00ff0000) >> 16)); Serial.write((uint8_t)((tmp_corr & 0x0000ff00) >> 8)); Serial.write((uint8_t) (tmp_corr & 0x000000ff)); - Serial.write(MSG_EOM1); - Serial.write(MSG_EOM2); + sendEOM(); } /*****************************************************************************/ @@ -560,3 +523,17 @@ void cc_clearReadDataBuffer() /*****************************************************************************/ +void sendSOM() +{ + Serial.write(MSG_SOM1); + Serial.write(MSG_SOM2); +} + +void sendEOM() +{ + Serial.write(MSG_EOM1); + Serial.write(MSG_EOM2); +} + +/*****************************************************************************/ +