idle animation & toggle alternate mode

This commit is contained in:
Jeremie GABOLDE 2022-09-25 22:58:14 +02:00
parent 90e0f98b92
commit d49ca41c83
3 changed files with 74 additions and 36 deletions

View File

@ -30,7 +30,7 @@ MemSlot memory[] = //128 bytes for the Teensy LC
{"bt10", 11, 0}, {"bt10", 11, 0},
{"bt11", 12, 0}, {"bt11", 12, 0},
{"bt12", 13, 0}, {"bt12", 13, 0},
{"effc", 14, 1}, {"effc", 14, 2},
{"afps", 15, 24}, {"afps", 15, 24},
{"clea", 0, 0}, // != 0 -> reset eeprom {"clea", 0, 0}, // != 0 -> reset eeprom
}; };
@ -141,7 +141,7 @@ int animationFrame = 0;
bool animationPlaying = false; bool animationPlaying = false;
unsigned int animationLastFrameMillis = 0; unsigned int animationLastFrameMillis = 0;
unsigned int animationFrameDelayMillis = 64; unsigned int animationFrameDelayMillis = 64;
bool animationLooping = false; bool animationIdle = false;
bool animationEnabled = true; bool animationEnabled = true;
/* /*
@ -225,25 +225,30 @@ void initSevSegDisplay()
void loop() void loop()
{ {
//animation draw tick //animation draw tick
if (animationIdle && !animationPlaying && animationEnabled && !isInMenu)
{
playAnimation(4);
}
if (animationEnabled && animationPlaying && (millis() > (animationLastFrameMillis + animationFrameDelayMillis))) if (animationEnabled && animationPlaying && (millis() > (animationLastFrameMillis + animationFrameDelayMillis)))
{ {
drawAnimation(animationLooping); drawAnimation();
animationLastFrameMillis = millis(); animationLastFrameMillis = millis();
} }
//INPUTS //INPUTS
if (millis() > lastInputMillis + inputCoolDown) if (millis() > lastInputMillis + inputCoolDown)
{ {
char customKey = keypad.getKey(); char customKey = keypad.getKey(); //-1 because our array stars with 1 instead of 0
if (customKey) if (customKey > 0)
{ {
sendInputOuput(customKey); sendInputOuput(customKey-1);
} }
if (rotaryEncoder.push() == 1) // = pressed if (rotaryEncoder.push() == 1) // = pressed
{ {
sendInputOuput(15); sendInputOuput(14);
} }
rotaryEncoderState = rotaryEncoder.rotate(); rotaryEncoderState = rotaryEncoder.rotate();
@ -252,12 +257,12 @@ void loop()
{ {
if ( rotaryEncoderState == 1 ) //Turned Clockwise if ( rotaryEncoderState == 1 ) //Turned Clockwise
{ {
sendInputOuput(14); sendInputOuput(13);
} }
if ( rotaryEncoderState == 2 ) //Turned Counter-Clockwise if ( rotaryEncoderState == 2 ) //Turned Counter-Clockwise
{ {
sendInputOuput(13); sendInputOuput(12);
} }
} }
} }
@ -430,6 +435,7 @@ void applyMemoryChange()
{ {
animationEnabled = (getMemorySlot("effc")->value > 0); animationEnabled = (getMemorySlot("effc")->value > 0);
animationFrameDelayMillis = 1000 / max(1, getMemorySlot("afps")->value); animationFrameDelayMillis = 1000 / max(1, getMemorySlot("afps")->value);
animationIdle = (getMemorySlot("effc")->value == 2);
} }
int menuGetSelectionIndexFromPath(String path) int menuGetSelectionIndexFromPath(String path)
@ -467,15 +473,14 @@ MemSlot* getMemorySlot(String memSlotName)
void sendInputOuput(int input) void sendInputOuput(int input)
{ {
int numberOfModes = sizeof(modes)/(sizeof(Action)*15); //get current presetID
int currentPreset = (getMemorySlot("alt")->value) % getPresetsCount();
int currentMode = (getMemorySlot("alt")->value) % numberOfModes;
//Hardware inputs ordered from the bottom left = 1 to the top right = 12 on the switch matrix, //Hardware inputs ordered from the bottom left = 1 to the top right = 12 on the switch matrix,
Action actionToRun = modes[currentMode][input-1]; //getting the virtual inputs from the modes array. Action actionToRun = getAction(currentPreset, input); //getting the virtual inputs from the modes array.
//Inputs uids starts with 1, so input - 1 to get the index 0 of the array //Inputs uids starts with 1, so input - 1 to get the index 0 of the array
String inputName = "bt"; String inputName = "bt";
inputName += input; inputName += input +1;
MemSlot* buttonInputMem = getMemorySlot(inputName); MemSlot* buttonInputMem = getMemorySlot(inputName);
Serial.println(inputName); Serial.println(inputName);
@ -486,16 +491,18 @@ void sendInputOuput(int input)
else else
{ {
playAnimation(1); playAnimation(1);
//toggle mode
if (buttonInputMem != NULL) if (buttonInputMem != NULL)
{ {
if (buttonInputMem->value == 1) if (buttonInputMem->value == 1)
{ {
buttonsVirtualState[input-1] = !buttonsVirtualState[input-1]; buttonsVirtualState[input] = !buttonsVirtualState[input];
actionToRun = getAction(currentPreset + (int)!buttonsVirtualState[input], input); //Toggle mode sends the next preset's input if the troggle state is true
} }
if (buttonInputMem->value == 0) if (buttonInputMem->value == 0)
{ {
buttonsVirtualState[input-1] = false; buttonsVirtualState[input] = false;
} }
} }
@ -517,15 +524,15 @@ void sendInputOuput(int input)
printMenu(); printMenu();
} }
} }
else if (isInMenu && input == 13) //menu prev, hardcoded input else if (isInMenu && input == 12) //menu prev, hardcoded input :/
{ {
menuPrev(); menuPrev();
} }
else if (isInMenu && input == 14) //menu next, hardcoded input else if (isInMenu && input == 13) //menu next, hardcoded input :/
{ {
menuNext(); menuNext();
} }
else if (isInMenu && input == 15) //menu enter, hardcoded input else if (isInMenu && input == 14) //menu enter, hardcoded input :/
{ {
menuEnter(); menuEnter();
} }
@ -535,11 +542,12 @@ void sendInputOuput(int input)
void clickKey(int key, int modifier) void clickKey(int key, int modifier)
{ {
Serial.println(key);
Keyboard.set_modifier(modifier); Keyboard.set_modifier(modifier);
Keyboard.press(key); Keyboard.press(key);
Keyboard.release(key); Keyboard.release(key);
Keyboard.set_modifier(0); Keyboard.set_modifier(0);
//Keyboard.send_now(); //can't remember what's that's for Keyboard.send_now(); //i think that helps to avoid key to stay active
} }
void playAnimation(int animID) void playAnimation(int animID)
@ -552,7 +560,7 @@ void playAnimation(int animID)
} }
} }
void drawAnimation(bool looping) void drawAnimation()
{ {
int currentAnimationFrameTotal = animData[currentAnimationID].animationFrameCount; int currentAnimationFrameTotal = animData[currentAnimationID].animationFrameCount;
sevseg.setSegments(animData[currentAnimationID].animationPtr + (animationFrame * (sizeof(const uint8_t)*4))); sevseg.setSegments(animData[currentAnimationID].animationPtr + (animationFrame * (sizeof(const uint8_t)*4)));
@ -560,7 +568,7 @@ void drawAnimation(bool looping)
if (animationFrame == currentAnimationFrameTotal) if (animationFrame == currentAnimationFrameTotal)
{ {
animationFrame = 0; animationFrame = 0;
if (!looping) if (!(animData[currentAnimationID].animationLooping))
{ {
animationPlaying = false; animationPlaying = false;
sevseg.blank(); sevseg.blank();

View File

@ -3,13 +3,14 @@ int currentAnimationID = 0;
struct AnimationData struct AnimationData
{ {
const uint8_t *animationPtr; const uint8_t *animationPtr;
unsigned int animationFrameCount; const unsigned int animationFrameCount;
const bool animationLooping;
}; };
//animations created using https://jasonacox.github.io/TM1637TinyDisplay/examples/7-segment-animator.html //animations created using https://jasonacox.github.io/TM1637TinyDisplay/examples/7-segment-animator.html
/* Animation Data - HGFEDCBA Map */ /* Animation Data - HGFEDCBA Map */
const uint8_t ANIMATION1[31][4] = { const uint8_t ANIMATION1[][4] = {
{ 0x00, 0x00, 0x00, 0x01 }, // Frame 0 { 0x00, 0x00, 0x00, 0x01 }, // Frame 0
{ 0x00, 0x00, 0x01, 0x00 }, // Frame 1 { 0x00, 0x00, 0x01, 0x00 }, // Frame 1
{ 0x00, 0x01, 0x00, 0x00 }, // Frame 2 { 0x00, 0x01, 0x00, 0x00 }, // Frame 2
@ -39,8 +40,6 @@ const uint8_t ANIMATION1[31][4] = {
{ 0x08, 0x08, 0x08, 0x0e }, // Frame 26 { 0x08, 0x08, 0x08, 0x0e }, // Frame 26
{ 0x39, 0x09, 0x09, 0x0f }, // Frame 27 { 0x39, 0x09, 0x09, 0x0f }, // Frame 27
{ 0x46, 0x76, 0x76, 0x70 }, // Frame 28 { 0x46, 0x76, 0x76, 0x70 }, // Frame 28
{ 0x40, 0x40, 0x40, 0x40 }, // Frame 29
{ 0x00, 0x40, 0x40, 0x00 } // Frame 30
}; };
/* Animation Data - HGFEDCBA Map */ /* Animation Data - HGFEDCBA Map */
@ -78,12 +77,32 @@ const uint8_t ANIMATION4[][4] = {
{ 0x40, 0x40, 0x40, 0x40 }, // Frame 3 { 0x40, 0x40, 0x40, 0x40 }, // Frame 3
{ 0x00, 0x40, 0x40, 0x00 }, // Frame 4 { 0x00, 0x40, 0x40, 0x00 }, // Frame 4
{ 0x00, 0x00, 0x00, 0x00 }, // Frame 5 { 0x00, 0x00, 0x00, 0x00 }, // Frame 5
{ 0x00, 0x00, 0x00, 0x00 }, // Frame 5
{ 0x00, 0x00, 0x00, 0x00 }, // Frame 5
};
/* Animation Data - HGFEDCBA Map */
const uint8_t ANIMATION5[13][4] = {
{ 0x39, 0x09, 0x09, 0x0f }, // Frame 0
{ 0x39, 0x08, 0x09, 0x0f }, // Frame 1
{ 0x38, 0x09, 0x09, 0x0f }, // Frame 2
{ 0x19, 0x09, 0x09, 0x0f }, // Frame 3
{ 0x29, 0x09, 0x09, 0x0f }, // Frame 4
{ 0x31, 0x09, 0x09, 0x0f }, // Frame 5
{ 0x39, 0x01, 0x09, 0x0f }, // Frame 6
{ 0x39, 0x09, 0x01, 0x0f }, // Frame 7
{ 0x39, 0x09, 0x09, 0x07 }, // Frame 8
{ 0x39, 0x09, 0x09, 0x0b }, // Frame 9
{ 0x39, 0x09, 0x09, 0x0d }, // Frame 10
{ 0x39, 0x09, 0x09, 0x0e }, // Frame 11
{ 0x39, 0x09, 0x08, 0x0f } // Frame 12
}; };
AnimationData animData[] = AnimationData animData[] =
{ {
{(const uint8_t*)ANIMATION1, sizeof(ANIMATION1) / (sizeof(const uint8_t)*4)}, {(const uint8_t*)ANIMATION1, sizeof(ANIMATION1) / (sizeof(const uint8_t)*4), false},
{(const uint8_t*)ANIMATION2, sizeof(ANIMATION2) / (sizeof(const uint8_t)*4)}, {(const uint8_t*)ANIMATION2, sizeof(ANIMATION2) / (sizeof(const uint8_t)*4), false},
{(const uint8_t*)ANIMATION3, sizeof(ANIMATION3) / (sizeof(const uint8_t)*4)}, {(const uint8_t*)ANIMATION3, sizeof(ANIMATION3) / (sizeof(const uint8_t)*4), false},
{(const uint8_t*)ANIMATION4, sizeof(ANIMATION4) / (sizeof(const uint8_t)*4)} {(const uint8_t*)ANIMATION4, sizeof(ANIMATION4) / (sizeof(const uint8_t)*4), false},
{(const uint8_t*)ANIMATION5, sizeof(ANIMATION5) / (sizeof(const uint8_t)*4), true}
}; };

