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

Dependents:   KeyboardTest

Committer:
osmeest
Date:
Thu Feb 03 22:01:57 2011 +0000
Revision:
3:1310c57aca77
Parent:
2:eb4cc53ff33d
improve code structure, add key mapping and long key press handling

Who changed what in which revision?

UserRevisionLine numberNew contents of line
osmeest 3:1310c57aca77 1 #ifndef KEYBOARD_STATE_H_
osmeest 3:1310c57aca77 2 #define KEYBOARD_STATE_H_
osmeest 3:1310c57aca77 3
osmeest 3:1310c57aca77 4 #include <vector>
osmeest 3:1310c57aca77 5 #include <iostream>
osmeest 3:1310c57aca77 6
osmeest 3:1310c57aca77 7 namespace kbd_mgr {
osmeest 3:1310c57aca77 8
osmeest 3:1310c57aca77 9 /**
osmeest 3:1310c57aca77 10 * @brief A class to hold the state of a keyboard.
osmeest 3:1310c57aca77 11 * A keyboard is handled as a set of key rows. Each row can have as many keys as there are bits in integers.
osmeest 3:1310c57aca77 12 * The class maintains an array of integers to handle the number of rows.
osmeest 3:1310c57aca77 13 * If possible, multiple rows are combined in a single integer to reduce memory footprint.
osmeest 3:1310c57aca77 14 */
osmeest 3:1310c57aca77 15 class KeyboardState {
osmeest 3:1310c57aca77 16 public:
osmeest 3:1310c57aca77 17 /**
osmeest 3:1310c57aca77 18 * @brief Constructor for a 0x0 keyboard state.
osmeest 3:1310c57aca77 19 */
osmeest 3:1310c57aca77 20 KeyboardState();
osmeest 3:1310c57aca77 21
osmeest 3:1310c57aca77 22 /**
osmeest 3:1310c57aca77 23 * @brief Constructor for a NxM keyboard state.
osmeest 3:1310c57aca77 24 * @param numRows Number of key rows (unlimited)
osmeest 3:1310c57aca77 25 * @param numKeysPerRow Number of keys per row (limited to the number of bits in an integer).
osmeest 3:1310c57aca77 26 */
osmeest 3:1310c57aca77 27 KeyboardState(std::size_t numRows, std::size_t numKeysPerRow);
osmeest 3:1310c57aca77 28
osmeest 3:1310c57aca77 29 std::size_t getNumRows() const { return this->numRows; }
osmeest 3:1310c57aca77 30 std::size_t getNumKeysPerRow() const { return this->numKeysPerRow; }
osmeest 3:1310c57aca77 31 std::size_t getNumKeys() const { return this->numRows * this->numKeysPerRow; }
osmeest 3:1310c57aca77 32
osmeest 3:1310c57aca77 33 void clear();
osmeest 3:1310c57aca77 34 void setRowState(std::size_t row, int rowState);
osmeest 3:1310c57aca77 35 int getRowState(std::size_t row) const;
osmeest 3:1310c57aca77 36 bool getKeyState(std::size_t key) const;
osmeest 3:1310c57aca77 37
osmeest 3:1310c57aca77 38 /**
osmeest 3:1310c57aca77 39 * @brief Executes a XOR between two states.
osmeest 3:1310c57aca77 40 * @return a state that represents the keys that changed of state.
osmeest 3:1310c57aca77 41 */
osmeest 3:1310c57aca77 42 KeyboardState operator^(const KeyboardState &other) const;
osmeest 3:1310c57aca77 43
osmeest 3:1310c57aca77 44 /**
osmeest 3:1310c57aca77 45 * @brief Executes an AND between two states.
osmeest 3:1310c57aca77 46 */
osmeest 3:1310c57aca77 47 KeyboardState operator&(const KeyboardState &mask) const;
osmeest 3:1310c57aca77 48
osmeest 3:1310c57aca77 49 bool operator==(const KeyboardState &other) const;
osmeest 3:1310c57aca77 50 bool operator!=(const KeyboardState &other) const { return !(*this == other); }
osmeest 3:1310c57aca77 51
osmeest 3:1310c57aca77 52 /**
osmeest 3:1310c57aca77 53 * @brief Checks if a keyboard state is full of 0.
osmeest 3:1310c57aca77 54 */
osmeest 3:1310c57aca77 55 bool empty() const;
osmeest 3:1310c57aca77 56
osmeest 3:1310c57aca77 57 enum KeyPressType {
osmeest 3:1310c57aca77 58 Idle, SingleKeyPress, MultiKeyPress
osmeest 3:1310c57aca77 59 };
osmeest 3:1310c57aca77 60
osmeest 3:1310c57aca77 61 /**
osmeest 3:1310c57aca77 62 * @brief Determines the kind of key press present in the state.
osmeest 3:1310c57aca77 63 * The keyboard state can represent an idle keyboard, a single key pressed
osmeest 3:1310c57aca77 64 * or a key combination. This method determines which type of state this is.
osmeest 3:1310c57aca77 65 * If a single key is represented, the key index can be retrieved.
osmeest 3:1310c57aca77 66 * @param key An integer where the single key pressed should be stored.
osmeest 3:1310c57aca77 67 */
osmeest 3:1310c57aca77 68 KeyPressType getKeyPressType(int *key = NULL) const;
osmeest 3:1310c57aca77 69 KeyPressType getKeyPressType(int &key) const { return getKeyPressType(&key); }
osmeest 3:1310c57aca77 70
osmeest 3:1310c57aca77 71 void streamTo(std::ostream &out) const;
osmeest 3:1310c57aca77 72
osmeest 3:1310c57aca77 73 private:
osmeest 3:1310c57aca77 74 int getRowInfo(std::size_t row, std::size_t &wordIndex, std::size_t &rowShift, int &rowMask) const;
osmeest 3:1310c57aca77 75
osmeest 3:1310c57aca77 76 std::size_t numRows;
osmeest 3:1310c57aca77 77 std::size_t numKeysPerRow;
osmeest 3:1310c57aca77 78 int rowMask;
osmeest 3:1310c57aca77 79 std::size_t numRowsPerWord;
osmeest 3:1310c57aca77 80 std::size_t numKeysPerWord;
osmeest 3:1310c57aca77 81 std::size_t numWords;
osmeest 3:1310c57aca77 82
osmeest 3:1310c57aca77 83 typedef std::vector<int> Data;
osmeest 3:1310c57aca77 84 Data data;
osmeest 3:1310c57aca77 85 };
osmeest 3:1310c57aca77 86
osmeest 3:1310c57aca77 87 inline std::ostream & operator<<(std::ostream &out, const KeyboardState &s) {
osmeest 3:1310c57aca77 88 s.streamTo(out);
osmeest 3:1310c57aca77 89 return out;
osmeest 3:1310c57aca77 90 }
osmeest 3:1310c57aca77 91
osmeest 3:1310c57aca77 92 } // kbd_mgr
osmeest 3:1310c57aca77 93
osmeest 3:1310c57aca77 94 #endif // KEYBOARD_STATE_H_