Onscreen QWERTY keypad for RA8857 based display. Uses the resistive touch panel.

Dependents:   RA8875_KeyPadDemo PUB_RA8875_Keypad IAC_Final_Monil_copy

Committer:
WiredHome
Date:
Sun Nov 13 02:05:41 2016 +0000
Revision:
6:4da6fa0fe51b
Parent:
4:edb5155f1b6f
Add ability to pre-initialize the data entry buffer

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 4:edb5155f1b6f 79 /// @param[in] keyboard is an optional pointer to a @ref keyboard_t data structure,
WiredHome 4:edb5155f1b6f 80 /// NULL is the default.
WiredHome 4:edb5155f1b6f 81 /// @param[in] enter_key is an optional character used to complete the entry,
WiredHome 4:edb5155f1b6f 82 /// KYBD_SYM_ENTER is the default.
WiredHome 4:edb5155f1b6f 83 /// @param[in] esc_key is an optional character used to abandon the entry,
WiredHome 4:edb5155f1b6f 84 /// KYBD_SYM_ESCAPE is the default.
WiredHome 1:7feeebbd8367 85 /// @returns true if it was accepted.
WiredHome 1:7feeebbd8367 86 /// @returns false if it could not be accepted (no known reason to return false at this time).
WiredHome 1:7feeebbd8367 87 ///
WiredHome 1:7feeebbd8367 88 bool SetKeyboard(const keyboard_t * kbd = NULL, char enter_key = KYBD_SYM_ENTER, char esc_key = KYBD_SYM_ESCAPE);
WiredHome 1:7feeebbd8367 89
WiredHome 4:edb5155f1b6f 90 /// Set Keyboard Font.
WiredHome 4:edb5155f1b6f 91 ///
WiredHome 4:edb5155f1b6f 92 /// This selects the font, or the font size, to be used on the keyboard.
WiredHome 4:edb5155f1b6f 93 ///
WiredHome 4:edb5155f1b6f 94 /// If the font is set, that font will be used and the fontscale will be ignored.\\
WiredHome 4:edb5155f1b6f 95 /// If the font is NULL, the display defined font will be used, and if that is not
WiredHome 4:edb5155f1b6f 96 /// set the internal font will be used.\\
WiredHome 4:edb5155f1b6f 97 /// If the internal font is being used, the fontscale parameter can be set to
WiredHome 4:edb5155f1b6f 98 /// choose a scale factor, where values 1, 2, 3, and 4 are permitted.
WiredHome 4:edb5155f1b6f 99 ///
WiredHome 4:edb5155f1b6f 100 /// @param[in] font is an optional pointer to a user font, NULL is the default.
WiredHome 4:edb5155f1b6f 101 /// @param[in] fontscale is an optional parameter to scale the built-in font, if
WiredHome 4:edb5155f1b6f 102 /// a user specified font was not used.
WiredHome 4:edb5155f1b6f 103 /// @returns true if it was accepted.
WiredHome 4:edb5155f1b6f 104 /// @returns false if it could not be accepted (no known reason to return false at this time).
WiredHome 4:edb5155f1b6f 105 ///
WiredHome 4:edb5155f1b6f 106 bool SetKeyboardFont(const uint8_t * font = NULL, int fontscale = 0);
WiredHome 4:edb5155f1b6f 107
WiredHome 1:7feeebbd8367 108 /// Get a handle to the keyboard currently in use.
WiredHome 1:7feeebbd8367 109 ///
WiredHome 1:7feeebbd8367 110 /// This returns a handle to the currently active keyboard. If you first SelectKeyboard(NULL),
WiredHome 1:7feeebbd8367 111 /// and then call GetKeyboard(), it will return a handle to the built-in keyboard.
WiredHome 3:402f1126a3ec 112 ///
WiredHome 3:402f1126a3ec 113 /// This API might be used if you want to use the built-in keyboard definition, but
WiredHome 3:402f1126a3ec 114 /// resize it meet your needs.
WiredHome 1:7feeebbd8367 115 ///
WiredHome 1:7feeebbd8367 116 /// @returns a handle to the currently active keyboard.
WiredHome 1:7feeebbd8367 117 ///
WiredHome 1:7feeebbd8367 118 const keyboard_t * GetKeyboard(void) { return kbd; }
WiredHome 1:7feeebbd8367 119
WiredHome 1:7feeebbd8367 120 /// Get a string of text entered on the onscreen keypad.
WiredHome 0:9b0b4ae5b47a 121 ///
WiredHome 0:9b0b4ae5b47a 122 /// text entry is terminated by <enter> or <esc>.
WiredHome 0:9b0b4ae5b47a 123 ///
WiredHome 1:7feeebbd8367 124 /// @param[in,out] buffer is the provided buffer into which the string is written.
WiredHome 0:9b0b4ae5b47a 125 /// @param[in] size is the size of the buffer.
WiredHome 4:edb5155f1b6f 126 /// @param[in] prompt is the optional text to prompt the user. It defaults
WiredHome 6:4da6fa0fe51b 127 /// to no prompt.
WiredHome 6:4da6fa0fe51b 128 /// @param[in] initFromBuffer causes initial text from buffer to seed the data entry.
WiredHome 6:4da6fa0fe51b 129 /// If this is true and if the strlen(buffer) < size, it will be shown.
WiredHome 0:9b0b4ae5b47a 130 /// @param[in] mask is the optional character, if non-zero, that is used to obscure
WiredHome 4:edb5155f1b6f 131 /// the text entry - for password purposes. It defaults to not hidden.
WiredHome 4:edb5155f1b6f 132 /// @param[in] autoclose is an optional parameter to erase the keyboard when done.
WiredHome 4:edb5155f1b6f 133 /// It defaults to true. Erase is accomplished by filling with the
WiredHome 4:edb5155f1b6f 134 /// 'back' color that instantiated the Keypad.
WiredHome 0:9b0b4ae5b47a 135 /// @returns true if text was entered.
WiredHome 0:9b0b4ae5b47a 136 /// @returns false if text was not entered (e.g. <esc> pressed).
WiredHome 0:9b0b4ae5b47a 137 ///
WiredHome 6:4da6fa0fe51b 138 bool GetString(char * buffer, size_t size, const char * prompt = NULL,
WiredHome 6:4da6fa0fe51b 139 bool initFromBuffer = false, char mask = 0, bool autoclose = true);
WiredHome 0:9b0b4ae5b47a 140
WiredHome 0:9b0b4ae5b47a 141 /// Erase the area where the keypad panel was drawn
WiredHome 0:9b0b4ae5b47a 142 ///
WiredHome 0:9b0b4ae5b47a 143 /// This API is usually not necessary, since the parent application will generally
WiredHome 0:9b0b4ae5b47a 144 /// redraw the screen after getting the text information. This might be more useful
WiredHome 0:9b0b4ae5b47a 145 /// if the Keypad is placed onto an alternate layer.
WiredHome 0:9b0b4ae5b47a 146 ///
WiredHome 0:9b0b4ae5b47a 147 /// @param[in] fillcolor is the color with which to fill the keyboard area.
WiredHome 0:9b0b4ae5b47a 148 ///
WiredHome 0:9b0b4ae5b47a 149 void Erase(color_t fillcolor = Black);
WiredHome 0:9b0b4ae5b47a 150
WiredHome 0:9b0b4ae5b47a 151 private:
WiredHome 0:9b0b4ae5b47a 152 /// Draw a key in the specified rectangle. Invert the colors if desired.
WiredHome 0:9b0b4ae5b47a 153 ///
WiredHome 0:9b0b4ae5b47a 154 /// @param[in] r is the rectangle defining the key.
WiredHome 0:9b0b4ae5b47a 155 /// @param[in] c is the character to draw.
WiredHome 0:9b0b4ae5b47a 156 /// @param[in] invert is the optional parameter to cause the presentation to be inverted.
WiredHome 0:9b0b4ae5b47a 157 ///
WiredHome 0:9b0b4ae5b47a 158 void DrawKey(rect_t r, char c, bool invert = false);
WiredHome 0:9b0b4ae5b47a 159
WiredHome 0:9b0b4ae5b47a 160 /// Draw the entire keypad
WiredHome 0:9b0b4ae5b47a 161 ///
WiredHome 0:9b0b4ae5b47a 162 void DrawKeypad(void);
WiredHome 0:9b0b4ae5b47a 163
WiredHome 0:9b0b4ae5b47a 164 /// Draw the entire input panel which includes the prompt, edit area and keyboard.
WiredHome 0:9b0b4ae5b47a 165 ///
WiredHome 0:9b0b4ae5b47a 166 /// @param[in] prompt is the text string to print to hint to the user what you
WiredHome 0:9b0b4ae5b47a 167 /// want them to enter the text for. It is good to keep this very brief.
WiredHome 0:9b0b4ae5b47a 168 ///
WiredHome 0:9b0b4ae5b47a 169 void DrawInputPanel(const char * prompt);
WiredHome 0:9b0b4ae5b47a 170
WiredHome 0:9b0b4ae5b47a 171 /// isKeyTouched scans the keyboard for the point of touch. If the touch is
WiredHome 0:9b0b4ae5b47a 172 /// within the constraints of a key, that key is identifed.
WiredHome 0:9b0b4ae5b47a 173 ///
WiredHome 0:9b0b4ae5b47a 174 /// This API optionally provides the rectangle from which the touch occurred.
WiredHome 0:9b0b4ae5b47a 175 ///
WiredHome 0:9b0b4ae5b47a 176 /// @param[in] point is a pointer to a point_t, containing the touch coordinates.
WiredHome 0:9b0b4ae5b47a 177 /// @param[inout] rectTouched is a pointer to a rect_t. If this pointer is not NULL,
WiredHome 0:9b0b4ae5b47a 178 /// it will be filled in with the rectangle within which the touch
WiredHome 0:9b0b4ae5b47a 179 /// was detected.
WiredHome 0:9b0b4ae5b47a 180 /// returns the character at that location on the keyboard, or zero, if no key
WiredHome 0:9b0b4ae5b47a 181 /// was touched.
WiredHome 0:9b0b4ae5b47a 182 ///
WiredHome 0:9b0b4ae5b47a 183 char isKeyTouched(point_t * point, rect_t * rectTouched = NULL);
WiredHome 0:9b0b4ae5b47a 184
WiredHome 0:9b0b4ae5b47a 185 /// Show the current edit buffer metrics - number of characters and the limit.
WiredHome 0:9b0b4ae5b47a 186 ///
WiredHome 0:9b0b4ae5b47a 187 void ShowBufferMetrics(void);
WiredHome 0:9b0b4ae5b47a 188
WiredHome 1:7feeebbd8367 189 /// Compute the coordinates for the keypad
WiredHome 1:7feeebbd8367 190 ///
WiredHome 1:7feeebbd8367 191 /// @returns rect_t for the keypad.
WiredHome 1:7feeebbd8367 192 ///
WiredHome 1:7feeebbd8367 193 rect_t ComputeKeypadRect(void);
WiredHome 1:7feeebbd8367 194
WiredHome 0:9b0b4ae5b47a 195 RA8875 & ra;
WiredHome 4:edb5155f1b6f 196 const uint8_t * restore_font; // save user font
WiredHome 4:edb5155f1b6f 197 const uint8_t * user_font; // keyboard restore user font
WiredHome 4:edb5155f1b6f 198 int restore_hScale; // save scale
WiredHome 4:edb5155f1b6f 199 int restore_vScale;
WiredHome 4:edb5155f1b6f 200 int user_font_scale; // keyboard font scale
WiredHome 1:7feeebbd8367 201 const keyboard_t * kbd;
WiredHome 1:7feeebbd8367 202 char enter_key;
WiredHome 1:7feeebbd8367 203 char esc_key;
WiredHome 0:9b0b4ae5b47a 204 bool shift;
WiredHome 0:9b0b4ae5b47a 205 char * pStart;
WiredHome 0:9b0b4ae5b47a 206 char * pNext;
WiredHome 0:9b0b4ae5b47a 207 int bufSize;
WiredHome 0:9b0b4ae5b47a 208 point_t userText;
WiredHome 0:9b0b4ae5b47a 209 color_t fore;
WiredHome 0:9b0b4ae5b47a 210 color_t back;
WiredHome 0:9b0b4ae5b47a 211 };
WiredHome 0:9b0b4ae5b47a 212
WiredHome 0:9b0b4ae5b47a 213 #endif