From 8244388fd8bc8e5e5ec3393233dd47bcb2c06782 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Fri, 19 Apr 2024 15:52:45 +1000 Subject: [PATCH] Refactor and cleanup old code --- ardustim/ardustim/ardustim.h | 4 +--- ardustim/ardustim/ardustim.ino | 27 ++++++++++++++++----------- ardustim/ardustim/comms.cpp | 18 +----------------- ardustim/ardustim/comms.h | 1 - ardustim/ardustim/storage.h | 4 ++-- ardustim/ardustim/storage.ino | 17 +++++++++++------ 6 files changed, 31 insertions(+), 40 deletions(-) diff --git a/ardustim/ardustim/ardustim.h b/ardustim/ardustim/ardustim.h index 78d03b8..97dc509 100644 --- a/ardustim/ardustim/ardustim.h +++ b/ardustim/ardustim/ardustim.h @@ -24,13 +24,11 @@ #include #include -/* Structures */ - /* Prototypes */ -int check_and_adjust_tcnt_limits(uint32_t *, uint32_t *); void reset_new_OCR1A(uint32_t); uint8_t get_bitshift_from_prescaler(uint8_t *); void get_prescaler_bits(uint32_t *, uint8_t *, uint8_t *); +void setRPM(uint16_t); /* Prototypes */ diff --git a/ardustim/ardustim/ardustim.ino b/ardustim/ardustim/ardustim.ino index cd8c3fa..101c6d1 100644 --- a/ardustim/ardustim/ardustim.ino +++ b/ardustim/ardustim/ardustim.ino @@ -24,7 +24,6 @@ #include "enums.h" #include "comms.h" #include "storage.h" -#include "user_defaults.h" #include "wheel_defs.h" #include #include @@ -289,8 +288,6 @@ void loop() /* Just handle the Serial UI, everything else is in * interrupt handlers or callbacks from SerialUI. */ - - if(Serial.available() > 0) { commandParser(); } if(config.mode == POT_RPM) @@ -300,8 +297,6 @@ void loop() adc0_read_complete = false; tmp_rpm = adc0 << TMP_RPM_SHIFT; if (tmp_rpm > TMP_RPM_CAP) { tmp_rpm = TMP_RPM_CAP; } - config.rpm = tmp_rpm; - reset_new_OCR1A(tmp_rpm); } } else if (config.mode == LINEAR_SWEPT_RPM) @@ -312,22 +307,32 @@ void loop() sweep_time_counter = micros(); if(sweep_direction == ASCENDING) { - config.rpm++; - if(config.rpm >= config.sweep_high_rpm) { sweep_direction = DESCENDING; } + tmp_rpm = config.rpm + 1; + if(tmp_rpm >= config.sweep_high_rpm) { sweep_direction = DESCENDING; } } else { - config.rpm--; - if(config.rpm <= config.sweep_low_rpm) { sweep_direction = ASCENDING; } + tmp_rpm = config.rpm - 1; + if(tmp_rpm <= config.sweep_low_rpm) { sweep_direction = ASCENDING; } } - reset_new_OCR1A(config.rpm); } } else if (config.mode == FIXED_RPM) { - + tmp_rpm = config.fixed_rpm; } + setRPM(tmp_rpm); +} + +/*! + * Validates the new user requested RPM and sets it if valid. + */ +void setRPM(uint16_t newRPM) +{ + if (newRPM < 10) { return; } + if(config.rpm != newRPM) { reset_new_OCR1A(newRPM); } + config.rpm = newRPM; } diff --git a/ardustim/ardustim/comms.cpp b/ardustim/ardustim/comms.cpp index 3889678..3020ce0 100644 --- a/ardustim/ardustim/comms.cpp +++ b/ardustim/ardustim/comms.cpp @@ -69,11 +69,7 @@ void commandParser() case 'f': //Set the fixed RPM value config.mode = FIXED_RPM; while(Serial.available() < 2) {} //Wait for the new RPM bytes - config.rpm = word(Serial.read(), Serial.read()); - config.fixed_rpm = config.rpm; - //config.rpm = 2000; - //reset_new_OCR1A(config.rpm); - setRPM(config.rpm); + config.fixed_rpm = word(Serial.read(), Serial.read()); break; case 'c': //Save the current config @@ -239,16 +235,4 @@ void select_previous_wheel_cb() config.wheel--; display_new_wheel(); -} - - -/*! - * Validates the new user requested RPM and sets it if valid. - */ -void setRPM(uint32_t newRPM) -{ - if (newRPM < 10) { return; } - - reset_new_OCR1A(newRPM); - config.rpm = newRPM; } \ No newline at end of file diff --git a/ardustim/ardustim/comms.h b/ardustim/ardustim/comms.h index ef11c54..eef66e9 100644 --- a/ardustim/ardustim/comms.h +++ b/ardustim/ardustim/comms.h @@ -35,7 +35,6 @@ void select_wheel_cb(); void set_rpm_cb(); void sweep_rpm_cb(); void reverse_wheel_direction_cb(); -void setRPM(uint32_t); /* General functions */ void serialSetup(); diff --git a/ardustim/ardustim/storage.h b/ardustim/ardustim/storage.h index 6fc3ad5..f5880ed 100644 --- a/ardustim/ardustim/storage.h +++ b/ardustim/ardustim/storage.h @@ -2,8 +2,8 @@ #define EEPROM_CURRENT_VERSION 1 #define EEPROM_VERSION 1 -#define EEPROM_CURRRENT_WHEEL 2 -#define EEPROM_CURRENT_RPM_MODE 3 +#define EEPROM_WHEEL 2 +#define EEPROM_RPM_MODE 3 #define EEPROM_CURRENT_RPM 4 //Note this is 2 bytes #define EEPROM_SWEEP_RPM_MIN 6 //Note this is 2 bytes #define EEPROM_SWEEP_RPM_MAX 8 //Note this is 2 bytes diff --git a/ardustim/ardustim/storage.ino b/ardustim/ardustim/storage.ino index 3f4f815..7ab964f 100644 --- a/ardustim/ardustim/storage.ino +++ b/ardustim/ardustim/storage.ino @@ -1,7 +1,6 @@ #include "storage.h" #include "EEPROM.h" #include "wheel_defs.h" -#include "user_defaults.h" #include "ardustim.h" #include "enums.h" #include "globals.h" @@ -9,17 +8,23 @@ void loadConfig() { if(EEPROM.read(EEPROM_VERSION) == 255) - //if(true) { //New arduino config.wheel = 5; //36-1 config.rpm = 3000; config.mode = POT_RPM; + + config.fixed_rpm = 3500; + config.sweep_high_rpm = 6000; + config.sweep_low_rpm = 1000; + config.sweep_interval = 1000; + + saveConfig(); } else { - config.wheel = EEPROM.read(EEPROM_CURRRENT_WHEEL); - config.mode = EEPROM.read(EEPROM_CURRENT_RPM_MODE); + config.wheel = EEPROM.read(EEPROM_WHEEL); + config.mode = EEPROM.read(EEPROM_RPM_MODE); byte highByte = EEPROM.read(EEPROM_CURRENT_RPM); byte lowByte = EEPROM.read(EEPROM_CURRENT_RPM+1); @@ -56,8 +61,8 @@ void loadConfig() void saveConfig() { - EEPROM.update(EEPROM_CURRRENT_WHEEL, config.wheel); - EEPROM.update(EEPROM_CURRENT_RPM_MODE, config.mode); + EEPROM.update(EEPROM_WHEEL, config.wheel); + EEPROM.update(EEPROM_RPM_MODE, config.mode); EEPROM.update(EEPROM_VERSION, EEPROM_CURRENT_VERSION); byte highByte = highByte(config.rpm);