Skip to content

Commit

Permalink
correctly implement saving to eeprom by cfg system
Browse files Browse the repository at this point in the history
  • Loading branch information
ablehobo committed Sep 4, 2024
1 parent ffd2d24 commit 0a2289c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 37 deletions.
8 changes: 8 additions & 0 deletions ui/anduril/load-save-config-fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ typedef struct Config {
uint8_t num_mode;
#endif

//// Morse Code Mode
#ifdef USE_MORSE_MODE
#define MAX_MESSAGE_LENGTH 40 // Maximum message length
uint8_t morse_speed;
uint8_t message[MAX_MESSAGE_LENGTH];
uint8_t message_length;
#endif

///// hardware config / globals menu
#ifdef USE_JUMP_START
uint8_t jump_start_level;
Expand Down
5 changes: 5 additions & 0 deletions ui/anduril/load-save-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ Config cfg = {
.therm_ceil = DEFAULT_THERM_CEIL,
.therm_cal_offset = -3,
#endif
#ifdef USE_MORSE_MODE
.morse_speed = DEFAULT_MORSE_SPEED,
// .message is an array, so you can't initialize it here
.message_length = INVALID_MORSE_CODE, // Default to invalid message so init runs
#endif

///// aux LEDs

Expand Down
47 changes: 23 additions & 24 deletions ui/anduril/morse-code.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <stdint.h>

// Define the morse_speed variable
uint8_t morse_speed = DEFAULT_MORSE_SPEED; // Speed in milliseconds
uint8_t message_length = INVALID_MORSE_CODE; // Default to invalid message so init runs
//uint8_t morse_speed = DEFAULT_MORSE_SPEED; // Speed in milliseconds
//uint8_t message_length = INVALID_MORSE_CODE; // Default to invalid message so init runs

typedef struct {
uint8_t sequence[4]; // Max length of Morse code is 4 elements
Expand Down Expand Up @@ -43,17 +43,17 @@ static const PreProcessedMorseCode morse_code_sequences[] = {

static void blink(uint8_t brightness, uint8_t duration) {
set_level(brightness);
nice_delay_ms(duration * morse_speed);
nice_delay_ms(duration * cfg.morse_speed);
set_level(0);
nice_delay_ms(morse_speed); // Inter-element gap
nice_delay_ms(cfg.morse_speed); // Inter-element gap
}

void decode_morse_pattern(uint8_t letter_index, uint8_t level) {
PreProcessedMorseCode code = morse_code_sequences[letter_index];

if (letter_index == 26) { // Assuming space is mapped to index 26
// Handle space: 7 * morse_speed for a space
nice_delay_ms(7 * morse_speed);
nice_delay_ms(7 * cfg.morse_speed);
} else {
for (int i = 0; i < code.length; i++) {
if (code.sequence[i] == 3) {
Expand All @@ -63,20 +63,20 @@ void decode_morse_pattern(uint8_t letter_index, uint8_t level) {
}

set_level(0);
nice_delay_ms(morse_speed); // Inter-element gap
nice_delay_ms(cfg.morse_speed); // Inter-element gap
}

nice_delay_ms(3 * morse_speed); // Final delay between letters
nice_delay_ms(3 * cfg.morse_speed); // Final delay between letters
}
}

uint8_t message[MAX_MESSAGE_LENGTH];
//uint8_t message[MAX_MESSAGE_LENGTH];

void init_message(void) {
message[0] = 14; // A
message[1] = 15; // B
message[2] = 20; // C
message_length = 3;
cfg.message[0] = 0; // A
cfg.message[1] = 1; // B
cfg.message[2] = 2; // C
cfg.message_length = 3;
}

MorseCode map_button_to_morse(uint8_t presses) {
Expand All @@ -90,38 +90,37 @@ MorseCode map_button_to_morse(uint8_t presses) {
}

void store_morse_code_input(uint8_t presses) {
if (message_length < MAX_MESSAGE_LENGTH) {
if (cfg.message_length < MAX_MESSAGE_LENGTH) {
MorseCode letter = map_button_to_morse(presses);
if (letter != INVALID_MORSE_CODE) {
message[message_length++] = letter;
cfg.message[cfg.message_length++] = letter;
}
save_config(); // Automatically save whenever input is stored
}
}


void display_morse_code_message(uint8_t brightness) {
for (uint8_t i = 0; i < message_length; i++) {
decode_morse_pattern(message[i], brightness);
}
for (uint8_t i = 0; i < cfg.message_length; i++) {
decode_morse_pattern(cfg.message[i], brightness);
}
}

void set_morse_speed(uint8_t speed) {
morse_speed = speed;
cfg.morse_speed = speed;
save_config(); // Automatically save whenever speed is set
}

void morse_config_save(uint8_t step, uint8_t value) {
if (value) {
if (step == 1) {
store_morse_code_input(value);
save_config();
} else if (step == 2) {
message_length = message_length > 0 ? message_length - 1 : 0;
cfg.message_length = cfg.message_length > 0 ? cfg.message_length - 1 : 0;
} else if (step == 3) {
message_length = 0;
cfg.message_length = 0;
save_config();
} else if (step == 4) {
set_morse_speed(value * 20);
save_config();
}
}
}
}
5 changes: 0 additions & 5 deletions ui/anduril/morse-code.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#define INVALID_MORSE_CODE 255 // Invalid Morse code
#define DEFAULT_MORSE_SPEED 125 // Default speed in milliseconds
#define MAX_MESSAGE_LENGTH 100 // Maximum message length

// Morse Code enumerator definition using a smaller type
typedef uint8_t MorseCode;
Expand All @@ -21,9 +20,5 @@ MorseCode map_button_to_morse(uint8_t presses);
uint8_t morse_state(Event event, uint16_t arg);
uint8_t morse_input_state(Event event, uint16_t arg);

// External variables
extern uint8_t morse_speed;
extern uint8_t message[MAX_MESSAGE_LENGTH];
extern uint8_t message_length;

#endif // MORSE_CODE_H
13 changes: 5 additions & 8 deletions ui/anduril/morse-mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,22 @@ uint8_t morse_state(Event event, uint16_t arg) {
set_state(battcheck_state, 0);
return EVENT_HANDLED;
} else if (event == EV_click1_hold) {
if (morse_speed > 10) {
morse_speed -= 1 * speed_direction;
if (cfg.morse_speed > 10) {
cfg.morse_speed -= 1 * speed_direction;
}
return EVENT_HANDLED;
} else if (event == EV_click1_hold_release) {
speed_direction = -speed_direction;
save_config();
return EVENT_HANDLED;
} else if (event == EV_click2_hold) {
if (morse_speed < 160) {
morse_speed += 1 * speed_direction;
if (cfg.morse_speed < 160) {
cfg.morse_speed += 1 * speed_direction;
}
return EVENT_HANDLED;
} else if (event == EV_click2_hold_release) {
save_config();
return EVENT_HANDLED;
} else if (event == EV_click3_hold) {
set_morse_speed(DEFAULT_MORSE_SPEED);
save_config();
return EVENT_HANDLED;
} else if (event == EV_click4_hold) {
push_state(morse_config_state, 0);
Expand Down Expand Up @@ -90,7 +87,7 @@ uint8_t morse_input_state(Event event, uint16_t arg) {
}

void morse_mode_iter(void) {
if (message_length == INVALID_MORSE_CODE) {
if (cfg.message_length == INVALID_MORSE_CODE) {
set_level(10);
nice_delay_ms(50);
set_level(0);
Expand Down

0 comments on commit 0a2289c

Please sign in to comment.