From ea818821cbe747755bb1ffc5aa8121b8bcd19273 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Thu, 23 May 2019 13:42:59 +0200 Subject: [PATCH] Added storing of master_volume and sound. First changes for storing configurations in EEPROM. --- MicroMDAEPiano.ino | 70 +++++++++++++++++++++++++++++++--------------- UI.hpp | 33 +++++++++++----------- config.h | 5 +++- 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/MicroMDAEPiano.ino b/MicroMDAEPiano.ino index 0e86570..0df9993 100644 --- a/MicroMDAEPiano.ino +++ b/MicroMDAEPiano.ino @@ -84,19 +84,19 @@ mdaEPiano* ep; extern void init_menus(void); extern int32_t encoder_value[NUM_ENCODER]; +extern Bounce but[NUM_ENCODER]; // more variables +uint8_t sound = 1; uint32_t xrun = 0; uint32_t overload = 0; uint32_t peak = 0; uint16_t render_time_max = 0; elapsedMicros fill_audio_buffer; elapsedMillis control_rate; -elapsedMillis autostore; const uint16_t audio_block_time_us = 1000000 / (SAMPLE_RATE / AUDIO_BLOCK_SAMPLES); config_t configuration = { 0xffff, // checksum - 1, // sound ENC_DECAY_DEFAULT, // decay ENC_RELEASE_DEFAULT, // release ENC_HARDNESS_DEFAULT, // hardness @@ -133,13 +133,16 @@ config_t configuration = { }; float _loudness = mapfloat(float(ENC_LOUDNESS_DEFAULT), ENC_LOUDNESS_MIN, ENC_LOUDNESS_MAX, 0.0, 1.0); +uint8_t master_volume = ENC_MASTER_VOLUME_DEFAULT; +int8_t pan = ENC_MASTER_PAN_DEFAULT; +uint8_t eeprom_config_update_flag = 0; +bool eeprom_master_volume_update_flag = false; +elapsedMillis eeprom_master_volume_update_timer; -bool eeprom_update_flag = false; #ifdef SHOW_CPU_LOAD_MSEC elapsedMillis cpu_mem_millis; #endif - //************************************************************************************************* //* SETUP FUNCTION //************************************************************************************************* @@ -166,9 +169,8 @@ void setup() // create EPiano object ep = new mdaEPiano(); - set_complete_configuration(); - // read initial EEPROM variables initial_values_from_eeprom(); + set_complete_configuration(); setup_midi_devices(); @@ -211,8 +213,6 @@ void setup() Serial.print(audio_block_time_us); Serial.println(F("ms)")); - - AudioInterrupts(); Serial.println(F("")); @@ -285,6 +285,12 @@ void loop() { control_rate = 0; handle_ui(); + + if ( eeprom_config_update_flag > 0 && ep->getActiveVoices() == 0) // write only to eeprom when no voice is active + eeprom_config_update(); + + if (eeprom_master_volume_update_flag == true && eeprom_master_volume_update_timer > STORE_MASTER_VOLUME_MS) + eeprom_master_volume_update(); } } @@ -441,6 +447,9 @@ void set_master_volume(uint8_t value) // http://files.csound-tutorial.net/floss_manual/Release03/Cs_FM_03_ScrapBook/b-panning-and-spatialization.html mixer_r.gain(0, tmp3 * sinf(tmp2 * PI / 2)); mixer_l.gain(0, tmp3 * cosf(tmp2 * PI / 2)); + + eeprom_master_volume_update_flag = true; + eeprom_master_volume_update_timer = 0; } /****************************************************************************** @@ -452,7 +461,7 @@ void initial_values_from_eeprom(void) uint32_t checksum; config_t tmp_conf; - EEPROM_readAnything(EEPROM_START_ADDRESS, tmp_conf); + EEPROM_readAnything(EEPROM_CONFIGURATIONS + sizeof(config_t) * (sound - 1), tmp_conf); checksum = crc32((byte*)&tmp_conf + 4, sizeof(tmp_conf) - 4); #ifdef DEBUG @@ -462,35 +471,52 @@ void initial_values_from_eeprom(void) Serial.print(checksum, HEX); #endif - if (checksum != tmp_conf.checksum) + if (checksum != tmp_conf.checksum || (but[0].read() == LOW && but[1].read() == LOW)) { #ifdef DEBUG - Serial.print(F(" - mismatch -> initializing EEPROM!")); + Serial.println(F(" - mismatch (or force) -> initializing EEPROM!")); #endif - eeprom_update(); + eeprom_config_update_flag = sound; + eeprom_config_update(); } else { - EEPROM_readAnything(EEPROM_START_ADDRESS, configuration); - Serial.print(F(" - OK, loading!")); + EEPROM_readAnything(EEPROM_CONFIGURATIONS + sizeof(config_t) * (sound - 1), configuration); + master_volume = EEPROM.read(EEPROM_MASTER_VOLUME); + Serial.print(F(" - OK, loading")); } #ifdef DEBUG - Serial.println(); + Serial.print(F(" - Master volume: ")); + Serial.println(master_volume, DEC); #endif } -void eeprom_write(void) +void eeprom_config_write(uint8_t value) { - autostore = 0; - eeprom_update_flag = true; + eeprom_config_update_flag = value; } -void eeprom_update(void) +void eeprom_config_update(void) { - eeprom_update_flag = false; configuration.checksum = crc32((byte*)&configuration + 4, sizeof(configuration) - 4); - EEPROM_writeAnything(EEPROM_START_ADDRESS, configuration); - Serial.println(F("Updating EEPROM with configuration data")); + EEPROM_writeAnything(EEPROM_CONFIGURATIONS + sizeof(config_t) * (eeprom_config_update_flag - 1), configuration); + Serial.print(F("Updating EEPROM configuration for sound ")); + Serial.print(eeprom_config_update_flag, DEC); + Serial.print(F(" at 0x")); + Serial.println(EEPROM_CONFIGURATIONS + sizeof(config_t) * (eeprom_config_update_flag - 1), HEX); + eeprom_config_update_flag = 0; +} + +void eeprom_master_volume_write(void) +{ + eeprom_master_volume_update_flag = true; +} + +void eeprom_master_volume_update(void) +{ + eeprom_master_volume_update_flag = false; + EEPROM.update(EEPROM_MASTER_VOLUME, master_volume); + Serial.println(F("Updating EEPROM with master_volume")); } uint32_t crc32(byte * calc_start, uint16_t calc_bytes) // base code from https://www.arduino.cc/en/Tutorial/EEPROMCrc diff --git a/UI.hpp b/UI.hpp index 8bec0a1..94e26b6 100644 --- a/UI.hpp +++ b/UI.hpp @@ -36,9 +36,6 @@ int32_t encoder_value[NUM_ENCODER]; Bounce but[NUM_ENCODER] = {Bounce(BUT_L_PIN, BUT_DEBOUNCE_MS), Bounce(BUT_R_PIN, BUT_DEBOUNCE_MS)}; elapsedMillis back_to_main; -uint8_t master_volume = ENC_MASTER_VOLUME_DEFAULT; -int8_t pan = ENC_MASTER_PAN_DEFAULT; - #define NUM_MENUS 41 #define MAIN 0 @@ -100,10 +97,13 @@ extern AudioMixer4 mixer_r; extern AudioMixer4 mixer_l; extern AudioAmplifier volume_r; extern AudioAmplifier volume_l; -extern void eeprom_write(void); +extern void eeprom_config_write(uint8_t value); extern mdaEPiano* ep; extern float _loudness; extern config_t configuration; +extern uint8_t sound; +extern uint8_t master_volume; +extern int8_t pan; /****************************************** TEXT GETTER FUCTIONS @@ -244,7 +244,7 @@ LiquidMenu main_menu(lcd); #define NUM_LOAD_SOUNDS_MENUS 1 const char load_sound_text1[] PROGMEM = "Load Sound"; LiquidLine load_sound_line1(1, 0, load_sound_text1); -LiquidLine load_sound_line2(1, 1, configuration.sound); +LiquidLine load_sound_line2(1, 1, sound); LiquidScreen load_sound_screen; LiquidMenu load_sound_menu(lcd); @@ -611,7 +611,7 @@ LiquidMenu eq_treble_menu(lcd); #define NUM_SAVE_SOUNDS_MENUS 1 const char save_sound_text1[] PROGMEM = "Save Sound"; LiquidLine save_sound_line1(1, 0, save_sound_text1); -LiquidLine save_sound_line2(1, 1, configuration.sound); +LiquidLine save_sound_line2(1, 1, sound); LiquidScreen save_sound_screen; LiquidMenu save_sound_menu(lcd); @@ -704,8 +704,8 @@ void callback_load_sound_function() { #endif menu_system.change_menu(load_sound_menu); menu_position[MAIN] = encoder_value[RIGHT_ENCODER]; - encoder_value[RIGHT_ENCODER] = configuration.sound; - enc[RIGHT_ENCODER].write(configuration.sound, 1, 99); + encoder_value[RIGHT_ENCODER] = sound; + enc[RIGHT_ENCODER].write(sound, 1, 99); menu_system.update(); } @@ -1113,8 +1113,8 @@ void callback_save_sound_function() { #endif menu_system.change_menu(save_sound_menu); menu_position[MAIN] = encoder_value[RIGHT_ENCODER]; - encoder_value[RIGHT_ENCODER] = configuration.sound; - enc[RIGHT_ENCODER].write(configuration.sound, 1, 99); + encoder_value[RIGHT_ENCODER] = sound; + enc[RIGHT_ENCODER].write(sound, 1, 99); menu_system.update(); } @@ -1650,6 +1650,7 @@ void init_menus(void) // setup encoder for (i = 0; i < NUM_ENCODER; i++) but[i].update(); + master_volume = EEPROM.read(EEPROM_MASTER_VOLUME); enc[LEFT_ENCODER].write(master_volume, ENC_MASTER_VOLUME_MIN, ENC_MASTER_VOLUME_MAX); encoder_value[LEFT_ENCODER] = master_volume; enc[RIGHT_ENCODER].write(0, 0, NUM_MAIN_MENUS - 1); @@ -1658,7 +1659,7 @@ void init_menus(void) void load_sound(uint8_t value) { - configuration.sound = value; + sound = value; #ifdef DEBUG Serial.print(F("Load sound ")); Serial.println(value); @@ -1672,7 +1673,7 @@ void save_sound(uint8_t value) Serial.print(F("Save sound ")); Serial.println(value); #endif - eeprom_write(); + eeprom_config_write(value); } void set_decay(uint8_t value) @@ -2440,18 +2441,18 @@ void handle_ui(void) // load sound #ifdef DEBUG Serial.print("Load sound "); - Serial.println(configuration.sound); + Serial.println(sound); #endif - load_sound(configuration.sound); + load_sound(sound); } else if (menu_system.get_currentScreen() == &save_sound_screen) { // save sound #ifdef DEBUG Serial.print("Save sound "); - Serial.println(configuration.sound); + Serial.println(sound); #endif - save_sound(configuration.sound); + save_sound(sound); } else { diff --git a/config.h b/config.h index ea7c921..0506438 100644 --- a/config.h +++ b/config.h @@ -127,6 +127,9 @@ // EEPROM address #define EEPROM_START_ADDRESS 0 +#define EEPROM_MASTER_VOLUME EEPROM_START_ADDRESS +#define EEPROM_SOUND EEPROM_START_ADDRESS+1 +#define EEPROM_CONFIGURATIONS EEPROM_SOUND+1 //************************************************************************************************* //* DO NO CHANGE ANYTHING BEYOND IF YOU DON'T KNOW WHAT YOU ARE DOING !!! @@ -134,6 +137,7 @@ #define CONTROL_RATE_MS 100 #define BACK_TO_MAIN_MS 800 +#define STORE_MASTER_VOLUME_MS 5000 // Encoder min/max values #define ENC_DECAY_MIN 0 @@ -294,7 +298,6 @@ // struct for holding the current configuration struct config_t { uint32_t checksum; - uint8_t sound; uint8_t decay; uint8_t release; uint8_t hardness;