Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI mbed-os BSP_DISCO_F429ZI
SecurityUnlockDemo-Main.cpp@0:04d4cc695e56, 2019-06-01 (annotated)
- Committer:
- Damotclese
- Date:
- Sat Jun 01 05:32:16 2019 +0000
- Revision:
- 0:04d4cc695e56
- Child:
- 1:316582aec4fb
Initial demonstration project check in.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Damotclese | 0:04d4cc695e56 | 1 | |
Damotclese | 0:04d4cc695e56 | 2 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 3 | // SecurityUnlockDemo-Main.cpp |
Damotclese | 0:04d4cc695e56 | 4 | // |
Damotclese | 0:04d4cc695e56 | 5 | // Fredric L. Rice, May 2019 |
Damotclese | 0:04d4cc695e56 | 6 | // |
Damotclese | 0:04d4cc695e56 | 7 | // This is a demonstration of how to build a keypad on the liquid |
Damotclese | 0:04d4cc695e56 | 8 | // crystal display and then use the touch screen as a keypad to enter |
Damotclese | 0:04d4cc695e56 | 9 | // an access code which grants access to other functionality. Two |
Damotclese | 0:04d4cc695e56 | 10 | // levels of access is possible, with the more permissive access a |
Damotclese | 0:04d4cc695e56 | 11 | // longer series of digits than the lesser-permissive access code. |
Damotclese | 0:04d4cc695e56 | 12 | // |
Damotclese | 0:04d4cc695e56 | 13 | // The keypad is built using position information in a table with some |
Damotclese | 0:04d4cc695e56 | 14 | // fairly easy ways to move the keypad around on the display. |
Damotclese | 0:04d4cc695e56 | 15 | // |
Damotclese | 0:04d4cc695e56 | 16 | // The demonstration code also allows for the push button to be used |
Damotclese | 0:04d4cc695e56 | 17 | // to key in Moorse Code pulses which are timed and then checked to |
Damotclese | 0:04d4cc695e56 | 18 | // see if they match an expected access code for Moorse access. |
Damotclese | 0:04d4cc695e56 | 19 | // |
Damotclese | 0:04d4cc695e56 | 20 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 21 | |
Damotclese | 0:04d4cc695e56 | 22 | #include "mbed.h" // The mbed operating system |
Damotclese | 0:04d4cc695e56 | 23 | #include "LCD_DISCO_F429ZI.h" // For controlling the LCD |
Damotclese | 0:04d4cc695e56 | 24 | #include "TS_DISCO_F429ZI.h" // For controlling the touch screen |
Damotclese | 0:04d4cc695e56 | 25 | #include "SecurityUnlockDemo-Main.h" // Always include our own header |
Damotclese | 0:04d4cc695e56 | 26 | |
Damotclese | 0:04d4cc695e56 | 27 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 28 | // Defined constants that we will use here |
Damotclese | 0:04d4cc695e56 | 29 | // |
Damotclese | 0:04d4cc695e56 | 30 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 31 | |
Damotclese | 0:04d4cc695e56 | 32 | // For some purposes we may want to know the dimentions of the screen |
Damotclese | 0:04d4cc695e56 | 33 | #define LCD_WIDTH 240 |
Damotclese | 0:04d4cc695e56 | 34 | #define LCD_HEIGHT 320 |
Damotclese | 0:04d4cc695e56 | 35 | |
Damotclese | 0:04d4cc695e56 | 36 | // We define this value to position the entire keypad on the screen |
Damotclese | 0:04d4cc695e56 | 37 | #define KEYPAD_LEFT_MARGIN 42 |
Damotclese | 0:04d4cc695e56 | 38 | #define KEYPAD_TOP_MARGIN 60 |
Damotclese | 0:04d4cc695e56 | 39 | |
Damotclese | 0:04d4cc695e56 | 40 | // The "line number" on the display to show the keys that get entered |
Damotclese | 0:04d4cc695e56 | 41 | #define ENTERED_KEYS_LINE 18 |
Damotclese | 0:04d4cc695e56 | 42 | |
Damotclese | 0:04d4cc695e56 | 43 | // We allow the operator to enter a maximum number of digits for |
Damotclese | 0:04d4cc695e56 | 44 | // the access code |
Damotclese | 0:04d4cc695e56 | 45 | #define MAX_SECURITY_DIGITS 15 |
Damotclese | 0:04d4cc695e56 | 46 | |
Damotclese | 0:04d4cc695e56 | 47 | // We allow a maximum number of Moorse Code characters to be entered |
Damotclese | 0:04d4cc695e56 | 48 | // using the push button |
Damotclese | 0:04d4cc695e56 | 49 | #define MAX_MOORSE_PULSES 10 |
Damotclese | 0:04d4cc695e56 | 50 | |
Damotclese | 0:04d4cc695e56 | 51 | // In this demonstration we define two access levels, access level 1 |
Damotclese | 0:04d4cc695e56 | 52 | // grants full access to all functionality whereas access level 2 is |
Damotclese | 0:04d4cc695e56 | 53 | // for accessing less functionality |
Damotclese | 0:04d4cc695e56 | 54 | static const uint8_t * ACCESS_LEVEL_1 = "31415926"; |
Damotclese | 0:04d4cc695e56 | 55 | static const uint8_t * ACCESS_LEVEL_2 = "2040"; |
Damotclese | 0:04d4cc695e56 | 56 | |
Damotclese | 0:04d4cc695e56 | 57 | // For the Moorse Code access, we describe the dots and dashes which |
Damotclese | 0:04d4cc695e56 | 58 | // describe the letters C and Q. We do not care about testing for a |
Damotclese | 0:04d4cc695e56 | 59 | // period of quiet beteen characters, we ignore silence, we only |
Damotclese | 0:04d4cc695e56 | 60 | // test the length of when the key is held down |
Damotclese | 0:04d4cc695e56 | 61 | static const uint8_t * ACCESS_MOORSE = "-.-.--.-"; |
Damotclese | 0:04d4cc695e56 | 62 | |
Damotclese | 0:04d4cc695e56 | 63 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 64 | // Define local data storage |
Damotclese | 0:04d4cc695e56 | 65 | // |
Damotclese | 0:04d4cc695e56 | 66 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 67 | |
Damotclese | 0:04d4cc695e56 | 68 | // We will be using the LCD so instantiate an object locally |
Damotclese | 0:04d4cc695e56 | 69 | static LCD_DISCO_F429ZI st_lcd; |
Damotclese | 0:04d4cc695e56 | 70 | |
Damotclese | 0:04d4cc695e56 | 71 | // We will be using the touch screen so instantiate an object |
Damotclese | 0:04d4cc695e56 | 72 | static TS_DISCO_F429ZI st_touchScreen; |
Damotclese | 0:04d4cc695e56 | 73 | |
Damotclese | 0:04d4cc695e56 | 74 | // Instantiate a digitial input mapped as our push button |
Damotclese | 0:04d4cc695e56 | 75 | static DigitalIn st_pushButton(PA_0); |
Damotclese | 0:04d4cc695e56 | 76 | |
Damotclese | 0:04d4cc695e56 | 77 | // When we build the keypad screen, we use the data in this table |
Damotclese | 0:04d4cc695e56 | 78 | // to position the touch keys rather than use math to determine |
Damotclese | 0:04d4cc695e56 | 79 | // their locations. The reason why we do this is so that we can |
Damotclese | 0:04d4cc695e56 | 80 | // use this table to determine which key was pressed on the touch |
Damotclese | 0:04d4cc695e56 | 81 | // screen, but also we may want to place some keys offset from |
Damotclese | 0:04d4cc695e56 | 82 | // others. A table gives us greater control over key location. |
Damotclese | 0:04d4cc695e56 | 83 | // We place 10 pixels between each keypad. |
Damotclese | 0:04d4cc695e56 | 84 | static KeypadLocation_t st_KeypadInformation[] = |
Damotclese | 0:04d4cc695e56 | 85 | { |
Damotclese | 0:04d4cc695e56 | 86 | // X, Y, H, W, Character |
Damotclese | 0:04d4cc695e56 | 87 | { KEYPAD_LEFT_MARGIN + 0, KEYPAD_TOP_MARGIN + 0, 40, 40, '1' }, |
Damotclese | 0:04d4cc695e56 | 88 | { KEYPAD_LEFT_MARGIN + 60, KEYPAD_TOP_MARGIN + 0, 40, 40, '2' }, |
Damotclese | 0:04d4cc695e56 | 89 | { KEYPAD_LEFT_MARGIN + 120, KEYPAD_TOP_MARGIN + 0, 40, 40, '3' }, |
Damotclese | 0:04d4cc695e56 | 90 | { KEYPAD_LEFT_MARGIN + 0, KEYPAD_TOP_MARGIN + 60, 40, 40, '4' }, |
Damotclese | 0:04d4cc695e56 | 91 | { KEYPAD_LEFT_MARGIN + 60, KEYPAD_TOP_MARGIN + 60, 40, 40, '5' }, |
Damotclese | 0:04d4cc695e56 | 92 | { KEYPAD_LEFT_MARGIN + 120, KEYPAD_TOP_MARGIN + 60, 40, 40, '6' }, |
Damotclese | 0:04d4cc695e56 | 93 | { KEYPAD_LEFT_MARGIN + 0, KEYPAD_TOP_MARGIN + 120, 40, 40, '7' }, |
Damotclese | 0:04d4cc695e56 | 94 | { KEYPAD_LEFT_MARGIN + 60, KEYPAD_TOP_MARGIN + 120, 40, 40, '8' }, |
Damotclese | 0:04d4cc695e56 | 95 | { KEYPAD_LEFT_MARGIN + 120, KEYPAD_TOP_MARGIN + 120, 40, 40, '9' }, |
Damotclese | 0:04d4cc695e56 | 96 | { KEYPAD_LEFT_MARGIN + 0, KEYPAD_TOP_MARGIN + 180, 40, 40, 'C' }, |
Damotclese | 0:04d4cc695e56 | 97 | { KEYPAD_LEFT_MARGIN + 60, KEYPAD_TOP_MARGIN + 180, 40, 40, '0' }, |
Damotclese | 0:04d4cc695e56 | 98 | { KEYPAD_LEFT_MARGIN + 120, KEYPAD_TOP_MARGIN + 180, 40, 40, 'E' }, |
Damotclese | 0:04d4cc695e56 | 99 | { 0, 0, 0, 0, '+' } // End of table |
Damotclese | 0:04d4cc695e56 | 100 | } ; |
Damotclese | 0:04d4cc695e56 | 101 | |
Damotclese | 0:04d4cc695e56 | 102 | // We allow a maximum number of keys to be entered for the access code |
Damotclese | 0:04d4cc695e56 | 103 | static uint8_t au8_enteredKeys[MAX_SECURITY_DIGITS + 1]; |
Damotclese | 0:04d4cc695e56 | 104 | |
Damotclese | 0:04d4cc695e56 | 105 | // We keep track of the number of digits entered for the access code |
Damotclese | 0:04d4cc695e56 | 106 | static uint8_t u8_enteredKeyCount; |
Damotclese | 0:04d4cc695e56 | 107 | |
Damotclese | 0:04d4cc695e56 | 108 | // To drive the Moorse Code access, we maintain counters which keep |
Damotclese | 0:04d4cc695e56 | 109 | // trash of how long the push button has been detected to be held |
Damotclese | 0:04d4cc695e56 | 110 | // down. Since the button is checked 10 times a second, the count |
Damotclese | 0:04d4cc695e56 | 111 | // indicates about how many milliseconds the button was held down. |
Damotclese | 0:04d4cc695e56 | 112 | // If it wraps, we don't care since that means the operator is holding |
Damotclese | 0:04d4cc695e56 | 113 | // the button down for a very long time |
Damotclese | 0:04d4cc695e56 | 114 | static uint16_t u16_buttonDownCount; |
Damotclese | 0:04d4cc695e56 | 115 | |
Damotclese | 0:04d4cc695e56 | 116 | // To determine whether the operator is finished with entering a |
Damotclese | 0:04d4cc695e56 | 117 | // Moorse Code access code, we maintain a counter of "up" time, |
Damotclese | 0:04d4cc695e56 | 118 | // a.k.a. quiet time. |
Damotclese | 0:04d4cc695e56 | 119 | static uint16_t u16_buttonUpCount; |
Damotclese | 0:04d4cc695e56 | 120 | |
Damotclese | 0:04d4cc695e56 | 121 | // When Moorse Code pulses are entered, we store the down time for |
Damotclese | 0:04d4cc695e56 | 122 | // else pulse in this array |
Damotclese | 0:04d4cc695e56 | 123 | static uint16_t au16_moorseCharacters[MAX_MOORSE_PULSES]; |
Damotclese | 0:04d4cc695e56 | 124 | |
Damotclese | 0:04d4cc695e56 | 125 | // As Moorse Code pulses are entered with the push button, we keep |
Damotclese | 0:04d4cc695e56 | 126 | // track of how many down presses there have been |
Damotclese | 0:04d4cc695e56 | 127 | static uint8_t u8_moorsePulseCount; |
Damotclese | 0:04d4cc695e56 | 128 | |
Damotclese | 0:04d4cc695e56 | 129 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 130 | // MainInit() |
Damotclese | 0:04d4cc695e56 | 131 | // |
Damotclese | 0:04d4cc695e56 | 132 | // This function will: |
Damotclese | 0:04d4cc695e56 | 133 | // o Initialize the module's locally-held data |
Damotclese | 0:04d4cc695e56 | 134 | // o Clear the LCD and set the display to WHITE |
Damotclese | 0:04d4cc695e56 | 135 | // o Set the default character font size |
Damotclese | 0:04d4cc695e56 | 136 | // o Initialize the touch screen by polling the size of the LCD |
Damotclese | 0:04d4cc695e56 | 137 | // o Sets the push button to have no pull-up |
Damotclese | 0:04d4cc695e56 | 138 | // |
Damotclese | 0:04d4cc695e56 | 139 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 140 | static void MainInit(void) |
Damotclese | 0:04d4cc695e56 | 141 | { |
Damotclese | 0:04d4cc695e56 | 142 | uint8_t u8_TouchScreenStatus = 0; |
Damotclese | 0:04d4cc695e56 | 143 | |
Damotclese | 0:04d4cc695e56 | 144 | // Initialize locall-held data in this module |
Damotclese | 0:04d4cc695e56 | 145 | u8_enteredKeyCount = 0; |
Damotclese | 0:04d4cc695e56 | 146 | u16_buttonDownCount = 0; |
Damotclese | 0:04d4cc695e56 | 147 | u16_buttonUpCount = 0; |
Damotclese | 0:04d4cc695e56 | 148 | u8_moorsePulseCount = 0; |
Damotclese | 0:04d4cc695e56 | 149 | |
Damotclese | 0:04d4cc695e56 | 150 | // Bring the LCD up |
Damotclese | 0:04d4cc695e56 | 151 | st_lcd.Clear(LCD_COLOR_WHITE); |
Damotclese | 0:04d4cc695e56 | 152 | |
Damotclese | 0:04d4cc695e56 | 153 | // Set the default font size |
Damotclese | 0:04d4cc695e56 | 154 | BSP_LCD_SetFont(&Font16); |
Damotclese | 0:04d4cc695e56 | 155 | |
Damotclese | 0:04d4cc695e56 | 156 | // Initialize the touch screen |
Damotclese | 0:04d4cc695e56 | 157 | u8_TouchScreenStatus = st_touchScreen.Init(st_lcd.GetXSize(), st_lcd.GetYSize()); |
Damotclese | 0:04d4cc695e56 | 158 | |
Damotclese | 0:04d4cc695e56 | 159 | if (TS_OK != u8_TouchScreenStatus) |
Damotclese | 0:04d4cc695e56 | 160 | { |
Damotclese | 0:04d4cc695e56 | 161 | st_lcd.DisplayStringAt(1, LINE(ENTERED_KEYS_LINE), |
Damotclese | 0:04d4cc695e56 | 162 | (uint8_t *)"Touch screen failed", CENTER_MODE); |
Damotclese | 0:04d4cc695e56 | 163 | } |
Damotclese | 0:04d4cc695e56 | 164 | |
Damotclese | 0:04d4cc695e56 | 165 | // Set the push button to not have an internal pull-up or down |
Damotclese | 0:04d4cc695e56 | 166 | st_pushButton.mode(PullNone); |
Damotclese | 0:04d4cc695e56 | 167 | } |
Damotclese | 0:04d4cc695e56 | 168 | |
Damotclese | 0:04d4cc695e56 | 169 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 170 | // MainDisplayEnteredKeys() |
Damotclese | 0:04d4cc695e56 | 171 | // |
Damotclese | 0:04d4cc695e56 | 172 | // This function will display the entered keys, if any, on the display |
Damotclese | 0:04d4cc695e56 | 173 | // on the line defined for the entered keys. It will center the |
Damotclese | 0:04d4cc695e56 | 174 | // characters. |
Damotclese | 0:04d4cc695e56 | 175 | // |
Damotclese | 0:04d4cc695e56 | 176 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 177 | static void MainDisplayEnteredKeys(void) |
Damotclese | 0:04d4cc695e56 | 178 | { |
Damotclese | 0:04d4cc695e56 | 179 | // Display the accumulated security code digits |
Damotclese | 0:04d4cc695e56 | 180 | st_lcd.DisplayStringAt(1, LINE(ENTERED_KEYS_LINE), au8_enteredKeys, CENTER_MODE); |
Damotclese | 0:04d4cc695e56 | 181 | } |
Damotclese | 0:04d4cc695e56 | 182 | |
Damotclese | 0:04d4cc695e56 | 183 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 184 | // MainGrantAccess() |
Damotclese | 0:04d4cc695e56 | 185 | // |
Damotclese | 0:04d4cc695e56 | 186 | // This function is invoked to display what access level has been |
Damotclese | 0:04d4cc695e56 | 187 | // granted. |
Damotclese | 0:04d4cc695e56 | 188 | // |
Damotclese | 0:04d4cc695e56 | 189 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 190 | static void MainGrantAccess(uint8_t u8_thisAccessLevel) |
Damotclese | 0:04d4cc695e56 | 191 | { |
Damotclese | 0:04d4cc695e56 | 192 | uint8_t au8_reportString[21] = { 0 }; |
Damotclese | 0:04d4cc695e56 | 193 | |
Damotclese | 0:04d4cc695e56 | 194 | // Clear the display |
Damotclese | 0:04d4cc695e56 | 195 | st_lcd.Clear(LCD_COLOR_WHITE); |
Damotclese | 0:04d4cc695e56 | 196 | |
Damotclese | 0:04d4cc695e56 | 197 | // Build a report to offer |
Damotclese | 0:04d4cc695e56 | 198 | (void)sprintf((char *)au8_reportString, "Level %u", u8_thisAccessLevel); |
Damotclese | 0:04d4cc695e56 | 199 | |
Damotclese | 0:04d4cc695e56 | 200 | // Display the level of access that was granted |
Damotclese | 0:04d4cc695e56 | 201 | st_lcd.DisplayStringAt(1, LINE(1), (uint8_t *)"Access granted", CENTER_MODE); |
Damotclese | 0:04d4cc695e56 | 202 | st_lcd.DisplayStringAt(1, LINE(2), au8_reportString, CENTER_MODE); |
Damotclese | 0:04d4cc695e56 | 203 | st_lcd.DisplayStringAt(1, LINE(3), (uint8_t *)"Press RESET to exit", CENTER_MODE); |
Damotclese | 0:04d4cc695e56 | 204 | } |
Damotclese | 0:04d4cc695e56 | 205 | |
Damotclese | 0:04d4cc695e56 | 206 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 207 | // MainProcessEntryCode() |
Damotclese | 0:04d4cc695e56 | 208 | // |
Damotclese | 0:04d4cc695e56 | 209 | // This function will: |
Damotclese | 0:04d4cc695e56 | 210 | // o Check the size of Level 1 access to see if it matches the |
Damotclese | 0:04d4cc695e56 | 211 | // number of characters that were entered |
Damotclese | 0:04d4cc695e56 | 212 | // o Check to see if the Level 1 access code matches the digits |
Damotclese | 0:04d4cc695e56 | 213 | // entered |
Damotclese | 0:04d4cc695e56 | 214 | // o Grant Access Level 1 if both the size and the digits match |
Damotclese | 0:04d4cc695e56 | 215 | // |
Damotclese | 0:04d4cc695e56 | 216 | // o Check the size of Level 2 access to see if it matches the |
Damotclese | 0:04d4cc695e56 | 217 | // number of characters that were entered |
Damotclese | 0:04d4cc695e56 | 218 | // o Check to see if the Level 2 access code matches the digits |
Damotclese | 0:04d4cc695e56 | 219 | // entered |
Damotclese | 0:04d4cc695e56 | 220 | // o Grant Access Level 2 if both the size and the digits match |
Damotclese | 0:04d4cc695e56 | 221 | // |
Damotclese | 0:04d4cc695e56 | 222 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 223 | static void MainProcessEntryCode(uint8_t u8_originalKeyCount) |
Damotclese | 0:04d4cc695e56 | 224 | { |
Damotclese | 0:04d4cc695e56 | 225 | // See if the access code that was entered is level 1 |
Damotclese | 0:04d4cc695e56 | 226 | if (strlen((char *)ACCESS_LEVEL_1) == u8_originalKeyCount) |
Damotclese | 0:04d4cc695e56 | 227 | { |
Damotclese | 0:04d4cc695e56 | 228 | // Do the digits entered match the level 1 access code? |
Damotclese | 0:04d4cc695e56 | 229 | if (! memcmp(ACCESS_LEVEL_1, au8_enteredKeys, u8_originalKeyCount)) |
Damotclese | 0:04d4cc695e56 | 230 | { |
Damotclese | 0:04d4cc695e56 | 231 | // It does so grant access level 1 |
Damotclese | 0:04d4cc695e56 | 232 | MainGrantAccess(1); |
Damotclese | 0:04d4cc695e56 | 233 | return; |
Damotclese | 0:04d4cc695e56 | 234 | } |
Damotclese | 0:04d4cc695e56 | 235 | } |
Damotclese | 0:04d4cc695e56 | 236 | |
Damotclese | 0:04d4cc695e56 | 237 | // That did not grant access, see if the entered value is level 2 |
Damotclese | 0:04d4cc695e56 | 238 | if (strlen((char *)ACCESS_LEVEL_2) == u8_originalKeyCount) |
Damotclese | 0:04d4cc695e56 | 239 | { |
Damotclese | 0:04d4cc695e56 | 240 | // Do the digits entered match the level 2 access code? |
Damotclese | 0:04d4cc695e56 | 241 | if (! memcmp(ACCESS_LEVEL_2, au8_enteredKeys, u8_originalKeyCount)) |
Damotclese | 0:04d4cc695e56 | 242 | { |
Damotclese | 0:04d4cc695e56 | 243 | // It does so grant access level 1 |
Damotclese | 0:04d4cc695e56 | 244 | MainGrantAccess(2); |
Damotclese | 0:04d4cc695e56 | 245 | return; |
Damotclese | 0:04d4cc695e56 | 246 | } |
Damotclese | 0:04d4cc695e56 | 247 | } |
Damotclese | 0:04d4cc695e56 | 248 | } |
Damotclese | 0:04d4cc695e56 | 249 | |
Damotclese | 0:04d4cc695e56 | 250 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 251 | // MainProcessKeypadKey() |
Damotclese | 0:04d4cc695e56 | 252 | // |
Damotclese | 0:04d4cc695e56 | 253 | // This function will: |
Damotclese | 0:04d4cc695e56 | 254 | // o Store the number of characters that have been entered, if any |
Damotclese | 0:04d4cc695e56 | 255 | // |
Damotclese | 0:04d4cc695e56 | 256 | // o Check to see if the character that is passed to the function |
Damotclese | 0:04d4cc695e56 | 257 | // is a 'C' for Clear |
Damotclese | 0:04d4cc695e56 | 258 | // o Clear the line of entered digits, if any |
Damotclese | 0:04d4cc695e56 | 259 | // o Set the acquired character count to zero |
Damotclese | 0:04d4cc695e56 | 260 | // |
Damotclese | 0:04d4cc695e56 | 261 | // o Check ti see if the character entered is an 'E' for Enter |
Damotclese | 0:04d4cc695e56 | 262 | // o Clear the line of any entered digits, if any |
Damotclese | 0:04d4cc695e56 | 263 | // o Set the acquired character count to zero |
Damotclese | 0:04d4cc695e56 | 264 | // o Call a function which evaluates the digits that have been |
Damotclese | 0:04d4cc695e56 | 265 | // entered, if any |
Damotclese | 0:04d4cc695e56 | 266 | // |
Damotclese | 0:04d4cc695e56 | 267 | // o Checks ti see if there is room to store the newly-entered |
Damotclese | 0:04d4cc695e56 | 268 | // digit in the accumulation buffer |
Damotclese | 0:04d4cc695e56 | 269 | // o Stores the character in to the buffer andincrements the |
Damotclese | 0:04d4cc695e56 | 270 | // entered digit counter |
Damotclese | 0:04d4cc695e56 | 271 | // o Ensures that the string of entered digits is NULL terminated |
Damotclese | 0:04d4cc695e56 | 272 | // o Calls a function which displays the entered digits |
Damotclese | 0:04d4cc695e56 | 273 | // |
Damotclese | 0:04d4cc695e56 | 274 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 275 | static void MainProcessKeypadKey(uint8_t u8_thisKeyASCIICharacter) |
Damotclese | 0:04d4cc695e56 | 276 | { |
Damotclese | 0:04d4cc695e56 | 277 | uint8_t u8_originalKeyCount = u8_enteredKeyCount; |
Damotclese | 0:04d4cc695e56 | 278 | |
Damotclese | 0:04d4cc695e56 | 279 | // Is the key a C for Clear? |
Damotclese | 0:04d4cc695e56 | 280 | if ('C' == u8_thisKeyASCIICharacter) |
Damotclese | 0:04d4cc695e56 | 281 | { |
Damotclese | 0:04d4cc695e56 | 282 | // It is, so clear the display line of accumulated characters |
Damotclese | 0:04d4cc695e56 | 283 | st_lcd.ClearStringLine(ENTERED_KEYS_LINE); |
Damotclese | 0:04d4cc695e56 | 284 | |
Damotclese | 0:04d4cc695e56 | 285 | // Discard our accumulated digit count |
Damotclese | 0:04d4cc695e56 | 286 | u8_enteredKeyCount = 0; |
Damotclese | 0:04d4cc695e56 | 287 | } |
Damotclese | 0:04d4cc695e56 | 288 | |
Damotclese | 0:04d4cc695e56 | 289 | // Is the character that was pressed en E for Enter? |
Damotclese | 0:04d4cc695e56 | 290 | else if ('E' == u8_thisKeyASCIICharacter) |
Damotclese | 0:04d4cc695e56 | 291 | { |
Damotclese | 0:04d4cc695e56 | 292 | // It is, so before we process the code, clear the entered digits |
Damotclese | 0:04d4cc695e56 | 293 | st_lcd.ClearStringLine(ENTERED_KEYS_LINE); |
Damotclese | 0:04d4cc695e56 | 294 | |
Damotclese | 0:04d4cc695e56 | 295 | // Discard our accumulated digit count |
Damotclese | 0:04d4cc695e56 | 296 | u8_enteredKeyCount = 0; |
Damotclese | 0:04d4cc695e56 | 297 | |
Damotclese | 0:04d4cc695e56 | 298 | // Process the entry code |
Damotclese | 0:04d4cc695e56 | 299 | MainProcessEntryCode(u8_originalKeyCount); |
Damotclese | 0:04d4cc695e56 | 300 | } |
Damotclese | 0:04d4cc695e56 | 301 | |
Damotclese | 0:04d4cc695e56 | 302 | // Anything else we assume is a numertic digit |
Damotclese | 0:04d4cc695e56 | 303 | else |
Damotclese | 0:04d4cc695e56 | 304 | { |
Damotclese | 0:04d4cc695e56 | 305 | // Do we have room for more digits? |
Damotclese | 0:04d4cc695e56 | 306 | if (u8_enteredKeyCount < MAX_SECURITY_DIGITS) |
Damotclese | 0:04d4cc695e56 | 307 | { |
Damotclese | 0:04d4cc695e56 | 308 | // Store the entered digit in to the accumulated key buffer |
Damotclese | 0:04d4cc695e56 | 309 | au8_enteredKeys[u8_enteredKeyCount++] = u8_thisKeyASCIICharacter; |
Damotclese | 0:04d4cc695e56 | 310 | |
Damotclese | 0:04d4cc695e56 | 311 | // Make sure that the character string is NULL terminated |
Damotclese | 0:04d4cc695e56 | 312 | au8_enteredKeys[u8_enteredKeyCount] = 0x00; |
Damotclese | 0:04d4cc695e56 | 313 | |
Damotclese | 0:04d4cc695e56 | 314 | // Update the display with the new key value |
Damotclese | 0:04d4cc695e56 | 315 | MainDisplayEnteredKeys(); |
Damotclese | 0:04d4cc695e56 | 316 | } |
Damotclese | 0:04d4cc695e56 | 317 | } |
Damotclese | 0:04d4cc695e56 | 318 | } |
Damotclese | 0:04d4cc695e56 | 319 | |
Damotclese | 0:04d4cc695e56 | 320 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 321 | // MainHandleKeyPress() |
Damotclese | 0:04d4cc695e56 | 322 | // |
Damotclese | 0:04d4cc695e56 | 323 | // When a key is pressed, the X and Y coordinates of the position where |
Damotclese | 0:04d4cc695e56 | 324 | // the LCD was touched gets passed to this function. |
Damotclese | 0:04d4cc695e56 | 325 | // |
Damotclese | 0:04d4cc695e56 | 326 | // The function steps throug each of the keys defined in the keypad |
Damotclese | 0:04d4cc695e56 | 327 | // map, checking an area bounded by the beginning X and Y coordinates |
Damotclese | 0:04d4cc695e56 | 328 | // of the keys, and by that position plus the height and width of |
Damotclese | 0:04d4cc695e56 | 329 | // trhe key. |
Damotclese | 0:04d4cc695e56 | 330 | // |
Damotclese | 0:04d4cc695e56 | 331 | // If the touch screen position that was touched matches the area of |
Damotclese | 0:04d4cc695e56 | 332 | // a known key, a function is called to process the new key. |
Damotclese | 0:04d4cc695e56 | 333 | // |
Damotclese | 0:04d4cc695e56 | 334 | // If a position of the screen was touched that does not match any |
Damotclese | 0:04d4cc695e56 | 335 | // known key position, the function ignores the screen touch. |
Damotclese | 0:04d4cc695e56 | 336 | // |
Damotclese | 0:04d4cc695e56 | 337 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 338 | static void MainHandleKeyPress(uint16_t u16_screenX, uint16_t u16_screenY) |
Damotclese | 0:04d4cc695e56 | 339 | { |
Damotclese | 0:04d4cc695e56 | 340 | uint16_t u16_keyMinimumX = 0; |
Damotclese | 0:04d4cc695e56 | 341 | uint16_t u16_keyMaximumX = 0; |
Damotclese | 0:04d4cc695e56 | 342 | uint16_t u16_keyMinimumY = 0; |
Damotclese | 0:04d4cc695e56 | 343 | uint16_t u16_keyMaximumY = 0; |
Damotclese | 0:04d4cc695e56 | 344 | uint8_t u8_keyPadItem = 0; |
Damotclese | 0:04d4cc695e56 | 345 | |
Damotclese | 0:04d4cc695e56 | 346 | // Step through the keys to check until we reach an entry that's zero |
Damotclese | 0:04d4cc695e56 | 347 | while(0 != st_KeypadInformation[u8_keyPadItem].u16_screenXLocation) |
Damotclese | 0:04d4cc695e56 | 348 | { |
Damotclese | 0:04d4cc695e56 | 349 | // Calculate the boundaries of this key |
Damotclese | 0:04d4cc695e56 | 350 | u16_keyMinimumX = st_KeypadInformation[u8_keyPadItem].u16_screenXLocation; |
Damotclese | 0:04d4cc695e56 | 351 | u16_keyMaximumX = u16_keyMinimumX + st_KeypadInformation[u8_keyPadItem].u16_keyHeight; |
Damotclese | 0:04d4cc695e56 | 352 | u16_keyMinimumY = st_KeypadInformation[u8_keyPadItem].u16_screenYLocation; |
Damotclese | 0:04d4cc695e56 | 353 | u16_keyMaximumY = u16_keyMinimumY + st_KeypadInformation[u8_keyPadItem].u16_keyWidth; |
Damotclese | 0:04d4cc695e56 | 354 | |
Damotclese | 0:04d4cc695e56 | 355 | // Is this the key that was pressed? |
Damotclese | 0:04d4cc695e56 | 356 | if (u16_screenX > u16_keyMinimumX && u16_screenX < u16_keyMaximumX) |
Damotclese | 0:04d4cc695e56 | 357 | { |
Damotclese | 0:04d4cc695e56 | 358 | if (u16_screenY > u16_keyMinimumY && u16_screenY < u16_keyMaximumY) |
Damotclese | 0:04d4cc695e56 | 359 | { |
Damotclese | 0:04d4cc695e56 | 360 | // This is the key that was pressed |
Damotclese | 0:04d4cc695e56 | 361 | MainProcessKeypadKey(st_KeypadInformation[u8_keyPadItem].u8_keyASCIICharacter); |
Damotclese | 0:04d4cc695e56 | 362 | |
Damotclese | 0:04d4cc695e56 | 363 | // We are finished searching |
Damotclese | 0:04d4cc695e56 | 364 | break; |
Damotclese | 0:04d4cc695e56 | 365 | } |
Damotclese | 0:04d4cc695e56 | 366 | } |
Damotclese | 0:04d4cc695e56 | 367 | |
Damotclese | 0:04d4cc695e56 | 368 | // Check the next possible key area |
Damotclese | 0:04d4cc695e56 | 369 | u8_keyPadItem++; |
Damotclese | 0:04d4cc695e56 | 370 | } |
Damotclese | 0:04d4cc695e56 | 371 | } |
Damotclese | 0:04d4cc695e56 | 372 | |
Damotclese | 0:04d4cc695e56 | 373 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 374 | // MainProcessButtonDownThenRelease() |
Damotclese | 0:04d4cc695e56 | 375 | // |
Damotclese | 0:04d4cc695e56 | 376 | // After the push button has been used to send a down pulse, this |
Damotclese | 0:04d4cc695e56 | 377 | // function is called to store the down time, if there is room to |
Damotclese | 0:04d4cc695e56 | 378 | // store it. |
Damotclese | 0:04d4cc695e56 | 379 | // |
Damotclese | 0:04d4cc695e56 | 380 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 381 | static void MainProcessButtonDownThenRelease(void) |
Damotclese | 0:04d4cc695e56 | 382 | { |
Damotclese | 0:04d4cc695e56 | 383 | // Make sure that we have a valid button down timer |
Damotclese | 0:04d4cc695e56 | 384 | if (u16_buttonDownCount > 0) |
Damotclese | 0:04d4cc695e56 | 385 | { |
Damotclese | 0:04d4cc695e56 | 386 | // Is there room to store another pulse period? |
Damotclese | 0:04d4cc695e56 | 387 | if (u8_moorsePulseCount < MAX_MOORSE_PULSES) |
Damotclese | 0:04d4cc695e56 | 388 | { |
Damotclese | 0:04d4cc695e56 | 389 | // There is, so store the down counter timer |
Damotclese | 0:04d4cc695e56 | 390 | au16_moorseCharacters[u8_moorsePulseCount++] = u16_buttonDownCount; |
Damotclese | 0:04d4cc695e56 | 391 | } |
Damotclese | 0:04d4cc695e56 | 392 | } |
Damotclese | 0:04d4cc695e56 | 393 | } |
Damotclese | 0:04d4cc695e56 | 394 | |
Damotclese | 0:04d4cc695e56 | 395 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 396 | // MainProcessMoorseDigits() |
Damotclese | 0:04d4cc695e56 | 397 | // |
Damotclese | 0:04d4cc695e56 | 398 | // After the push button has been used to send Moore pulses, and after |
Damotclese | 0:04d4cc695e56 | 399 | // the button has not been pressed for 3 seconds, this function gets |
Damotclese | 0:04d4cc695e56 | 400 | // called to process the pulses. |
Damotclese | 0:04d4cc695e56 | 401 | // |
Damotclese | 0:04d4cc695e56 | 402 | // We consider a value of 5 or higher to be 500 milliseconds down, or |
Damotclese | 0:04d4cc695e56 | 403 | // a "dash." Any down pulse shorter than that is considere to be a "dot." |
Damotclese | 0:04d4cc695e56 | 404 | // |
Damotclese | 0:04d4cc695e56 | 405 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 406 | static void MainProcessMoorseDigits(void) |
Damotclese | 0:04d4cc695e56 | 407 | { |
Damotclese | 0:04d4cc695e56 | 408 | uint8_t u8_testLoop = 0; |
Damotclese | 0:04d4cc695e56 | 409 | uint8_t au8_reportString[21] = { 0 }; |
Damotclese | 0:04d4cc695e56 | 410 | uint8_t au8_moorseString[21] = { 0 }; |
Damotclese | 0:04d4cc695e56 | 411 | |
Damotclese | 0:04d4cc695e56 | 412 | // Since we have pulses, clear the display to get ready for a report |
Damotclese | 0:04d4cc695e56 | 413 | st_lcd.Clear(LCD_COLOR_WHITE); |
Damotclese | 0:04d4cc695e56 | 414 | |
Damotclese | 0:04d4cc695e56 | 415 | for (u8_testLoop = 0; u8_testLoop < u8_moorsePulseCount; u8_testLoop++) |
Damotclese | 0:04d4cc695e56 | 416 | { |
Damotclese | 0:04d4cc695e56 | 417 | // Build a report of the times that we measured |
Damotclese | 0:04d4cc695e56 | 418 | (void)sprintf((char *)au8_reportString, "Time: %u", |
Damotclese | 0:04d4cc695e56 | 419 | au16_moorseCharacters[u8_testLoop]); |
Damotclese | 0:04d4cc695e56 | 420 | |
Damotclese | 0:04d4cc695e56 | 421 | // Was the time down greater than 500 milliseconds? |
Damotclese | 0:04d4cc695e56 | 422 | if (au16_moorseCharacters[u8_testLoop] > 5) |
Damotclese | 0:04d4cc695e56 | 423 | { |
Damotclese | 0:04d4cc695e56 | 424 | // It was, so we consider that to be a dash |
Damotclese | 0:04d4cc695e56 | 425 | au8_moorseString[u8_testLoop] = '-'; |
Damotclese | 0:04d4cc695e56 | 426 | } |
Damotclese | 0:04d4cc695e56 | 427 | else |
Damotclese | 0:04d4cc695e56 | 428 | { |
Damotclese | 0:04d4cc695e56 | 429 | // It was not so we consider that to be a dot |
Damotclese | 0:04d4cc695e56 | 430 | au8_moorseString[u8_testLoop] = '.'; |
Damotclese | 0:04d4cc695e56 | 431 | } |
Damotclese | 0:04d4cc695e56 | 432 | |
Damotclese | 0:04d4cc695e56 | 433 | // Display the times that we measured |
Damotclese | 0:04d4cc695e56 | 434 | st_lcd.DisplayStringAt(1, LINE(u8_testLoop + 1), au8_reportString, LEFT_MODE); |
Damotclese | 0:04d4cc695e56 | 435 | } |
Damotclese | 0:04d4cc695e56 | 436 | |
Damotclese | 0:04d4cc695e56 | 437 | // Make sure that the string of Moorse Code pulses is NULL terminated |
Damotclese | 0:04d4cc695e56 | 438 | au8_moorseString[u8_testLoop] = 0x00; |
Damotclese | 0:04d4cc695e56 | 439 | |
Damotclese | 0:04d4cc695e56 | 440 | // Build a report showing what the Moorse Code looks like |
Damotclese | 0:04d4cc695e56 | 441 | (void)sprintf((char *)au8_reportString, "Moorse code: %s", au8_moorseString); |
Damotclese | 0:04d4cc695e56 | 442 | |
Damotclese | 0:04d4cc695e56 | 443 | // Display what the Moorse Code looked like |
Damotclese | 0:04d4cc695e56 | 444 | st_lcd.DisplayStringAt(1, LINE(u8_testLoop + 1), au8_moorseString, LEFT_MODE); |
Damotclese | 0:04d4cc695e56 | 445 | |
Damotclese | 0:04d4cc695e56 | 446 | // Is that enough pulses to satisfy all required Moorse pulses? |
Damotclese | 0:04d4cc695e56 | 447 | if (u8_moorsePulseCount == strlen((char *)ACCESS_MOORSE)) |
Damotclese | 0:04d4cc695e56 | 448 | { |
Damotclese | 0:04d4cc695e56 | 449 | // It was, are the pulses what we expect for Level 1 access? |
Damotclese | 0:04d4cc695e56 | 450 | if (! memcmp(au8_moorseString, ACCESS_MOORSE, u8_moorsePulseCount)) |
Damotclese | 0:04d4cc695e56 | 451 | { |
Damotclese | 0:04d4cc695e56 | 452 | st_lcd.DisplayStringAt(1, LINE(u8_testLoop + 2), |
Damotclese | 0:04d4cc695e56 | 453 | (uint8_t *)"Access granted", LEFT_MODE); |
Damotclese | 0:04d4cc695e56 | 454 | |
Damotclese | 0:04d4cc695e56 | 455 | // Leave everything up on the screen for 5 seconds |
Damotclese | 0:04d4cc695e56 | 456 | wait(5.0); |
Damotclese | 0:04d4cc695e56 | 457 | |
Damotclese | 0:04d4cc695e56 | 458 | // Grant access level 1 |
Damotclese | 0:04d4cc695e56 | 459 | MainGrantAccess(1); |
Damotclese | 0:04d4cc695e56 | 460 | } |
Damotclese | 0:04d4cc695e56 | 461 | } |
Damotclese | 0:04d4cc695e56 | 462 | } |
Damotclese | 0:04d4cc695e56 | 463 | |
Damotclese | 0:04d4cc695e56 | 464 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 465 | // MainScanPushButton() |
Damotclese | 0:04d4cc695e56 | 466 | // |
Damotclese | 0:04d4cc695e56 | 467 | // This function will check to see what the state of the push button |
Damotclese | 0:04d4cc695e56 | 468 | // is, and it will drive the Moorse Code entry access functionality. |
Damotclese | 0:04d4cc695e56 | 469 | // |
Damotclese | 0:04d4cc695e56 | 470 | // A push button held down for 500 milliseconds or longer is considered |
Damotclese | 0:04d4cc695e56 | 471 | // to be a "dash." And down time shorter than that is considered to be |
Damotclese | 0:04d4cc695e56 | 472 | // a "dot." After 3 seconds of quiet time after a down time has been |
Damotclese | 0:04d4cc695e56 | 473 | // detected, we consider the operator to have finished pulsing in all |
Damotclese | 0:04d4cc695e56 | 474 | // Moorse Code pulses. |
Damotclese | 0:04d4cc695e56 | 475 | // |
Damotclese | 0:04d4cc695e56 | 476 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 477 | static void MainScanPushButton(void) |
Damotclese | 0:04d4cc695e56 | 478 | { |
Damotclese | 0:04d4cc695e56 | 479 | // Acquire / poll the state of the push button |
Damotclese | 0:04d4cc695e56 | 480 | int i_buttonState = st_pushButton.read(); |
Damotclese | 0:04d4cc695e56 | 481 | |
Damotclese | 0:04d4cc695e56 | 482 | // Is the button not being pressed? |
Damotclese | 0:04d4cc695e56 | 483 | if (0 == i_buttonState) |
Damotclese | 0:04d4cc695e56 | 484 | { |
Damotclese | 0:04d4cc695e56 | 485 | // The button is not down, was the button down before? |
Damotclese | 0:04d4cc695e56 | 486 | if (u16_buttonDownCount > 0) |
Damotclese | 0:04d4cc695e56 | 487 | { |
Damotclese | 0:04d4cc695e56 | 488 | // The push button was down and now it has been released. |
Damotclese | 0:04d4cc695e56 | 489 | // Process the button down time |
Damotclese | 0:04d4cc695e56 | 490 | MainProcessButtonDownThenRelease(); |
Damotclese | 0:04d4cc695e56 | 491 | } |
Damotclese | 0:04d4cc695e56 | 492 | |
Damotclese | 0:04d4cc695e56 | 493 | // Add another count to the time that the button has been up |
Damotclese | 0:04d4cc695e56 | 494 | u16_buttonUpCount++; |
Damotclese | 0:04d4cc695e56 | 495 | |
Damotclese | 0:04d4cc695e56 | 496 | // Has the button been up for at least 3 seconds? Since |
Damotclese | 0:04d4cc695e56 | 497 | // we are called 10 times a second, we check for 3 counts. |
Damotclese | 0:04d4cc695e56 | 498 | // We only check if there has been at least 1 down pulse. |
Damotclese | 0:04d4cc695e56 | 499 | if (u8_moorsePulseCount > 0 && u16_buttonUpCount >= 30) |
Damotclese | 0:04d4cc695e56 | 500 | { |
Damotclese | 0:04d4cc695e56 | 501 | // The operator appears to be finished with entering |
Damotclese | 0:04d4cc695e56 | 502 | // the Moorse digits so process what may have come in |
Damotclese | 0:04d4cc695e56 | 503 | MainProcessMoorseDigits(); |
Damotclese | 0:04d4cc695e56 | 504 | |
Damotclese | 0:04d4cc695e56 | 505 | // Discard all Moorse down pulses we may have accumulated |
Damotclese | 0:04d4cc695e56 | 506 | u8_moorsePulseCount = 0; |
Damotclese | 0:04d4cc695e56 | 507 | } |
Damotclese | 0:04d4cc695e56 | 508 | |
Damotclese | 0:04d4cc695e56 | 509 | // Discard the button down counter |
Damotclese | 0:04d4cc695e56 | 510 | u16_buttonDownCount = 0; |
Damotclese | 0:04d4cc695e56 | 511 | } |
Damotclese | 0:04d4cc695e56 | 512 | else |
Damotclese | 0:04d4cc695e56 | 513 | { |
Damotclese | 0:04d4cc695e56 | 514 | // The button is down so count how long it is down |
Damotclese | 0:04d4cc695e56 | 515 | u16_buttonDownCount++; |
Damotclese | 0:04d4cc695e56 | 516 | |
Damotclese | 0:04d4cc695e56 | 517 | // Discard any button on time |
Damotclese | 0:04d4cc695e56 | 518 | u16_buttonUpCount = 0; |
Damotclese | 0:04d4cc695e56 | 519 | } |
Damotclese | 0:04d4cc695e56 | 520 | } |
Damotclese | 0:04d4cc695e56 | 521 | |
Damotclese | 0:04d4cc695e56 | 522 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 523 | // MainThread() |
Damotclese | 0:04d4cc695e56 | 524 | // |
Damotclese | 0:04d4cc695e56 | 525 | // Called ten times a second. |
Damotclese | 0:04d4cc695e56 | 526 | // |
Damotclese | 0:04d4cc695e56 | 527 | // This function will: |
Damotclese | 0:04d4cc695e56 | 528 | // o Get the state of the touch screen |
Damotclese | 0:04d4cc695e56 | 529 | // o Check to see if a touch screen touch was detected |
Damotclese | 0:04d4cc695e56 | 530 | // o Extract the X and Y coordinates of the screen touch |
Damotclese | 0:04d4cc695e56 | 531 | // o Call a function which will handle the screen touch |
Damotclese | 0:04d4cc695e56 | 532 | // o Call a function to monitor the push button to drive the |
Damotclese | 0:04d4cc695e56 | 533 | // Moorse Code access functionality |
Damotclese | 0:04d4cc695e56 | 534 | // |
Damotclese | 0:04d4cc695e56 | 535 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 536 | static void MainThread(void) |
Damotclese | 0:04d4cc695e56 | 537 | { |
Damotclese | 0:04d4cc695e56 | 538 | uint16_t u16_screenX = 0; |
Damotclese | 0:04d4cc695e56 | 539 | uint16_t u16_screenY = 0; |
Damotclese | 0:04d4cc695e56 | 540 | TS_StateTypeDef st_touchScreenState; |
Damotclese | 0:04d4cc695e56 | 541 | |
Damotclese | 0:04d4cc695e56 | 542 | // Get the status of the Touch Screen interface |
Damotclese | 0:04d4cc695e56 | 543 | st_touchScreen.GetState(&st_touchScreenState); |
Damotclese | 0:04d4cc695e56 | 544 | |
Damotclese | 0:04d4cc695e56 | 545 | // Has the screen been touched? |
Damotclese | 0:04d4cc695e56 | 546 | if (st_touchScreenState.TouchDetected) |
Damotclese | 0:04d4cc695e56 | 547 | { |
Damotclese | 0:04d4cc695e56 | 548 | // It has been, extract the X and Y coordinates touched |
Damotclese | 0:04d4cc695e56 | 549 | u16_screenX = st_touchScreenState.X; |
Damotclese | 0:04d4cc695e56 | 550 | u16_screenY = st_touchScreenState.Y; |
Damotclese | 0:04d4cc695e56 | 551 | |
Damotclese | 0:04d4cc695e56 | 552 | // Call the routine which handles the key press |
Damotclese | 0:04d4cc695e56 | 553 | MainHandleKeyPress(u16_screenX, u16_screenY); |
Damotclese | 0:04d4cc695e56 | 554 | } |
Damotclese | 0:04d4cc695e56 | 555 | |
Damotclese | 0:04d4cc695e56 | 556 | // Drive the Moorse Code access functionality, scanning the |
Damotclese | 0:04d4cc695e56 | 557 | // push button 10 times a second |
Damotclese | 0:04d4cc695e56 | 558 | MainScanPushButton(); |
Damotclese | 0:04d4cc695e56 | 559 | } |
Damotclese | 0:04d4cc695e56 | 560 | |
Damotclese | 0:04d4cc695e56 | 561 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 562 | // MainDrawKeypad() |
Damotclese | 0:04d4cc695e56 | 563 | // |
Damotclese | 0:04d4cc695e56 | 564 | // This function will set the LCD background to WHITE and set the |
Damotclese | 0:04d4cc695e56 | 565 | // default text color to BLUE, thehn it will display information about |
Damotclese | 0:04d4cc695e56 | 566 | // what theoperator should do, then the keypad is constructed on the |
Damotclese | 0:04d4cc695e56 | 567 | // display using the information in the keypad map. |
Damotclese | 0:04d4cc695e56 | 568 | // |
Damotclese | 0:04d4cc695e56 | 569 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 570 | static void MainDrawKeypad(void) |
Damotclese | 0:04d4cc695e56 | 571 | { |
Damotclese | 0:04d4cc695e56 | 572 | uint8_t u8_keyPadItem = 0; |
Damotclese | 0:04d4cc695e56 | 573 | |
Damotclese | 0:04d4cc695e56 | 574 | // For the keypad, we want the entire screen to be this color |
Damotclese | 0:04d4cc695e56 | 575 | st_lcd.SetBackColor(LCD_COLOR_WHITE); |
Damotclese | 0:04d4cc695e56 | 576 | |
Damotclese | 0:04d4cc695e56 | 577 | // For the keypad's general text we want characters to be this color |
Damotclese | 0:04d4cc695e56 | 578 | st_lcd.SetTextColor(LCD_COLOR_BLUE); |
Damotclese | 0:04d4cc695e56 | 579 | |
Damotclese | 0:04d4cc695e56 | 580 | // Build the ketypad display |
Damotclese | 0:04d4cc695e56 | 581 | st_lcd.DisplayStringAt(1, LINE(0), (uint8_t *)"Enter access code or", CENTER_MODE); |
Damotclese | 0:04d4cc695e56 | 582 | st_lcd.DisplayStringAt(1, LINE(1), (uint8_t *)"use the Moorse Code", CENTER_MODE); |
Damotclese | 0:04d4cc695e56 | 583 | st_lcd.DisplayStringAt(1, LINE(2), (uint8_t *)"push button to unlock", CENTER_MODE); |
Damotclese | 0:04d4cc695e56 | 584 | |
Damotclese | 0:04d4cc695e56 | 585 | // Step through the keys to plot until we reach an entry that's zero |
Damotclese | 0:04d4cc695e56 | 586 | while(0 != st_KeypadInformation[u8_keyPadItem].u16_screenXLocation) |
Damotclese | 0:04d4cc695e56 | 587 | { |
Damotclese | 0:04d4cc695e56 | 588 | // Draw the rectangle |
Damotclese | 0:04d4cc695e56 | 589 | st_lcd.FillRect(st_KeypadInformation[u8_keyPadItem].u16_screenXLocation, |
Damotclese | 0:04d4cc695e56 | 590 | st_KeypadInformation[u8_keyPadItem].u16_screenYLocation, |
Damotclese | 0:04d4cc695e56 | 591 | st_KeypadInformation[u8_keyPadItem].u16_keyHeight, |
Damotclese | 0:04d4cc695e56 | 592 | st_KeypadInformation[u8_keyPadItem].u16_keyWidth); |
Damotclese | 0:04d4cc695e56 | 593 | |
Damotclese | 0:04d4cc695e56 | 594 | // Display the character near the lower right corner of the rectangle |
Damotclese | 0:04d4cc695e56 | 595 | st_lcd.DisplayChar( |
Damotclese | 0:04d4cc695e56 | 596 | st_KeypadInformation[u8_keyPadItem].u16_screenXLocation + |
Damotclese | 0:04d4cc695e56 | 597 | (st_KeypadInformation[u8_keyPadItem].u16_keyHeight / 2) + 6, |
Damotclese | 0:04d4cc695e56 | 598 | st_KeypadInformation[u8_keyPadItem].u16_screenYLocation + |
Damotclese | 0:04d4cc695e56 | 599 | (st_KeypadInformation[u8_keyPadItem].u16_keyWidth / 2) + 2, |
Damotclese | 0:04d4cc695e56 | 600 | st_KeypadInformation[u8_keyPadItem].u8_keyASCIICharacter); |
Damotclese | 0:04d4cc695e56 | 601 | |
Damotclese | 0:04d4cc695e56 | 602 | // Go to the next keypad to create |
Damotclese | 0:04d4cc695e56 | 603 | u8_keyPadItem++; |
Damotclese | 0:04d4cc695e56 | 604 | } |
Damotclese | 0:04d4cc695e56 | 605 | } |
Damotclese | 0:04d4cc695e56 | 606 | |
Damotclese | 0:04d4cc695e56 | 607 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 608 | // main() |
Damotclese | 0:04d4cc695e56 | 609 | // |
Damotclese | 0:04d4cc695e56 | 610 | // This is the main entry called by thembed operating system. |
Damotclese | 0:04d4cc695e56 | 611 | // |
Damotclese | 0:04d4cc695e56 | 612 | // This function will: |
Damotclese | 0:04d4cc695e56 | 613 | // o Initialize this module |
Damotclese | 0:04d4cc695e56 | 614 | // o Call the function that established the LCD screen and |
Damotclese | 0:04d4cc695e56 | 615 | // draws the keypad |
Damotclese | 0:04d4cc695e56 | 616 | // o Goes in to a forever loop that wakes up 10 times a second |
Damotclese | 0:04d4cc695e56 | 617 | // o Calls a function which drives the main task which scans the |
Damotclese | 0:04d4cc695e56 | 618 | // LCD touch screen for screen touches |
Damotclese | 0:04d4cc695e56 | 619 | // |
Damotclese | 0:04d4cc695e56 | 620 | // ---------------------------------------------------------------------- |
Damotclese | 0:04d4cc695e56 | 621 | int main(void) |
Damotclese | 0:04d4cc695e56 | 622 | { |
Damotclese | 0:04d4cc695e56 | 623 | // Perform local module initialization, if any |
Damotclese | 0:04d4cc695e56 | 624 | MainInit(); |
Damotclese | 0:04d4cc695e56 | 625 | |
Damotclese | 0:04d4cc695e56 | 626 | // When we start up we will draw the touch screen keypad |
Damotclese | 0:04d4cc695e56 | 627 | MainDrawKeypad(); |
Damotclese | 0:04d4cc695e56 | 628 | |
Damotclese | 0:04d4cc695e56 | 629 | // Enter in to a forever loop which wakes up once a second |
Damotclese | 0:04d4cc695e56 | 630 | while (true) |
Damotclese | 0:04d4cc695e56 | 631 | { |
Damotclese | 0:04d4cc695e56 | 632 | // Sleep for a tenth of a second |
Damotclese | 0:04d4cc695e56 | 633 | wait(0.1); |
Damotclese | 0:04d4cc695e56 | 634 | |
Damotclese | 0:04d4cc695e56 | 635 | // Call the main process |
Damotclese | 0:04d4cc695e56 | 636 | MainThread(); |
Damotclese | 0:04d4cc695e56 | 637 | } |
Damotclese | 0:04d4cc695e56 | 638 | } |
Damotclese | 0:04d4cc695e56 | 639 | |
Damotclese | 0:04d4cc695e56 | 640 | // End of file |
Damotclese | 0:04d4cc695e56 | 641 |