Fredric Rice / Mbed 2 deprecated DemoKeypadMoorseCodeUnlockTouchAndButton

Dependencies:   LCD_DISCO_F429ZI mbed TS_DISCO_F429ZI mbed-os BSP_DISCO_F429ZI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SecurityUnlockDemo-Main.cpp Source File

SecurityUnlockDemo-Main.cpp

00001 
00002 // ----------------------------------------------------------------------
00003 // SecurityUnlockDemo-Main.cpp
00004 //
00005 // Fredric L. Rice, June 2019
00006 //
00007 // This is a demonstration of how to build a keypad on the liquid 
00008 // crystal display and then use the touch screen as a keypad to enter
00009 // an access code which grants access to other functionality. Two
00010 // levels of access is possible, with the more permissive access a
00011 // longer series of digits than the lesser-permissive access code.
00012 //
00013 // The keypad is built using position information in a table with some
00014 // fairly easy ways to move the keypad around on the display.
00015 //
00016 // The demonstration code also allows for the push button to be used
00017 // to key in Moorse Code pulses which are timed and then checked to
00018 // see if they match an expected access code for Moorse access.
00019 //
00020 // ----------------------------------------------------------------------
00021 
00022 #include "mbed.h"                           // The mbed operating system
00023 #include "LCD_DISCO_F429ZI.h"               // For controlling the LCD
00024 #include "TS_DISCO_F429ZI.h"                // For controlling the touch screen
00025 #include "SecurityUnlockDemo-Main.h"        // Always include our own header
00026 #include "SecurityUnlockDemo-Keypad.h"      // For all of the keypad functionality
00027 #include "SecurityUnlockDemo-Moorse.h"      // For all of the Moorse Code functionality
00028 #include "SecurityUnlockDemo-Animation.h"   // For performing animation
00029 
00030 // ----------------------------------------------------------------------
00031 // Define global data storage which we will export
00032 //
00033 // ----------------------------------------------------------------------
00034 
00035     // We will be using the LCD so instantiate an object locally
00036     LCD_DISCO_F429ZI st_lcd;
00037 
00038     // We will be using the touch screen so instantiate an object
00039     TS_DISCO_F429ZI st_touchScreen;
00040     
00041 // ----------------------------------------------------------------------
00042 // Local data storage this module will hold
00043 //
00044 // ----------------------------------------------------------------------
00045 
00046     // Because the mbed randomizer is not very good, we maintain the
00047     // following counter which is incremented every time the main
00048     // thread executes so that we can use it to assist in randoming
00049     // our numbers for the animation
00050     static uint32_t u32_randomSeeder;
00051     
00052     // We keep track of what access level we may have granted
00053     static uint8_t u8_thisAccessLevel;
00054     
00055 // ----------------------------------------------------------------------
00056 // MainInit()
00057 //
00058 // This function will:
00059 //      o Initialize the module's locally-held data
00060 //      o Clear the LCD and set the display to WHITE
00061 //      o Set the default character font size
00062 //      o Initialize the touch screen by polling the size of the LCD
00063 //      o Sets the push button to have no pull-up
00064 //
00065 // ----------------------------------------------------------------------
00066 static void MainInit(void)
00067 {
00068     // Initialize locally-held data
00069     u32_randomSeeder   = 0;
00070     u8_thisAccessLevel = 0;
00071     
00072     uint8_t u8_TouchScreenStatus = 0;
00073         
00074     // Bring the LCD up 
00075     st_lcd.Clear(LCD_COLOR_WHITE);
00076     
00077     // Set the default font size
00078     BSP_LCD_SetFont(&Font16);
00079     
00080     // Initialize the touch screen
00081     u8_TouchScreenStatus = st_touchScreen.Init(st_lcd.GetXSize(), st_lcd.GetYSize());
00082     
00083     if (TS_OK != u8_TouchScreenStatus)
00084     {
00085         st_lcd.DisplayStringAt(1, LINE(ENTERED_KEYS_LINE), 
00086             (uint8_t *)"Touch screen failed", CENTER_MODE);    
00087     }
00088 }
00089 
00090 // ----------------------------------------------------------------------
00091 // MainGrantAccess()
00092 //
00093 // This function is invoked to display what access level has been 
00094 // granted.
00095 //
00096 // ----------------------------------------------------------------------
00097 void MainGrantAccess(uint8_t u8_accessLevel)
00098 {
00099     uint8_t au8_reportString[21] = { 0 };
00100     
00101     // Clear the display 
00102     st_lcd.Clear(LCD_COLOR_WHITE);
00103     
00104     // Build a report to offer    
00105     (void)sprintf((char *)au8_reportString, "Level %u", u8_accessLevel);
00106     
00107     // Display the level of access that was granted
00108     st_lcd.DisplayStringAt(1, LINE(1), (uint8_t *)"Access granted", CENTER_MODE);
00109     st_lcd.DisplayStringAt(1, LINE(2), au8_reportString, CENTER_MODE);
00110     st_lcd.DisplayStringAt(1, LINE(3), (uint8_t *)"Press RESET to exit", CENTER_MODE);
00111     
00112     // Perform the screen animation
00113     AnimationPerformAnimation(u32_randomSeeder);
00114     
00115     // Flag the fact that we have granted access already
00116     u8_thisAccessLevel = u8_accessLevel;
00117 }
00118 
00119 // ----------------------------------------------------------------------
00120 // MainThread()
00121 //
00122 // Called ten times a second.
00123 //
00124 // This function will:
00125 //      o Get the state of the touch screen
00126 //      o Check to see if a touch screen touch was detected
00127 //      o Extract the X and Y coordinates of the screen touch
00128 //      o Call a function which will handle the screen touch
00129 //      o Call a function to monitor the push button to drive the
00130 //        Moorse Code access functionality
00131 //
00132 // ----------------------------------------------------------------------
00133 static void MainThread(void)
00134 {
00135     uint16_t        u16_screenX          = 0;
00136     uint16_t        u16_screenY          = 0;
00137     TS_StateTypeDef st_touchScreenState;
00138     
00139     // Have we not offered access already?
00140     if (0 == u8_thisAccessLevel)
00141     {
00142         // Get the status of the Touch Screen interface
00143         st_touchScreen.GetState(&st_touchScreenState);
00144         
00145         // Has the screen been touched?
00146         if (st_touchScreenState.TouchDetected)
00147         {
00148             // It has been, extract the X and Y coordinates touched
00149             u16_screenX = st_touchScreenState.X;
00150             u16_screenY = st_touchScreenState.Y;
00151         
00152             // Call the routine which handles the key press
00153             KeypadHandleKeyPress(u16_screenX, u16_screenY);
00154         }
00155     
00156         // Drive the Moorse Code access functionality, scanning the
00157         // push button 10 times a second
00158         MoorseScanPushButton();
00159     }
00160     
00161     // Keep track of how many times this function has been called
00162     // so that we can attempt to see the random generator
00163     u32_randomSeeder++;
00164 }
00165 
00166 // ----------------------------------------------------------------------
00167 // main()
00168 //
00169 // This is the main entry called by thembed operating system.
00170 //
00171 // This function will:
00172 //      o Initialize this module
00173 //      o Call the function that established the LCD screen and
00174 //        draws the keypad
00175 //      o Goes in to a forever loop that wakes up 10 times a second
00176 //      o Calls a function which drives the main task which scans the
00177 //        LCD touch screen for screen touches
00178 //
00179 // ----------------------------------------------------------------------
00180 int main(void)
00181 {
00182     // Perform local module initialization, if any
00183     MainInit();
00184     
00185     // Initialize the Keypad module
00186     KeypadInit();
00187     
00188     // Initialize the Moorse Code module
00189     MoorseInit();
00190     
00191     // Initialize the Animation module
00192     AnimationInit();
00193     
00194     // When we start up we will draw the touch screen keypad
00195     KeypadDrawKeypad();
00196 
00197     // Enter in to a forever loop which wakes up once a second
00198     while (true)
00199     {
00200         // Sleep for a tenth of a second
00201         wait(0.1);
00202    
00203         // Call the main process
00204         MainThread();
00205     }
00206 }
00207 
00208 // End of file
00209