8 years, 7 months ago.

read status of interrupt pin

Hello

I've recently started working with a lpc11u24, and so far this site and forum helped me quite alot. Thanks

For a project at school I need to simulate the protocol of Tascam RC-3F 3-Way Footswitch (http://tascam.com/product/rc-3f/) to do it wireless.

This is my code so far:

//includes
#include "mbed.h"
#include <bitset>

//global variables
int check = 3;

// A class for the key with timer
class Key {
public:
    Key(PinName, unsigned int, PinName);        //constructor
    
private:
    InterruptIn intrpt;                         //interrupt inputpin
    
    DigitalOut led;                             //led for testing Key class
    DigitalOut ledUpdate;                       //led for testing ticker
    
    Ticker t;                                   //timer for sending the code each 100msec
    volatile bool isFirst;                      //bool for checking just pushed or held pushed
    bitset<9> code;                             //9bit code to send status of key
    
    void isPushed(void);                        //function for what to do when key is pushed
    void isReleased(void);                      //function for what to do when key is released
    bool chatRejection(int);                    //function to check if the key is really pushed and no noise
};

Key::Key(PinName pin, unsigned int x, PinName pLed) : intrpt(pin), isFirst(1), code(x), led(pLed), ledUpdate(LED4) {
    intrpt.rise(this, &Key::isPushed);          //add rising edge interrupt
    intrpt.fall(this, &Key::isReleased);        //add falling edge interrupt
}

bool Key::chatRejection(int j) {                //check at least 'j' times pin input to know it's not noise
    for (int i = 0; i < j; ++i) {
        wait(0.01);                             //wait 10ms between each check
        //if (pin != high) return 0;   
    }
    return 1;
}

void Key::isPushed(void) {          
    if (isFirst) {                              //checking if it's just pushed in
        if (chatRejection(check)) {             //checking if it's no noise
            led = 1;                            //sent code
            isFirst = 0;                        //key is being pushed down
        }
    } else {
        ledUpdate = 1;                          //sent other code
        wait(0.2);
        ledUpdate = 0;
    }
    t.attach(this, &Key::isPushed, 0.1);       //set timer to send code each 100msec
}
    
void Key::isReleased(void) {
    isFirst = 1;                                //key is released
    led = 0;                                    //sent code
    t.detach();                                 //kill 100msec timer
}
    

Key left(p36, 2, LED1);                         //make left key at p36
Key center(p35, 1, LED2);                       //make center key at p35
Key right(p34, 3, LED3);                        //make right key at p34

int main() {
    while(1) {
    }
}

I used leds for now to simulate my class on the board without to need of sending the code yet, later this will be a 9bit code sequence over UART. At the moment I'm stuck with bool Key::chatRejection(int j). I need to check when a key is being pushed down for at least 3 times in a time interval of 10ms so I know it's no noise. But I can't seem to find how to read the value of the interrupt pin, hence my question, how can I fix this problem? I checked the api but there is no function that does this and the source code isn't available so I can't write my own function.

Also, as my knowledge of the c++ language is rusty, it's been quite some time I programmed in c++, I'd really appreciate it if someone could give some feedback on my coding style. I know the wait(0.01) in chatRejection is not a good idea but I'm thinking to use also an extra timer for this.

1 Answer

8 years, 7 months ago.

I was going to answer you can read it as a DigitalIn, since it inherits from DigitalIn, but apparantly to my surprise it doesn't. Ah well, time for plan B: First make it a DigitalIn, next make it an InterruptIn (not 100% sure the other way around would work too). Then read the DigitalIn if you want to know its state.

Regarding chatRejection: Does it need to do other stuff why you don't want it to block? Then you need to modify it, if it doesn't, keep it as is. Yes having delays in interrupt routines is bad. But it also depends on what else you are doing with it. If it is doing only one task anyway, it doesn't really matter alot. Although for re-usability of course it is nice if you don't lock it in an interrupt routine.

Two options: Option A: just set a flag in the IRQ handler, and outside of the IRQ handler in the main loop do what needs to be done. However then you need to call some class function in your main loop, and poll that flag in your main loop.

Option B: Do the same as what your Ticker, but now with a TimeOut. If I understand it correctly, it needs to keep its state for 10ms before you want to do anything with it? Start a 10ms TimeOut, and keep your pin interrupts active: When the interrupt is called again, cancel your TimeOut/set it again depending on what you want. If the interrupt is not called again, the TimeOut interrupt handles the rest. (Of course you need to have at least fall interrupt active to check if it is just some noise).

(On a related note: There is absolutely no way noise can give incorrect edges. If you need to debounce it, you can also have a look at some of the debounce libraries).

Accepted Answer

Thanks for your answer. I've used Option A. I didn't want a delay in the IRQ handler for re-usability.

I talked to my promotor today and it turn out I don't need that chattering rejection function as the 3 keys will be simulated pure software. -.-

But I've learned something I can use later on :)

posted by Swedgin . 26 Oct 2015