Tobi's ubw test branch

Dependencies:   mavlink_bridge mbed

Fork of AIT_UWB_Range by Benjamin Hepp

InterruptMultiplexer.h

Committer:
bhepp
Date:
2015-11-26
Revision:
50:50b8aea54a51
Parent:
48:5999e510f154
Child:
52:94688f414dcd

File content as of revision 50:50b8aea54a51:

#pragma once

#include "mbed.h"
#include <vector>
#include <utility>

//#include "PC.h"
//static PC _pc(USBTX, USBRX, 115200);           // USB UART Terminal

class InterruptMultiplexer {
public:

    InterruptMultiplexer(PinName irq_pin, PinName status_pin = D2)
        : irq(irq_pin), status(D2) {
        if (status.is_connected())
            status = 1;
    }

    void clear() {
        isr_array.clear();
    }

    InterruptIn& getIRQ() {
        return irq;
    }

    void enableCallback(int index) {
        if (index < isr_array.size()) {
//            _pc.printf("Enabled %d\r\n", index);
            isr_array[index].second = true;
        }
    }

    void disableCallback(int index) {
        if (index < isr_array.size()) {
//            _pc.printf("Disabled %d\r\n", index);
            isr_array[index].second = false;
        }
    }

    void trigger() {
        if (status.is_connected()) {
            if (status)
                status = 0;
            else
                status = 1;
        }
//        _pc.printf("Trigger\r\n");
        for (int i = 0; i < isr_array.size(); ++i) {
            bool enable = isr_array[i].second;
            if (enable) {
                FunctionPointer& fptr = isr_array[i].first;
                fptr.call();
            }
        }
        //myled1 = 0;
//        _pc.printf("Trigger stop\r\n");
    }

    int addCallback(void (*isr)(void), bool enable = true) {
        FunctionPointer fptr;
        fptr.attach(isr);
        isr_array.push_back(std::make_pair(fptr, enable));
        return isr_array.size() - 1;
    }

    template <typename T>
    int addCallback(T* tptr, void (T::*isr)(void), bool enable = true) {
        FunctionPointer fptr;
        fptr.attach(tptr, isr);
        isr_array.push_back(std::make_pair(fptr, enable));
        return isr_array.size() - 1;
    }

private:
    InterruptIn irq;
    std::vector<std::pair<FunctionPointer, bool> > isr_array;
    DigitalOut status;
};