A DTMF sequence editor and player for HAM radio equipment command & control.

Dependencies:   mbed ExtTextLCD

Committer:
osmeest
Date:
Mon Mar 07 22:51:19 2011 +0000
Revision:
0:1324e7d9d471

        

Who changed what in which revision?

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