0 Members and 1 Guest are viewing this topic.
while(!isKeyPressed(NSPIRE_KEY_ESC)){// Code}
// Insert whatever value of x belowsleep(x);if(isKeyPressed(someKey)){}
// I used 5 as the minimum number of iterations hereint numIterations = 5;while(!isKeyPressed(NSPIRE_KEY_ESC)){ // Optional, but saves power idle(); if(isKeyPressed(someKey) && numIterations == 5) { numIterations = 0; // Stuff } // Prevents numIterations from going above 5 if (numIterations < 5) numIterations++;}
My new method uses a counter to see how many iterations it has gone through the while loop since the previous key press, and uses idle() instead of sleep(). A key will only register if it has been at least x iterations.
int key_pressed(t_key is_pressed){ if ( isKeyPressed(is_pressed) ) { while ( isKeyPressed(is_pressed) { //loop to prevent more than one reaction per key-press } return 1 ; } return 0 ; }
I really like your solution
That is a very good solution
the only problem I see with it is that it holds up the whole program while the key is held down which may not be desirable in some situations.
int (key_pressed(t_key is_pressed, int* block_value){ if ( *block_value < VALUE_A && isKeyPressed(is_pressed) ) { *block_value += VALUE_B ; return 1 ; } return 0 ;}int main(){ int block_value = 0 ; while( SOMETHING ) //main loop { if ( key_pressed(KEY_NSPIRE_WHATEVER, &block_value) { //do what you want } //etc. if ( block_value > 0 ) block_value -= VALUE_C ; //or any other way to reduce "block_value", can probably done in a more controlled fashion like it gets updated when an action finishes or so... } return 0 ;}
I don't understand your code; specifically, I don't get block_value, VALUE_A, and VALUE_B.
int key_pressed(t_key is_pressed){ if ( isKeyPressed(is_pressed) ) { unsigned time_start = *(volatile unsigned*)0x90090000 ; //get the time when key is first pressed down while ( *(volatile unsigned*)0x90090000 < time_start+SECONDS_TO_WAIT && isKeyPressed(is_pressed) ) //compare additionally to the current time { //loop to prevent more than one reaction per key-press or per "SECONDS_TO_WAIT" seconds. } return 1 ; } else return 0 ;}