funkey_gpio_management.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <signal.h> // Defines signal-handling functions (i.e. trap Ctrl-C)
  8. #include <sys/select.h>
  9. #include "gpio-utils.h"
  10. #include <assert.h>
  11. #include "uinput.h"
  12. #include "gpio_mapping.h"
  13. #include "read_conf_file.h"
  14. /****************************************************************
  15. * Defines
  16. ****************************************************************/
  17. /****************************************************************
  18. * Global variables
  19. ****************************************************************/
  20. int keepgoing = 1; // Set to 0 when ctrl-c is pressed
  21. /****************************************************************
  22. * signal_handler
  23. ****************************************************************/
  24. void signal_handler(int sig);
  25. // Callback called when SIGINT is sent to the process (Ctrl-C)
  26. void signal_handler(int sig)
  27. {
  28. printf( "Ctrl-C pressed, cleaning up and exiting..\n" );
  29. keepgoing = 0;
  30. }
  31. /****************************************************************
  32. * Local functions
  33. ****************************************************************/
  34. /****************************************************************
  35. * Main
  36. ****************************************************************/
  37. int main(int argc, char **argv, char **envp)
  38. {
  39. // Variables
  40. STRUCT_MAPPED_GPIO * chained_list_mapping = NULL;
  41. int nb_valid_gpios = 0;
  42. int * gpio_pins = NULL;
  43. // Set the signal callback for Ctrl-C
  44. signal(SIGINT, signal_handler);
  45. // Init uinput device
  46. init_uinput();
  47. // Read Conf File: Get GPIO pins to declare and all valid pin mappings
  48. get_mapping_from_conf_file(&chained_list_mapping, &nb_valid_gpios, &gpio_pins);
  49. // Init GPIOs
  50. init_mapping_gpios(gpio_pins, nb_valid_gpios, chained_list_mapping);
  51. // Main Loop
  52. while (keepgoing) {
  53. listen_gpios_interrupts();
  54. }
  55. // De-Init GPIOs
  56. deinit_mapping_gpios();
  57. /*
  58. * Give userspace some time to read the events before we destroy the
  59. * device with UI_DEV_DESTOY.
  60. */
  61. close_uinput();
  62. return 0;
  63. }