Skip to content

Commit

Permalink
Mostly working config saving
Browse files Browse the repository at this point in the history
  • Loading branch information
noisymime committed Apr 19, 2024
1 parent fe6238d commit b13f9b9
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 60 deletions.
43 changes: 39 additions & 4 deletions UI/renderer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const serialport = require('serialport')
const usb = require('usb').usb;
const Readline = require('@serialport/parser-readline')
const ByteLength = require('@serialport/parser-byte-length')
//const ByteLength = require('@serialport/parser-byte-length')
const ByteLengthParser = require('@serialport/parser-byte-length')
const {ipcRenderer} = require("electron")
var port = new serialport('/dev/tty-usbserial1', { autoOpen: false })

var onConnectInterval;
var CONFIG_SIZE = 13;
var onConnectIntervalConfig;
var onConnectIntervalWheels;
var isConnected=false;
var currentRPM = 0;
var initComplete = false;
Expand Down Expand Up @@ -119,7 +122,8 @@ function openSerialPort()
function onSerialConnect()
{
console.log("Serial port opened");
onConnectInterval = setInterval(requestPatternList, 3000);
onConnectIntervalConfig = setInterval(requestConfig, 2500);
//onConnectIntervalWheels = setInterval(requestPatternList, 3000);

//Activate the links
document.getElementById("link_live").href = "#live";
Expand Down Expand Up @@ -183,12 +187,43 @@ function saveData()
port.write("c"); //Send the command to perform EEPROM burn
}

function requestConfig()
{
//Clear the interval
clearInterval(onConnectIntervalConfig);

//Attach the readline parser
const parser = port.pipe(new ByteLengthParser({ length: CONFIG_SIZE }));
parser.on('data', receiveConfig);

//Request the config from the arduino
port.write("C");
console.log("Requesting config");
}

function receiveConfig(data)
{
console.log("Received config: " + data);
console.log("Mode: " + data[0]);

document.getElementById("rpmSelect").value = data[0];
document.getElementById("fixedRPM").value = (((data[6] & 0xff) << 8) | (data[5] & 0xff));
document.getElementById("rpmSweepMin").value = (((data[8] & 0xff) << 8) | (data[7] & 0xff));
document.getElementById("rpmSweepMax").value = (((data[10] & 0xff) << 8) | (data[9] & 0xff));
document.getElementById("rpmSweepSpeed").value = (((data[12] & 0xff) << 8) | (data[11] & 0xff));

port.unpipe();

setRPMMode();
requestPatternList();
}

