From 494067d83a7574d236a0df6f864e12166f2a0717 Mon Sep 17 00:00:00 2001 From: bdring Date: Tue, 27 Mar 2018 14:00:08 -0500 Subject: [PATCH] Fixed EEPROM Crashing Turning off the sound interrupt durring EEPROM writing fixed the problem. --- TWANG32.ino | 2 +- settings.h | 42 +++++++++++++++++++++++------------------- sound.h | 15 +++++++++++++-- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/TWANG32.ino b/TWANG32.ino index 3d26208..5a26193 100644 --- a/TWANG32.ino +++ b/TWANG32.ino @@ -37,7 +37,7 @@ #define CLOCK_PIN 17 #define LED_TYPE APA102 //#define LED_COLOR_ORDER BGR -#define NUM_LEDS 288 +#define NUM_LEDS 144 // what type of LED Strip....pick one #define USE_APA102 diff --git a/settings.h b/settings.h index fd5b536..e70e116 100644 --- a/settings.h +++ b/settings.h @@ -2,6 +2,7 @@ #define SETTINGS_H #include +#include "sound.h" // change this whenever the saved settings are not compatible with a change // It forces a reset from defaults. @@ -22,7 +23,7 @@ const uint8_t LIVES_PER_LEVEL = 3; // default lives per level #define JOYSTICK_DEADZONE 8 // Angle to ignore // AUDIO -#define MAX_VOLUME 5 // 0 to 255 +#define MAX_VOLUME 180 // 0 to 255 #define DAC_AUDIO_PIN 25 // should be 25 or 26 only enum ErrorNums{ @@ -108,6 +109,9 @@ void processSerial(char inChar) case 'R': readIndex = 0; reset_settings(); + settings_eeprom_write(); + delay(1000); + ESP.restart(); return; break; @@ -116,18 +120,9 @@ void processSerial(char inChar) user_settings.total_points = 0; user_settings.high_score = 0; user_settings.boss_kills = 0; - return; - break; - - case '!': - ESP.restart(); - break; - - case 'E': settings_eeprom_write(); - delay(1000); return; - break; + break; default: @@ -163,7 +158,7 @@ void change_setting(char *line) { char setting_val[6]; char param; uint16_t newValue; - Serial.print("Read Buffer: "); Serial.println(readBuffer); + //Serial.print("Read Buffer: "); Serial.println(readBuffer); if (readBuffer[1] != '='){ // check if the equals sign is there Serial.print("Missing '=' in command"); @@ -192,8 +187,12 @@ void change_setting(char *line) { switch (param) { case 'B': // brightness - if(newValue >=5 && newValue <=255) + if(newValue >=5 && newValue <=255) { user_settings.led_brightness = (uint8_t)newValue; + settings_eeprom_write(); + delay(1000); + ESP.restart(); // this one requires a restart right now + } else { printError(ERR_SETTING_RANGE); return; @@ -242,7 +241,7 @@ void change_setting(char *line) { return; break; - } + } show_settings_menu(); } @@ -276,7 +275,7 @@ void show_settings_menu() { Serial.print("\r\nB="); Serial.print(user_settings.led_brightness); - Serial.println(" (LED Brightness 5-255)"); + Serial.println(" (LED Brightness 5-255..forces restart)"); Serial.print("S="); Serial.print(user_settings.audio_volume); @@ -298,8 +297,8 @@ void show_settings_menu() { Serial.println(" ? to show current settings"); Serial.println(" R to reset everything to defaults)"); Serial.println(" P to reset play statistics)"); - Serial.println(" E to write changes to eeprom)"); - Serial.println(" ! to restart with current settings"); + //Serial.println(" E to write changes to eeprom)"); + //Serial.println(" ! to restart with current settings"); show_game_stats(); } @@ -345,7 +344,9 @@ void settings_eeprom_read() } -void settings_eeprom_write() { +void settings_eeprom_write() { + + sound_pause(); // prevent interrupt from causing crash EEPROM.begin(EEPROM_SIZE); @@ -363,6 +364,9 @@ void settings_eeprom_write() { EEPROM.commit(); EEPROM.end(); + + sound_resume(); // restore sound interrupt + } void printError(int reason) { @@ -380,4 +384,4 @@ void printError(int reason) { } } -#endif \ No newline at end of file +#endif diff --git a/sound.h b/sound.h index 9af92f1..d0dbfdb 100644 --- a/sound.h +++ b/sound.h @@ -7,7 +7,8 @@ * * */ - +#ifndef SOUND_H + #define SOUND_H #include "esp32-hal-timer.h"; @@ -55,6 +56,16 @@ void sound_init(int pin){ // pin must be a DAC pin number !! (typically 25 or 2 timerAlarmEnable(sndTimer); } +void sound_pause() // this prevents the interrupt from firing ... use during eeprom write +{ + timerStop(sndTimer); +} + +void sound_resume() // resume from pause ... after eeprom write +{ + timerRestart(sndTimer); +} + bool sound(uint16_t freq, uint8_t volume){ if (volume == 0) { soundOff(); @@ -77,7 +88,7 @@ void soundOff(){ - +#endif