menu, presets and actions

This commit is contained in:
Jeremie GABOLDE 2022-09-18 14:38:00 +02:00
parent d8e51001b3
commit 38e3882d6a
3 changed files with 418 additions and 37 deletions

View File

@ -8,20 +8,76 @@
*/
#include <EEPROM.h>
int address = 0;
byte value;
struct MemSlot
{
char* displayName;
int adress;
String displayName;
int address;
byte value;
};
MemSlot memory[3] = //128 bytes for the Teensy LC
{ {"test1", 0, 0}};
MemSlot memory[] = //128 bytes for the Teensy LC
{
{"prst", 1, 0},
{"p1_1", 2, 63},
{"p1_2", 3, 63},
{"p1_3", 4, 63},
{"p1_4", 5, 63},
{"p1_5", 6, 63},
{"p1_6", 7, 63},
{"p1_7", 8, 63},
{"p1_8", 9, 63},
{"p1_9", 10, 63},
{"p1_a", 11, 63},
{"p1_b", 12, 63},
{"p1_c", 13, 63},
{"p1_d", 14, 63},
{"p1_e", 15, 63},
{"p1_f", 16, 63}
};
/*
* Menu
*/
bool isInMenu = false;
struct MenuItem
{
String path;
bool isValue;
String displayName;
};
MenuItem menuEntries[] =
{
{"", true, "prst"},
{"", false, "edit"},
{"/edit", false, "p1"},
{"/edit", false, "p2"},
{"/edit", false, "p3"},
{"/edit/p1", true, "p1_1"},
{"/edit/p1", true, "p1_2"},
{"/edit/p1", true, "p1_3"},
{"/edit/p1", true, "p1_4"},
{"/edit/p1", true, "p1_5"},
{"/edit/p1", true, "p1_6"},
{"/edit/p1", true, "p1_7"},
{"/edit/p1", true, "p1_8"},
{"/edit/p1", true, "p1_9"},
{"/edit/p1", true, "p1_a"},
//{"/edit/p1", true, "p1_b"},
/*{"/edit/p1", true, "p1_c"},
{"/edit/p1", true, "p1_d"},
{"/edit/p1", true, "p1_e"},
{"/edit/p1", true, "p1_f"},*/
};
String menuCurrentPath = "";
unsigned int menuSelectionIndex = 0;
unsigned int menuCurrentPathItems = 0;
MenuItem menuCurrentItem;
bool menuIsSettingValue = false;
/*
* 7 Segment 4 digits display
*/
@ -38,10 +94,10 @@ SevSeg sevseg;
const byte rows = 4; //four rows
const byte cols = 3; //three columns
char keys[rows][cols] = { // assiging arbitraty uid to each button
{1,5,9},
{2,6,10},
{3,7,11},
{4,8,12}
{9,5,1},
{10,6,2},
{11,7,3},
{12,8,4}
};
byte rowPins[rows] = {1, 2, 3, 0}; //connect to the row pinouts of the keypad
byte colPins[cols] = {21, 22, 23}; //connect to the column pinouts of the keypad
@ -59,6 +115,11 @@ byte rotaryEncoderState = 0;
const int BUTTON_Rotary = 4;
bool BUTTON_Rotary_lastState = HIGH;
/*
* mode presets and action
*/
#include "modeAction.h"
/*
* custom display animations
*/
@ -67,6 +128,13 @@ bool BUTTON_Rotary_lastState = HIGH;
int animationFrame = 0;
bool animationLooping = true;
/*
* time cooldown managment
*/
unsigned int lastInputMillis = 0;
unsigned int inputCoolDown = 50;
unsigned int sevSegCoolDown = 5000;
/*
* INIT
*/
@ -76,13 +144,29 @@ bool animationLooping = true;
Serial.begin(9600);
Serial.println("setup start");
//write eeprom first init
byte memValue = EEPROM.read(0);
if (memValue == 255) // cannot find settings, writes defualt ones
{
for (byte i = 0; i < (sizeof(memory)/sizeof(MemSlot)); i++)
{
EEPROM.write(memory[i].address, memory[i].value);
}
EEPROM.write(0,1);
}
//read eeprom
for (byte i = 0; i < (sizeof(memory)/sizeof(MemSlot)); i++)
{
memory[i].value = EEPROM.read(memory[i].address);
}
//Init display
byte numDigits = 4;
byte digitPins[] = {7, 8, 9, 6};
byte segmentPins[] = {10, 12, 14, 16, 17, 11, 13, 15};
bool resistorsOnSegments = true;
bool updateWithDelaysIn = true;
//bool updateWithDelaysIn = true;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
@ -96,7 +180,11 @@ bool animationLooping = true;
// Init keyboard output
Keyboard.begin();
//init cooldowns
lastInputMillis = millis();
Serial.println("setup complete");
}
/*
@ -105,32 +193,44 @@ bool animationLooping = true;
void loop()
{
//INPUTS
char customKey = keypad.getKey();
if (millis() > lastInputMillis + inputCoolDown)
{
char customKey = keypad.getKey();
rotaryEncoderState = rotaryEncoder.rotate();
rotaryEncoderState = rotaryEncoder.rotate();
//sevseg.refreshDisplay();
//Serial.println(customKey);
value = EEPROM.read(address);
if (customKey)
{
sendInputOuput(customKey);
}
Serial.print(address);
Serial.print("\t");
Serial.print(value, DEC);
Serial.println();
if (digitalRead(BUTTON_Rotary) == LOW && BUTTON_Rotary_lastState == HIGH) // = pressed
{
sendInputOuput(15);
}
else if (rotaryEncoderState == 1 || rotaryEncoderState == 2)
{
if ( rotaryEncoderState == 1 ) //Turned Clockwise
{
sendInputOuput(14);
}
// advance to the next address of the EEPROM
address = address + 1;
if ( rotaryEncoderState == 2 ) //Turned Counter-Clockwise
{
sendInputOuput(13);
}
}
}
if (millis() > lastInputMillis + sevSegCoolDown)
{
sevseg.blank();
}
sevseg.refreshDisplay();
// there are only 512 bytes of EEPROM, from 0 to 511, so if we're
// on address 512, wrap around to address 0
if (address == 128)
address = 0;
// Teensy 1.0 has 512 bytes
// Teensy 2.0 has 1024 bytes
// Teensy++ 1.0 has 2048 bytes
// Teensy++ 2.0 has 4096 bytes
BUTTON_Rotary_lastState = digitalRead(BUTTON_Rotary);
delay(500);
}
void clickKey(int key)
@ -138,3 +238,222 @@ void clickKey(int key)
Keyboard.press(key);
Keyboard.release(key);
}
void printMenu()
{
Serial.println("printMenu");
Serial.println(menuCurrentPath + "/ ");
if (isInMenu)
{
if(menuIsSettingValue)
{
sevseg.setNumber(menuGetValue(menuCurrentItem.displayName));
Serial.println(menuGetValue(menuCurrentItem.displayName));
}
else
{
updateMenu();
sevseg.setChars(menuCurrentItem.displayName.c_str());
Serial.println("3e :" + menuCurrentItem.displayName);
}
}
}
void updateMenu()
{
unsigned int correspondingPathItemIndex = 0;
for (unsigned int i = 0; i < (sizeof(menuEntries)/sizeof(MenuItem)); i++)
{
if (menuEntries[i].path == menuCurrentPath)
{
if(correspondingPathItemIndex == menuSelectionIndex)
{
Serial.println("1e :" + menuEntries[i].displayName);
menuCurrentItem = menuEntries[i];
Serial.println("2e :" + menuCurrentItem.displayName);
}
correspondingPathItemIndex++;
}
}
menuCurrentPathItems = correspondingPathItemIndex;
//Serial.println(menuCurrentPathItems);
}
void menuBack()
{
if (isInMenu)
{
if (menuCurrentPath == "/" || menuCurrentPath == "" )
{
Serial.println("menuExit");
isInMenu = false;
menuSelectionIndex = 0;
menuCurrentPath = "";
sevseg.blank();
}
else
{
Serial.println("menuBack");
menuSelectionIndex = menuGetSelectionIndexFromPath(menuCurrentPath);
menuCurrentPath = menuCurrentPath.substring(0,menuCurrentPath.lastIndexOf('/'));
menuIsSettingValue = false;
printMenu();
}
}
}
void menuEnter()
{
Serial.println("menuEnter");
if (isInMenu)
{
menuCurrentPath = menuCurrentItem.path + '/';
menuCurrentPath+= menuCurrentItem.displayName;
menuSelectionIndex = 0;
if (menuCurrentItem.isValue)
{
if (menuIsSettingValue)
{
for (unsigned int i = 0; i < (sizeof(memory)/sizeof(MemSlot)); i++)
{
if (memory[i].displayName == menuCurrentItem.displayName)
{
EEPROM.write(memory[i].address, memory[i].value);
Serial.println("value saved in eeprom");
menuBack();
return;
}
}
}
else
{
menuIsSettingValue = true;
Serial.println("menuIsSettingValue");
}
}
printMenu();
}
}
void menuPrev()
{
if (isInMenu)
{
if (menuIsSettingValue)
{
menuSetValue(menuCurrentItem.displayName, menuGetValue(menuCurrentItem.displayName) - 1);
}
else if (menuSelectionIndex > 0)
{
menuSelectionIndex = menuSelectionIndex - 1;
}
printMenu();
}
}
void menuNext()
{
if (isInMenu)
{
if (menuIsSettingValue)
{
menuSetValue(menuCurrentItem.displayName, menuGetValue(menuCurrentItem.displayName) + 1);
}
else if (menuSelectionIndex < menuCurrentPathItems-1)
{
menuSelectionIndex = menuSelectionIndex + 1;
}
printMenu();
}
}
byte menuGetValue(String variableName)
{
MemSlot* memorySlot = getMemorySlot(variableName);
if (memorySlot != NULL)
{
return memorySlot->value;
}
return 0;
}
bool menuSetValue(String variableName, byte newValue)
{
MemSlot* memorySlot = getMemorySlot(variableName);
if (memorySlot != NULL)
{
memorySlot->value = newValue;
return true;
}
return false;
}
int menuGetSelectionIndexFromPath(String path)
{
int correspondingPathItemIndex = 0;
String makePath = "";
String parentPath = path.substring(0,path.lastIndexOf('/'));
for (unsigned int i = 0; i < (sizeof(menuEntries)/sizeof(MenuItem)); i++)
{
if (menuEntries[i].path == parentPath)
{
makePath = menuEntries[i].path + '/';
makePath += menuEntries[i].displayName;
if(makePath == path)
{
return correspondingPathItemIndex;
}
correspondingPathItemIndex++;
}
}
return 0;
}
MemSlot* getMemorySlot(String memSlotName)
{
for (unsigned int i = 0; i < (sizeof(memory)/sizeof(MemSlot)); i++)
{
if (memory[i].displayName == memSlotName)
{
return &memory[i];
}
}
return NULL;
}
void sendInputOuput(int input)
{
lastInputMillis = millis();
sevseg.setNumber(input, -1);
int mode = (getMemorySlot("prst")->value) % 3;
Action actionToRun = modes[mode][input-1];
if (actionToRun.keyID == -1) //menu
{
if (isInMenu)
{
menuBack();
}
else
{
isInMenu = true;
sevseg.blank();
printMenu();
}
}
else if (isInMenu && input == 13) //menu prev
{
menuPrev();
}
else if (isInMenu && input == 14) //menu next
{
menuNext();
}
else if (isInMenu && input == 15) //menu enter
{
menuEnter();
}
else
{
clickKey(actionToRun.keyID);
}
}

62
Maybe/modeAction.h Normal file
View File

@ -0,0 +1,62 @@
#include <Keyboard.h>
struct Action
{
int keyID;
String arguments;
};
Action modes[][15] =
{
{
{KEY_F13, ""},
{KEY_F14, ""},
{KEY_F15, ""},
{KEY_F16, ""},
{KEY_F17, ""},
{KEY_F18, ""},
{KEY_F19, ""},
{KEY_F20, ""},
{KEY_F21, ""},
{KEY_F22, ""},
{KEY_F23, ""},
{-1, ""}, //menu
{KEY_MEDIA_VOLUME_DEC, ""},
{KEY_MEDIA_VOLUME_INC, ""},
{KEY_MEDIA_MUTE, ""}
},
{
{KEY_F13, "MODIFIERKEY_SHIFT"},
{KEY_F14, "MODIFIERKEY_SHIFT"},
{KEY_F15, "MODIFIERKEY_SHIFT"},
{KEY_F16, "MODIFIERKEY_SHIFT"},
{KEY_F17, "MODIFIERKEY_SHIFT"},
{KEY_F18, "MODIFIERKEY_SHIFT"},
{KEY_F19, "MODIFIERKEY_SHIFT"},
{KEY_F20, "MODIFIERKEY_SHIFT"},
{KEY_F21, "MODIFIERKEY_SHIFT"},
{KEY_F22, "MODIFIERKEY_SHIFT"},
{KEY_F23, "MODIFIERKEY_SHIFT"},
{-1, ""}, //menu
{KEY_MEDIA_VOLUME_DEC, ""},
{KEY_MEDIA_VOLUME_INC, ""},
{KEY_MEDIA_MUTE, ""}
},
{
{KEY_MEDIA_PREV_TRACK, ""},
{KEY_MEDIA_PLAY_PAUSE, ""},
{KEY_MEDIA_NEXT_TRACK, ""},
{KEY_F16, "MODIFIERKEY_CTRL"},
{KEY_F17, "MODIFIERKEY_CTRL"},
{KEY_F18, "MODIFIERKEY_CTRL"},
{KEY_F19, "MODIFIERKEY_CTRL"},
{KEY_F20, "MODIFIERKEY_CTRL"},
{KEY_F21, "MODIFIERKEY_CTRL"},
{KEY_F22, "MODIFIERKEY_CTRL"},
{KEY_F23, "MODIFIERKEY_CTRL"},
{-1, ""}, //menu
{KEY_MEDIA_VOLUME_DEC, ""},
{KEY_MEDIA_VOLUME_INC, ""},
{KEY_MEDIA_MUTE, ""}
}
};

View File

@ -156,7 +156,7 @@ void loop()
Keyboard.press(KEY_F24);
Keyboard.release(KEY_F24);
}
waitDelay = 5000;
waitDelay = 10000;
} else if (digitalRead(BUTTON_Rotary) == LOW && BUTTON_Rotary_lastState == HIGH) // = pressed
{
sevseg.setNumber(13, -1);