var patternOptionCounter = 0;
var numPatterns = 0;
function requestPatternList()
{
//Clear the interval
clearInterval(onConnectInterval);
clearInterval(onConnectIntervalWheels);

//Attach the readline parser
const parser = port.pipe(new Readline({ delimiter: '\r\n' }));
Expand Down
34 changes: 19 additions & 15 deletions ardustim/ardustim/ardustim.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ volatile bool adc1_read_complete = false;
volatile bool reset_prescaler = false;
volatile bool normal = true;
volatile uint8_t output_invert_mask = 0x00; /* Don't invert anything */
volatile uint8_t sweep_direction = ASCENDING;
volatile uint8_t prescaler_bits = 0;
volatile uint8_t last_prescaler_bits = 0;
volatile uint16_t new_OCR1A = 5000; /* sane default */
volatile uint16_t edge_counter = 0;
uint32_t sweep_time_counter = 0;
uint8_t sweep_direction = ASCENDING;

/* Less sensitive globals */
uint8_t bitshift = 0;
Expand Down Expand Up @@ -122,8 +122,8 @@ wheels Wheels[MAX_WHEELS] = {

/* Initialization */
void setup() {
serialSetup();
loadConfig();
serialSetup();

cli(); // stop interrupts

Expand Down Expand Up @@ -214,7 +214,7 @@ void setup() {
// Set ADSC in ADCSRA (0x7A) to start the ADC conversion
ADCSRA |= B01000000;
/* Make sure we are using the DEFAULT RPM on startup */
reset_new_OCR1A(wanted_rpm);
reset_new_OCR1A(config.rpm);

} // End setup

Expand Down Expand Up @@ -255,19 +255,19 @@ ISR(ADC_vect){
*/
ISR(TIMER1_COMPA_vect) {
/* This is VERY simple, just walk the array and wrap when we hit the limit */
PORTB = output_invert_mask ^ pgm_read_byte(&Wheels[selected_wheel].edge_states_ptr[edge_counter]); /* Write it to the port */
PORTB = output_invert_mask ^ pgm_read_byte(&Wheels[config.wheel].edge_states_ptr[edge_counter]); /* Write it to the port */
/* Normal direction overflow handling */
if (normal)
{
edge_counter++;
if (edge_counter == Wheels[selected_wheel].wheel_max_edges) {
if (edge_counter == Wheels[config.wheel].wheel_max_edges) {
edge_counter = 0;
}
}
else
{
if (edge_counter == 0)
edge_counter = Wheels[selected_wheel].wheel_max_edges;
edge_counter = Wheels[config.wheel].wheel_max_edges;
edge_counter--;
}
/* The tables are in flash so we need pgm_read_byte() */
Expand Down Expand Up @@ -300,7 +300,7 @@ void loop()
adc0_read_complete = false;
tmp_rpm = adc0 << TMP_RPM_SHIFT;
if (tmp_rpm > TMP_RPM_CAP) { tmp_rpm = TMP_RPM_CAP; }
wanted_rpm = tmp_rpm;
config.rpm = tmp_rpm;
reset_new_OCR1A(tmp_rpm);
}
}
Expand All @@ -312,17 +312,21 @@ void loop()
sweep_time_counter = micros();
if(sweep_direction == ASCENDING)
{
wanted_rpm++;
if(wanted_rpm >= config.sweep_high_rpm) { sweep_direction = DESCENDING; }
config.rpm++;
if(config.rpm >= config.sweep_high_rpm) { sweep_direction = DESCENDING; }
}
else
{
wanted_rpm--;
if(wanted_rpm <= config.sweep_low_rpm) { sweep_direction = ASCENDING; }
config.rpm--;
if(config.rpm <= config.sweep_low_rpm) { sweep_direction = ASCENDING; }
}
reset_new_OCR1A(wanted_rpm);
reset_new_OCR1A(config.rpm);
}
}
else if (config.mode == FIXED_RPM)
{

}

}

Expand All @@ -332,9 +336,9 @@ void reset_new_OCR1A(uint32_t new_rpm)
uint32_t tmp;
uint8_t bitshift;
uint8_t tmp_prescaler_bits;
tmp = (uint32_t)(8000000.0/(Wheels[selected_wheel].rpm_scaler * (float)(new_rpm < 10 ? 10:new_rpm)));
//tmp = (uint32_t)(8000000/(Wheels[selected_wheel].rpm_scaler * (new_rpm < 10 ? 10:new_rpm)));
uint64_t x = 800000000000ULL;
tmp = (uint32_t)(8000000.0/(Wheels[config.wheel].rpm_scaler * (float)(new_rpm < 10 ? 10:new_rpm)));
//tmp = (uint32_t)(8000000/(Wheels[config.wheel].rpm_scaler * (new_rpm < 10 ? 10:new_rpm)));
//uint64_t x = 800000000000ULL;

get_prescaler_bits(&tmp,&tmp_prescaler_bits,&bitshift);

Expand Down
55 changes: 27 additions & 28 deletions ardustim/ardustim/comms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,10 @@
#include <math.h>
#include <util/delay.h>

/* File local variables */
extern uint16_t wanted_rpm;

/* External Globla Variables */
extern wheels Wheels[];

/* Volatile variables (USED in ISR's) */
extern volatile uint8_t selected_wheel;
extern volatile uint8_t sweep_direction;
extern volatile bool normal;
extern volatile uint16_t edge_counter;
extern volatile uint16_t new_OCR1A;
Expand All @@ -63,6 +58,7 @@ void commandParser()
char buf[80];
byte tmp_wheel;
byte tmp_mode;
void* pnt_Config = &config;
if (cmdPending == false) { currentCommand = Serial.read(); }

switch (currentCommand)
Expand All @@ -73,19 +69,22 @@ void commandParser()
case 'f': //Set the fixed RPM value
config.mode = FIXED_RPM;
while(Serial.available() < 2) {} //Wait for the new RPM bytes
wanted_rpm = word(Serial.read(), Serial.read());
//wanted_rpm = 2000;
//reset_new_OCR1A(wanted_rpm);
setRPM(wanted_rpm);
config.rpm = word(Serial.read(), Serial.read());
config.fixed_rpm = config.rpm;
//config.rpm = 2000;
//reset_new_OCR1A(config.rpm);
setRPM(config.rpm);
break;

case 'c': //Save the current config
saveConfig();
break;

case 'C': //Send the current config
sizeof(config);

for(uint8_t x=0; x<sizeof(struct configTable); x++)
{
Serial.write(*((uint8_t *)pnt_Config + x)); //Each byte is simply the location in memory of the config Page + the offset
}
break;

case 'L': // send the list of wheel names
Expand Down Expand Up @@ -114,30 +113,30 @@ void commandParser()
break;

case 'N': //Send the number of the current wheel
Serial.println(selected_wheel);
Serial.println(config.wheel);
break;

case 'p': //Send the size of the current wheel
Serial.println(Wheels[selected_wheel].wheel_max_edges);
Serial.println(Wheels[config.wheel].wheel_max_edges);
break;

case 'P': //Send the pattern for the current wheel
numTeeth = pgm_read_word(Wheels[selected_wheel].wheel_max_edges);
numTeeth = pgm_read_word(Wheels[config.wheel].wheel_max_edges);

for(uint16_t x=0; x<Wheels[selected_wheel].wheel_max_edges; x++)
for(uint16_t x=0; x<Wheels[config.wheel].wheel_max_edges; x++)
{
if(x != 0) { Serial.print(","); }

byte tempByte = pgm_read_byte(&Wheels[selected_wheel].edge_states_ptr[x]);
byte tempByte = pgm_read_byte(&Wheels[config.wheel].edge_states_ptr[x]);
Serial.print(tempByte);
}
Serial.println("");
//2nd row of data sent is the number of degrees the wheel runs over (360 or 720 typically)
Serial.println(Wheels[selected_wheel].wheel_degrees);
Serial.println(Wheels[config.wheel].wheel_degrees);
break;

case 'R': //Send the current RPM
Serial.println(wanted_rpm);
Serial.println(config.rpm);
break;

case 's': //Set the high and low RPM for sweep mode
Expand All @@ -157,14 +156,14 @@ void commandParser()
tmp_wheel = Serial.read();
if(tmp_wheel < MAX_WHEELS)
{
selected_wheel = tmp_wheel;
config.wheel = tmp_wheel;
display_new_wheel();
}
break;

case 'X': //Just a test method for switching the to the next wheel
select_next_wheel_cb();
strcpy_P(buf,Wheels[selected_wheel].decoder_name);
strcpy_P(buf,Wheels[config.wheel].decoder_name);
Serial.println(buf);
break;

Expand Down Expand Up @@ -204,7 +203,7 @@ void toggle_invert_secondary_cb()

void display_new_wheel()
{
reset_new_OCR1A(wanted_rpm);
reset_new_OCR1A(config.rpm);
edge_counter = 0; // Reset to beginning of the wheel pattern */
}

Expand All @@ -217,10 +216,10 @@ void display_new_wheel()
*/
void select_next_wheel_cb()
{
if (selected_wheel == (MAX_WHEELS-1))
selected_wheel = 0;
if (config.wheel == (MAX_WHEELS-1))
config.wheel = 0;
else
selected_wheel++;
config.wheel++;

display_new_wheel();
}
Expand All @@ -234,10 +233,10 @@ void select_next_wheel_cb()
*/
void select_previous_wheel_cb()
{
if (selected_wheel == 0)
selected_wheel = MAX_WHEELS-1;
if (config.wheel == 0)
config.wheel = MAX_WHEELS-1;
else
selected_wheel--;
config.wheel--;

display_new_wheel();
}
Expand All @@ -251,5 +250,5 @@ void setRPM(uint32_t newRPM)
if (newRPM < 10) { return; }

reset_new_OCR1A(newRPM);
wanted_rpm = newRPM;
config.rpm = newRPM;
}
5 changes: 5 additions & 0 deletions ardustim/ardustim/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@
#define __GLOBALS_H__

#include "Arduino.h"
#include "wheel_defs.h"

#define TMP_RPM_SHIFT 4 /* x16, 0-16384 RPM via pot */
#define TMP_RPM_CAP 9000 /* MAX RPM via pot control. Adjusted to 9,000rpm max from 16,384rpm to match the GUI */
#define EEPROM_LAST_MODE 100

struct configTable {
uint8_t version;
uint16_t rpm = 6000;
uint8_t wheel = FOUR_TWENTY_A;
uint8_t mode;
uint16_t fixed_rpm = 2500;
uint16_t sweep_low_rpm = 250;
uint16_t sweep_high_rpm = 4000;
uint16_t sweep_interval = 1000;
Expand Down
2 changes: 2 additions & 0 deletions ardustim/ardustim/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#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
#define EEPROM_SWEEP_RPM_INT 10 //Note this is 2 bytes
#define EEPROM_FIXED_RPM 12 //Note this is 2 bytes

void loadConfig();
void saveConfig();
Loading

0 comments on commit b13f9b9

Please sign in to comment.