global: add central user input storage
This commit is contained in:
parent
77263ecaa2
commit
ae3ac4e2ed
48
global.c
48
global.c
|
@ -35,6 +35,11 @@
|
|||
|
||||
volatile bool button_flag = false;
|
||||
volatile uint32_t sleep_duration = 0; /**< sleep duration count down (in SysTick interrupts) */
|
||||
volatile bool user_input_available = false;
|
||||
|
||||
static volatile uint8_t user_input_buffer[64] = {0}; /**< ring buffer for received data */
|
||||
static volatile uint8_t user_input_i = 0; /**< current position of read received data */
|
||||
static volatile uint8_t user_input_used = 0; /**< how much data has been received and not red */
|
||||
|
||||
char* b2s(uint64_t binary, uint8_t rjust)
|
||||
{
|
||||
|
@ -159,6 +164,44 @@ void sys_tick_handler(void)
|
|||
}
|
||||
}
|
||||
|
||||
char user_input_get(void)
|
||||
{
|
||||
// this is prone to error if user input is stored meanwhile (e.g. by interrupts)
|
||||
if (!user_input_used) { // no user input available
|
||||
return 0;
|
||||
}
|
||||
volatile char to_return = user_input_buffer[user_input_i]; // get the next available character
|
||||
user_input_i = (user_input_i+1)%LENGTH(user_input_buffer); // update used buffer
|
||||
user_input_used--; // update used buffer
|
||||
user_input_available = (user_input_used!=0); // update available data
|
||||
return to_return;
|
||||
}
|
||||
|
||||
void user_input_store(char c)
|
||||
{
|
||||
// only save data if there is space in the buffer
|
||||
if (user_input_used>=LENGTH(user_input_buffer)) { // if buffer is full
|
||||
user_input_i = (user_input_i+1)%LENGTH(user_input_buffer); // drop oldest data
|
||||
user_input_used--; // update used buffer information
|
||||
}
|
||||
user_input_buffer[(user_input_i+user_input_used)%LENGTH(user_input_buffer)] = c; // put character in buffer
|
||||
user_input_used++; // update used buffer
|
||||
user_input_available = true; // update available data
|
||||
}
|
||||
|
||||
bool wait_space(void)
|
||||
{
|
||||
while (!user_input_available) { // wait for user input
|
||||
__WFI(); // go to sleep
|
||||
}
|
||||
if (' '==user_input_get()) { // space entered
|
||||
return true;
|
||||
} else { // something else entered
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void board_setup(void)
|
||||
{
|
||||
// setup LED
|
||||
|
@ -193,6 +236,11 @@ void board_setup(void)
|
|||
exti_enable_request(EXTI(BUTTON_PIN)); // enable external interrupt
|
||||
nvic_enable_irq(NVIC_EXTI_IRQ(BUTTON_PIN)); // enable interrupt
|
||||
#endif
|
||||
|
||||
// reset user input buffer
|
||||
user_input_available = false;
|
||||
user_input_i = 0;
|
||||
user_input_used = 0;
|
||||
}
|
||||
|
||||
#if defined(BUTTON_PIN)
|
||||
|
|
19
global.h
19
global.h
|
@ -378,8 +378,10 @@ extern uint32_t __application_beginning;
|
|||
* @note this symbol will be provided by the linker script
|
||||
*/
|
||||
extern uint32_t __application_end;
|
||||
|
||||
extern volatile bool button_flag; /**< flag set when board user button has been pressed/released */
|
||||
/** flag set when board user button has been pressed/released */
|
||||
extern volatile bool button_flag;
|
||||
/** flag set when user input is available */
|
||||
extern volatile bool user_input_available;
|
||||
|
||||
/** get binary representation of a number
|
||||
* @param[in] binary number to represent in binary
|
||||
|
@ -415,8 +417,19 @@ void sleep_us(uint32_t duration);
|
|||
*/
|
||||
void sleep_ms(uint32_t duration);
|
||||
|
||||
/** get user input
|
||||
* @note verify availability using user_input_available
|
||||
* @return user input character
|
||||
*/
|
||||
char user_input_get(void);
|
||||
|
||||
/** store user input
|
||||
* @param[in] c user input character to store
|
||||
* @note old data will be overwritten when buffer is full
|
||||
*/
|
||||
void user_input_store(char c);
|
||||
|
||||
/** wait for user to press space
|
||||
* @note needs to be implemented by the main application depending on the user inputs
|
||||
* @return if the user press space (true) or something else
|
||||
*/
|
||||
bool wait_space(void);
|
||||
|
|
Loading…
Reference in New Issue