View File

@ -5,7 +5,7 @@ struct Action
}; };
//Hardware buttons and Actions are ordered from the bottom left to the top right matrix. Rotary encoder: left, right then press //Hardware buttons and Actions are ordered from the bottom left to the top right matrix. Rotary encoder: left, right then press
Action modes[][15] = Action presets[][15] =
{ {
{ {
{KEY_MEDIA_PREV_TRACK, 0}, {KEY_MEDIA_PREV_TRACK, 0},
@ -37,9 +37,9 @@ Action modes[][15] =
{KEY_F22, MODIFIERKEY_SHIFT}, {KEY_F22, MODIFIERKEY_SHIFT},
{KEY_F23, MODIFIERKEY_SHIFT}, {KEY_F23, MODIFIERKEY_SHIFT},
{-1, 0}, //menu / back {-1, 0}, //menu / back
{KEYPAD_MINUS, 0}, {KEYPAD_MINUS, MODIFIERKEY_ALT},
{KEYPAD_PLUS, 0}, {KEYPAD_PLUS, MODIFIERKEY_ALT},
{KEYPAD_ENTER, 0} {KEYPAD_ASTERIX, MODIFIERKEY_ALT}
}, },
{ {
{KEY_F13, MODIFIERKEY_CTRL}, {KEY_F13, MODIFIERKEY_CTRL},
@ -56,6 +56,17 @@ Action modes[][15] =
{-1, 0}, //menu / back {-1, 0}, //menu / back
{KEYPAD_MINUS, MODIFIERKEY_CTRL}, {KEYPAD_MINUS, MODIFIERKEY_CTRL},
{KEYPAD_PLUS, MODIFIERKEY_CTRL}, {KEYPAD_PLUS, MODIFIERKEY_CTRL},
{KEYPAD_ENTER, MODIFIERKEY_CTRL} {KEYPAD_ASTERIX, MODIFIERKEY_CTRL}
} }
}; };
int getPresetsCount()
{
return sizeof(presets)/(sizeof(Action)*15);
}
Action getAction(int presetID, int physicalKeyID)
{
int presetCount = getPresetsCount();
return presets[(presetID % presetCount)][physicalKeyID];
}