funkey_gpio_management.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_idx_declared = NULL;
  43. bool * gpios_pins_active_high = NULL;
  44. // Set the signal callback for Ctrl-C
  45. signal(SIGINT, signal_handler);
  46. // Init uinput device
  47. init_uinput();
  48. // Read Conf File: Get GPIO pins to declare and all valid pin mappings
  49. get_mapping_from_conf_file(&chained_list_mapping, &nb_valid_gpios, &gpio_pins_idx_declared, &gpios_pins_active_high);
  50. // Init GPIOs
  51. init_mapping_gpios(gpio_pins_idx_declared, gpios_pins_active_high, nb_valid_gpios, chained_list_mapping);
  52. // Main Loop
  53. while (keepgoing) {
  54. listen_gpios_interrupts();
  55. }
  56. // De-Init GPIOs
  57. deinit_mapping_gpios();
  58. /*
  59. * Give userspace some time to read the events before we destroy the
  60. * device with UI_DEV_DESTOY.
  61. */
  62. close_uinput();
  63. return 0;
  64. }