2323 } while(0)
2424
2525#define DEBUG_GPIO_PRINTF (0)
26+ #define DEBUG_PERIODIC_CHECK_PRINTF (0)
2627#define ERROR_GPIO_PRINTF (1)
2728
2829#if (DEBUG_GPIO_PRINTF )
3132#define GPIO_PRINTF (...)
3233#endif
3334
35+ #if (DEBUG_PERIODIC_CHECK_PRINTF )
36+ #define PERIODIC_CHECK_PRINTF (...) printf(__VA_ARGS__);
37+ #else
38+ #define PERIODIC_CHECK_PRINTF (...)
39+ #endif
40+
3441#if (ERROR_GPIO_PRINTF )
3542#define GPIO_ERROR_PRINTF (...) printf(__VA_ARGS__);
3643#else
5360 * Static variables
5461 ****************************************************************/
5562static int nb_mapped_gpios ;
56- static int * gpio_pins ;
63+ static int * gpio_pins_idx_declared ;
64+ static bool * gpios_pins_active_high ;
5765STRUCT_MAPPED_GPIO * chained_list_mapping ;
5866static int max_fd = 0 ;
5967static int gpio_fd_interrupt_expander_gpio ;
@@ -62,8 +70,6 @@ static fd_set fds;
6270static bool * mask_gpio_value ;
6371static bool interrupt_i2c_expander_found = false;
6472static bool interrupt_axp209_found = false;
65- static bool mapping_PEK_short_press_activated = false;
66- static bool mapping_PEK_long_press_activated = false;
6773
6874
6975/****************************************************************
@@ -136,7 +142,7 @@ static void find_and_call_mapping_function(int idx_gpio_interrupted,
136142
137143 //GPIO_PRINTF(" Mapping searching for idx_gpio_interrupted: %d, and %s: \n", idx_gpio_interrupted, activation?"activation":"deactivation")
138144 for (i = 0 ; i < current -> nb_simultaneous_pins ; i ++ ){
139- //GPIO_PRINTF(" Pin in mapping: %d, pin_idx=%d\n", gpio_pins [current->pins_idx[i]], current->pins_idx[i]);
145+ //GPIO_PRINTF(" Pin in mapping: %d, pin_idx=%d\n", gpio_pins_idx_declared [current->pins_idx[i]], current->pins_idx[i]);
140146 // Find if current mapping contains interrupted pin
141147 if (current -> pins_idx [i ] == idx_gpio_interrupted ){
142148 gpio_found_pin_in_mapping = true;
@@ -156,7 +162,7 @@ static void find_and_call_mapping_function(int idx_gpio_interrupted,
156162 // if real mapping already found => need to deactivate previously activated ones
157163 if (mapping_found && current -> activated ){
158164 GPIO_PRINTF (" Mapping Deactivation because real one already found: GPIO %d found in following activated mapping: \n" ,
159- gpio_pins [idx_gpio_interrupted ]);
165+ gpio_pins_idx_declared [idx_gpio_interrupted ]);
160166 print_chained_list_node (current );
161167 apply_mapping_desactivation (current );
162168 }
@@ -167,15 +173,15 @@ static void find_and_call_mapping_function(int idx_gpio_interrupted,
167173 }
168174 // Print information and activate mapping
169175 GPIO_PRINTF (" Mapping Activation: GPIO %d found in following deactivated mapping: \n" ,
170- gpio_pins [idx_gpio_interrupted ]);
176+ gpio_pins_idx_declared [idx_gpio_interrupted ]);
171177 print_chained_list_node (current );
172178 apply_mapping_activation (current );
173179 }
174180 }
175181 else { // Treating deactivation cases
176182 if (current -> activated ){
177183 GPIO_PRINTF (" Mapping Desactivation: GPIO %d found in following activated mapping: \n" ,
178- gpio_pins [idx_gpio_interrupted ]);
184+ gpio_pins_idx_declared [idx_gpio_interrupted ]);
179185 print_chained_list_node (current );
180186 apply_mapping_desactivation (current );
181187 }
@@ -229,7 +235,9 @@ static int deinit_gpio_interrupt(int fd_saved)
229235 * Public functions
230236 ****************************************************************/
231237/***** Init I2C expander pin mappings *****/
232- int init_mapping_gpios (int * gpio_pins_to_declare , int nb_gpios_to_declare ,
238+ int init_mapping_gpios (int * gpio_pins_to_declare ,
239+ bool * gpios_pins_active_high_declared ,
240+ int nb_gpios_to_declare ,
233241 STRUCT_MAPPED_GPIO * chained_list_mapping_gpios )
234242{
235243 // Variables
@@ -238,7 +246,8 @@ int init_mapping_gpios(int * gpio_pins_to_declare, int nb_gpios_to_declare,
238246
239247 // Store arguments
240248 nb_mapped_gpios = nb_gpios_to_declare ;
241- gpio_pins = gpio_pins_to_declare ;
249+ gpio_pins_idx_declared = gpio_pins_to_declare ;
250+ gpios_pins_active_high = gpios_pins_active_high_declared ;
242251 chained_list_mapping = chained_list_mapping_gpios ;
243252
244253 // Init values
@@ -308,6 +317,7 @@ int listen_gpios_interrupts(void)
308317 int nb_interrupts = 0 ;
309318 bool previous_mask_gpio_value [nb_mapped_gpios ];
310319 bool mask_gpio_current_interrupts [nb_mapped_gpios ];
320+ bool forced_interrupt = false;
311321
312322 // Back up master
313323 fd_set dup = fds ;
@@ -331,11 +341,12 @@ int listen_gpios_interrupts(void)
331341#endif //TIMEOUT_SEC_SANITY_CHECK_GPIO_EXP
332342 if (!nb_interrupts ){
333343 // Timeout case
334- GPIO_PRINTF (" Timeout, forcing sanity check\n" );
344+ PERIODIC_CHECK_PRINTF (" Timeout, forcing sanity check\n" );
335345
336346 // Timeout forcing a "Found interrupt" event for sanity check
337347 interrupt_i2c_expander_found = true;
338348 interrupt_axp209_found = true;
349+ forced_interrupt = true;
339350 }
340351 else if ( nb_interrupts < 0 ) {
341352 perror ("select" );
@@ -378,7 +389,12 @@ int listen_gpios_interrupts(void)
378389
379390#ifdef ENABLE_AXP209_INTERRUPTS
380391 if (interrupt_axp209_found ){
381- GPIO_PRINTF (" Processing interrupt AXP209\n" );
392+ if (forced_interrupt ){
393+ PERIODIC_CHECK_PRINTF (" Processing forced interrupt AXP209\n" );
394+ }
395+ else {
396+ GPIO_PRINTF (" Processing real interrupt AXP209\n" );
397+ }
382398 int val_int_bank_3 = axp209_read_interrupt_bank_3 ();
383399 if (val_int_bank_3 < 0 ){
384400 GPIO_PRINTF (" Could not read AXP209 with I2C\n" );
@@ -389,21 +405,23 @@ int listen_gpios_interrupts(void)
389405 if (val_int_bank_3 & AXP209_INTERRUPT_PEK_SHORT_PRESS ){
390406 GPIO_PRINTF (" AXP209 short PEK key press detected\n" );
391407 sendKeyAndStopKey (KEY_IDX_MAPPED_FOR_SHORT_PEK_PRESS );
392- /*sendKey(KEY_IDX_MAPPED_FOR_SHORT_PEK_PRESS, !mapping_PEK_short_press_activated);
393- mapping_PEK_short_press_activated = !mapping_PEK_short_press_activated;*/
394408
395409 }
396410 if (val_int_bank_3 & AXP209_INTERRUPT_PEK_LONG_PRESS ){
397411 GPIO_PRINTF (" AXP209 long PEK key press detected\n" );
398412 sendKeyAndStopKey (KEY_IDX_MAPPED_FOR_LONG_PEK_PRESS );
399- /*sendKey(KEY_IDX_MAPPED_FOR_LONG_PEK_PRESS, !mapping_PEK_long_press_activated);
400- mapping_PEK_long_press_activated = !mapping_PEK_long_press_activated;*/
401413 }
402414 }
403415#endif //ENABLE_AXP209_INTERRUPTS
404416
405417 if (interrupt_i2c_expander_found ){
406- GPIO_PRINTF (" Processing interrupt PCAL6416AHF\n" );
418+ if (forced_interrupt ){
419+ PERIODIC_CHECK_PRINTF (" Processing forced interrupt PCAL6416AHF\n" );
420+ }
421+ else {
422+ GPIO_PRINTF (" Processing real interrupt PCAL6416AHF\n" );
423+ }
424+
407425 // Read I2C GPIO masks:
408426 int val_i2c_mask_interrupted = pcal6416a_read_mask_interrupts ();
409427 if (val_i2c_mask_interrupted < 0 ){
@@ -420,25 +438,26 @@ int listen_gpios_interrupts(void)
420438
421439 // Find GPIO idx correspondance
422440 for (idx_gpio = 0 ; idx_gpio < nb_mapped_gpios ; idx_gpio ++ ){
423- uint16_t tmp_mask_gpio = (1 << gpio_pins [idx_gpio ]);
441+ uint16_t tmp_mask_gpio = (1 << gpio_pins_idx_declared [idx_gpio ]);
424442
425443 // Found GPIO idx in active GPIOs
426444 if (val_i2c_mask_active & tmp_mask_gpio ){
427445 mask_gpio_value [idx_gpio ] = true;
428446 }
429-
447+
430448 // Found GPIO idx in interrupt mask
431449 if (val_i2c_mask_interrupted & tmp_mask_gpio ){
432450 // Print information
433- GPIO_PRINTF (" --> Interrupt GPIO: %d, idx_pin: %d\n" , gpio_pins [idx_gpio ], idx_gpio );
451+ GPIO_PRINTF (" --> Interrupt GPIO: %d, idx_pin: %d\n" , gpio_pins_idx_declared [idx_gpio ], idx_gpio );
434452 mask_gpio_current_interrupts [idx_gpio ] = true;
435453 }
436454
437455 // Sanity check: if we missed an interrupt for some reason, check if the new values are the same
438456 if ( !mask_gpio_current_interrupts [idx_gpio ] &&
439457 (mask_gpio_value [idx_gpio ] != previous_mask_gpio_value [idx_gpio ]) ){
440458 // Print information
441- GPIO_PRINTF (" --> No Interrupt (missed) but value has changed on GPIO: %d, idx_pin: %d\n" , gpio_pins [idx_gpio ], idx_gpio );
459+ GPIO_PRINTF (" --> No Interrupt (missed) but value has changed on GPIO: %d, idx_pin: %d\n" ,
460+ gpio_pins_idx_declared [idx_gpio ], idx_gpio );
442461 mask_gpio_current_interrupts [idx_gpio ] = true;
443462 }
444463 }
@@ -448,7 +467,8 @@ int listen_gpios_interrupts(void)
448467 for (idx_gpio = 0 ; idx_gpio < nb_mapped_gpios ; idx_gpio ++ ){
449468 if (!mask_gpio_current_interrupts [idx_gpio ]) continue ;
450469
451- if (mask_gpio_value [idx_gpio ]){
470+ if ( (mask_gpio_value [idx_gpio ] && gpios_pins_active_high [idx_gpio ]) ||
471+ (!mask_gpio_value [idx_gpio ] && !gpios_pins_active_high [idx_gpio ]) ){
452472 find_and_call_mapping_function (idx_gpio ,
453473 chained_list_mapping ,
454474 mask_gpio_current_interrupts ,
0 commit comments