KeyboardManager: a class to manage the polling of a switch-matrix keyboard

Dependents:   KeyboardTest

kbd_mgr/KeyboardManager.h

Committer:
osmeest
Date:
2011-01-23
Revision:
2:eb4cc53ff33d
Child:
3:1310c57aca77

File content as of revision 2:eb4cc53ff33d:

#ifndef KEYBOARD_MANAGER_HPP_
#define KEYBOARD_MANAGER_HPP_

#include "kbd_mgr/KeyPressEventHandler.h"

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

namespace kbd_mgr {

/**
 * @brief A class that polls the state of a NxM switch-matrix keyboard.
 * For efficiency reasons, this class uses contiguous bits in a GPIO ports for reading the state of the keys in a row.
 * Key rows are activated one by one using a set of digital outputs.
 * Each time a key press or a key release is detected, the specified event handler is invoked.
 * As the managed switch matrix is not protected against cross-overs, multiple key presses are
 * ignored (only the state changes of the first key pressed are reported).
 */
class KeyboardManager {
public:
    typedef std::vector<PinName> OutPinsSet;
    
    /**
     * @param inPort        Port to be used for reading the key state.
     * @param numKeysPerRow Number of keys in a row (N)
     * @param inLowestBit   Index of the lowest bit of inPort (using inLowestBit to inLowestBit+N).
     * @param outPins       Pins to be used for powering each key row.
     * @param handler       Event handler called to report key presses and releases.
     */
    KeyboardManager(PortName inPort, std::size_t numKeysPerRow, int inLowestBit, 
            const OutPinsSet &outPins, KeyPressEventHandler *handler);
    
    /**
     * @brief Enables the polling of the keyboard state.
     * @param pollingPeriod Frequency of the polling ticker (1kHz by default, min 20 microsec).
     */
    void start(float pollingPeriod = 1e-3);
    
    /**
     * @brief Disables the polling of the keyboard state.
     */
    void stop();
    
private:
    struct Impl; // opaque structure hiding implementation details
    std::auto_ptr<Impl> pimpl;
};

} // kbd_mgr

#endif // KEYBOARD_MANAGER_HPP_