This is a demonstration program which draws a keypad on the LCD and uses the touch screen to allow an operator to key-in an access code. Two possible codes are allowed to grant to different levels of access. Additionally the push button is used to allow an operator to send Moorse Code pulses in to the device which is checked against two characters to determine if there was a match, and if so, access is granted.

Dependencies:   LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI mbed-os BSP_DISCO_F429ZI

Also draws a mushroom on the screen and animates it.

Committer:
Damotclese
Date:
Sat Jun 01 20:43:11 2019 +0000
Revision:
1:316582aec4fb
Child:
2:444eeedb41f0
Added animation of a mushroom sprite that occasionally teleports.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Damotclese 1:316582aec4fb 1
Damotclese 1:316582aec4fb 2 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 3 // SecurityUnlockDemo-Moorse.cpp
Damotclese 1:316582aec4fb 4 //
Damotclese 1:316582aec4fb 5 // Fredric L. Rice, June 2019
Damotclese 1:316582aec4fb 6 //
Damotclese 1:316582aec4fb 7 // This module contains the code which performs the Moorse Code
Damotclese 1:316582aec4fb 8 // functionality
Damotclese 1:316582aec4fb 9 //
Damotclese 1:316582aec4fb 10 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 11
Damotclese 1:316582aec4fb 12 #include "mbed.h" // The mbed operating system
Damotclese 1:316582aec4fb 13 #include "LCD_DISCO_F429ZI.h" // For controlling the LCD
Damotclese 1:316582aec4fb 14 #include "TS_DISCO_F429ZI.h" // For controlling the touch screen
Damotclese 1:316582aec4fb 15 #include "SecurityUnlockDemo-Main.h" // Bring in the main module
Damotclese 1:316582aec4fb 16 #include "SecurityUnlockDemo-Moorse.h" // Always include our own header
Damotclese 1:316582aec4fb 17
Damotclese 1:316582aec4fb 18 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 19 // Describe data which is defined externally that we may access
Damotclese 1:316582aec4fb 20 //
Damotclese 1:316582aec4fb 21 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 22
Damotclese 1:316582aec4fb 23 // We may be accessing the LCD
Damotclese 1:316582aec4fb 24 extern LCD_DISCO_F429ZI st_lcd;
Damotclese 1:316582aec4fb 25
Damotclese 1:316582aec4fb 26 // We may be accessing the touch screen
Damotclese 1:316582aec4fb 27 extern TS_DISCO_F429ZI st_touchScreen;
Damotclese 1:316582aec4fb 28
Damotclese 1:316582aec4fb 29 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 30 // Define local data storage
Damotclese 1:316582aec4fb 31 //
Damotclese 1:316582aec4fb 32 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 33
Damotclese 1:316582aec4fb 34 // When the push button is used, we drive the lEDs
Damotclese 1:316582aec4fb 35 static DigitalOut st_moorseLED(LED1);
Damotclese 1:316582aec4fb 36 static DigitalOut st_moorseDash(LED2);
Damotclese 1:316582aec4fb 37
Damotclese 1:316582aec4fb 38 // For the Moorse Code access, we describe the dots and dashes which
Damotclese 1:316582aec4fb 39 // describe the letters C and Q. We do not care about testing for a
Damotclese 1:316582aec4fb 40 // period of quiet beteen characters, we ignore silence, we only
Damotclese 1:316582aec4fb 41 // test the length of when the key is held down
Damotclese 1:316582aec4fb 42 static const uint8_t * ACCESS_MOORSE = "-.-.--.-";
Damotclese 1:316582aec4fb 43
Damotclese 1:316582aec4fb 44 // Instantiate a digitial input mapped as our push button
Damotclese 1:316582aec4fb 45 static DigitalIn st_pushButton(PA_0);
Damotclese 1:316582aec4fb 46
Damotclese 1:316582aec4fb 47 // To drive the Moorse Code access, we maintain counters which keep
Damotclese 1:316582aec4fb 48 // trash of how long the push button has been detected to be held
Damotclese 1:316582aec4fb 49 // down. Since the button is checked 10 times a second, the count
Damotclese 1:316582aec4fb 50 // indicates about how many milliseconds the button was held down.
Damotclese 1:316582aec4fb 51 // If it wraps, we don't care since that means the operator is holding
Damotclese 1:316582aec4fb 52 // the button down for a very long time
Damotclese 1:316582aec4fb 53 static uint16_t u16_buttonDownCount;
Damotclese 1:316582aec4fb 54
Damotclese 1:316582aec4fb 55 // To determine whether the operator is finished with entering a
Damotclese 1:316582aec4fb 56 // Moorse Code access code, we maintain a counter of "up" time,
Damotclese 1:316582aec4fb 57 // a.k.a. quiet time.
Damotclese 1:316582aec4fb 58 static uint16_t u16_buttonUpCount;
Damotclese 1:316582aec4fb 59
Damotclese 1:316582aec4fb 60 // When Moorse Code pulses are entered, we store the down time for
Damotclese 1:316582aec4fb 61 // else pulse in this array
Damotclese 1:316582aec4fb 62 static uint16_t au16_moorseCharacters[MAX_MOORSE_PULSES];
Damotclese 1:316582aec4fb 63
Damotclese 1:316582aec4fb 64 // As Moorse Code pulses are entered with the push button, we keep
Damotclese 1:316582aec4fb 65 // track of how many down presses there have been
Damotclese 1:316582aec4fb 66 static uint8_t u8_moorsePulseCount;
Damotclese 1:316582aec4fb 67
Damotclese 1:316582aec4fb 68 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 69 // MoorseInit()
Damotclese 1:316582aec4fb 70 //
Damotclese 1:316582aec4fb 71 // This function initializes the locally-held data. It also sets the
Damotclese 1:316582aec4fb 72 // push button to have no pull-up or pull-down resister
Damotclese 1:316582aec4fb 73 //
Damotclese 1:316582aec4fb 74 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 75 void MoorseInit(void)
Damotclese 1:316582aec4fb 76 {
Damotclese 1:316582aec4fb 77 // Initialize locall-held data in this module
Damotclese 1:316582aec4fb 78 u16_buttonDownCount = 0;
Damotclese 1:316582aec4fb 79 u16_buttonUpCount = 0;
Damotclese 1:316582aec4fb 80 u8_moorsePulseCount = 0;
Damotclese 1:316582aec4fb 81
Damotclese 1:316582aec4fb 82 // Set the push button to not have an internal pull-up or down
Damotclese 1:316582aec4fb 83 st_pushButton.mode(PullNone);
Damotclese 1:316582aec4fb 84
Damotclese 1:316582aec4fb 85 // Ensure that both of the LEDs are turned OFF
Damotclese 1:316582aec4fb 86 st_moorseLED = st_moorseDash = 0;
Damotclese 1:316582aec4fb 87 }
Damotclese 1:316582aec4fb 88
Damotclese 1:316582aec4fb 89 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 90 // MoorseProcessButtonDownThenRelease()
Damotclese 1:316582aec4fb 91 //
Damotclese 1:316582aec4fb 92 // After the push button has been used to send a down pulse, this
Damotclese 1:316582aec4fb 93 // function is called to store the down time, if there is room to
Damotclese 1:316582aec4fb 94 // store it.
Damotclese 1:316582aec4fb 95 //
Damotclese 1:316582aec4fb 96 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 97 static void MoorseProcessButtonDownThenRelease(void)
Damotclese 1:316582aec4fb 98 {
Damotclese 1:316582aec4fb 99 // Make sure that we have a valid button down timer
Damotclese 1:316582aec4fb 100 if (u16_buttonDownCount > 0)
Damotclese 1:316582aec4fb 101 {
Damotclese 1:316582aec4fb 102 // Is there room to store another pulse period?
Damotclese 1:316582aec4fb 103 if (u8_moorsePulseCount < MAX_MOORSE_PULSES)
Damotclese 1:316582aec4fb 104 {
Damotclese 1:316582aec4fb 105 // There is, so store the down counter timer
Damotclese 1:316582aec4fb 106 au16_moorseCharacters[u8_moorsePulseCount++] = u16_buttonDownCount;
Damotclese 1:316582aec4fb 107 }
Damotclese 1:316582aec4fb 108 }
Damotclese 1:316582aec4fb 109 }
Damotclese 1:316582aec4fb 110
Damotclese 1:316582aec4fb 111 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 112 // MoorseProcessMoorseDigits()
Damotclese 1:316582aec4fb 113 //
Damotclese 1:316582aec4fb 114 // After the push button has been used to send Moore pulses, and after
Damotclese 1:316582aec4fb 115 // the button has not been pressed for 3 seconds, this function gets
Damotclese 1:316582aec4fb 116 // called to process the pulses.
Damotclese 1:316582aec4fb 117 //
Damotclese 1:316582aec4fb 118 // We consider a value of 5 or higher to be 500 milliseconds down, or
Damotclese 1:316582aec4fb 119 // a "dash." Any down pulse shorter than that is considere to be a "dot."
Damotclese 1:316582aec4fb 120 //
Damotclese 1:316582aec4fb 121 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 122 static void MoorseProcessMoorseDigits(void)
Damotclese 1:316582aec4fb 123 {
Damotclese 1:316582aec4fb 124 uint8_t u8_testLoop = 0;
Damotclese 1:316582aec4fb 125 uint8_t au8_reportString[21] = { 0 };
Damotclese 1:316582aec4fb 126 uint8_t au8_moorseString[21] = { 0 };
Damotclese 1:316582aec4fb 127
Damotclese 1:316582aec4fb 128 // Since we have pulses, clear the display to get ready for a report
Damotclese 1:316582aec4fb 129 st_lcd.Clear(LCD_COLOR_WHITE);
Damotclese 1:316582aec4fb 130
Damotclese 1:316582aec4fb 131 for (u8_testLoop = 0; u8_testLoop < u8_moorsePulseCount; u8_testLoop++)
Damotclese 1:316582aec4fb 132 {
Damotclese 1:316582aec4fb 133 // Build a report of the times that we measured
Damotclese 1:316582aec4fb 134 (void)sprintf((char *)au8_reportString, "Time: %u",
Damotclese 1:316582aec4fb 135 au16_moorseCharacters[u8_testLoop]);
Damotclese 1:316582aec4fb 136
Damotclese 1:316582aec4fb 137 // Was the time down greater than 500 milliseconds?
Damotclese 1:316582aec4fb 138 if (au16_moorseCharacters[u8_testLoop] > 5)
Damotclese 1:316582aec4fb 139 {
Damotclese 1:316582aec4fb 140 // It was, so we consider that to be a dash
Damotclese 1:316582aec4fb 141 au8_moorseString[u8_testLoop] = '-';
Damotclese 1:316582aec4fb 142 }
Damotclese 1:316582aec4fb 143 else
Damotclese 1:316582aec4fb 144 {
Damotclese 1:316582aec4fb 145 // It was not so we consider that to be a dot
Damotclese 1:316582aec4fb 146 au8_moorseString[u8_testLoop] = '.';
Damotclese 1:316582aec4fb 147 }
Damotclese 1:316582aec4fb 148
Damotclese 1:316582aec4fb 149 // Display the times that we measured
Damotclese 1:316582aec4fb 150 st_lcd.DisplayStringAt(1, LINE(u8_testLoop + 1), au8_reportString, LEFT_MODE);
Damotclese 1:316582aec4fb 151 }
Damotclese 1:316582aec4fb 152
Damotclese 1:316582aec4fb 153 // Make sure that the string of Moorse Code pulses is NULL terminated
Damotclese 1:316582aec4fb 154 au8_moorseString[u8_testLoop] = 0x00;
Damotclese 1:316582aec4fb 155
Damotclese 1:316582aec4fb 156 // Build a report showing what the Moorse Code looks like
Damotclese 1:316582aec4fb 157 (void)sprintf((char *)au8_reportString, "Moorse code: %s", au8_moorseString);
Damotclese 1:316582aec4fb 158
Damotclese 1:316582aec4fb 159 // Display what the Moorse Code looked like
Damotclese 1:316582aec4fb 160 st_lcd.DisplayStringAt(1, LINE(u8_testLoop + 1), au8_moorseString, LEFT_MODE);
Damotclese 1:316582aec4fb 161
Damotclese 1:316582aec4fb 162 // Is that enough pulses to satisfy all required Moorse pulses?
Damotclese 1:316582aec4fb 163 if (u8_moorsePulseCount == strlen((char *)ACCESS_MOORSE))
Damotclese 1:316582aec4fb 164 {
Damotclese 1:316582aec4fb 165 // It was, are the pulses what we expect for Level 1 access?
Damotclese 1:316582aec4fb 166 if (! memcmp(au8_moorseString, ACCESS_MOORSE, u8_moorsePulseCount))
Damotclese 1:316582aec4fb 167 {
Damotclese 1:316582aec4fb 168 st_lcd.DisplayStringAt(1, LINE(u8_testLoop + 2),
Damotclese 1:316582aec4fb 169 (uint8_t *)"Access granted", LEFT_MODE);
Damotclese 1:316582aec4fb 170
Damotclese 1:316582aec4fb 171 // Leave everything up on the screen for 5 seconds
Damotclese 1:316582aec4fb 172 wait(5.0);
Damotclese 1:316582aec4fb 173
Damotclese 1:316582aec4fb 174 // Grant access level 1
Damotclese 1:316582aec4fb 175 MainGrantAccess(1);
Damotclese 1:316582aec4fb 176 }
Damotclese 1:316582aec4fb 177 }
Damotclese 1:316582aec4fb 178 }
Damotclese 1:316582aec4fb 179
Damotclese 1:316582aec4fb 180 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 181 // MoorseScanPushButton()
Damotclese 1:316582aec4fb 182 //
Damotclese 1:316582aec4fb 183 // This function will check to see what the state of the push button
Damotclese 1:316582aec4fb 184 // is, and it will drive the Moorse Code entry access functionality.
Damotclese 1:316582aec4fb 185 //
Damotclese 1:316582aec4fb 186 // A push button held down for 500 milliseconds or longer is considered
Damotclese 1:316582aec4fb 187 // to be a "dash." And down time shorter than that is considered to be
Damotclese 1:316582aec4fb 188 // a "dot." After 3 seconds of quiet time after a down time has been
Damotclese 1:316582aec4fb 189 // detected, we consider the operator to have finished pulsing in all
Damotclese 1:316582aec4fb 190 // Moorse Code pulses.
Damotclese 1:316582aec4fb 191 //
Damotclese 1:316582aec4fb 192 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 193 void MoorseScanPushButton(void)
Damotclese 1:316582aec4fb 194 {
Damotclese 1:316582aec4fb 195 // Acquire / poll the state of the push button
Damotclese 1:316582aec4fb 196 int i_buttonState = st_moorseLED = st_pushButton.read();
Damotclese 1:316582aec4fb 197
Damotclese 1:316582aec4fb 198 // Is the button not being pressed?
Damotclese 1:316582aec4fb 199 if (0 == i_buttonState)
Damotclese 1:316582aec4fb 200 {
Damotclese 1:316582aec4fb 201 // The button is not down, was the button down before?
Damotclese 1:316582aec4fb 202 if (u16_buttonDownCount > 0)
Damotclese 1:316582aec4fb 203 {
Damotclese 1:316582aec4fb 204 // The push button was down and now it has been released.
Damotclese 1:316582aec4fb 205 // Process the button down time
Damotclese 1:316582aec4fb 206 MoorseProcessButtonDownThenRelease();
Damotclese 1:316582aec4fb 207 }
Damotclese 1:316582aec4fb 208
Damotclese 1:316582aec4fb 209 // Add another count to the time that the button has been up
Damotclese 1:316582aec4fb 210 u16_buttonUpCount++;
Damotclese 1:316582aec4fb 211
Damotclese 1:316582aec4fb 212 // Since the button is not being pressed, make sure that
Damotclese 1:316582aec4fb 213 // the Dash LED is turned OFF
Damotclese 1:316582aec4fb 214 st_moorseDash = 0;
Damotclese 1:316582aec4fb 215
Damotclese 1:316582aec4fb 216 // Has the button been up for at least 3 seconds? Since
Damotclese 1:316582aec4fb 217 // we are called 10 times a second, we check for 3 counts.
Damotclese 1:316582aec4fb 218 // We only check if there has been at least 1 down pulse.
Damotclese 1:316582aec4fb 219 if (u8_moorsePulseCount > 0 && u16_buttonUpCount >= 30)
Damotclese 1:316582aec4fb 220 {
Damotclese 1:316582aec4fb 221 // The operator appears to be finished with entering
Damotclese 1:316582aec4fb 222 // the Moorse digits so process what may have come in
Damotclese 1:316582aec4fb 223 MoorseProcessMoorseDigits();
Damotclese 1:316582aec4fb 224
Damotclese 1:316582aec4fb 225 // Discard all Moorse down pulses we may have accumulated
Damotclese 1:316582aec4fb 226 u8_moorsePulseCount = 0;
Damotclese 1:316582aec4fb 227 }
Damotclese 1:316582aec4fb 228
Damotclese 1:316582aec4fb 229 // Discard the button down counter
Damotclese 1:316582aec4fb 230 u16_buttonDownCount = 0;
Damotclese 1:316582aec4fb 231 }
Damotclese 1:316582aec4fb 232 else
Damotclese 1:316582aec4fb 233 {
Damotclese 1:316582aec4fb 234 // The button is down so count how long it is down
Damotclese 1:316582aec4fb 235 u16_buttonDownCount++;
Damotclese 1:316582aec4fb 236
Damotclese 1:316582aec4fb 237 // Discard any button on time
Damotclese 1:316582aec4fb 238 u16_buttonUpCount = 0;
Damotclese 1:316582aec4fb 239
Damotclese 1:316582aec4fb 240 // Is the button being held down long enough to indicate a dash?
Damotclese 1:316582aec4fb 241 if (u16_buttonDownCount >= 5)
Damotclese 1:316582aec4fb 242 {
Damotclese 1:316582aec4fb 243 // It is, so indicate the fact by illuminating the Dash LED
Damotclese 1:316582aec4fb 244 st_moorseDash = 1;
Damotclese 1:316582aec4fb 245 }
Damotclese 1:316582aec4fb 246 }
Damotclese 1:316582aec4fb 247 }
Damotclese 1:316582aec4fb 248
Damotclese 1:316582aec4fb 249 // End of file
Damotclese 1:316582aec4fb 250