David Smart / Keypad

Dependents:   RA8875_KeyPadDemo PUB_RA8875_Keypad IAC_Final_Monil_copy

Committer:
WiredHome
Date:
Sat Mar 05 16:47:58 2016 +0000
Revision:
3:402f1126a3ec
Parent:
2:6d05764dfde7
Child:
4:edb5155f1b6f
Remove some debug print statements, also hide the cursor while touching a key.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:9b0b4ae5b47a 1
WiredHome 0:9b0b4ae5b47a 2 #ifndef KEYPAD_H
WiredHome 0:9b0b4ae5b47a 3 #define KEYPAD_H
WiredHome 0:9b0b4ae5b47a 4 #include "mbed.h"
WiredHome 0:9b0b4ae5b47a 5 #include "RA8875.h"
WiredHome 0:9b0b4ae5b47a 6
WiredHome 1:7feeebbd8367 7 // OK, these key assignments are a bit odd looking. The value are mapped to the character set of the RA8875
WiredHome 1:7feeebbd8367 8 // internal font. The selected characters shown various symbols - left arrow for the <bs>, up arrow for <shift>
WiredHome 1:7feeebbd8367 9 // and so on. They then have to be mapped to legitimate characters as needed.
WiredHome 1:7feeebbd8367 10
WiredHome 1:7feeebbd8367 11 #define KYBD_SYM_ENTER 0x1C ///< This is a symbol in the RA8875 character set that looks most like <enter>, so it used to detect enter
WiredHome 1:7feeebbd8367 12 #define KYBD_SYM_ESCAPE 0x15 ///< This is a symbol in the RA8875 character set that is used to show and detect escape
WiredHome 1:7feeebbd8367 13 #define KYBD_SYM_BS 0x1B ///< This is a symbol in the RA8875 character set that is used to show and detect backspace
WiredHome 1:7feeebbd8367 14 #define KYBD_SYM_SHIFT 0x18 ///< This is a symbol in the RA8875 character set that is used to show and detect shift
WiredHome 1:7feeebbd8367 15 #define KYBD_SYM_TAB 0x1A ///< This is a symbol in the RA8875 character set that is used to show and detect tab (which translates to <space>
WiredHome 1:7feeebbd8367 16
WiredHome 1:7feeebbd8367 17
WiredHome 0:9b0b4ae5b47a 18 /// A QWERTY keypad, intended to be coupled with the touchscreen, provides
WiredHome 0:9b0b4ae5b47a 19 /// on-screen data entry.
WiredHome 0:9b0b4ae5b47a 20 ///
WiredHome 0:9b0b4ae5b47a 21 /// This keypad takes over the bottom of the screen to show an onscreen keyboard.
WiredHome 0:9b0b4ae5b47a 22 /// It shows a prompt, and allows basic text entry, including backspace. It does
WiredHome 0:9b0b4ae5b47a 23 /// not allow more complex editing.
WiredHome 0:9b0b4ae5b47a 24 ///
WiredHome 0:9b0b4ae5b47a 25 /// Since it takes over a significant chunk of the screen, the parent application
WiredHome 0:9b0b4ae5b47a 26 /// must be prepared to redraw the underlying data when the keyboard is no longer
WiredHome 0:9b0b4ae5b47a 27 /// in use. Alternately, the parent application may choose to select an
WiredHome 0:9b0b4ae5b47a 28 /// alternate display layer, and then activate the keypad.
WiredHome 0:9b0b4ae5b47a 29 ///
WiredHome 0:9b0b4ae5b47a 30 class Keypad {
WiredHome 0:9b0b4ae5b47a 31 public:
WiredHome 1:7feeebbd8367 32
WiredHome 1:7feeebbd8367 33 /// keyboard definition structure, used to define a keyboard.
WiredHome 1:7feeebbd8367 34 typedef struct {
WiredHome 1:7feeebbd8367 35 loc_t x; ///< left edge of keypad. typically 0 to +n. negative is translated to zero.
WiredHome 1:7feeebbd8367 36 loc_t y; ///< top edge of keypad. typically 0 to +n, and more typically in the bottom half of the screen height.
WiredHome 1:7feeebbd8367 37 /// negative number allows keypad to size from the bottom of the screen upward.
WiredHome 1:7feeebbd8367 38 dim_t width; ///< width of keypad. be sure that the keys can fit. zero is translated to screen-width.
WiredHome 1:7feeebbd8367 39 dim_t height; ///< height of keypad. be sure the number of rows will fit, based on the desired key height.
WiredHome 1:7feeebbd8367 40 /// zero number allows keypad to size from the bottom of the screen upward.
WiredHome 1:7feeebbd8367 41 int rows; ///< number of rows to show.
WiredHome 1:7feeebbd8367 42 int cols; ///< number of columns (keys) in the longest row
WiredHome 1:7feeebbd8367 43
WiredHome 1:7feeebbd8367 44 const char * keydef1; ///< pointer to the base keyboard data stream
WiredHome 1:7feeebbd8367 45 const char * keydef2; ///< pointer to an alternate keyboard data stream (e.g. after <shift> is activated)
WiredHome 1:7feeebbd8367 46 } keyboard_t;
WiredHome 1:7feeebbd8367 47
WiredHome 1:7feeebbd8367 48
WiredHome 0:9b0b4ae5b47a 49 /// Instantiator the Keypad object.
WiredHome 0:9b0b4ae5b47a 50 ///
WiredHome 0:9b0b4ae5b47a 51 /// Shows a keypad like this:
WiredHome 0:9b0b4ae5b47a 52 /// @code
WiredHome 0:9b0b4ae5b47a 53 /// Prompt: [Input text field here.......]
WiredHome 0:9b0b4ae5b47a 54 /// `123456...
WiredHome 0:9b0b4ae5b47a 55 /// qwertyui...
WiredHome 0:9b0b4ae5b47a 56 /// asdfgh...
WiredHome 0:9b0b4ae5b47a 57 /// zxcvb....
WiredHome 0:9b0b4ae5b47a 58 /// [ space ] 000/020
WiredHome 0:9b0b4ae5b47a 59 /// @endcode
WiredHome 0:9b0b4ae5b47a 60 /// The bottom right corner shows how many characters have been entered and how
WiredHome 0:9b0b4ae5b47a 61 /// many are permitted.
WiredHome 0:9b0b4ae5b47a 62 ///
WiredHome 0:9b0b4ae5b47a 63 /// @param[in] _ra is a reference to the hosting applications RA8875 display
WiredHome 0:9b0b4ae5b47a 64 /// object. This grants Keypad access to the display.
WiredHome 0:9b0b4ae5b47a 65 /// @param[in] fore is the foreground color which is for the keys, prompt, text
WiredHome 0:9b0b4ae5b47a 66 /// @param[in] back is the background color.
WiredHome 0:9b0b4ae5b47a 67 ///
WiredHome 0:9b0b4ae5b47a 68 Keypad(RA8875 & _ra, color_t fore = Blue, color_t back = White);
WiredHome 0:9b0b4ae5b47a 69
WiredHome 0:9b0b4ae5b47a 70 /// Destructor for the Keypad object.
WiredHome 0:9b0b4ae5b47a 71 ///
WiredHome 0:9b0b4ae5b47a 72 ~Keypad();
WiredHome 0:9b0b4ae5b47a 73
WiredHome 1:7feeebbd8367 74 /// Select a keyboard to use.
WiredHome 1:7feeebbd8367 75 ///
WiredHome 1:7feeebbd8367 76 /// This selects the internally defined keyboard (when NULL) or a user defined
WiredHome 1:7feeebbd8367 77 /// keyboard.
WiredHome 1:7feeebbd8367 78 ///
WiredHome 1:7feeebbd8367 79 /// @param[in] keyboard is a pointer to a @ref keyboard_t data structure.
WiredHome 1:7feeebbd8367 80 /// @param[in] enter_key is the character used to complete the entry
WiredHome 1:7feeebbd8367 81 /// @param[in] esc_key is the character used to abandon the entry
WiredHome 1:7feeebbd8367 82 /// @returns true if it was accepted.
WiredHome 1:7feeebbd8367 83 /// @returns false if it could not be accepted (no known reason to return false at this time).
WiredHome 1:7feeebbd8367 84 ///
WiredHome 1:7feeebbd8367 85 bool SetKeyboard(const keyboard_t * kbd = NULL, char enter_key = KYBD_SYM_ENTER, char esc_key = KYBD_SYM_ESCAPE);
WiredHome 1:7feeebbd8367 86
WiredHome 1:7feeebbd8367 87 /// Get a handle to the keyboard currently in use.
WiredHome 1:7feeebbd8367 88 ///
WiredHome 1:7feeebbd8367 89 /// This returns a handle to the currently active keyboard. If you first SelectKeyboard(NULL),
WiredHome 1:7feeebbd8367 90 /// and then call GetKeyboard(), it will return a handle to the built-in keyboard.
WiredHome 3:402f1126a3ec 91 ///
WiredHome 3:402f1126a3ec 92 /// This API might be used if you want to use the built-in keyboard definition, but
WiredHome 3:402f1126a3ec 93 /// resize it meet your needs.
WiredHome 1:7feeebbd8367 94 ///
WiredHome 1:7feeebbd8367 95 /// @returns a handle to the currently active keyboard.
WiredHome 1:7feeebbd8367 96 ///
WiredHome 1:7feeebbd8367 97 const keyboard_t * GetKeyboard(void) { return kbd; }
WiredHome 1:7feeebbd8367 98
WiredHome 1:7feeebbd8367 99 /// Get a string of text entered on the onscreen keypad.
WiredHome 0:9b0b4ae5b47a 100 ///
WiredHome 0:9b0b4ae5b47a 101 /// text entry is terminated by <enter> or <esc>.
WiredHome 0:9b0b4ae5b47a 102 ///
WiredHome 1:7feeebbd8367 103 /// @param[in,out] buffer is the provided buffer into which the string is written.
WiredHome 0:9b0b4ae5b47a 104 /// @param[in] size is the size of the buffer.
WiredHome 0:9b0b4ae5b47a 105 /// @param[in] prompt is the optional text to prompt the user.
WiredHome 0:9b0b4ae5b47a 106 /// @param[in] mask is the optional character, if non-zero, that is used to obscure
WiredHome 0:9b0b4ae5b47a 107 /// the text entry - for password purposes.
WiredHome 0:9b0b4ae5b47a 108 /// @returns true if text was entered.
WiredHome 0:9b0b4ae5b47a 109 /// @returns false if text was not entered (e.g. <esc> pressed).
WiredHome 0:9b0b4ae5b47a 110 ///
WiredHome 0:9b0b4ae5b47a 111 bool GetString(char * buffer, size_t size, const char * prompt = NULL, char mask = 0);
WiredHome 0:9b0b4ae5b47a 112
WiredHome 0:9b0b4ae5b47a 113 /// Erase the area where the keypad panel was drawn
WiredHome 0:9b0b4ae5b47a 114 ///
WiredHome 0:9b0b4ae5b47a 115 /// This API is usually not necessary, since the parent application will generally
WiredHome 0:9b0b4ae5b47a 116 /// redraw the screen after getting the text information. This might be more useful
WiredHome 0:9b0b4ae5b47a 117 /// if the Keypad is placed onto an alternate layer.
WiredHome 0:9b0b4ae5b47a 118 ///
WiredHome 0:9b0b4ae5b47a 119 /// @param[in] fillcolor is the color with which to fill the keyboard area.
WiredHome 0:9b0b4ae5b47a 120 ///
WiredHome 0:9b0b4ae5b47a 121 void Erase(color_t fillcolor = Black);
WiredHome 0:9b0b4ae5b47a 122
WiredHome 0:9b0b4ae5b47a 123 private:
WiredHome 0:9b0b4ae5b47a 124 /// Draw a key in the specified rectangle. Invert the colors if desired.
WiredHome 0:9b0b4ae5b47a 125 ///
WiredHome 0:9b0b4ae5b47a 126 /// @param[in] r is the rectangle defining the key.
WiredHome 0:9b0b4ae5b47a 127 /// @param[in] c is the character to draw.
WiredHome 0:9b0b4ae5b47a 128 /// @param[in] invert is the optional parameter to cause the presentation to be inverted.
WiredHome 0:9b0b4ae5b47a 129 ///
WiredHome 0:9b0b4ae5b47a 130 void DrawKey(rect_t r, char c, bool invert = false);
WiredHome 0:9b0b4ae5b47a 131
WiredHome 0:9b0b4ae5b47a 132 /// Draw the entire keypad
WiredHome 0:9b0b4ae5b47a 133 ///
WiredHome 0:9b0b4ae5b47a 134 void DrawKeypad(void);
WiredHome 0:9b0b4ae5b47a 135
WiredHome 0:9b0b4ae5b47a 136 /// Draw the entire input panel which includes the prompt, edit area and keyboard.
WiredHome 0:9b0b4ae5b47a 137 ///
WiredHome 0:9b0b4ae5b47a 138 /// @param[in] prompt is the text string to print to hint to the user what you
WiredHome 0:9b0b4ae5b47a 139 /// want them to enter the text for. It is good to keep this very brief.
WiredHome 0:9b0b4ae5b47a 140 ///
WiredHome 0:9b0b4ae5b47a 141 void DrawInputPanel(const char * prompt);
WiredHome 0:9b0b4ae5b47a 142
WiredHome 0:9b0b4ae5b47a 143 /// isKeyTouched scans the keyboard for the point of touch. If the touch is
WiredHome 0:9b0b4ae5b47a 144 /// within the constraints of a key, that key is identifed.
WiredHome 0:9b0b4ae5b47a 145 ///
WiredHome 0:9b0b4ae5b47a 146 /// This API optionally provides the rectangle from which the touch occurred.
WiredHome 0:9b0b4ae5b47a 147 ///
WiredHome 0:9b0b4ae5b47a 148 /// @param[in] point is a pointer to a point_t, containing the touch coordinates.
WiredHome 0:9b0b4ae5b47a 149 /// @param[inout] rectTouched is a pointer to a rect_t. If this pointer is not NULL,
WiredHome 0:9b0b4ae5b47a 150 /// it will be filled in with the rectangle within which the touch
WiredHome 0:9b0b4ae5b47a 151 /// was detected.
WiredHome 0:9b0b4ae5b47a 152 /// returns the character at that location on the keyboard, or zero, if no key
WiredHome 0:9b0b4ae5b47a 153 /// was touched.
WiredHome 0:9b0b4ae5b47a 154 ///
WiredHome 0:9b0b4ae5b47a 155 char isKeyTouched(point_t * point, rect_t * rectTouched = NULL);
WiredHome 0:9b0b4ae5b47a 156
WiredHome 0:9b0b4ae5b47a 157 /// Show the current edit buffer metrics - number of characters and the limit.
WiredHome 0:9b0b4ae5b47a 158 ///
WiredHome 0:9b0b4ae5b47a 159 void ShowBufferMetrics(void);
WiredHome 0:9b0b4ae5b47a 160
WiredHome 1:7feeebbd8367 161 /// Compute the coordinates for the keypad
WiredHome 1:7feeebbd8367 162 ///
WiredHome 1:7feeebbd8367 163 /// @returns rect_t for the keypad.
WiredHome 1:7feeebbd8367 164 ///
WiredHome 1:7feeebbd8367 165 rect_t ComputeKeypadRect(void);
WiredHome 1:7feeebbd8367 166
WiredHome 0:9b0b4ae5b47a 167 RA8875 & ra;
WiredHome 1:7feeebbd8367 168 const keyboard_t * kbd;
WiredHome 1:7feeebbd8367 169 char enter_key;
WiredHome 1:7feeebbd8367 170 char esc_key;
WiredHome 0:9b0b4ae5b47a 171 bool shift;
WiredHome 0:9b0b4ae5b47a 172 char * pStart;
WiredHome 0:9b0b4ae5b47a 173 char * pNext;
WiredHome 0:9b0b4ae5b47a 174 int bufSize;
WiredHome 0:9b0b4ae5b47a 175 point_t userText;
WiredHome 0:9b0b4ae5b47a 176 color_t fore;
WiredHome 0:9b0b4ae5b47a 177 color_t back;
WiredHome 0:9b0b4ae5b47a 178 };
WiredHome 0:9b0b4ae5b47a 179
WiredHome 0:9b0b4ae5b47a 180 #endif