Fredric Rice / Mbed 2 deprecated DemoKeypadMoorseCodeUnlockTouchAndButton

Dependencies:   LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI mbed-os BSP_DISCO_F429ZI

Committer:
Damotclese
Date:
Wed Jun 05 02:27:51 2019 +0000
Revision:
5:5c507689ae66
Parent:
3:0e554d8d5a19
I put a pizza BMP on the screen;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Damotclese 0:04d4cc695e56 1
Damotclese 0:04d4cc695e56 2 // ----------------------------------------------------------------------
Damotclese 0:04d4cc695e56 3 // SecurityUnlockDemo-Main.cpp
Damotclese 0:04d4cc695e56 4 //
Damotclese 1:316582aec4fb 5 // Fredric L. Rice, June 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 1:316582aec4fb 22 #include "mbed.h" // The mbed operating system
Damotclese 1:316582aec4fb 23 #include "LCD_DISCO_F429ZI.h" // For controlling the LCD
Damotclese 1:316582aec4fb 24 #include "TS_DISCO_F429ZI.h" // For controlling the touch screen
Damotclese 1:316582aec4fb 25 #include "SecurityUnlockDemo-Main.h" // Always include our own header
Damotclese 1:316582aec4fb 26 #include "SecurityUnlockDemo-Keypad.h" // For all of the keypad functionality
Damotclese 1:316582aec4fb 27 #include "SecurityUnlockDemo-Moorse.h" // For all of the Moorse Code functionality
Damotclese 1:316582aec4fb 28 #include "SecurityUnlockDemo-Animation.h" // For performing animation
Damotclese 0:04d4cc695e56 29
Damotclese 0:04d4cc695e56 30 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 31 // Define global data storage which we will export
Damotclese 0:04d4cc695e56 32 //
Damotclese 0:04d4cc695e56 33 // ----------------------------------------------------------------------
Damotclese 0:04d4cc695e56 34
Damotclese 0:04d4cc695e56 35 // We will be using the LCD so instantiate an object locally
Damotclese 1:316582aec4fb 36 LCD_DISCO_F429ZI st_lcd;
Damotclese 0:04d4cc695e56 37
Damotclese 0:04d4cc695e56 38 // We will be using the touch screen so instantiate an object
Damotclese 1:316582aec4fb 39 TS_DISCO_F429ZI st_touchScreen;
Damotclese 0:04d4cc695e56 40
Damotclese 1:316582aec4fb 41 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 42 // Local data storage this module will hold
Damotclese 1:316582aec4fb 43 //
Damotclese 1:316582aec4fb 44 // ----------------------------------------------------------------------
Damotclese 1:316582aec4fb 45
Damotclese 1:316582aec4fb 46 // Because the mbed randomizer is not very good, we maintain the
Damotclese 1:316582aec4fb 47 // following counter which is incremented every time the main
Damotclese 1:316582aec4fb 48 // thread executes so that we can use it to assist in randoming
Damotclese 1:316582aec4fb 49 // our numbers for the animation
Damotclese 1:316582aec4fb 50 static uint32_t u32_randomSeeder;
Damotclese 0:04d4cc695e56 51
Damotclese 3:0e554d8d5a19 52 // We keep track of what access level we may have granted
Damotclese 3:0e554d8d5a19 53 static uint8_t u8_thisAccessLevel;
Damotclese 3:0e554d8d5a19 54
Damotclese 0:04d4cc695e56 55 // ----------------------------------------------------------------------
Damotclese 0:04d4cc695e56 56 // MainInit()
Damotclese 0:04d4cc695e56 57 //
Damotclese 0:04d4cc695e56 58 // This function will:
Damotclese 0:04d4cc695e56 59 // o Initialize the module's locally-held data
Damotclese 0:04d4cc695e56 60 // o Clear the LCD and set the display to WHITE
Damotclese 0:04d4cc695e56 61 // o Set the default character font size
Damotclese 0:04d4cc695e56 62 // o Initialize the touch screen by polling the size of the LCD
Damotclese 0:04d4cc695e56 63 // o Sets the push button to have no pull-up
Damotclese 0:04d4cc695e56 64 //
Damotclese 0:04d4cc695e56 65 // ----------------------------------------------------------------------
Damotclese 0:04d4cc695e56 66 static void MainInit(void)
Damotclese 0:04d4cc695e56 67 {
Damotclese 1:316582aec4fb 68 // Initialize locally-held data
Damotclese 3:0e554d8d5a19 69 u32_randomSeeder = 0;
Damotclese 3:0e554d8d5a19 70 u8_thisAccessLevel = 0;
Damotclese 0:04d4cc695e56 71
Damotclese 1:316582aec4fb 72 uint8_t u8_TouchScreenStatus = 0;
Damotclese 1:316582aec4fb 73
Damotclese 0:04d4cc695e56 74 // Bring the LCD up
Damotclese 0:04d4cc695e56 75 st_lcd.Clear(LCD_COLOR_WHITE);
Damotclese 0:04d4cc695e56 76
Damotclese 0:04d4cc695e56 77 // Set the default font size
Damotclese 0:04d4cc695e56 78 BSP_LCD_SetFont(&Font16);
Damotclese 0:04d4cc695e56 79
Damotclese 0:04d4cc695e56 80 // Initialize the touch screen
Damotclese 0:04d4cc695e56 81 u8_TouchScreenStatus = st_touchScreen.Init(st_lcd.GetXSize(), st_lcd.GetYSize());
Damotclese 0:04d4cc695e56 82
Damotclese 0:04d4cc695e56 83 if (TS_OK != u8_TouchScreenStatus)
Damotclese 0:04d4cc695e56 84 {
Damotclese 0:04d4cc695e56 85 st_lcd.DisplayStringAt(1, LINE(ENTERED_KEYS_LINE),
Damotclese 0:04d4cc695e56 86 (uint8_t *)"Touch screen failed", CENTER_MODE);
Damotclese 0:04d4cc695e56 87 }
Damotclese 0:04d4cc695e56 88 }
Damotclese 0:04d4cc695e56 89
Damotclese 0:04d4cc695e56 90 // ----------------------------------------------------------------------
Damotclese 0:04d4cc695e56 91 // MainGrantAccess()
Damotclese 0:04d4cc695e56 92 //
Damotclese 0:04d4cc695e56 93 // This function is invoked to display what access level has been
Damotclese 0:04d4cc695e56 94 // granted.
Damotclese 0:04d4cc695e56 95 //
Damotclese 0:04d4cc695e56 96 // ----------------------------------------------------------------------
Damotclese 5:5c507689ae66 97 void MainGrantAccess(uint8_t u8_accessLevel)
Damotclese 0:04d4cc695e56 98 {
Damotclese 0:04d4cc695e56 99 uint8_t au8_reportString[21] = { 0 };
Damotclese 0:04d4cc695e56 100
Damotclese 0:04d4cc695e56 101 // Clear the display
Damotclese 0:04d4cc695e56 102 st_lcd.Clear(LCD_COLOR_WHITE);
Damotclese 0:04d4cc695e56 103
Damotclese 0:04d4cc695e56 104 // Build a report to offer
Damotclese 5:5c507689ae66 105 (void)sprintf((char *)au8_reportString, "Level %u", u8_accessLevel);
Damotclese 0:04d4cc695e56 106
Damotclese 0:04d4cc695e56 107 // Display the level of access that was granted
Damotclese 0:04d4cc695e56 108 st_lcd.DisplayStringAt(1, LINE(1), (uint8_t *)"Access granted", CENTER_MODE);
Damotclese 0:04d4cc695e56 109 st_lcd.DisplayStringAt(1, LINE(2), au8_reportString, CENTER_MODE);
Damotclese 0:04d4cc695e56 110 st_lcd.DisplayStringAt(1, LINE(3), (uint8_t *)"Press RESET to exit", CENTER_MODE);
Damotclese 0:04d4cc695e56 111
Damotclese 1:316582aec4fb 112 // Perform the screen animation
Damotclese 1:316582aec4fb 113 AnimationPerformAnimation(u32_randomSeeder);
Damotclese 3:0e554d8d5a19 114
Damotclese 3:0e554d8d5a19 115 // Flag the fact that we have granted access already
Damotclese 5:5c507689ae66 116 u8_thisAccessLevel = u8_accessLevel;
Damotclese 0:04d4cc695e56 117 }
Damotclese 0:04d4cc695e56 118
Damotclese 0:04d4cc695e56 119 // ----------------------------------------------------------------------
Damotclese 0:04d4cc695e56 120 // MainThread()
Damotclese 0:04d4cc695e56 121 //
Damotclese 0:04d4cc695e56 122 // Called ten times a second.
Damotclese 0:04d4cc695e56 123 //
Damotclese 0:04d4cc695e56 124 // This function will:
Damotclese 0:04d4cc695e56 125 // o Get the state of the touch screen
Damotclese 0:04d4cc695e56 126 // o Check to see if a touch screen touch was detected
Damotclese 0:04d4cc695e56 127 // o Extract the X and Y coordinates of the screen touch
Damotclese 0:04d4cc695e56 128 // o Call a function which will handle the screen touch
Damotclese 0:04d4cc695e56 129 // o Call a function to monitor the push button to drive the
Damotclese 0:04d4cc695e56 130 // Moorse Code access functionality
Damotclese 0:04d4cc695e56 131 //
Damotclese 0:04d4cc695e56 132 // ----------------------------------------------------------------------
Damotclese 0:04d4cc695e56 133 static void MainThread(void)
Damotclese 0:04d4cc695e56 134 {
Damotclese 0:04d4cc695e56 135 uint16_t u16_screenX = 0;
Damotclese 0:04d4cc695e56 136 uint16_t u16_screenY = 0;
Damotclese 0:04d4cc695e56 137 TS_StateTypeDef st_touchScreenState;
Damotclese 0:04d4cc695e56 138
Damotclese 3:0e554d8d5a19 139 // Have we not offered access already?
Damotclese 3:0e554d8d5a19 140 if (0 == u8_thisAccessLevel)
Damotclese 0:04d4cc695e56 141 {
Damotclese 3:0e554d8d5a19 142 // Get the status of the Touch Screen interface
Damotclese 3:0e554d8d5a19 143 st_touchScreen.GetState(&st_touchScreenState);
Damotclese 0:04d4cc695e56 144
Damotclese 3:0e554d8d5a19 145 // Has the screen been touched?
Damotclese 3:0e554d8d5a19 146 if (st_touchScreenState.TouchDetected)
Damotclese 3:0e554d8d5a19 147 {
Damotclese 3:0e554d8d5a19 148 // It has been, extract the X and Y coordinates touched
Damotclese 3:0e554d8d5a19 149 u16_screenX = st_touchScreenState.X;
Damotclese 3:0e554d8d5a19 150 u16_screenY = st_touchScreenState.Y;
Damotclese 3:0e554d8d5a19 151
Damotclese 3:0e554d8d5a19 152 // Call the routine which handles the key press
Damotclese 3:0e554d8d5a19 153 KeypadHandleKeyPress(u16_screenX, u16_screenY);
Damotclese 3:0e554d8d5a19 154 }
Damotclese 0:04d4cc695e56 155
Damotclese 3:0e554d8d5a19 156 // Drive the Moorse Code access functionality, scanning the
Damotclese 3:0e554d8d5a19 157 // push button 10 times a second
Damotclese 3:0e554d8d5a19 158 MoorseScanPushButton();
Damotclese 3:0e554d8d5a19 159 }
Damotclese 0:04d4cc695e56 160
Damotclese 1:316582aec4fb 161 // Keep track of how many times this function has been called
Damotclese 1:316582aec4fb 162 // so that we can attempt to see the random generator
Damotclese 1:316582aec4fb 163 u32_randomSeeder++;
Damotclese 0:04d4cc695e56 164 }
Damotclese 0:04d4cc695e56 165
Damotclese 0:04d4cc695e56 166 // ----------------------------------------------------------------------
Damotclese 0:04d4cc695e56 167 // main()
Damotclese 0:04d4cc695e56 168 //
Damotclese 0:04d4cc695e56 169 // This is the main entry called by thembed operating system.
Damotclese 0:04d4cc695e56 170 //
Damotclese 0:04d4cc695e56 171 // This function will:
Damotclese 0:04d4cc695e56 172 // o Initialize this module
Damotclese 0:04d4cc695e56 173 // o Call the function that established the LCD screen and
Damotclese 0:04d4cc695e56 174 // draws the keypad
Damotclese 0:04d4cc695e56 175 // o Goes in to a forever loop that wakes up 10 times a second
Damotclese 0:04d4cc695e56 176 // o Calls a function which drives the main task which scans the
Damotclese 0:04d4cc695e56 177 // LCD touch screen for screen touches
Damotclese 0:04d4cc695e56 178 //
Damotclese 0:04d4cc695e56 179 // ----------------------------------------------------------------------
Damotclese 0:04d4cc695e56 180 int main(void)
Damotclese 0:04d4cc695e56 181 {
Damotclese 0:04d4cc695e56 182 // Perform local module initialization, if any
Damotclese 0:04d4cc695e56 183 MainInit();
Damotclese 0:04d4cc695e56 184
Damotclese 1:316582aec4fb 185 // Initialize the Keypad module
Damotclese 1:316582aec4fb 186 KeypadInit();
Damotclese 1:316582aec4fb 187
Damotclese 1:316582aec4fb 188 // Initialize the Moorse Code module
Damotclese 1:316582aec4fb 189 MoorseInit();
Damotclese 1:316582aec4fb 190
Damotclese 1:316582aec4fb 191 // Initialize the Animation module
Damotclese 1:316582aec4fb 192 AnimationInit();
Damotclese 1:316582aec4fb 193
Damotclese 0:04d4cc695e56 194 // When we start up we will draw the touch screen keypad
Damotclese 1:316582aec4fb 195 KeypadDrawKeypad();
Damotclese 0:04d4cc695e56 196
Damotclese 0:04d4cc695e56 197 // Enter in to a forever loop which wakes up once a second
Damotclese 0:04d4cc695e56 198 while (true)
Damotclese 0:04d4cc695e56 199 {
Damotclese 0:04d4cc695e56 200 // Sleep for a tenth of a second
Damotclese 0:04d4cc695e56 201 wait(0.1);
Damotclese 0:04d4cc695e56 202
Damotclese 0:04d4cc695e56 203 // Call the main process
Damotclese 0:04d4cc695e56 204 MainThread();
Damotclese 0:04d4cc695e56 205 }
Damotclese 0:04d4cc695e56 206 }
Damotclese 0:04d4cc695e56 207
Damotclese 0:04d4cc695e56 208 // End of file
Damotclese 0:04d4cc695e56 209