From 4eb4bee9f715c36ea9d56c27584db414abffefea Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Fri, 5 Oct 2018 13:41:39 +0200 Subject: [PATCH] =?UTF-8?q?Timing=20optimization=20while=20writing=20data?= =?UTF-8?q?=20to=20EEPROM:=20Now=20everything=20is=20written=20with=20some?= =?UTF-8?q?=20time=20between=20the=20write=20updates=20for=20avoiding=20dr?= =?UTF-8?q?op=20outs=20due=20to=20long=20u=C3=BCdate=20operations.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MicroDexed.ino | 110 +++++++++++++++++++++++++++++++++++++------------ UI.cpp | 24 ++++++----- UI.h | 6 +-- config.h | 9 ++++ 4 files changed, 109 insertions(+), 40 deletions(-) diff --git a/MicroDexed.ino b/MicroDexed.ino index 568116d..fdecc22 100644 --- a/MicroDexed.ino +++ b/MicroDexed.ino @@ -97,6 +97,10 @@ char bank_name[BANK_NAME_LEN]; char voice_name[VOICE_NAME_LEN]; char bank_names[MAX_BANKS][BANK_NAME_LEN]; char voice_names[MAX_VOICES][VOICE_NAME_LEN]; +elapsedMillis autostore; +uint8_t eeprom_update_status = 0; +uint16_t autostore_value = AUTOSTORE_MS; + #ifdef MASTER_KEY_MIDI bool master_key_enabled = false; #endif @@ -315,6 +319,13 @@ void loop() queue1.playBuffer(); } + // EEPROM update handling + if (eeprom_update_status > 0 && autostore >= autostore_value) + { + autostore=0; + eeprom_update(); + } + // MIDI input handling handle_input(); #ifdef I2C_DISPLAY @@ -413,7 +424,7 @@ bool handle_master_key(uint8_t data) Serial.print(F("Loading voice number ")); Serial.println(num, DEC); #endif - eeprom_write_sound(); + eeprom_write(EEPROM_UPDATE_VOICE); #ifdef I2C_DISPLAY lcd.show(1, 0, 2, voice + 1); lcd.show(1, 2, 1, " "); @@ -445,6 +456,7 @@ bool handle_master_key(uint8_t data) Serial.print(F("Bank switch to: ")); Serial.println(bank, DEC); #endif + eeprom_write(EEPROM_UPDATE_BANK); #ifdef I2C_DISPLAY if (get_voice_names_from_bank(bank)) { @@ -588,9 +600,6 @@ void set_volume(float v, float vr, float vl) vol_right = vr; vol_left = vl; -#ifndef I2C_DISPLAY - eeprom_write_volume(); -#endif #ifdef DEBUG uint8_t tmp; Serial.print(F("Setting volume: VOL=")); @@ -708,9 +717,7 @@ void initial_values_from_eeprom(void) #ifdef DEBUG Serial.print(F(" - mismatch -> initializing EEPROM!")); #endif - eeprom_write_sound(); - eeprom_write_volume(); - eeprom_write_midichannel(); + eeprom_write(EEPROM_UPDATE_BANK & EEPROM_UPDATE_VOICE & EEPROM_UPDATE_VOL & EEPROM_UPDATE_VOL_R & EEPROM_UPDATE_VOL_L & EEPROM_UPDATE_MIDICHANNEL); } else { @@ -768,40 +775,91 @@ uint32_t eeprom_crc32(uint16_t calc_start, uint16_t calc_bytes) // base code fro return (crc); } -void eeprom_write_sound(void) +void eeprom_write(uint8_t status) { - EEPROM.update(EEPROM_OFFSET + EEPROM_BANK_ADDR, bank); - EEPROM.update(EEPROM_OFFSET + EEPROM_VOICE_ADDR, voice); - update_eeprom_checksum(); + eeprom_update_status |= status; + if (eeprom_update_status != 0) + autostore = 0; #ifdef DEBUG - Serial.println(F("Sound written to EEPROM")); + Serial.print(F("Updating EEPROM to state to: ")); + Serial.println(eeprom_update_status); #endif } -void eeprom_write_volume(void) +void eeprom_update(void) { - EEPROM.update(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR, uint8_t(vol * UCHAR_MAX)); - EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR, uint8_t(vol_right * UCHAR_MAX)); - EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR, uint8_t(vol_left * UCHAR_MAX)); - update_eeprom_checksum(); + autostore_value = AUTOSTORE_DIST_MS; + + if (eeprom_update_status & EEPROM_UPDATE_BANK) + { + EEPROM.update(EEPROM_OFFSET + EEPROM_BANK_ADDR, bank); #ifdef DEBUG - Serial.println(F("Volume written to EEPROM")); + Serial.println(F("Bank written to EEPROM")); #endif -} - -void eeprom_write_midichannel(void) -{ - EEPROM.update(EEPROM_OFFSET + EEPROM_MIDICHANNEL_ADDR, midi_channel); - update_eeprom_checksum(); + eeprom_update_status &= ~EEPROM_UPDATE_BANK; + } + else if (eeprom_update_status & EEPROM_UPDATE_VOICE) + { + EEPROM.update(EEPROM_OFFSET + EEPROM_VOICE_ADDR, voice); #ifdef DEBUG - Serial.println(F("MIDI channel written to EEPROM")); + Serial.println(F("Voice written to EEPROM")); #endif + eeprom_update_status &= ~EEPROM_UPDATE_VOICE; + } + else if (eeprom_update_status & EEPROM_UPDATE_VOL) + { + EEPROM.update(EEPROM_OFFSET + EEPROM_MASTER_VOLUME_ADDR, uint8_t(vol * UCHAR_MAX)); +#ifdef DEBUG + Serial.println(F("Volume written to EEPROM")); +#endif + eeprom_update_status &= ~EEPROM_UPDATE_VOL; + } + else if (eeprom_update_status & EEPROM_UPDATE_VOL_R) + { + EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_RIGHT_ADDR, uint8_t(vol_right * UCHAR_MAX)); +#ifdef DEBUG + Serial.println(F("Volume right written to EEPROM")); +#endif + eeprom_update_status &= ~EEPROM_UPDATE_VOL_R; + } + else if (eeprom_update_status & EEPROM_UPDATE_VOL_L) + { + EEPROM.update(EEPROM_OFFSET + EEPROM_VOLUME_LEFT_ADDR, uint8_t(vol_left * UCHAR_MAX)); +#ifdef DEBUG + Serial.println(F("Volume left written to EEPROM")); +#endif + eeprom_update_status &= ~EEPROM_UPDATE_VOL_L; + } + else if (eeprom_update_status & EEPROM_UPDATE_MIDICHANNEL ) + { + EEPROM.update(EEPROM_OFFSET + EEPROM_MIDICHANNEL_ADDR, midi_channel); + update_eeprom_checksum(); +#ifdef DEBUG + Serial.println(F("MIDI channel written to EEPROM")); +#endif + eeprom_update_status &= ~EEPROM_UPDATE_MIDICHANNEL; + } + else if (eeprom_update_status & EEPROM_UPDATE_CHECKSUM) + { + update_eeprom_checksum(); +#ifdef DEBUG + Serial.println(F("Checksum written to EEPROM")); +#endif + eeprom_update_status &= ~EEPROM_UPDATE_CHECKSUM; + autostore_value = AUTOSTORE_MS; + return; + } + + if (eeprom_update_status == 0) + eeprom_update_status |= EEPROM_UPDATE_CHECKSUM; } #if defined (DEBUG) && defined (SHOW_CPU_LOAD_MSEC) void show_cpu_and_mem_usage(void) { - Serial.print(F("CPU: ")); + Serial.print(F("EEPROM state: ")); + Serial.print(eeprom_update_status, DEC); + Serial.print(F(" CPU: ")); Serial.print(AudioProcessorUsage(), 2); Serial.print(F("% CPU MAX: ")); Serial.print(AudioProcessorUsageMax(), 2); diff --git a/UI.cpp b/UI.cpp index 4e36d8a..6a5dc25 100644 --- a/UI.cpp +++ b/UI.cpp @@ -33,7 +33,6 @@ #ifdef I2C_DISPLAY // selecting sounds by encoder, button and display elapsedMillis ui_back_to_main; -elapsedMillis autostore_sound; void handle_ui(void) { @@ -41,18 +40,21 @@ void handle_ui(void) { enc[0].write(map(vol * 100, 0, 100, 0, ENC_VOL_STEPS)); enc_val[0] = enc[0].read(); + /* switch (ui_main_state) + { + case UI_VOLUME: + eeprom_write(EEPROM_UPDATE_VOL); + break; + case UI_MIDICHANNEL: + eeprom_write(EEPROM_UPDATE_MIDICHANNEL); + break; + }*/ ui_show_main(); - eeprom_write_volume(); - eeprom_write_midichannel(); } - if (autostore_sound >= AUTOSTORE_MS && (ui_main_state == UI_MAIN_VOICE_SELECTED || ui_main_state == UI_MAIN_BANK_SELECTED)) + if (autostore >= AUTOSTORE_MS && (ui_main_state == UI_MAIN_VOICE_SELECTED || ui_main_state == UI_MAIN_BANK_SELECTED)) { -#ifdef DEBUG - Serial.println(F("Autostore triggered")); -#endif ui_show_main(); - eeprom_write_sound(); switch (ui_main_state) { case UI_MAIN_VOICE_SELECTED: @@ -134,6 +136,7 @@ void handle_ui(void) else if (enc[i].read() >= ENC_VOL_STEPS) enc[i].write(ENC_VOL_STEPS); set_volume(float(map(enc[i].read(), 0, ENC_VOL_STEPS, 0, 100)) / 100, vol_left, vol_right); + eeprom_write(EEPROM_UPDATE_VOL); ui_show_volume(); break; case UI_MIDICHANNEL: @@ -142,6 +145,7 @@ void handle_ui(void) else if (enc[i].read() >= 16) enc[i].write(16); midi_channel = enc[i].read(); + eeprom_write(EEPROM_UPDATE_MIDICHANNEL); ui_show_midichannel(); break; } @@ -159,7 +163,7 @@ void handle_ui(void) bank = enc[i].read(); get_voice_names_from_bank(bank); load_sysex(bank, voice); - autostore_sound = 0; + eeprom_write(EEPROM_UPDATE_BANK); break; case UI_MAIN_VOICE: ui_main_state = UI_MAIN_VOICE_SELECTED; @@ -188,7 +192,7 @@ void handle_ui(void) } voice = enc[i].read(); load_sysex(bank, voice); - autostore_sound = 0; + eeprom_write(EEPROM_UPDATE_VOICE); break; } ui_show_main(); diff --git a/UI.h b/UI.h index 57a6496..3e885dc 100644 --- a/UI.h +++ b/UI.h @@ -48,11 +48,9 @@ extern char voice_name[VOICE_NAME_LEN]; extern uint8_t ui_state; extern uint8_t ui_main_state; extern uint8_t midi_channel; -extern void update_eeprom_checksum(void); +extern void eeprom_write(uint8_t status); extern void set_volume(float v, float vr, float vl); -extern void eeprom_write_sound(void); -extern void eeprom_write_volume(void); -extern void eeprom_write_midichannel(void); +extern elapsedMillis autostore; void handle_ui(void); void ui_show_main(void); diff --git a/config.h b/config.h index 06c672d..e6cdf88 100644 --- a/config.h +++ b/config.h @@ -97,6 +97,7 @@ #define LCD_LINES 2 #define UI_AUTO_BACK_MS 3000 #define AUTOSTORE_MS 5000 +#define AUTOSTORE_DIST_MS 50 // Encoder with button #define ENC_VOL_STEPS 43 @@ -123,4 +124,12 @@ #define EEPROM_VOLUME_RIGHT_ADDR 3 #define EEPROM_VOLUME_LEFT_ADDR 4 #define EEPROM_MIDICHANNEL_ADDR 5 + +#define EEPROM_UPDATE_BANK (1<<0) +#define EEPROM_UPDATE_VOICE (1<<1) +#define EEPROM_UPDATE_VOL (1<<2) +#define EEPROM_UPDATE_VOL_R (1<<3) +#define EEPROM_UPDATE_VOL_L (1<<4) +#define EEPROM_UPDATE_MIDICHANNEL (1<<5) +#define EEPROM_UPDATE_CHECKSUM (1<<7) #endif