Hi guys.
First of all, I am not developer at all, just trying to make thing works.
Thing is working, I am pretty satisfied, and will try to be as short as possible with my question.
It is 16x16 LED matrix, where I'm playing Tetris, Snake and Breakout via PS4 controller, running this on ESP32 with ~2700 lines of code (it would be probably half if I am developer ), and combining this with one more ESP32 with WLED, so I can control WLED from PS4 controller as well. Nevertheless my question should be simple and certain, I'm writing all this however, because I'm aware that nobody likes snippets only, people like to see whole code when asking for help, so if whole code is needed, you can find shorter version here, without WLED implementation and without some features I added recently. This shorter version is fully working, and it is also completely related with the question regarding controller.
Question: Since PS4 controller is sending insane amount of events, even if button is pressed very short, games were not playable out of box. E.g. if I want to rotate a falling sprite in Tetris, even if I press rotate button very shortly, sprite would rotate 10 or 15 times, same for moving sprite left/right, it would hit the edge in most of the cases. So I had to slow down it somehow. As said before, I am not a dev (not even close), so I done it on most stupid way with delay(x);
, but I don't know another:
if(!PS4.isConnected()){
currentApp = 4;
return false;
} else {
if (PS4.Right()){
currentInput = RIGHT;
delay(140);
}
if (PS4.Down()){
currentInput = DOWN;
delay(50);
}
if (PS4.Circle()){
currentInput = CIRCLE;
delay(200);
}
if (PS4.Cross()){
currentInput = CIRCLE;
delay(200);
}
if (PS4.Left()){
currentInput = LEFT;
delay(140);
}
if (PS4.Options()){
currentApp = -1;
return false;
delay(500);
}
}
Of course, delay();
is blocking whole code until it finish, so it can be even used for "cheating", cause e.g. if you are holding left or right, you will drastically slow the game, and this could be useful in hard situations when you need a time to thinking where to put current sprite
What would be right way to limit or slow down key events received from PS4 controller? Please note: I would like to keep a feature I currently have, if I'm holding the button, sprite keep moving in "button direction".
Thank you very much.