Cleanup and added joystick button pins
This commit is contained in:
@@ -8,16 +8,16 @@
|
|||||||
|
|
||||||
// change this whenever the saved settings are not compatible with a change
|
// change this whenever the saved settings are not compatible with a change
|
||||||
// It forces a reset from defaults.
|
// It forces a reset from defaults.
|
||||||
#define SETTINGS_VERSION 2
|
#define SETTINGS_VERSION 2
|
||||||
#define EEPROM_SIZE 256
|
#define EEPROM_SIZE 256
|
||||||
|
|
||||||
// LEDS
|
// LEDS
|
||||||
#define NUM_LEDS 144
|
#define NUM_LEDS 144
|
||||||
#define MIN_LEDS 60
|
#define MIN_LEDS 60
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define DEFAULT_BRIGHTNESS 150
|
#define DEFAULT_BRIGHTNESS 50
|
||||||
#define MIN_BRIGHTNESS 10
|
#define MIN_BRIGHTNESS 10
|
||||||
#define MAX_BRIGHTNESS 255
|
#define MAX_BRIGHTNESS 255
|
||||||
|
|
||||||
@@ -34,6 +34,11 @@ const uint8_t LIVES_PER_LEVEL = 3; // default lives per level
|
|||||||
#define MIN_ATTACK_THRESHOLD 20000
|
#define MIN_ATTACK_THRESHOLD 20000
|
||||||
#define MAX_ATTACK_THRESHOLD 30000
|
#define MAX_ATTACK_THRESHOLD 30000
|
||||||
|
|
||||||
|
#define leftButtonPinNumber 26
|
||||||
|
#define rightButtonPinNumber 18
|
||||||
|
#define upButtonPinNumber 19
|
||||||
|
#define downButtonPinNumber 23
|
||||||
|
|
||||||
|
|
||||||
#define DEFAULT_JOYSTICK_DEADZONE 8 // Angle to ignore
|
#define DEFAULT_JOYSTICK_DEADZONE 8 // Angle to ignore
|
||||||
#define MIN_JOYSTICK_DEADZONE 3
|
#define MIN_JOYSTICK_DEADZONE 3
|
||||||
@@ -73,24 +78,24 @@ SemaphoreHandle_t xMutex;
|
|||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t settings_version; // stores the settings format version
|
uint8_t settings_version; // stores the settings format version
|
||||||
|
|
||||||
uint16_t led_count;
|
uint16_t led_count;
|
||||||
uint8_t led_brightness;
|
uint8_t led_brightness;
|
||||||
|
|
||||||
uint8_t joystick_deadzone;
|
uint8_t joystick_deadzone;
|
||||||
uint16_t attack_threshold;
|
uint16_t attack_threshold;
|
||||||
|
|
||||||
uint8_t audio_volume;
|
uint8_t audio_volume;
|
||||||
|
|
||||||
uint8_t lives_per_level;
|
uint8_t lives_per_level;
|
||||||
|
|
||||||
// saved statistics
|
// saved statistics
|
||||||
uint16_t games_played;
|
uint16_t games_played;
|
||||||
uint32_t total_points;
|
uint32_t total_points;
|
||||||
uint16_t high_score;
|
uint16_t high_score;
|
||||||
uint16_t boss_kills;
|
uint16_t boss_kills;
|
||||||
|
|
||||||
}settings_t;
|
}settings_t;
|
||||||
|
|
||||||
settings_t user_settings;
|
settings_t user_settings;
|
||||||
@@ -100,10 +105,10 @@ settings_t user_settings;
|
|||||||
char readBuffer[READ_BUFFER_LEN];
|
char readBuffer[READ_BUFFER_LEN];
|
||||||
uint8_t readIndex = 0;
|
uint8_t readIndex = 0;
|
||||||
|
|
||||||
void settings_init() {
|
void settings_init() {
|
||||||
|
|
||||||
settings_eeprom_read();
|
settings_eeprom_read();
|
||||||
show_settings_menu();
|
show_settings_menu();
|
||||||
show_game_stats();
|
show_game_stats();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,38 +128,38 @@ void processSerial(char inChar)
|
|||||||
show_settings_menu();
|
show_settings_menu();
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'R':
|
case 'R':
|
||||||
readIndex = 0;
|
readIndex = 0;
|
||||||
reset_settings();
|
reset_settings();
|
||||||
settings_eeprom_write();
|
settings_eeprom_write();
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'P':
|
case 'P':
|
||||||
user_settings.games_played = 0;
|
user_settings.games_played = 0;
|
||||||
user_settings.total_points = 0;
|
user_settings.total_points = 0;
|
||||||
user_settings.high_score = 0;
|
user_settings.high_score = 0;
|
||||||
user_settings.boss_kills = 0;
|
user_settings.boss_kills = 0;
|
||||||
settings_eeprom_write();
|
settings_eeprom_write();
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '!':
|
case '!':
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
default:
|
default:
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (readBuffer[readIndex] == CARRIAGE_RETURN) {
|
if (readBuffer[readIndex] == CARRIAGE_RETURN) {
|
||||||
if (readIndex < 3) {
|
if (readIndex < 3) {
|
||||||
// not enough characters
|
// not enough characters
|
||||||
readIndex = 0;
|
readIndex = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
readBuffer[readIndex] = 0; // mark it as the end of the string
|
readBuffer[readIndex] = 0; // mark it as the end of the string
|
||||||
change_setting_serial(readBuffer);
|
change_setting_serial(readBuffer);
|
||||||
readIndex = 0;
|
readIndex = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,21 +174,21 @@ void change_setting_serial(char *line) {
|
|||||||
// line formate should be ss=nn
|
// line formate should be ss=nn
|
||||||
// ss is always a 2 character integer
|
// ss is always a 2 character integer
|
||||||
// nn starts at index 3 and can be up to a 5 character unsigned integer
|
// nn starts at index 3 and can be up to a 5 character unsigned integer
|
||||||
|
|
||||||
//12=12345
|
//12=12345
|
||||||
//01234567
|
//01234567
|
||||||
|
|
||||||
char setting_val[6];
|
char setting_val[6];
|
||||||
char param;
|
char param;
|
||||||
uint16_t newValue;
|
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
|
if (readBuffer[1] != '='){ // check if the equals sign is there
|
||||||
Serial.print("Missing '=' in command");
|
Serial.print("Missing '=' in command");
|
||||||
readIndex = 0;
|
readIndex = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// move the value characters into a char array while verifying they are digits
|
// move the value characters into a char array while verifying they are digits
|
||||||
for(int i=0; i<5; i++) {
|
for(int i=0; i<5; i++) {
|
||||||
if (i+2 < readIndex) {
|
if (i+2 < readIndex) {
|
||||||
@@ -192,96 +197,96 @@ void change_setting_serial(char *line) {
|
|||||||
else {
|
else {
|
||||||
Serial.println("Invalid setting value");
|
Serial.println("Invalid setting value");
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
setting_val[i] = 0;
|
setting_val[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
param = readBuffer[0];
|
param = readBuffer[0];
|
||||||
newValue = atoi(setting_val); // convert the val section to an integer
|
newValue = atoi(setting_val); // convert the val section to an integer
|
||||||
|
|
||||||
memset(readBuffer,0,sizeof(readBuffer));
|
memset(readBuffer,0,sizeof(readBuffer));
|
||||||
|
|
||||||
change_setting(param, newValue);
|
change_setting(param, newValue);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void change_setting(char paramCode, uint16_t newValue)
|
void change_setting(char paramCode, uint16_t newValue)
|
||||||
{
|
{
|
||||||
switch (paramCode) {
|
switch (paramCode) {
|
||||||
|
|
||||||
|
|
||||||
lastInputTime = millis(); // reset screensaver count
|
lastInputTime = millis(); // reset screensaver count
|
||||||
|
|
||||||
case 'C': // LED Count
|
case 'C': // LED Count
|
||||||
user_settings.led_count = constrain(newValue, MIN_LEDS, MAX_LEDS);
|
user_settings.led_count = constrain(newValue, MIN_LEDS, MAX_LEDS);
|
||||||
settings_eeprom_write();
|
settings_eeprom_write();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'B': // brightness
|
case 'B': // brightness
|
||||||
user_settings.led_brightness = constrain(newValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
|
user_settings.led_brightness = constrain(newValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS);
|
||||||
FastLED.setBrightness(user_settings.led_brightness);
|
FastLED.setBrightness(user_settings.led_brightness);
|
||||||
settings_eeprom_write();
|
settings_eeprom_write();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'S': // sound
|
case 'S': // sound
|
||||||
user_settings.audio_volume = constrain(newValue, MIN_VOLUME, MAX_VOLUME);
|
user_settings.audio_volume = constrain(newValue, MIN_VOLUME, MAX_VOLUME);
|
||||||
settings_eeprom_write();
|
settings_eeprom_write();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'D': // deadzone, joystick
|
case 'D': // deadzone, joystick
|
||||||
user_settings.joystick_deadzone = constrain(newValue, MIN_JOYSTICK_DEADZONE, MAX_JOYSTICK_DEADZONE);
|
user_settings.joystick_deadzone = constrain(newValue, MIN_JOYSTICK_DEADZONE, MAX_JOYSTICK_DEADZONE);
|
||||||
settings_eeprom_write();
|
settings_eeprom_write();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'A': // attack threshold, joystick
|
case 'A': // attack threshold, joystick
|
||||||
user_settings.attack_threshold = constrain(newValue, MIN_ATTACK_THRESHOLD, MAX_ATTACK_THRESHOLD);
|
user_settings.attack_threshold = constrain(newValue, MIN_ATTACK_THRESHOLD, MAX_ATTACK_THRESHOLD);
|
||||||
settings_eeprom_write();
|
settings_eeprom_write();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'L': // lives per level
|
case 'L': // lives per level
|
||||||
user_settings.lives_per_level = constrain(newValue, MIN_LIVES_PER_LEVEL, MAX_LIVES_PER_LEVEL);
|
user_settings.lives_per_level = constrain(newValue, MIN_LIVES_PER_LEVEL, MAX_LIVES_PER_LEVEL);
|
||||||
settings_eeprom_write();
|
settings_eeprom_write();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
Serial.print("Command Error: ");
|
Serial.print("Command Error: ");
|
||||||
Serial.println(readBuffer[0]);
|
Serial.println(readBuffer[0]);
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
show_settings_menu();
|
show_settings_menu();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset_settings() {
|
void reset_settings() {
|
||||||
|
|
||||||
|
|
||||||
user_settings.settings_version = SETTINGS_VERSION;
|
user_settings.settings_version = SETTINGS_VERSION;
|
||||||
|
|
||||||
user_settings.led_count = NUM_LEDS;
|
user_settings.led_count = NUM_LEDS;
|
||||||
user_settings.led_brightness = DEFAULT_BRIGHTNESS;
|
user_settings.led_brightness = DEFAULT_BRIGHTNESS;
|
||||||
|
|
||||||
user_settings.joystick_deadzone = DEFAULT_JOYSTICK_DEADZONE;
|
user_settings.joystick_deadzone = DEFAULT_JOYSTICK_DEADZONE;
|
||||||
user_settings.attack_threshold = DEFAULT_ATTACK_THRESHOLD;
|
user_settings.attack_threshold = DEFAULT_ATTACK_THRESHOLD;
|
||||||
|
|
||||||
user_settings.audio_volume = DEFAULT_VOLUME;
|
user_settings.audio_volume = DEFAULT_VOLUME;
|
||||||
|
|
||||||
user_settings.lives_per_level = LIVES_PER_LEVEL;
|
user_settings.lives_per_level = LIVES_PER_LEVEL;
|
||||||
|
|
||||||
user_settings.games_played = 0;
|
user_settings.games_played = 0;
|
||||||
user_settings.total_points = 0;
|
user_settings.total_points = 0;
|
||||||
user_settings.high_score = 0;
|
user_settings.high_score = 0;
|
||||||
user_settings.boss_kills = 0;
|
user_settings.boss_kills = 0;
|
||||||
|
|
||||||
Serial.println("Settings reset...");
|
Serial.println("Settings reset...");
|
||||||
|
|
||||||
settings_eeprom_write();
|
settings_eeprom_write();
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_settings_menu() {
|
void show_settings_menu() {
|
||||||
@@ -290,34 +295,34 @@ void show_settings_menu() {
|
|||||||
Serial.println("= Send new values like B=150 =");
|
Serial.println("= Send new values like B=150 =");
|
||||||
Serial.println("= with a carriage return =");
|
Serial.println("= with a carriage return =");
|
||||||
Serial.println("===================================");
|
Serial.println("===================================");
|
||||||
|
|
||||||
Serial.print("\r\nC=");
|
Serial.print("\r\nC=");
|
||||||
Serial.print(user_settings.led_count);
|
Serial.print(user_settings.led_count);
|
||||||
|
|
||||||
Serial.print(" (LED Count 60-");
|
Serial.print(" (LED Count 60-");
|
||||||
Serial.print(MAX_LEDS);
|
Serial.print(MAX_LEDS);
|
||||||
Serial.println(")");
|
Serial.println(")");
|
||||||
|
|
||||||
Serial.print("B=");
|
Serial.print("B=");
|
||||||
Serial.print(user_settings.led_brightness);
|
Serial.print(user_settings.led_brightness);
|
||||||
Serial.println(" (LED Brightness 5-255)");
|
Serial.println(" (LED Brightness 5-255)");
|
||||||
|
|
||||||
Serial.print("S=");
|
Serial.print("S=");
|
||||||
Serial.print(user_settings.audio_volume);
|
Serial.print(user_settings.audio_volume);
|
||||||
Serial.println(" (Sound Volume 0-255)");
|
Serial.println(" (Sound Volume 0-255)");
|
||||||
|
|
||||||
Serial.print("D=");
|
Serial.print("D=");
|
||||||
Serial.print(user_settings.joystick_deadzone);
|
Serial.print(user_settings.joystick_deadzone);
|
||||||
Serial.println(" (Joystick Deadzone 3-12)");
|
Serial.println(" (Joystick Deadzone 3-12)");
|
||||||
|
|
||||||
Serial.print("A=");
|
Serial.print("A=");
|
||||||
Serial.print(user_settings.attack_threshold);
|
Serial.print(user_settings.attack_threshold);
|
||||||
Serial.println(" (Attack Sensitivity 20000-35000)");
|
Serial.println(" (Attack Sensitivity 20000-35000)");
|
||||||
|
|
||||||
Serial.print("L=");
|
Serial.print("L=");
|
||||||
Serial.print(user_settings.lives_per_level);
|
Serial.print(user_settings.lives_per_level);
|
||||||
Serial.println(" (Lives per Level (3-9))");
|
Serial.println(" (Lives per Level (3-9))");
|
||||||
|
|
||||||
Serial.println("\r\n(Send...)");
|
Serial.println("\r\n(Send...)");
|
||||||
Serial.println(" ? to show current settings");
|
Serial.println(" ? to show current settings");
|
||||||
Serial.println(" R to reset everything to defaults)");
|
Serial.println(" R to reset everything to defaults)");
|
||||||
@@ -332,13 +337,13 @@ void show_game_stats()
|
|||||||
Serial.print("Average Score: ");Serial.println(user_settings.total_points / user_settings.games_played);
|
Serial.print("Average Score: ");Serial.println(user_settings.total_points / user_settings.games_played);
|
||||||
}
|
}
|
||||||
Serial.print("High Score: ");Serial.println(user_settings.high_score);
|
Serial.print("High Score: ");Serial.println(user_settings.high_score);
|
||||||
Serial.print("Boss kills: ");Serial.println(user_settings.boss_kills);
|
Serial.print("Boss kills: ");Serial.println(user_settings.boss_kills);
|
||||||
}
|
}
|
||||||
|
|
||||||
void settings_eeprom_read() {
|
void settings_eeprom_read() {
|
||||||
|
|
||||||
EEPROM.begin(EEPROM_SIZE);
|
EEPROM.begin(EEPROM_SIZE);
|
||||||
|
|
||||||
uint8_t ver = EEPROM.read(0);
|
uint8_t ver = EEPROM.read(0);
|
||||||
uint8_t temp[sizeof(user_settings)];
|
uint8_t temp[sizeof(user_settings)];
|
||||||
|
|
||||||
@@ -346,49 +351,49 @@ void settings_eeprom_read() {
|
|||||||
Serial.print("Error: EEPROM settings read failed:"); Serial.println(ver);
|
Serial.print("Error: EEPROM settings read failed:"); Serial.println(ver);
|
||||||
Serial.println("Loading defaults...");
|
Serial.println("Loading defaults...");
|
||||||
EEPROM.end();
|
EEPROM.end();
|
||||||
reset_settings();
|
reset_settings();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Serial.print("Settings version: "); Serial.println(ver);
|
Serial.print("Settings version: "); Serial.println(ver);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i=0; i<sizeof(user_settings); i++)
|
for (int i=0; i<sizeof(user_settings); i++)
|
||||||
{
|
{
|
||||||
temp[i] = EEPROM.read(i);
|
temp[i] = EEPROM.read(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
EEPROM.end();
|
EEPROM.end();
|
||||||
|
|
||||||
memcpy((char*)&user_settings, temp, sizeof(user_settings));
|
memcpy((char*)&user_settings, temp, sizeof(user_settings));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void settings_eeprom_write() {
|
void settings_eeprom_write() {
|
||||||
|
|
||||||
sound_pause(); // prevent interrupt from causing crash
|
sound_pause(); // prevent interrupt from causing crash
|
||||||
|
|
||||||
EEPROM.begin(EEPROM_SIZE);
|
EEPROM.begin(EEPROM_SIZE);
|
||||||
|
|
||||||
uint8_t temp[sizeof(user_settings)];
|
uint8_t temp[sizeof(user_settings)];
|
||||||
memcpy(temp, (uint8_t*)&user_settings, sizeof(user_settings));
|
memcpy(temp, (uint8_t*)&user_settings, sizeof(user_settings));
|
||||||
|
|
||||||
for (int i=0; i<sizeof(user_settings); i++)
|
for (int i=0; i<sizeof(user_settings); i++)
|
||||||
{
|
{
|
||||||
EEPROM.write(i, temp[i]);
|
EEPROM.write(i, temp[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
EEPROM.end();
|
EEPROM.end();
|
||||||
|
|
||||||
sound_resume(); // restore sound interrupt
|
sound_resume(); // restore sound interrupt
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void printError(int reason) {
|
void printError(int reason) {
|
||||||
|
|
||||||
switch(reason) {
|
switch(reason) {
|
||||||
case ERR_SETTING_NUM:
|
case ERR_SETTING_NUM:
|
||||||
Serial.print("Error: Invalid setting number");
|
Serial.print("Error: Invalid setting number");
|
||||||
@@ -399,7 +404,7 @@ void printError(int reason) {
|
|||||||
default:
|
default:
|
||||||
Serial.print("Error:");Serial.println(reason);
|
Serial.print("Error:");Serial.println(reason);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user