1
0
mirror of https://github.com/MatMoul/g810-led.git synced 2024-12-24 01:36:11 +00:00

Add possibility to start multiple effects at the same time.

This commit is contained in:
Landrovan 2017-03-25 07:23:20 -04:00
parent 33dd0713e1
commit 01790b0b1f
2 changed files with 33 additions and 20 deletions

View File

@ -6,6 +6,7 @@
#include <chrono> #include <chrono>
#include <functional> #include <functional>
#include <thread> #include <thread>
#include <vector>
// Global variables to know when a user activate input // Global variables to know when a user activate input
std::atomic< bool > KeyPressed; std::atomic< bool > KeyPressed;
@ -54,18 +55,31 @@ void Test2Effects( LedKeyboard& kbd )
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
int StartEffectsAndWaitForUser( LedKeyboard& kbd, std::function< void( LedKeyboard& ) > EffectsFunction ) int StartEffectsAndWaitForUser( LedKeyboard& kbd, std::vector< std::function< void( LedKeyboard& ) > >& EffectFunctions )
{ {
// Start the effects on a thread if ( EffectFunctions.size() == 0 )
std::thread lThread( EffectsFunction, std::ref( kbd ) ); {
std::cout << "No effects to apply." << std::endl;
return 1;
}
// Start all the effects on a thread
std::vector< std::thread > ThreadList;
for( unsigned int i = 0; i < EffectFunctions.size(); ++i )
{
ThreadList.emplace_back( EffectFunctions[i], std::ref( kbd ) );
}
// Wait for user input // Wait for user input
std::cout << "Press enter to quit"; std::cout << "Press enter to quit";
getchar(); getchar();
// Stop the thread and wait for it to finish // Stop the threads and wait for them to finish
KeyPressed = true; KeyPressed = true;
lThread.join(); for( unsigned int i = 0; i < ThreadList.size(); ++i )
{
ThreadList[i].join();
}
return 0; return 0;
} }
@ -78,22 +92,21 @@ int StartCustomEffects( LedKeyboard& kbd, int argc, char** argv )
// Not enough parameters // Not enough parameters
return 1; return 1;
} }
KeyPressed = false; KeyPressed = false;
std::string Type = argv[0];
if ( Type == "test1" ) return StartEffectsAndWaitForUser( kbd, Test1Effects ); // Each arguments is an effects. Check all the arguments to get the list of all wanted effects
else if ( Type == "test2" ) return StartEffectsAndWaitForUser( kbd, Test2Effects ); std::vector< std::function< void( LedKeyboard& ) > > EffectFunctions;
for ( int i = 0; i < argc; ++i )
{
std::string Type = argv[i];
if ( Type == "test1" ) EffectFunctions.emplace_back( Test1Effects );
else if ( Type == "test2" ) EffectFunctions.emplace_back( Test2Effects );
else else
{ {
// No custom effects of this name // No custom effects of this name
std::cout << "No effects of name: " << Type << std::endl; std::cout << "No effects of name: " << Type << std::endl;
} }
}
// First param is the custom effects to create. Convert it to an enum return StartEffectsAndWaitForUser( kbd, EffectFunctions );
/*for ( int i = 0; i < argc; ++i )
{
std::cout << argv[i] << std::endl;
}*/
return 0;
} }

View File

@ -248,7 +248,7 @@ int main(int argc, char **argv) {
else if (argc > 5 && arg == "-fx") return setFX(kbd, argv[2], argv[3], argv[4], argv[5]); else if (argc > 5 && arg == "-fx") return setFX(kbd, argv[2], argv[3], argv[4], argv[5]);
else if (argc > 4 && arg == "-fx") return setFX(kbd, argv[2], argv[3], argv[4]); else if (argc > 4 && arg == "-fx") return setFX(kbd, argv[2], argv[3], argv[4]);
else if (argc > 2 && arg == "-cfx") return StartCustomEffects( kbd, argc + 2, argv + 2 ); else if (argc > 2 && arg == "-cfx") return StartCustomEffects( kbd, argc - 2, argv + 2 );
else if (argc > 2 && arg == "--startup-mode") return setStartupMode(kbd, argv[2]); else if (argc > 2 && arg == "--startup-mode") return setStartupMode(kbd, argv[2]);