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

Dependents:   KeyboardTest

Committer:
osmeest
Date:
Sun Jan 23 23:15:36 2011 +0000
Revision:
2:eb4cc53ff33d
Child:
3:1310c57aca77
Thorough split of the code to have one responsibility per class. Generalisation of the keyboard layout (not limited to 4x4 anymore). Introduction of the kbd_mgr namespace (also apparent in header filenames).

Who changed what in which revision?

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