6 years, 3 months ago.

Continuous Sampling of multiple buttons to debounce

Using this library for the PinDetect functionality, could you please advise what the easiest way to make these routines monitor multiple inputs would be? I don't really want to copy the full code n number of times with different variables? If it helps, I am only using the pin released and pin held released functions.


Question relating to:

InterruptIn style DigitalIn debounced with callbacks for pin state change and pin state hold. debounce, DigitalIn, InterruptIn

1 Answer

6 years, 3 months ago.

PinDetect works on individual channels, so I don't see any way of batch-configuring them. Not without writing even more code, anyway.

Here's my working code for a handful of inputs, anyway, in case it is useful.

// define the PinDetect Channels
PinDetect countIn (P0_4, PullUp);   // 
PinDetect prnCmd ( P2_12, PullUp );  
PinDetect  pb2 ( P2_11, PullUp  );   //   
PinDetect  pb3 ( P1_26, PullUp  );   //   
PinDetect  pb4 ( P1_25, PullUp  );   //   
PinDetect  pb5 ( P3_25, PullUp  );   //
PinDetect  pb6 ( P3_26, PullUp  );   //   

Each input does something unique, so we must set them up individually:

// Configure PinDetect channels for their actions:
void inputsInit(void)  {
    countIn.attach_asserted( &countInPressed );
    countIn.attach_deasserted( &countInReleased );
    pb2.attach_asserted( &holdPressed );
    pb2.attach_deasserted( &holdReleased );
    pb2.attach_asserted_held( &holdPressedHeld );
    pb3.attach_asserted( &trip1Pressed );
    pb3.attach_deasserted( &trip1Released );
    pb3.attach_asserted_held( &trip1PressedHeld );
    pb4.attach_asserted( &trip2Pressed );
    pb4.attach_deasserted( &trip2Released );
    pb4.attach_asserted_held( &trip2PressedHeld );    
    pb5.attach_asserted( &startPressed );
    pb5.attach_deasserted( &startReleased );
    pb5.attach_asserted_held( &startPressedHeld );
    pb6.attach_asserted( &menuPressed );
    pb6.attach_deasserted( &menuReleased );
    pb6.attach_asserted_held( &menuPressedHeld );     
    prnCmd.attach_asserted( &footSwitch );          
    prnCmd.attach_deasserted( &startReleased );
    countIn.setSampleFrequency(2300); // 2300us = 2,3ms. 10K = 1ms. Defaults to 20ms. 
    pb2.setSampleFrequency(); // Defaults to 20ms. 
    pb3.setSampleFrequency(); // Defaults to 20ms. 
    pb4.setSampleFrequency(); // Defaults to 20ms. 
    pb5.setSampleFrequency(); // Defaults to 20ms. 
    pb6.setSampleFrequency(); // Defaults to 20ms. 
    pb6.setSamplesTillHeld(21); //default is 50
    prnCmd.setSampleFrequency(); // Defaults to 20ms. 
    printf("PinDetect channels initialised\r\n");