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.
Diff: SecurityUnlockDemo-Main.cpp
- Revision:
- 1:316582aec4fb
- Parent:
- 0:04d4cc695e56
- Child:
- 3:0e554d8d5a19
--- a/SecurityUnlockDemo-Main.cpp Sat Jun 01 05:32:16 2019 +0000 +++ b/SecurityUnlockDemo-Main.cpp Sat Jun 01 20:43:11 2019 +0000 @@ -2,7 +2,7 @@ // ---------------------------------------------------------------------- // SecurityUnlockDemo-Main.cpp // -// Fredric L. Rice, May 2019 +// Fredric L. Rice, June 2019 // // This is a demonstration of how to build a keypad on the liquid // crystal display and then use the touch screen as a keypad to enter @@ -19,113 +19,36 @@ // // ---------------------------------------------------------------------- -#include "mbed.h" // The mbed operating system -#include "LCD_DISCO_F429ZI.h" // For controlling the LCD -#include "TS_DISCO_F429ZI.h" // For controlling the touch screen -#include "SecurityUnlockDemo-Main.h" // Always include our own header +#include "mbed.h" // The mbed operating system +#include "LCD_DISCO_F429ZI.h" // For controlling the LCD +#include "TS_DISCO_F429ZI.h" // For controlling the touch screen +#include "SecurityUnlockDemo-Main.h" // Always include our own header +#include "SecurityUnlockDemo-Keypad.h" // For all of the keypad functionality +#include "SecurityUnlockDemo-Moorse.h" // For all of the Moorse Code functionality +#include "SecurityUnlockDemo-Animation.h" // For performing animation // ---------------------------------------------------------------------- -// Defined constants that we will use here -// -// ---------------------------------------------------------------------- - -// For some purposes we may want to know the dimentions of the screen -#define LCD_WIDTH 240 -#define LCD_HEIGHT 320 - -// We define this value to position the entire keypad on the screen -#define KEYPAD_LEFT_MARGIN 42 -#define KEYPAD_TOP_MARGIN 60 - -// The "line number" on the display to show the keys that get entered -#define ENTERED_KEYS_LINE 18 - -// We allow the operator to enter a maximum number of digits for -// the access code -#define MAX_SECURITY_DIGITS 15 - -// We allow a maximum number of Moorse Code characters to be entered -// using the push button -#define MAX_MOORSE_PULSES 10 - -// In this demonstration we define two access levels, access level 1 -// grants full access to all functionality whereas access level 2 is -// for accessing less functionality -static const uint8_t * ACCESS_LEVEL_1 = "31415926"; -static const uint8_t * ACCESS_LEVEL_2 = "2040"; - -// For the Moorse Code access, we describe the dots and dashes which -// describe the letters C and Q. We do not care about testing for a -// period of quiet beteen characters, we ignore silence, we only -// test the length of when the key is held down -static const uint8_t * ACCESS_MOORSE = "-.-.--.-"; - -// ---------------------------------------------------------------------- -// Define local data storage +// Define global data storage which we will export // // ---------------------------------------------------------------------- // We will be using the LCD so instantiate an object locally - static LCD_DISCO_F429ZI st_lcd; + LCD_DISCO_F429ZI st_lcd; // We will be using the touch screen so instantiate an object - static TS_DISCO_F429ZI st_touchScreen; - - // Instantiate a digitial input mapped as our push button - static DigitalIn st_pushButton(PA_0); + TS_DISCO_F429ZI st_touchScreen; - // When we build the keypad screen, we use the data in this table - // to position the touch keys rather than use math to determine - // their locations. The reason why we do this is so that we can - // use this table to determine which key was pressed on the touch - // screen, but also we may want to place some keys offset from - // others. A table gives us greater control over key location. - // We place 10 pixels between each keypad. - static KeypadLocation_t st_KeypadInformation[] = - { - // X, Y, H, W, Character - { KEYPAD_LEFT_MARGIN + 0, KEYPAD_TOP_MARGIN + 0, 40, 40, '1' }, - { KEYPAD_LEFT_MARGIN + 60, KEYPAD_TOP_MARGIN + 0, 40, 40, '2' }, - { KEYPAD_LEFT_MARGIN + 120, KEYPAD_TOP_MARGIN + 0, 40, 40, '3' }, - { KEYPAD_LEFT_MARGIN + 0, KEYPAD_TOP_MARGIN + 60, 40, 40, '4' }, - { KEYPAD_LEFT_MARGIN + 60, KEYPAD_TOP_MARGIN + 60, 40, 40, '5' }, - { KEYPAD_LEFT_MARGIN + 120, KEYPAD_TOP_MARGIN + 60, 40, 40, '6' }, - { KEYPAD_LEFT_MARGIN + 0, KEYPAD_TOP_MARGIN + 120, 40, 40, '7' }, - { KEYPAD_LEFT_MARGIN + 60, KEYPAD_TOP_MARGIN + 120, 40, 40, '8' }, - { KEYPAD_LEFT_MARGIN + 120, KEYPAD_TOP_MARGIN + 120, 40, 40, '9' }, - { KEYPAD_LEFT_MARGIN + 0, KEYPAD_TOP_MARGIN + 180, 40, 40, 'C' }, - { KEYPAD_LEFT_MARGIN + 60, KEYPAD_TOP_MARGIN + 180, 40, 40, '0' }, - { KEYPAD_LEFT_MARGIN + 120, KEYPAD_TOP_MARGIN + 180, 40, 40, 'E' }, - { 0, 0, 0, 0, '+' } // End of table - } ; +// ---------------------------------------------------------------------- +// Local data storage this module will hold +// +// ---------------------------------------------------------------------- + + // Because the mbed randomizer is not very good, we maintain the + // following counter which is incremented every time the main + // thread executes so that we can use it to assist in randoming + // our numbers for the animation + static uint32_t u32_randomSeeder; - // We allow a maximum number of keys to be entered for the access code - static uint8_t au8_enteredKeys[MAX_SECURITY_DIGITS + 1]; - - // We keep track of the number of digits entered for the access code - static uint8_t u8_enteredKeyCount; - - // To drive the Moorse Code access, we maintain counters which keep - // trash of how long the push button has been detected to be held - // down. Since the button is checked 10 times a second, the count - // indicates about how many milliseconds the button was held down. - // If it wraps, we don't care since that means the operator is holding - // the button down for a very long time - static uint16_t u16_buttonDownCount; - - // To determine whether the operator is finished with entering a - // Moorse Code access code, we maintain a counter of "up" time, - // a.k.a. quiet time. - static uint16_t u16_buttonUpCount; - - // When Moorse Code pulses are entered, we store the down time for - // else pulse in this array - static uint16_t au16_moorseCharacters[MAX_MOORSE_PULSES]; - - // As Moorse Code pulses are entered with the push button, we keep - // track of how many down presses there have been - static uint8_t u8_moorsePulseCount; - // ---------------------------------------------------------------------- // MainInit() // @@ -139,14 +62,11 @@ // ---------------------------------------------------------------------- static void MainInit(void) { - uint8_t u8_TouchScreenStatus = 0; + // Initialize locally-held data + u32_randomSeeder = 0; - // Initialize locall-held data in this module - u8_enteredKeyCount = 0; - u16_buttonDownCount = 0; - u16_buttonUpCount = 0; - u8_moorsePulseCount = 0; - + uint8_t u8_TouchScreenStatus = 0; + // Bring the LCD up st_lcd.Clear(LCD_COLOR_WHITE); @@ -161,23 +81,6 @@ st_lcd.DisplayStringAt(1, LINE(ENTERED_KEYS_LINE), (uint8_t *)"Touch screen failed", CENTER_MODE); } - - // Set the push button to not have an internal pull-up or down - st_pushButton.mode(PullNone); -} - -// ---------------------------------------------------------------------- -// MainDisplayEnteredKeys() -// -// This function will display the entered keys, if any, on the display -// on the line defined for the entered keys. It will center the -// characters. -// -// ---------------------------------------------------------------------- -static void MainDisplayEnteredKeys(void) -{ - // Display the accumulated security code digits - st_lcd.DisplayStringAt(1, LINE(ENTERED_KEYS_LINE), au8_enteredKeys, CENTER_MODE); } // ---------------------------------------------------------------------- @@ -187,7 +90,7 @@ // granted. // // ---------------------------------------------------------------------- -static void MainGrantAccess(uint8_t u8_thisAccessLevel) +void MainGrantAccess(uint8_t u8_thisAccessLevel) { uint8_t au8_reportString[21] = { 0 }; @@ -201,322 +104,9 @@ st_lcd.DisplayStringAt(1, LINE(1), (uint8_t *)"Access granted", CENTER_MODE); st_lcd.DisplayStringAt(1, LINE(2), au8_reportString, CENTER_MODE); st_lcd.DisplayStringAt(1, LINE(3), (uint8_t *)"Press RESET to exit", CENTER_MODE); -} - -// ---------------------------------------------------------------------- -// MainProcessEntryCode() -// -// This function will: -// o Check the size of Level 1 access to see if it matches the -// number of characters that were entered -// o Check to see if the Level 1 access code matches the digits -// entered -// o Grant Access Level 1 if both the size and the digits match -// -// o Check the size of Level 2 access to see if it matches the -// number of characters that were entered -// o Check to see if the Level 2 access code matches the digits -// entered -// o Grant Access Level 2 if both the size and the digits match -// -// ---------------------------------------------------------------------- -static void MainProcessEntryCode(uint8_t u8_originalKeyCount) -{ - // See if the access code that was entered is level 1 - if (strlen((char *)ACCESS_LEVEL_1) == u8_originalKeyCount) - { - // Do the digits entered match the level 1 access code? - if (! memcmp(ACCESS_LEVEL_1, au8_enteredKeys, u8_originalKeyCount)) - { - // It does so grant access level 1 - MainGrantAccess(1); - return; - } - } - // That did not grant access, see if the entered value is level 2 - if (strlen((char *)ACCESS_LEVEL_2) == u8_originalKeyCount) - { - // Do the digits entered match the level 2 access code? - if (! memcmp(ACCESS_LEVEL_2, au8_enteredKeys, u8_originalKeyCount)) - { - // It does so grant access level 1 - MainGrantAccess(2); - return; - } - } -} - -// ---------------------------------------------------------------------- -// MainProcessKeypadKey() -// -// This function will: -// o Store the number of characters that have been entered, if any -// -// o Check to see if the character that is passed to the function -// is a 'C' for Clear -// o Clear the line of entered digits, if any -// o Set the acquired character count to zero -// -// o Check ti see if the character entered is an 'E' for Enter -// o Clear the line of any entered digits, if any -// o Set the acquired character count to zero -// o Call a function which evaluates the digits that have been -// entered, if any -// -// o Checks ti see if there is room to store the newly-entered -// digit in the accumulation buffer -// o Stores the character in to the buffer andincrements the -// entered digit counter -// o Ensures that the string of entered digits is NULL terminated -// o Calls a function which displays the entered digits -// -// ---------------------------------------------------------------------- -static void MainProcessKeypadKey(uint8_t u8_thisKeyASCIICharacter) -{ - uint8_t u8_originalKeyCount = u8_enteredKeyCount; - - // Is the key a C for Clear? - if ('C' == u8_thisKeyASCIICharacter) - { - // It is, so clear the display line of accumulated characters - st_lcd.ClearStringLine(ENTERED_KEYS_LINE); - - // Discard our accumulated digit count - u8_enteredKeyCount = 0; - } - - // Is the character that was pressed en E for Enter? - else if ('E' == u8_thisKeyASCIICharacter) - { - // It is, so before we process the code, clear the entered digits - st_lcd.ClearStringLine(ENTERED_KEYS_LINE); - - // Discard our accumulated digit count - u8_enteredKeyCount = 0; - - // Process the entry code - MainProcessEntryCode(u8_originalKeyCount); - } - - // Anything else we assume is a numertic digit - else - { - // Do we have room for more digits? - if (u8_enteredKeyCount < MAX_SECURITY_DIGITS) - { - // Store the entered digit in to the accumulated key buffer - au8_enteredKeys[u8_enteredKeyCount++] = u8_thisKeyASCIICharacter; - - // Make sure that the character string is NULL terminated - au8_enteredKeys[u8_enteredKeyCount] = 0x00; - - // Update the display with the new key value - MainDisplayEnteredKeys(); - } - } -} - -// ---------------------------------------------------------------------- -// MainHandleKeyPress() -// -// When a key is pressed, the X and Y coordinates of the position where -// the LCD was touched gets passed to this function. -// -// The function steps throug each of the keys defined in the keypad -// map, checking an area bounded by the beginning X and Y coordinates -// of the keys, and by that position plus the height and width of -// trhe key. -// -// If the touch screen position that was touched matches the area of -// a known key, a function is called to process the new key. -// -// If a position of the screen was touched that does not match any -// known key position, the function ignores the screen touch. -// -// ---------------------------------------------------------------------- -static void MainHandleKeyPress(uint16_t u16_screenX, uint16_t u16_screenY) -{ - uint16_t u16_keyMinimumX = 0; - uint16_t u16_keyMaximumX = 0; - uint16_t u16_keyMinimumY = 0; - uint16_t u16_keyMaximumY = 0; - uint8_t u8_keyPadItem = 0; - - // Step through the keys to check until we reach an entry that's zero - while(0 != st_KeypadInformation[u8_keyPadItem].u16_screenXLocation) - { - // Calculate the boundaries of this key - u16_keyMinimumX = st_KeypadInformation[u8_keyPadItem].u16_screenXLocation; - u16_keyMaximumX = u16_keyMinimumX + st_KeypadInformation[u8_keyPadItem].u16_keyHeight; - u16_keyMinimumY = st_KeypadInformation[u8_keyPadItem].u16_screenYLocation; - u16_keyMaximumY = u16_keyMinimumY + st_KeypadInformation[u8_keyPadItem].u16_keyWidth; - - // Is this the key that was pressed? - if (u16_screenX > u16_keyMinimumX && u16_screenX < u16_keyMaximumX) - { - if (u16_screenY > u16_keyMinimumY && u16_screenY < u16_keyMaximumY) - { - // This is the key that was pressed - MainProcessKeypadKey(st_KeypadInformation[u8_keyPadItem].u8_keyASCIICharacter); - - // We are finished searching - break; - } - } - - // Check the next possible key area - u8_keyPadItem++; - } -} - -// ---------------------------------------------------------------------- -// MainProcessButtonDownThenRelease() -// -// After the push button has been used to send a down pulse, this -// function is called to store the down time, if there is room to -// store it. -// -// ---------------------------------------------------------------------- -static void MainProcessButtonDownThenRelease(void) -{ - // Make sure that we have a valid button down timer - if (u16_buttonDownCount > 0) - { - // Is there room to store another pulse period? - if (u8_moorsePulseCount < MAX_MOORSE_PULSES) - { - // There is, so store the down counter timer - au16_moorseCharacters[u8_moorsePulseCount++] = u16_buttonDownCount; - } - } -} - -// ---------------------------------------------------------------------- -// MainProcessMoorseDigits() -// -// After the push button has been used to send Moore pulses, and after -// the button has not been pressed for 3 seconds, this function gets -// called to process the pulses. -// -// We consider a value of 5 or higher to be 500 milliseconds down, or -// a "dash." Any down pulse shorter than that is considere to be a "dot." -// -// ---------------------------------------------------------------------- -static void MainProcessMoorseDigits(void) -{ - uint8_t u8_testLoop = 0; - uint8_t au8_reportString[21] = { 0 }; - uint8_t au8_moorseString[21] = { 0 }; - - // Since we have pulses, clear the display to get ready for a report - st_lcd.Clear(LCD_COLOR_WHITE); - - for (u8_testLoop = 0; u8_testLoop < u8_moorsePulseCount; u8_testLoop++) - { - // Build a report of the times that we measured - (void)sprintf((char *)au8_reportString, "Time: %u", - au16_moorseCharacters[u8_testLoop]); - - // Was the time down greater than 500 milliseconds? - if (au16_moorseCharacters[u8_testLoop] > 5) - { - // It was, so we consider that to be a dash - au8_moorseString[u8_testLoop] = '-'; - } - else - { - // It was not so we consider that to be a dot - au8_moorseString[u8_testLoop] = '.'; - } - - // Display the times that we measured - st_lcd.DisplayStringAt(1, LINE(u8_testLoop + 1), au8_reportString, LEFT_MODE); - } - - // Make sure that the string of Moorse Code pulses is NULL terminated - au8_moorseString[u8_testLoop] = 0x00; - - // Build a report showing what the Moorse Code looks like - (void)sprintf((char *)au8_reportString, "Moorse code: %s", au8_moorseString); - - // Display what the Moorse Code looked like - st_lcd.DisplayStringAt(1, LINE(u8_testLoop + 1), au8_moorseString, LEFT_MODE); - - // Is that enough pulses to satisfy all required Moorse pulses? - if (u8_moorsePulseCount == strlen((char *)ACCESS_MOORSE)) - { - // It was, are the pulses what we expect for Level 1 access? - if (! memcmp(au8_moorseString, ACCESS_MOORSE, u8_moorsePulseCount)) - { - st_lcd.DisplayStringAt(1, LINE(u8_testLoop + 2), - (uint8_t *)"Access granted", LEFT_MODE); - - // Leave everything up on the screen for 5 seconds - wait(5.0); - - // Grant access level 1 - MainGrantAccess(1); - } - } -} - -// ---------------------------------------------------------------------- -// MainScanPushButton() -// -// This function will check to see what the state of the push button -// is, and it will drive the Moorse Code entry access functionality. -// -// A push button held down for 500 milliseconds or longer is considered -// to be a "dash." And down time shorter than that is considered to be -// a "dot." After 3 seconds of quiet time after a down time has been -// detected, we consider the operator to have finished pulsing in all -// Moorse Code pulses. -// -// ---------------------------------------------------------------------- -static void MainScanPushButton(void) -{ - // Acquire / poll the state of the push button - int i_buttonState = st_pushButton.read(); - - // Is the button not being pressed? - if (0 == i_buttonState) - { - // The button is not down, was the button down before? - if (u16_buttonDownCount > 0) - { - // The push button was down and now it has been released. - // Process the button down time - MainProcessButtonDownThenRelease(); - } - - // Add another count to the time that the button has been up - u16_buttonUpCount++; - - // Has the button been up for at least 3 seconds? Since - // we are called 10 times a second, we check for 3 counts. - // We only check if there has been at least 1 down pulse. - if (u8_moorsePulseCount > 0 && u16_buttonUpCount >= 30) - { - // The operator appears to be finished with entering - // the Moorse digits so process what may have come in - MainProcessMoorseDigits(); - - // Discard all Moorse down pulses we may have accumulated - u8_moorsePulseCount = 0; - } - - // Discard the button down counter - u16_buttonDownCount = 0; - } - else - { - // The button is down so count how long it is down - u16_buttonDownCount++; - - // Discard any button on time - u16_buttonUpCount = 0; - } + // Perform the screen animation + AnimationPerformAnimation(u32_randomSeeder); } // ---------------------------------------------------------------------- @@ -550,58 +140,16 @@ u16_screenY = st_touchScreenState.Y; // Call the routine which handles the key press - MainHandleKeyPress(u16_screenX, u16_screenY); + KeypadHandleKeyPress(u16_screenX, u16_screenY); } // Drive the Moorse Code access functionality, scanning the // push button 10 times a second - MainScanPushButton(); -} - -// ---------------------------------------------------------------------- -// MainDrawKeypad() -// -// This function will set the LCD background to WHITE and set the -// default text color to BLUE, thehn it will display information about -// what theoperator should do, then the keypad is constructed on the -// display using the information in the keypad map. -// -// ---------------------------------------------------------------------- -static void MainDrawKeypad(void) -{ - uint8_t u8_keyPadItem = 0; - - // For the keypad, we want the entire screen to be this color - st_lcd.SetBackColor(LCD_COLOR_WHITE); - - // For the keypad's general text we want characters to be this color - st_lcd.SetTextColor(LCD_COLOR_BLUE); + MoorseScanPushButton(); - // Build the ketypad display - st_lcd.DisplayStringAt(1, LINE(0), (uint8_t *)"Enter access code or", CENTER_MODE); - st_lcd.DisplayStringAt(1, LINE(1), (uint8_t *)"use the Moorse Code", CENTER_MODE); - st_lcd.DisplayStringAt(1, LINE(2), (uint8_t *)"push button to unlock", CENTER_MODE); - - // Step through the keys to plot until we reach an entry that's zero - while(0 != st_KeypadInformation[u8_keyPadItem].u16_screenXLocation) - { - // Draw the rectangle - st_lcd.FillRect(st_KeypadInformation[u8_keyPadItem].u16_screenXLocation, - st_KeypadInformation[u8_keyPadItem].u16_screenYLocation, - st_KeypadInformation[u8_keyPadItem].u16_keyHeight, - st_KeypadInformation[u8_keyPadItem].u16_keyWidth); - - // Display the character near the lower right corner of the rectangle - st_lcd.DisplayChar( - st_KeypadInformation[u8_keyPadItem].u16_screenXLocation + - (st_KeypadInformation[u8_keyPadItem].u16_keyHeight / 2) + 6, - st_KeypadInformation[u8_keyPadItem].u16_screenYLocation + - (st_KeypadInformation[u8_keyPadItem].u16_keyWidth / 2) + 2, - st_KeypadInformation[u8_keyPadItem].u8_keyASCIICharacter); - - // Go to the next keypad to create - u8_keyPadItem++; - } + // Keep track of how many times this function has been called + // so that we can attempt to see the random generator + u32_randomSeeder++; } // ---------------------------------------------------------------------- @@ -623,8 +171,17 @@ // Perform local module initialization, if any MainInit(); + // Initialize the Keypad module + KeypadInit(); + + // Initialize the Moorse Code module + MoorseInit(); + + // Initialize the Animation module + AnimationInit(); + // When we start up we will draw the touch screen keypad - MainDrawKeypad(); + KeypadDrawKeypad(); // Enter in to a forever loop which wakes up once a second while (true)