Register / Login  |  Desktop view  |  Jump to bottom of page

IoAbstraction & TaskManagerIO » How to input keys with KeyboardManager.h to do someting

Author: Frank_G_PGH
27/02/2021 10:37:04
Arduino Due Lighting Project.
WS2815 with Arduino Controller

Description of project
https://www.reddit.com/r/FastLED/comments/lebw5x/fastled_shelf_project_with_three_independent/

Details:
* 4 modes: Static patterns, chases, RGB, & sound activated
* 24 buttons for preset colors and chases.
* Four faders for RGB control with brightness and possibly dual functions (Press a key to activate a fader to control chase speed.)
* Small LCD to display pattern & prompt user to select chase or color.
* LED Indicators above buttons.
* FastLED

Color: Newbie Alert: This is my first major project using Arduino. There are so many different approaches on how to do this. I'm just a little overwhelmed. I appreciate your help and advice.

I am writing and testing the code in blocks to simplify programming. My plan is to get the keypad working smoothly and then add functions to drive the LED strip. I’m writing/coding with event-based software “taskmanager.h” or I could use switch or case statements?

I built a 3 X 8 matrix keypad with a hardwired debounce circuit (74HC14 Schmitt Trigger).
For testing purposes, I used a modified Library. The keypad works and is responsive without any glitching. Thanks for your work Dave!!
https://github.com/Frank-PittsburghEventLighting/KeyboardManagerFG.h/blob/main/KeyboardManagerFG_TestKey1.ino


I do not know the syntax for "getkey / input key", or how (or where in the code) to send that value to a case statement.
Example: key “1” is pressed and all the LEDs in the strip turn red using FastLED.
key “2” is pressed and all the LEDs in the strip turn blue, and so on.

I apologize for my ignorance, I have read and played with the code from your examples and can’t figure it out. After six hours I gave up.

class MyKeyboardListener : public KeyboardListener {
public:
void onKeyPressed(char key, bool held) {
// do something when pressed
}

void onKeyReleased(char key) {
// do something when released
}
} myListener;

Thank you,
Frank

Author: davetcc
28/02/2021 17:19:47
> I do not know the syntax for "getkey / input key", or how (or where in the code) to send that value to a case statement.

The keyboard manager is event-based, so rather than get key, it tells you when a key is pressed, you could implement modal operation in your keyboard listener class yourself (fairly easily), but it's not implemented by default.

> Example: key “1” is pressed and all the LEDs in the strip turn red using FastLED. key “2” is pressed and all the LEDs in the strip turn blue, and so on.

In the easiest case, adjust the key pressed or key released of the keyboard listener you would have switch statement that did something when the key-press or key-release was 1 and something else when the keypress was 2, for example here's a rough incomplete example to get you started (in the example we ignore keys being released):

void doFunctionForOne() {
   // take action when 1 typed on keypad
}


void doFunctionForTwo() {
   // take action when 2 typed on keypad
}

class MyKeyboardListener : public KeyboardListener {
public:
    void keyPressed(char key, bool held) override {
        switch(key) {
        case '1':
            doFunctionForOne();
            break;
        case '2':
             doFunctionForTwo();
             break;
        }
    }

    void keyReleased(char key) override {   }
} myListener;

Author: Frank_G_PGH
28/02/2021 22:04:11
Hi Dave,

Thank you very, very much!!!

The last time I coded was my first time in college during the late 80s. I was coding 8085 assembly language.

After some tinkering, I got it working with solid colors and simple static (non-moving) patterns. Now I need to make the chase patterns work. The chases move only one step when I press the key. I see the problem, now I have to read & research how to put the loop in the Void Function And break the loop when a different key is pressed.
Thank you again for your help and training.

Sincerely,
Frank

Author: Frank_G_PGH
01/03/2021 09:27:21
Hi Dave,
I got a little further. I think I’m close.
When running FastLED.show(); for Static patterns, it loads the pallet into the LED strip and all is good. ??

I got the chase to work using the void and the void & void setup() command:

void RedFunctionFor1() {
// take action when 1 typed on keypad
static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors( startIndex);
FastLED.show();
}
void setup()
uint8_t taskId = taskManager.scheduleFixedRate(100, RedFunctionFor1);

I can't seem to come up with the right syntax to cancel it. I want to Cancel the pattern moving/stepping When a new key is pressed

case '2':
OrangeFunctionFor2();
Serial.print("Orange ");
taskManager.cancelTask(RedFunctionFor1);
break;

I get an error message when I try to run this. I think this is where am going wrong?
taskManager.cancelTask(uint8_t RedFunctionFor1);

(expected primary-expression before 'RedFunctionFor1')

complete code for now:
link: https://github.com/Frank-PittsburghEventLighting/KeyboardManagerFG_Prests_good_test_ChaseTest4/blob/main/KeyboardManagerFG_Prests_good_test_ChaseTest4.ino

Thanks
Frank

Author: davetcc
01/03/2021 10:35:55
8085, that takes me back a while, I used to write 6502, 68000, and 8086 assembler going back many years before I started learning C.

I can't seem to come up with the right syntax to cancel it. I want to Cancel the pattern moving/stepping When a new key is pressed


When you want to cancel a task, you pass the ID that was returned when you added the task. For example:

auto myTaskId = taskManager.scheduleOnce(100, somethingToSchedule);
taskManager.cancelTask(myTaskId);




Register / Login  |  Desktop view  |  Jump to top of page