Skip to content

Commit

Permalink
Refactor and cleanup old code
Browse files Browse the repository at this point in the history
  • Loading branch information
noisymime committed Apr 19, 2024
1 parent 90119d9 commit 8244388
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
4 changes: 1 addition & 3 deletions ardustim/ardustim/ardustim.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
#include <stdint.h>
#include <Arduino.h>

/* 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 */

Expand Down
27 changes: 16 additions & 11 deletions ardustim/ardustim/ardustim.ino
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "enums.h"
#include "comms.h"
#include "storage.h"
#include "user_defaults.h"
#include "wheel_defs.h"
#include <avr/pgmspace.h>
#include <EEPROM.h>
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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;
}


Expand Down
18 changes: 1 addition & 17 deletions ardustim/ardustim/comms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
1 change: 0 additions & 1 deletion ardustim/ardustim/comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions ardustim/ardustim/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 11 additions & 6 deletions ardustim/ardustim/storage.ino
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
#include "storage.h"
#include "EEPROM.h"
#include "wheel_defs.h"
#include "user_defaults.h"
#include "ardustim.h"
#include "enums.h"
#include "globals.h"

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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8244388

Please sign in to comment.