removed unnecessary complexity when registering scenes

This commit is contained in:
KlausMu 2024-03-12 19:12:43 +01:00
parent 63d50baa11
commit 934a2cd9af
7 changed files with 132 additions and 117 deletions

View file

@ -8,6 +8,7 @@
// https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work
struct scene_definition {
scene_setKeys this_scene_setKeys;
scene_start_sequence this_scene_start_sequence;
scene_end_sequence this_scene_end_sequence;
key_repeatModes this_key_repeatModes;
@ -19,6 +20,7 @@ std::map<std::string, scene_definition> registered_scenes;
void register_scene(
std::string a_scene_name,
scene_setKeys a_scene_setKeys,
scene_start_sequence a_scene_start_sequence,
scene_end_sequence a_scene_end_sequence,
key_repeatModes a_key_repeatModes,
@ -26,6 +28,7 @@ void register_scene(
key_commands_long a_key_commands_long) {
registered_scenes[a_scene_name] = scene_definition{
a_scene_setKeys,
a_scene_start_sequence,
a_scene_end_sequence,
a_key_repeatModes,
@ -33,6 +36,16 @@ void register_scene(
a_key_commands_long
};
// Whenever a new scene is registered, normally a new scene command has been defined immediately before (e.g. see register_scene_TV()).
// But this new scene command could have been already been used before in the key definition of another scene. The command at this time was 0, which is undefined.
// So we have to set the keys again for all scenes that have been registered before.
// 1. set again the defaultKeys
register_scene_defaultKeys();
// 2. loop over all registered scenes and call setKeys()
for (std::map<std::string, scene_definition>::iterator it = registered_scenes.begin(); it != registered_scenes.end(); ++it) {
it->second.this_scene_setKeys();
}
}
bool sceneExists(std::string sceneName) {

View file

@ -5,6 +5,7 @@
#include <map>
#include "applicationInternal/keys.h"
typedef void (*scene_setKeys)(void);
typedef void (*scene_start_sequence)(void);
typedef void (*scene_end_sequence)(void);
typedef std::map<char, repeatModes> *key_repeatModes;
@ -13,6 +14,7 @@ typedef std::map<char, uint16_t> *key_commands_long;
void register_scene(
std::string a_scene_name,
scene_setKeys a_scene_setKeys,
scene_start_sequence a_scene_start_sequence,
scene_end_sequence a_scene_end_sequence,
key_repeatModes a_key_repeatModes,

View file

@ -91,14 +91,7 @@ int main(int argc, char *argv[]) {
init_gui();
gui_loop(); // Run the LVGL UI once before the loop takes over
// register the scenes
// First the commands of all scenes have to be registered, so that they get their unique ids.
// Later they will be used in register_scenes_*, where the commands will be used when defining the key_commands_*
register_scene_allOff_commands();
register_scene_TV_commands();
register_scene_fireTV_commands();
register_scene_chromecast_commands();
// now the scenes and their key_commands_* can be defined
// register the scenes and their key_commands_*
register_scene_defaultKeys();
register_scene_allOff();
register_scene_TV();

View file

@ -14,29 +14,7 @@ std::map<char, repeatModes> key_repeatModes_TV;
std::map<char, uint16_t> key_commands_short_TV;
std::map<char, uint16_t> key_commands_long_TV;
void scene_start_sequence_TV(void) {
executeCommand(SAMSUNG_POWER_ON);
delay(500);
executeCommand(YAMAHA_POWER_ON);
delay(1500);
executeCommand(YAMAHA_INPUT_DVD);
delay(3000);
executeCommand(SAMSUNG_INPUT_TV);
}
void scene_end_sequence_TV(void) {
}
std::string scene_name_TV = "TV";
void register_scene_TV_commands(void) {
register_command(&SCENE_TV , makeCommandData(SCENE, {scene_name_TV}));
}
void register_scene_TV(void) {
void scene_setKeys_TV() {
key_repeatModes_TV = {
{KEY_STOP, SHORT_REPEATED }, {KEY_REWI, SHORT }, {KEY_PLAY, SHORT }, {KEY_FORW, SHORT_REPEATED },
@ -68,8 +46,31 @@ void register_scene_TV(void) {
};
}
void scene_start_sequence_TV(void) {
executeCommand(SAMSUNG_POWER_ON);
delay(500);
executeCommand(YAMAHA_POWER_ON);
delay(1500);
executeCommand(YAMAHA_INPUT_DVD);
delay(3000);
executeCommand(SAMSUNG_INPUT_TV);
}
void scene_end_sequence_TV(void) {
}
std::string scene_name_TV = "TV";
void register_scene_TV(void) {
register_command(&SCENE_TV , makeCommandData(SCENE, {scene_name_TV}));
register_scene(
scene_name_TV,
& scene_setKeys_TV,
& scene_start_sequence_TV,
& scene_end_sequence_TV,
& key_repeatModes_TV,

View file

@ -14,39 +14,7 @@ std::map<char, repeatModes> key_repeatModes_allOff;
std::map<char, uint16_t> key_commands_short_allOff;
std::map<char, uint16_t> key_commands_long_allOff;
void scene_start_sequence_allOff(void) {
executeCommand(SAMSUNG_POWER_OFF);
delay(500);
executeCommand(YAMAHA_POWER_OFF);
delay(500);
// repeat IR to be sure
executeCommand(SAMSUNG_POWER_OFF);
delay(500);
executeCommand(YAMAHA_POWER_OFF);
delay(500);
// repeat IR to be sure
executeCommand(SAMSUNG_POWER_OFF);
delay(500);
executeCommand(YAMAHA_POWER_OFF);
delay(500);
// you cannot power off FireTV, but at least you can stop the currently running app
executeCommand(KEYBOARD_HOME);
delay(500);
executeCommand(KEYBOARD_HOME);
}
void scene_end_sequence_allOff(void) {
}
std::string scene_name_allOff = "Off";
void register_scene_allOff_commands(void) {
register_command(&SCENE_ALLOFF , makeCommandData(SCENE, {scene_name_allOff}));
}
void register_scene_allOff(void) {
void scene_setKeys_allOff() {
key_repeatModes_allOff = {
@ -78,8 +46,42 @@ void register_scene_allOff(void) {
};
}
void scene_start_sequence_allOff(void) {
executeCommand(SAMSUNG_POWER_OFF);
delay(500);
executeCommand(YAMAHA_POWER_OFF);
delay(500);
// repeat IR to be sure
executeCommand(SAMSUNG_POWER_OFF);
delay(500);
executeCommand(YAMAHA_POWER_OFF);
delay(500);
// repeat IR to be sure
executeCommand(SAMSUNG_POWER_OFF);
delay(500);
executeCommand(YAMAHA_POWER_OFF);
delay(500);
// you cannot power off FireTV, but at least you can stop the currently running app
executeCommand(KEYBOARD_HOME);
delay(500);
executeCommand(KEYBOARD_HOME);
}
void scene_end_sequence_allOff(void) {
}
std::string scene_name_allOff = "Off";
void register_scene_allOff(void) {
register_command(&SCENE_ALLOFF , makeCommandData(SCENE, {scene_name_allOff}));
register_scene(
scene_name_allOff,
& scene_setKeys_allOff,
& scene_start_sequence_allOff,
& scene_end_sequence_allOff,
& key_repeatModes_allOff,

View file

@ -14,28 +14,7 @@ std::map<char, repeatModes> key_repeatModes_chromecast;
std::map<char, uint16_t> key_commands_short_chromecast;
std::map<char, uint16_t> key_commands_long_chromecast;
void scene_start_sequence_chromecast(void) {
executeCommand(SAMSUNG_POWER_ON);
delay(500);
executeCommand(YAMAHA_POWER_ON);
delay(1500);
executeCommand(YAMAHA_INPUT_DVD);
delay(3000);
executeCommand(SAMSUNG_INPUT_HDMI_1);
}
void scene_end_sequence_chromecast(void) {
}
std::string scene_name_chromecast = "Chromecast";
void register_scene_chromecast_commands(void) {
register_command(&SCENE_CHROMECAST , makeCommandData(SCENE, {scene_name_chromecast}));
}
void register_scene_chromecast(void) {
void scene_setKeys_chromecast() {
key_repeatModes_chromecast = {
@ -67,8 +46,31 @@ void register_scene_chromecast(void) {
};
}
void scene_start_sequence_chromecast(void) {
executeCommand(SAMSUNG_POWER_ON);
delay(500);
executeCommand(YAMAHA_POWER_ON);
delay(1500);
executeCommand(YAMAHA_INPUT_DVD);
delay(3000);
executeCommand(SAMSUNG_INPUT_HDMI_1);
}
void scene_end_sequence_chromecast(void) {
}
std::string scene_name_chromecast = "Chromecast";
void register_scene_chromecast(void) {
register_command(&SCENE_CHROMECAST , makeCommandData(SCENE, {scene_name_chromecast}));
register_scene(
scene_name_chromecast,
& scene_setKeys_chromecast,
& scene_start_sequence_chromecast,
& scene_end_sequence_chromecast,
& key_repeatModes_chromecast,

View file

@ -14,37 +14,7 @@ std::map<char, repeatModes> key_repeatModes_fireTV;
std::map<char, uint16_t> key_commands_short_fireTV;
std::map<char, uint16_t> key_commands_long_fireTV;
void scene_start_sequence_fireTV(void) {
executeCommand(SAMSUNG_POWER_ON);
delay(500);
executeCommand(YAMAHA_POWER_ON);
delay(1500);
executeCommand(YAMAHA_INPUT_DTV);
delay(3000);
executeCommand(SAMSUNG_INPUT_HDMI_2);
delay(100);
executeCommand(KEYBOARD_HOME);
delay(500);
executeCommand(KEYBOARD_HOME);
}
void scene_end_sequence_fireTV(void) {
// you cannot power off FireTV, but at least you can stop the currently running app
executeCommand(KEYBOARD_HOME);
delay(500);
executeCommand(KEYBOARD_HOME);
}
std::string scene_name_fireTV = "Fire TV";
void register_scene_fireTV_commands(void) {
register_command(&SCENE_FIRETV , makeCommandData(SCENE, {scene_name_fireTV}));
}
void register_scene_fireTV(void) {
void scene_setKeys_fireTV() {
key_repeatModes_fireTV = {
{KEY_REWI, SHORTorLONG }, {KEY_PLAY, SHORT }, {KEY_FORW, SHORTorLONG },
@ -76,8 +46,40 @@ void register_scene_fireTV(void) {
{KEY_FORW, KEYBOARD_FASTFORWARD_LONG},
};
}
void scene_start_sequence_fireTV(void) {
executeCommand(SAMSUNG_POWER_ON);
delay(500);
executeCommand(YAMAHA_POWER_ON);
delay(1500);
executeCommand(YAMAHA_INPUT_DTV);
delay(3000);
executeCommand(SAMSUNG_INPUT_HDMI_2);
delay(100);
executeCommand(KEYBOARD_HOME);
delay(500);
executeCommand(KEYBOARD_HOME);
}
void scene_end_sequence_fireTV(void) {
// you cannot power off FireTV, but at least you can stop the currently running app
executeCommand(KEYBOARD_HOME);
delay(500);
executeCommand(KEYBOARD_HOME);
}
std::string scene_name_fireTV = "Fire TV";
void register_scene_fireTV(void) {
register_command(&SCENE_FIRETV , makeCommandData(SCENE, {scene_name_fireTV}));
register_scene(
scene_name_fireTV,
& scene_setKeys_fireTV,
& scene_start_sequence_fireTV,
& scene_end_sequence_fireTV,
& key_repeatModes_fireTV,