@@ -32,8 +32,8 @@ static void MoveCursor(int row);
3232static void HideCursor (void );
3333static void PrintWarning (void );
3434static void ProcessReceivedChar (char receivedChar , char * window , int * windowIndex );
35- static void ResetWindowOnMismatch (char * window , int * windowIndex , char receivedChar );
36- static void CheckForUnlockCommand (char * window , int windowIndex );
35+ static void ResetWindowOnMismatch (char * window , int * windowIndex );
36+ static void CheckForUnlockCommand (char * window , int * windowIndex );
3737
3838int main (void )
3939{
@@ -85,48 +85,41 @@ static void PrintWarning(void)
8585static void ProcessReceivedChar (char receivedChar , char * window , int * windowIndex )
8686{
8787 MoveCursor (10 );
88- printf ( "%c" , receivedChar );
89- if (receivedChar == UNLOCK_COMMAND [* windowIndex ])
88+
89+ if (* windowIndex < WINDOW_SIZE && receivedChar == UNLOCK_COMMAND [* windowIndex ])
9090 {
91+ printf ("%c" , receivedChar );
9192 window [(* windowIndex )++ ] = receivedChar ;
9293 window [* windowIndex ] = '\0' ;
93-
94- CheckForUnlockCommand (window , * windowIndex );
94+ CheckForUnlockCommand (window , windowIndex );
9595 }
9696 else
9797 {
98- ResetWindowOnMismatch (window , windowIndex , receivedChar );
98+ ClearTerminalLine ();
99+ ResetWindowOnMismatch (window , windowIndex );
99100 }
100101}
101102
102- static void ResetWindowOnMismatch (char * window , int * windowIndex , char receivedChar )
103+ static void ResetWindowOnMismatch (char * window , int * windowIndex )
103104{
104105 MoveCursor (5 );
105106 ClearTerminalLine ();
106- printf ("Incorrect command received. Try again." );
107- if (receivedChar == UNLOCK_COMMAND [0 ])
108- {
109- * windowIndex = 1 ;
110- window [0 ] = receivedChar ;
111- }
112- else
113- {
114- * windowIndex = 0 ;
115- window [0 ] = '\0' ;
116- }
107+ printf ("Invalid character entered. Try again." );
108+ * windowIndex = 0 ;
109+ memset (window , 0 , WINDOW_SIZE + 1 );
117110}
118111
119- static void CheckForUnlockCommand (char * window , int windowIndex )
112+ static void CheckForUnlockCommand (char * window , int * windowIndex )
120113{
121114 int patternLength = strlen (UNLOCK_COMMAND );
122115
123- if (windowIndex == patternLength )
116+ if (* windowIndex == patternLength )
124117 {
125118 MoveCursor (5 );
126119 ClearTerminalLine ();
127120 printf ("Unlock command for ICSP Inhibit received.\n" );
128121 HideCursor ();
129- memset ( window , 0 , WINDOW_SIZE + 1 ); // Clear the window buffer
130- windowIndex = 0 ;
122+ * windowIndex = 0 ;
123+ memset ( window , 0 , WINDOW_SIZE + 1 ) ;
131124 }
132125}
0 commit comments