From 1f76a72c2b7e0eeb33319dc5e2d47b03c42e904b Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Fri, 28 Apr 2017 10:27:53 +0530 Subject: [PATCH] Refactor gpio_interrupt code --- gpio_interrupt/gpio_interrupt.c | 39 +++++++++++++++------------------ 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/gpio_interrupt/gpio_interrupt.c b/gpio_interrupt/gpio_interrupt.c index b9e9902..0d12988 100644 --- a/gpio_interrupt/gpio_interrupt.c +++ b/gpio_interrupt/gpio_interrupt.c @@ -14,12 +14,23 @@ * libsoc GPIO documentation: http://jackmitch.github.io/libsoc/c/gpio/#gpio */ -volatile bool interrupt_occurred = false; volatile bool led_status = false; +static uint32_t times = 0; + +gpio *gpio_interrupt = NULL; +gpio *gpio_led = NULL; int gpio_interrupt_callback(void* arg) { - interrupt_occurred = true; + if (!led_status) { + led_status = true; + libsoc_gpio_set_level(gpio_led, HIGH); + } else { + led_status = false; + libsoc_gpio_set_level(gpio_led, LOW); + } + times++; + printf("Interrupt occurred %d times\n", times); return 0; } @@ -29,8 +40,6 @@ int main(void) uint32_t gpio_input; uint32_t led_output; uint32_t ret = 0; - gpio *gpio_interrupt = NULL; - gpio *gpio_led = NULL; printf("Enter the GPIO number to use as interrupt:\t"); scanf("%d", &gpio_input); @@ -79,25 +88,13 @@ int main(void) goto exit; } - printf("Waiting for interrupt\n"); + printf("Waiting for interrupt. Press 'q' and 'Enter' at any time to exit\n"); + char key = -1; while (true) { - if (interrupt_occurred) { - - interrupt_occurred = false; - - if (!led_status) { - led_status = true; - libsoc_gpio_set_level(gpio_led, HIGH); - } else { - led_status = false; - libsoc_gpio_set_level(gpio_led, LOW); - } - printf("Interrupt occurred\n"); - /* Uncomment this to exit on first interrupt */ - //goto exit; - } - usleep(200); + key = fgetc(stdin); + if (key == 'q') + goto exit; } exit: