2121
2222#include <stdio.h>
2323#include <string.h>
24+ #include <stdbool.h>
2425
2526#define WINDOW_SIZE 10
2627#define UNLOCK_COMMAND "LOCKDEVICE"
@@ -31,6 +32,7 @@ static void ClearTerminalLine(void);
3132static void MoveCursor (int row );
3233static void HideCursor (void );
3334static void PrintWarning (void );
35+ static void AppendCharToWindow (char receivedChar , char * window , int * windowIndex );
3436static void ProcessReceivedChar (char receivedChar , char * window , int * windowIndex );
3537static void ResetWindowOnMismatch (char * window , int * windowIndex );
3638static void CheckForUnlockCommand (char * window , int * windowIndex );
@@ -43,13 +45,13 @@ int main(void)
4345 SYSTEM_Initialize ();
4446 ClearTerminalScreen ();
4547 PrintWarning ();
46-
4748 while (1 )
4849 {
4950 if (UART1_IsRxReady ())
5051 {
5152 char receivedChar = UART1_Read ();
5253 ProcessReceivedChar (receivedChar , window , & windowIndex );
54+ CheckForUnlockCommand (window , & windowIndex );
5355 }
5456 }
5557}
@@ -84,24 +86,30 @@ static void PrintWarning(void)
8486
8587static void ProcessReceivedChar (char receivedChar , char * window , int * windowIndex )
8688{
87- MoveCursor ( 10 );
89+ bool isCharValid = ( * windowIndex < strlen ( UNLOCK_COMMAND )) && ( receivedChar == UNLOCK_COMMAND [ * windowIndex ] );
8890
89- if (* windowIndex < WINDOW_SIZE && receivedChar == UNLOCK_COMMAND [ * windowIndex ] )
91+ if (isCharValid )
9092 {
91- printf ("%c" , receivedChar );
92- window [(* windowIndex )++ ] = receivedChar ;
93- window [* windowIndex ] = '\0' ;
94- CheckForUnlockCommand (window , windowIndex );
93+ AppendCharToWindow (receivedChar , window , windowIndex );
94+ MoveCursor (10 );
95+ printf ("%s" , window );
9596 }
9697 else
9798 {
98- ClearTerminalLine ();
9999 ResetWindowOnMismatch (window , windowIndex );
100100 }
101101}
102102
103+ static void AppendCharToWindow (char receivedChar , char * window , int * windowIndex )
104+ {
105+ window [(* windowIndex )++ ] = receivedChar ;
106+ window [* windowIndex ] = '\0' ;
107+ }
108+
103109static void ResetWindowOnMismatch (char * window , int * windowIndex )
104110{
111+ MoveCursor (10 );
112+ ClearTerminalLine ();
105113 MoveCursor (5 );
106114 ClearTerminalLine ();
107115 printf ("Invalid character entered. Try again." );
@@ -111,14 +119,13 @@ static void ResetWindowOnMismatch(char *window, int *windowIndex)
111119
112120static void CheckForUnlockCommand (char * window , int * windowIndex )
113121{
114- int patternLength = strlen (UNLOCK_COMMAND );
115-
116- if (* windowIndex == patternLength )
122+ if (strncmp (window , UNLOCK_COMMAND , * windowIndex ) == 0 && * windowIndex == strlen (UNLOCK_COMMAND ))
117123 {
118124 MoveCursor (5 );
119125 ClearTerminalLine ();
120- printf ("Unlock command for ICSP Inhibit received.\n" );
121- HideCursor ();
126+ printf ("Unlock command successful.\n" );
127+ MoveCursor (10 );
128+ ClearTerminalLine ();
122129 * windowIndex = 0 ;
123130 memset (window , 0 , WINDOW_SIZE + 1 );
124131 }
0 commit comments