コード例 #1
0
ファイル: keypad.c プロジェクト: jjc10/ECSE426
int getPressedButton(){
		int rows[4], columns[4];
		int row = -1;
		int column = -1;
		/* Place high and low values into the arrays rows and columns */
		scanKeypad(rows, columns);
		/* Scan these arrays to check if a button has been pressed and if so what column it's in */
		column = scanInputArray(columns, 4);
		/* If a button was pressed */
		if(column >= 0){
			switchRowToInput();
			switchColumnToOutput();
			/* Place high and low values into the arrays rows and columns */
			scanKeypad(rows, columns);
			/* Check to see which row the button is in and place index in row */
			row = scanInputArray(rows, 4);
			switchRowToOutput();
			switchColumnToInput();
		}
		if(column >= 0 && rows >= 0){
			return keypadArray[row][column];
		}else{
			return -1;
		}

}
コード例 #2
0
ファイル: keypad.c プロジェクト: jjyothilinga/AndonTerminal
/*
*------------------------------------------------------------------------------
* rom void UpdateKeypadTask(void)
*
* Summary	: The function update the key pressed into keypad buffer
*			  This must be schedule at approx every 50 - 200 ms .
*
* Input		: None
*
* Output	: None
*------------------------------------------------------------------------------
*/
rom void KEYPAD_task(void)
{
	UINT8 currentKey, functionKey;

	duration++;
   	// Scan Keypad here...
   	if (scanKeypad(&currentKey) == 0)
    {
    	// No new Key data - just return
      	return;
    }

   	// Want to read into index 0, if old data has been read
   	// (simple ~circular buffer)
   	if (keypadInWaitingIndex == kepadInReadIndex)
    {
    	keypadInWaitingIndex = 0;
      	kepadInReadIndex = 0;
    }

   	// Load Keypad data into buffer
   	keypadReceiveBuffer[keypadInWaitingIndex][0] = currentKey;
	keypadReceiveBuffer[keypadInWaitingIndex][1] = duration;

   	if (keypadInWaitingIndex < KEYPAD_BUFFER_LENGTH)
    {
    	// Increment without overflowing buffer
      	keypadInWaitingIndex++;
    }
	duration = 0;
}
コード例 #3
0
ファイル: phi_interfaces.cpp プロジェクト: NAzT/Arduino-NAzT
/**
 * \details This is the public method to get a key press from the keypad. The key press is translated into key_names or NO_KEY.
 * This function is inherited from multiple_button_inputs. All Keypad subclasses such as phi_matrix_keypads and phi_analog_keypads share this code.
 * Since all multiple_button_inputs devices have this method, you can treat all of them as multiple_button_inputs and call this method to get a key press.
 * This is the function you should call to sense key presses. 
 * It is only few lines of code and is generic enough for all phi_keypads.
 * \return It returns the name of the key that is pressed down.
 */
byte phi_keypads::getKey()
{
  byte key=scanKeypad();
  if (key==NO_KEYs) key=NO_KEY;
  else key=key_names[key];
  return key;
}
コード例 #4
0
ファイル: main.c プロジェクト: arwerchan/lab2
int main(void) {
    
    SYSTEMConfigPerformance(40000000);
    initKeypad();
    enableEnterruptKeypad();
    initTimer2();
    initLCD();
    enableInterrupts();
    moveCursorLCD(0,0);
    state = Wait;

    while (1) {
        switch (state) {
            case Wait:
                break;

            case Scan:
                key = scanKeypad();
                state = MoveCursor;
                break;
                
            case MoveCursor:
                if(count == 0) moveCursorLCD(0,0);
                else if (count == 9) moveCursorLCD(1,0);
                state = Print;
                break;
                
            case debounce1:
                delayUs(500);
                state = Scan;
                break;
                
            case debounce2:
                delayUs(500);
                state = MoveCursor;
                break;

            case Print:
                delayUs(100);
                if(key == 0) printCharLCD('0');
                else if(key == 1) printCharLCD('1');
                else if(key == 2) printCharLCD('2');
                else if(key == 3) printCharLCD('3');
                else if(key == 4) printCharLCD('4');
                else if(key == 5) printCharLCD('5');
                else if(key == 6) printCharLCD('6');
                else if(key == 7) printCharLCD('7');
                else if(key == 8) printCharLCD('8');
                else if(key == 9) printCharLCD('9');
                else if(key == 10) printCharLCD('*');
                else if(key == 11) printCharLCD('#');
                state = Wait;
                break;

        }
    }

    return 0;
}
コード例 #5
0
ファイル: SousVide.c プロジェクト: HarrisonW/ECE4760
//Debounce the button using a debounce state machine
void debounce(void){
	uint8_t key = scanKeypad(); //Scan the keypad
	switch(keyState){
		
		//in the RELEASED state: stay in this state if a key is not pressed
		//go to UNKNOWN if any key is pressed and reset the debounce countdown.
		case RELEASED:
		if (key){
			keyState = UNKNOWN;
			prevKeyState = RELEASED;
			debouncing = 1;
			checkKey = key;
		}
		break;
		
		//in the UNKNOWN state: go to released if the button is not down
		//go to PUSHED if the button is down
		case UNKNOWN:
		if (key){
			if (key == checkKey){
				keyState = PUSHED;
				prevKeyState = UNKNOWN;
				debouncing = 0;
			}
			else {
				debouncing = 1;
				checkKey = key;
			}
		}
		else{
			keyState = RELEASED;
			if (prevKeyState == PUSHED) {
				curKey = checkKey; //The key to be checked has been pressed and debounced
			}
			prevKeyState = UNKNOWN;
			debouncing = 0;
		}
		
		break;
		
		//in the PUSHED state go to UnKnown if the button is not down.
		//stay in PUSHED if the button is down
		case PUSHED:
		if (!key){
			keyState = UNKNOWN;
			prevKeyState = PUSHED;
			debouncing = 1;
		}
		else if (key != checkKey){
			keyState = UNKNOWN;
			prevKeyState = RELEASED; //...???
			debouncing = 1;
			curKey = checkKey;
			checkKey = key;
		}
		break;
	}
}
コード例 #6
0
ファイル: main.c プロジェクト: Nathanh1/Lab2
int main(void)
{  
    initLCD();
    initKeypad();
    enableInterrupts();
    
    int counter = 1;
    char output = 'a';
    
    while(1)
    {
        switch(state)
        {
            case Read_Keypad:
               
               if(ROW1 == PRESSED || ROW2 == PRESSED || ROW3 == PRESSED || ROW4 == PRESSED)
               {
                   output = scanKeypad();
               }
               
               if(ROW1 == RELEASED || ROW2 == RELEASED || ROW3 == RELEASED ||  ROW4 == RELEASED)   
               {
                   state = Print_LCD;  
               }
               break;
                
            case Print_LCD:
                 
                if( counter < 8)
                {
                    printCharLCD(output);
                    counter++;          
                }
                else if (counter == 8)
                {
                    printCharLCD(output);
                    counter++; 
                    moveCursorLCD(1, 2);
                }
                else if (counter > 8 && counter < 16)
                {
                    printCharLCD(output);
                    counter++;        
                }
                else if(counter == 16)
                {
                    printCharLCD(output);
                    counter = 0; 
                    moveCursorLCD(1, 1);      
                }
                
                state = Read_Keypad;
            
            break;         
         }
    } 
    return 0;
}
コード例 #7
0
ファイル: main.c プロジェクト: Nathanh1/Lab2
int main(void)
{  
    initLCD();
    initKeypad();
    enableInterrupts();
    
    int counter = 1;
    char output = 'a';
    int count = 0;
    
    char p1[4]={'1','3','3','7'};//PRESET PASSWORD 1
    char p2[4];//PASSWORD 2
    char p3[4];//PASSWORD 3
    char p4[4];//PASSWORD 4
    
    char UserInput[4]; //USER INPUT ARRAY
    int i=0; //USER INPUT ARRAY ELEMENT
    
    int InSet = TURNOFF;
    int InEnter = TURNON;
    int GUESS1;
    int GUESS2;
    int GUESS3;
    int GUESS4;
    
    int StarCount=0;
    
    int PASSWORD2 = EMPTY;
    int PASSWORD3 = EMPTY;
    int PASSWORD4 = EMPTY;
    
    int USERINPUT= VALID; //for checking * or 
    
    while(1)
    {
        switch(state)
        {
            case  Enter_Mode:
                
                InSet = TURNOFF;
                InEnter = TURNON;             
                clearLCD();
                printStringLCD("Enter Code:");
                moveCursorLCD(0,2); //COL ROW
                count=0;
                StarCount=0;
            
                if(count == 4)
                {
                   count = 0;
                
                   for(i=0;i<4;i++)
                   {
                        if(p1[i]!=UserInput[i])
                        {
                            GUESS1=BAD;
                            break;
                        }
                        else
                        {
                            GUESS1=GOOD;
                        }
                    }
                    for(i=0;i<4;i++)
                    {
                        if(p2[i]!=UserInput[i])
                        {
                           GUESS2=BAD;
                           break;
                        }
                        else
                        {
                           GUESS2=GOOD;
                        }
                    }
                    for(i=0;i<4;i++)
                    {
                        if(p3[i]!=UserInput[i])
                        {
                            GUESS3=BAD;
                            break;
                        }
                        else
                        {
                            GUESS3=GOOD;
                        }
                    }
                    for(i=0;i<4;i++)
                    {
                        if(p4[i]!=UserInput[i])
                        {
                            GUESS4=BAD;
                            break;
                        }
                        else
                        {
                            GUESS4=GOOD;
                        }
                    }  
                    if(GUESS1==GOOD || GUESS2==GOOD || GUESS3==GOOD || GUESS4==GOOD)
                    {
                        clearLCD();
                        printStringLCD("GOOD");
                        for(i=0;i<500;i++)
                        {
                            delayUs(1000);
                        }
                    }
                    else if(GUESS1==BAD && GUESS2==BAD && GUESS3==BAD && GUESS4==BAD)
                    {
                        clearLCD();
                        printStringLCD("BAD");
                        for(i=0;i<500;i++)
                        {
                            delayUs(1000);
                        }
                    }        
                }
                if(StarCount==1)
                {
                    USERINPUT=VALID; //ASSUME
                    InSet = TURNOFF;
                    InEnter = TURNON;
                    StarCount=0;
                    count=0;
                
                    for(i=0;i<4;i++)//RESET USER INPUT
                    {
                        UserInput[i]='0';
                    }  
                    state = Set_Mode;
                }
            
                break;
            
            case Read_Keypad:
               
               if(ROW1 == PRESSED || ROW2 == PRESSED || ROW3 == PRESSED || ROW4 == PRESSED)
               {
                   output = scanKeypad();
               }
               
               if(ROW1 == RELEASED || ROW2 == RELEASED || ROW3 == RELEASED ||  ROW4 == RELEASED)   
               {
                   state = Print_LCD;  
               }
               break;
                
            case Print_LCD:
                 
                if( counter < 8)
                {
                    printCharLCD(output);
                    counter++;          
                }
                else if (counter == 8)
                {
                    printCharLCD(output);
                    counter++; 
                    moveCursorLCD(1, 2);
                }
                else if (counter > 8 && counter < 16)
                {
                    printCharLCD(output);
                    counter++;        
                }
                else if(counter == 16)
                {
                    printCharLCD(output);
                    counter = 0; 
                    moveCursorLCD(1, 1);      
                }
                
                count++;
                          
                break;
            
            case Set_Mode:
                
                InSet = TURNON;
                InEnter = TURNOFF;
                
                clearLCD();
                printStringLCD("Set Mode:");
                moveCursorLCD(0,2); //COL ROW
                
                
                USERINPUT=VALID;
            
                for(i=0;i<4;i++)//Check UserInput values UI[0]->UI[4]      
                {
                    if(UserInput[i]=='*' || UserInput[i]=='#')//Check for * or # 
                    {
                        USERINPUT=NOTVALID; //SET USERINPUT to NOTVALID
                        
                    }
                }
            
                if(USERINPUT==VALID)// No * or # in USERINPUT VALUES
                {
                    if(PASSWORD2==EMPTY)
                    {
                                for(i=0;i<4;i++)
                                    {
                                        p2[i]=UserInput[i]; 

                                    }
                                PASSWORD2 = FULL;
                            
                                clearLCD();
                                printStringLCD("PW2 STORED");
                                for(i=0;i<500;i++)
                                    {
                                        delayUs(1000);
                                    }
           
                    }
                    else if(PASSWORD2==FULL && PASSWORD3==EMPTY)
                    {
                        for(i=0;i<4;i++)
                        {
                            p3[i]=UserInput[i]; 

                        }
                        PASSWORD3 = FULL;
                            
                        clearLCD();
                        printStringLCD("PW3 STORED");
                        for(i=0;i<500;i++)
                        {
                            delayUs(1000);
                        }
                    }
                    
                     else if(PASSWORD2==FULL && PASSWORD3==FULL && PASSWORD4==EMPTY)
                    {
                        for(i=0;i<4;i++)
                        {
                            p4[i]=UserInput[i]; 
                        }
                        PASSWORD4 = FULL;
                            
                        clearLCD();
                        printStringLCD("PW4 STORED");
                            
                        for(i=0;i<500;i++)
                        {
                            delayUs(1000);
                        }

                        clearLCD();
                        printStringLCD("VALID"); //PW4 IS THE LAST PASSWORD ENTERED

                        for(i=0;i<500;i++)
                        {
                            delayUs(1000);
                        }
                    }
                    if (PASSWORD2==FULL && PASSWORD3==FULL && PASSWORD4==FULL) //ALL PASSWORDS ARE FULL
                    {
                        clearLCD();
                        printStringLCD("STORAGE FULL");
                        for(i=0;i<500;i++)
                        {
                            delayUs(1000);
                        }
                    }
                    else if (PASSWORD2==EMPTY || PASSWORD3==EMPTY || PASSWORD4==EMPTY) //At LEAST ONE PASSWORD IS EMPTY
                    {
                        clearLCD();
                        printStringLCD("VALID");
                        for(i=0;i<500;i++)
                        {
                            delayUs(1000);
                        }
                    }
                
                }
            
            else if(USERINPUT==NOTVALID)// * or # is in USERINPUT VALUES
            {
                    clearLCD();
                    printStringLCD("NOT VALID");
                    
                    for(i=0;i<500;i++)
                        {
                            delayUs(1000);
                        }
            }
                break;
          
         }   
    } 
    return 0;
}
コード例 #8
0
ファイル: main.c プロジェクト: Els0/ECE372A
int main(void) {

    SYSTEMConfigPerformance(40000000);
    enableInterrupts();
    initTimer2();
    initLCD();
    clearLCD();
    initKeypad();
    
    while (1) {
        switch (state) {
            case Start:
                initLCD();
                clearLCD();
                state = ROW1;
                break;


            case ROW1:
                Row=1;
                //printCharLCD('1');
                LATGbits.LATG0 = DISABLED;
                LATFbits.LATF1 = DISABLED;
                LATDbits.LATD12 = DISABLED;
                LATGbits.LATG13 = ENABLED;                
                //delayUs2(70);
                state = ROW2;
                break;

            case ROW2:
                Row=2;
                //printCharLCD('2');
                LATDbits.LATD12 = DISABLED;
                LATFbits.LATF1 = DISABLED;
                LATGbits.LATG13 = DISABLED;
                LATGbits.LATG0 = ENABLED;
                //delayUs2(70);
                state = ROW3;
                break;

            case ROW3:
                Row=3;
                //printCharLCD('3');
                LATDbits.LATD12 = DISABLED;
                LATGbits.LATG13 = DISABLED;
                LATGbits.LATG0 = DISABLED;
                LATFbits.LATF1 = ENABLED;
                //delayUs2(70);
                state = ROW4;
                break;

            case ROW4:
                Row=4;
                LATGbits.LATG13 = DISABLED;
                LATGbits.LATG0 = DISABLED;
                LATFbits.LATF1 = DISABLED;
                LATDbits.LATD12 = ENABLED;
                //delayUs2(70);
                state = ROW1;
                break;

            case DebouncePress:
                delayUs2(700);
                state = WaitRelease;
                break;

            case WaitRelease:
                state = WaitRelease;
                break;

            case DebounceRelease:
                delayUs2(700);
                state = WriteLCD;
                break;

                
            case WriteLCD:       
                key=scanKeypad(Col, Row);
                printCharLCD(key);
                state = ROW1;
                break;
        }
    }

    return 0;
}
コード例 #9
0
ファイル: main.c プロジェクト: Team-212/Lab2Part1
int main(void)
{
    SYSTEMConfigPerformance(10000000);
    enableInterrupts();
   initTimer2();
   initKeypad();
   initLCD();
   
    char password1[5] = {'a','a','a','a','\0'}; //2D array of passwords 
 char password2[5] = {'a','a','a','a','\0'};
 char password3[5] = {'a','a','a','a','\0'};
 char password4[5] = {'a','a','a','a','\0'};
 char tempPassword[5] = {'\0','\0','\0','\0','\0'}; //password being entered

 char keyPressed = 'f';
 int row = 1;
 int position = 0; 
 int counter = 1;
 int numPasswords = 0; //number of passwords 
 int i =0; 
 int j =0;
 int set = 0; 
 
   //TRISA.RA7
   TRISAbits.TRISA7 = 0;
   
   //TRISCbits.TRISC4 = 0;
    while(1)
    {
       
        
        switch(state){
            
            case Wait: 
                            
                if( set == 1){
//                moveCursorLCD(1,1); //first line 
//                printStringLCD("Set Mode");
                }              
                else {
                    
                moveCursorLCD(1,1); //first line 
                printStringLCD("Enter"); 
                }
                
               
                break;
                
            case Wait2:
                
                
                if((PORTBbits.RB10 == 1) && (PORTBbits.RB12 == 1) && (PORTBbits.RB14 == 1) ) { //waits for button release
         
                   
                state = DeBounceRelease;
                break;
                }
                
                else state = Wait2;
                break;
                
            case DeBounce: 
                 
                //moveCursorLCD(1,1); //first line 
                //printStringLCD("DeBounce State");
                
                delayUs(500);
                state = SearchKeypad;
                
                break;
                
            case DeBounceRelease: 
                 
                //moveCursorLCD(1,1); //first line 
                //printStringLCD("DeBounce State");
                
                delayUs(500);
               // moveCursorLCD(1,2); //first line 
               // printStringLCD("Button Released");
                state = EnterPassword;
                //IEC1bits.CNBIE = ON;
                break;
                
            case Set:
                moveCursorLCD(1,1); //first line 
                printStringLCD("Set Mode");
                
                moveCursorLCD(1,2); // clears second line 
                    clearLCD;
                set = 1; 
                state = Wait; 
                
                break; 
                
            case EnterPassword:
                
                                                
                if(i == 4){
                    i = 0;
                                        
                }
               tempPassword[i] =  keyPressed;
               i = i + 1;
               
               if( i == 2){
                        
                        //if((passwords[0][numPasswords] == '*') && (passwords[1][numPasswords] == '*')) {
                         if((tempPassword[0] == '*') && (tempPassword[1] == '*')) {   
                            i = 0;
                            state = Set;
                        } 
                    }
               
               state = WriteLCD;
                break;
                
              case CheckNewPassword:
                        
                for(j = 0; j < 4; j++){ //checks all stored passwords 
                    
                    if ((tempPassword[j] == '*') || (tempPassword[j] == '#')){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Invalid"); //print valid
                        delayUs(2000000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                        set = 0;
                        state = Wait;
                 
                        }
                }
                
                
                    moveCursorLCD(1,1); //first line 
                    printStringLCD("Valid"); //print invalid
                    delayUs(2000000);
                    moveCursorLCD(1,2); // clears second line 
                    clearLCD;
                    
                    numPasswords = numPasswords +1;
                    
                      if(numPasswords == 4){
                        numPasswords = 0;
                    }
                    
                    if(numPasswords == 0){
                        
                        for(j =0; j < 5; j++){
                            password1[j] = tempPassword[j];
                        }
                    }
                    
                    else if(numPasswords == 1){
                        
                        for(j =0; j < 5; j++){
                            password2[j] = tempPassword[j];
                        }
                    }
                    
                    else if(numPasswords == 2){
                        
                        for(j =0; j < 5; j++){
                            password3[j] = tempPassword[j];
                        }
                    }
                    
                    else if(numPasswords = 3){
                        
                        for(j =0; j < 5; j++){
                            password4[j] = tempPassword[j];
                        }
                    }
                    set = 0;
                    
                    state = Wait;
                
                    
                        
                        
                break;
                
            case CheckPassword:
                        
                                  
                    if (strcmp(password1, tempPassword) == 0){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Good"); //print valid
                        delayUs(20000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                 
                        state = Wait;
                 
                        }
                     if (strcmp(password2, tempPassword) == 0){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Good"); //print valid
                        delayUs(20000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                 
                        state = Wait;
                 
                        }
                    
                     if (strcmp(password3, tempPassword) == 0){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Good"); //print valid
                        delayUs(20000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                 
                        state = Wait;
                 
                        }
                    
                     if (strcmp(password4, tempPassword) == 0){
                        moveCursorLCD(1,1); //first line 
                        printStringLCD("Good"); //print valid
                        delayUs(20000);
                        moveCursorLCD(1,2); // clears second line 
                        clearLCD;
                 
                        state = Wait;
                 
                        }
                
                
                
                    moveCursorLCD(1,1); //first line 
                    printStringLCD("Bad"); //print invalid
                    delayUs(20000);
                    moveCursorLCD(1,2); // clears second line 
                    clearLCD;
                    
                    
                    state = Wait;
                
                    
                        
                        
                break;
                        
            case SearchKeypad: 
                
                //moveCursorLCD(1,1); //first line 
                //printStringLCD("Search Keypad");
                //delayUs(5000);
                
              //  IEC1bits.CNBIE = OFF;// turn off ISR 
                
                CNCONBbits.ON = 0; //turn change notifications off 
                
                LATEbits.LATE0 = 1;// open drain collector for outputs
                LATEbits.LATE2 = 1;
                LATEbits.LATE4 = 1;
                LATEbits.LATE6 = 1;
                
                keyPressed = scanKeypad();
                
                LATEbits.LATE0 = 0;// open drain collector for outputs
                LATEbits.LATE2 = 0;
                LATEbits.LATE4 = 0;
                LATEbits.LATE6 = 0;
                
                CNCONBbits.ON = 1; // CN on 
                
               state = Wait2;
               //state = WriteLCD;
                break;
                
            case WriteLCD: 
                
                if (counter == 9){
//                    row = ~row;
                    counter = 1;
                }
                
                moveCursorLCD(counter,2);
                  delayUs(2000);
                printCharLCD(keyPressed);
                delayUs(200);
                counter = counter + 1;
                  
                if(i == 3){
                    
                    if(set == 1){
                        state = CheckNewPassword;
                    }
                    state = CheckPassword;
                }
                        state = Wait; 
                
               //IEC1bits.CNBIE = ON;
                break;
            
            
            
        }
    }
    
    return 0;
}
コード例 #10
0
ファイル: main.c プロジェクト: lodwkeef/general_projects
int main(void) {
    SYSTEMConfigPerformance(10000000);
    enableInterrupts();
    initTimerDebounce();
    initTimerDelay();
    initLCD();
    initKeypad();

    int runQATests = NTEST; //test keypad software functions if TEST, don't if NTEST
    if(runQATests == TEST) {
        test_clearString();
        delayUs(2000000);
        clearLCD();
        test_checkPass();
        delayUs(2000000);
        clearLCD();
        test_updatePass();
        return -1;
    }

    char key = NULL;
    char passStringa[5] = "";
    char passStringb[5] = "";
    char passStringc[5] = "";
    char passStringd[5] = "";
    int PassCount = 0;
    int correct = NOTMATCH;
    char tempPass[5] = "";
    char guessString[5] = "";
    moveCursorLCD(0,0);

    while(1) {
        switch(state) {
        case enter:
            if(mode != ENTER || prevState != enter) {
                clearLCD();
                printStringLCD("Enter");
                moveCursorLCD(1,0);
                mode = ENTER;
            }
            prevState = enter;
            break;
        case debounce:
            break;
        case update:
            if(row != -1) {
                key = scanKeypad(row);
            }
            else {
                break;
            }
            if(key != -1) {
                printCharLCD(key);
                if(mode == ENTER) {
                    guessString[keyPresses] = key;//append key to guessString
                    state = enter;
                }
                else if(mode == SET) {
                    tempPass[keyPresses] = key;//append key to passString
                    state = set_mode;
                }
                keyPresses = keyPresses + 1;
                if(mode == ENTER) {
                    correct = NOTMATCH;
                    correct = checkPass(guessString, passStringa, passStringb, passStringc, passStringd);
                    if(keyPresses == 4 && (correct == NOTMATCH)) {
                        state = bad;
                    }
                    else if(key == '#') {
                        state = bad;
                    }
                    else if((keyPresses > 1) && (keyPresses < 4) && (key == '*') && (strcmp(guessString, "**")!=0)) {
                        state = bad;
                    }
                    else if((keyPresses==2)&&(strcmp(guessString,"**")==0)) {
                        state = set_mode;
                    }
                    else if((keyPresses==2) && (key!='*')&&(guessString[0]=='*')) {
                        state = bad;
                    }
                    else if((keyPresses==4)&&(correct == 0)) {
                        state = good;
                    }
                }
                else if((keyPresses<4)&&(mode==SET)&&(key!='#')&&(key!='*')) {
                    state = set_mode;
                }
                else if(((key=='#')||(key=='*'))&&(mode==SET)) {
                    state = invalid;
                }
                else if((mode==SET)&&(keyPresses==4)&&(key!='#')&&(key!='*')) {
                    state = valid;
                }
                else if(keyPresses < 4 && mode == ENTER && key != '#') state = enter;
            }
            else {
                printCharLCD(' ');
                moveCursorLCD(1, keyPresses);
                if(mode == ENTER) state = enter;
                else if(mode == SET) state = set_mode;
            }
            key = NULL;
            break;
        case set_mode:
            if(mode != SET || prevState != set_mode) {
                clearLCD();
                printStringLCD("Set Mode");
                moveCursorLCD(1,0);
                keyPresses = 0;
                strcpy(guessString, "");
                clearString(guessString);
                mode = SET;
            }
            prevState = set_mode;
            break;
        case good:
            clearLCD();
            printStringLCD("Good");
            keyPresses = 0;
            clearString(guessString);
            state = enter;
            prevState = good;
            delayUs(2000000);
            break;
        case bad:
            clearLCD();
            printStringLCD("Bad");
            keyPresses = 0;
            clearString(guessString);
            state = enter;
            prevState = bad;
            delayUs(2000000);
            break;
        case valid:
            clearLCD();
            if(PassCount == 3) {
                PassCount = 0;
            }
            else PassCount = PassCount + 1;
            updatePass(tempPass, passStringa, passStringb, passStringc, passStringd, PassCount);
            printStringLCD("Valid");
            keyPresses = 0;
            clearString(tempPass);
            mode = ENTER;
            state = enter;
            prevState = valid;
            delayUs(2000000);
            break;
        case invalid:
            clearLCD();
            printStringLCD("Invalid");
            keyPresses = 0;
            clearString(tempPass);
            mode = ENTER;
            state = enter;
            prevState = invalid;
            delayUs(2000000);
            break;
        }
    }
    return 0;
}
コード例 #11
0
ファイル: main.c プロジェクト: nramohalli/ECE372-Team
int main(void)
{
    
    int i = 0;
    int c = 0;
    int r = 0;
    
    char pass1[5] = "0000";
    char pass2[5] = "0000";
    char pass3[5] = "0000";
    char pass4[5] = "0000";
    char passEntry[5] = "";
    
    enableInterrupts();                   //This function is necessary to use interrupts.
    initTimer2();
    initLCD();
    initKeypad();
    
    SYSTEMConfigPerformance(10000000);
    
    while(1){
        
        switch(state){
            case WAIT4PRESS:
                //INTERRUPT_E_PINS = _ON;
                delayUs(1000);
                KEYPAD_LAT_COL0 = ODC_CLOSED;
                KEYPAD_LAT_COL1 = ODC_CLOSED;
                KEYPAD_LAT_COL2 = ODC_CLOSED;
                if ((i == 4) && (set_mode == 0)){
                    if (!(strcmp(passEntry, pass1)) || !(strcmp(passEntry, pass2)) || !(strcmp(passEntry, pass3)) || !(strcmp(passEntry, pass4))){
                        state = GOOD; 
                    }
                    else{
                        state = BAD;
                    }
                }
                else if ((i == 4) && (set_mode == 1)){
                    j = 0;
                    for(j=0;j<4;j++){
                        if ((passEntry[j] == '*') || (passEntry[j] == '#')){
                            state = INVALID;
                            break;
                        }
                        else{
                            state = VALID;
                        }
                    }
                }
                else if ((i == 2) && (set_mode == 0)){
                    if ((passEntry[0] == '*') && (passEntry[1] == '*')){
                        i = 0;
                        set_mode = 1;
                        state = SETMODE;
                    }
                }
                if (i == 4) i = 0;
                break;
                
            case WAIT:
                KEYPAD_LAT_COL0 = ODC_OPEN;
                KEYPAD_LAT_COL1 = ODC_OPEN;
                KEYPAD_LAT_COL2 = ODC_OPEN;
                state = COL0;
                break;
                
            case WAIT4RELEASE:
                INTERRUPT_E_PINS = _ON;
                break;
                
            case COL0:
                KEYPAD_LAT_COL1 = ODC_OPEN;
                KEYPAD_LAT_COL2 = ODC_OPEN;
                KEYPAD_LAT_COL0 = ODC_CLOSED;
                delayUs(1000);
                //********************************
                returnedKey = scanKeypad(0);
                if (returnedKey != '!'){
                    delayUs(1000);
                    //printCharLCD(returnedKey);
                    //moveCursorLCD(1,0);
                    if (i > 3) i = 0;
                    passEntry[i] = returnedKey;
                    i = i + 1;
                    delayUs(1000);
                    state = WAIT4RELEASE;
                }
                else state = COL1;
                break;
                
            case COL1:
                KEYPAD_LAT_COL0 = ODC_OPEN;
                KEYPAD_LAT_COL2 = ODC_OPEN;
                KEYPAD_LAT_COL1 = ODC_CLOSED;
                delayUs(1000);
                //********************************
                returnedKey = scanKeypad(1);
                if (returnedKey != '!'){
                    delayUs(1000);
                    //printCharLCD(returnedKey);
                    //moveCursorLCD(1,0);
                    if (i > 3) i = 0;
                    passEntry[i] = returnedKey;
                    i = i + 1;
                    delayUs(1000);
                    state = WAIT4RELEASE;
                }
                else state = COL2;
                break;
                
            case COL2:
                KEYPAD_LAT_COL0 = ODC_OPEN;
                KEYPAD_LAT_COL1 = ODC_OPEN;
                KEYPAD_LAT_COL2 = ODC_CLOSED;
                delayUs(1000);
                //********************************
                returnedKey = scanKeypad(2);
                if (returnedKey != '!'){
                    delayUs(1000);
                    //printCharLCD(returnedKey);
                    //moveCursorLCD(1,0);
                    if (i > 3) i = 0;
                    passEntry[i] = returnedKey;
                    i = i + 1;
                    delayUs(1000);
                    state = WAIT4RELEASE;
                }
                //********************************
                state = WAIT4RELEASE;
                break;
                
            //************PART2************************************************
            case ENTER:
                printStringLCD("Enter");
                state = WAIT4PRESS;
                break;

            case SETMODE:
                clearLCD();
                delayUs(1000);
                printStringLCD("Set Mode");
                state = WAIT4PRESS;
                break;
            case SETMODE_ENTRY:
                break;
            case GOOD:
                clearLCD();
                printStringLCD("Good");
                j = 0;
                for(j=0;j<1000;j++) delayUs(1000);
                clearLCD();
                set_mode = 0;
                state = ENTER;
                break;
                
            case BAD:
                clearLCD();
                printStringLCD("Bad");
                j = 0;
                for(j=0;j<1000;j++) delayUs(1000);
                clearLCD();
                set_mode = 0;
                state = ENTER;
                break;
                
            case VALID:
                clearLCD();
                printStringLCD("Valid");
                j = 0;
                for(j=0;j<1000;j++) delayUs(1000);
                clearLCD();
                j = 0;
                if (c == 4) c = 0;
                c = c + 1;

                if (c == 1) strcpy(pass1,passEntry);
                if (c == 2) strcpy(pass2,passEntry);
                if (c == 3) strcpy(pass3,passEntry);
                if (c == 4) strcpy(pass4,passEntry);
                    
                set_mode = 0;
                state = ENTER;
                break;
                
            case INVALID:
                clearLCD();
                printStringLCD("Invalid");
                j = 0;
                for(j=0;j<1000;j++) delayUs(1000);
                clearLCD();
                set_mode = 0;
                state = ENTER;
                break;
        }
        
    }
    
    return 0;
}
コード例 #12
0
ファイル: main.c プロジェクト: ECE372FA15/Team
int main(void) {

    char passWord[passwords][wordLen]; //[nump][lenp]; 
    char temp[wordLen]; //string to hold characters as they are typed in
    int pwItt = 0; //index of the password being typed in by user 
    int pwStoreIndex = 0; //index for string# in passWord[][] array
    //index is used to add pws to passWord[][]

    int numCharsPrinted = 0; // counter to keep things tidy on the display 
    int modeStateEnable = 0; // enables the second state machine 
    int i = 0, j = 0;
    int match = 0;
    int count = 0;
    ANSELE = 0;
    SYSTEMConfigPerformance(40000000);
    initLCD();
    clearLCD();
    writeLCD(0b00001111, 0, 50);
    initTimer1();
    initKeypad();
    enableInterrupts();
    clearBuff(wordLen, temp);
    //initialize temp to NULL
    for (i = 0; i < wordLen; i++) {
        temp [i] = NULL;
    }
    //initialize passcode array to NULL
    for (i = 0; i < passwords; i++) {
        for (j = 0; j < wordLen; j++) {
            passWord[i][j] = NULL;
        }
    }

#ifdef TEST2
    int flag = 0;

    //Precode 2 passwords into password array and test if working

    for (i = 0; i < 2; i++) {
        for (j = 0; j < wordLen-3; j++) {
            if (flag == 0) {
                passWord[i][j] = '0'+j;
            } else {
                passWord[i][j] = '0'+j + 1;
            }
        }
        flag++;
        passWord[i][j] = '\0';
    }

#endif
    
    // initialize the state machine output 
    printStringLCD("Enter");

#ifdef run  
    while (1) {

        //<><><><>  button de bounce stuff  <><><><><><><><><><><><><><><><><><><><><><>        
        switch (state) {
            case printKey:
                modeStateEnable = 1;
                temp[pwItt] = keyScanned;
                pwItt++;
                state = waitForRelease;
                break;

            case scanKey:
                keyScanned = scanKeypad();
                state = printKey;
                break;

            case dbPress:
                delayUs(DBDelayTime);
                state = scanKey;
                break;

            case dbRelease:
                delayUs(DBDelayTime);
                state = waitForPress;
                break;

            case waitForPress:
                //enableInterrupts();
                break;

            case waitForRelease:
                // enableInterrupts(); 
                break;
        }
        //<><><><> END button de bounce stuff  <><><><><><><><><><><><><><><><><><><><><><>  

        //!!! TODO !!! every exit branch of the state machine WILL:
        //        enable/disable interrupts
        //        set the next state (even if it does not change)
        //        set modeStateEnable to 1 or 0

        //    THIS will help with debugging  

        //<><><><>  Mode setting state machine <><><><><><><><><><><><><><><><><><><><><><>       
        if (modeStateEnable == 1) { // need to ensure this is correct...
            disableInterrupts(); //messing with printing?
            modeStateEnable = 0; //wait for key input
            // the newKeyPressed variable gets changed to 1 everytime a key press is detected    
            switch (modeState) {
                case firstStar: //interrupts are ENABLED DELETEME
                    if (temp[1] == '*') {
                        modeState = set; // the state that allows you to add pws 
                        modeStateEnable = 0; //wait for new key to be pressed
                        clearLCD();
                        printStringLCD("Set Mode");
                        moveCursorLCD(2, 1);
                        enableInterrupts();
                    } else {
                        modeState = dispBad;
                        modeStateEnable = 1; //goto state
                        disableInterrupts();
                    }
                    break;

                case dispGood: //interrupts are DISABLED DELETEME
                    printOutput("Good");
                    clearLCD();
                    printStringLCD("Enter"); //prompt enter state
                    clearBuff(wordLen, temp); // clear the temp string 
                    pwItt = 0; // reset the pw itterator 
                    modeState = dispEnter; //switch state
                    modeStateEnable = 0; //wait for new key to be pressed
                    enableInterrupts();
                    break;

                case dispBad: //interrupts are DISABLED DELETEME
                    printOutput("Bad");
                    clearLCD();
                    printStringLCD("Enter"); // prompt enter state
                    modeState = dispEnter; // switch state
                    clearBuff(wordLen, temp); // clear the temp string 
                    pwItt = 0; // reset the pw itterator 
                    modeStateEnable = 0; // wait for new key to be pressed
                    enableInterrupts();
                    break;

                case dispEnter: //interrupts are DISABLED DELETEME
                    clearLCD();
                    printStringLCD("Enter");
                    moveCursorLCD(2, 1);
                    printStringLCD(temp); // print the characters as they are entered 
                    if (pwItt == 4) {


                        if (checkValid(temp, passWord) == 0) {
                            //if( match == 0){ 
                            modeState = dispBad; // 0 means invalid pw
                            modeStateEnable = 1; //goto state
                            disableInterrupts();
                        } else {
                            modeState = dispGood; //1 means valid pw
                            modeStateEnable = 1; //goto state
                            disableInterrupts();
                            printCharLCD('&');
                        }
                    } else if (temp[0] == '*') {
                        modeState = firstStar;
                        modeStateEnable = 0; //wait for new key to be pressed 
                        enableInterrupts();
                    } else if (temp[0] == '#') {
                        modeState = dispBad;
                        modeStateEnable = 1; //continue
                        disableInterrupts();
                    } else {
                        modeState = dispEnter;
                        modeStateEnable = 0; //wait for new key to be pressed 
                        enableInterrupts();
                    }

                    break;
                case dispValid://-
                    printOutput("Valid   ");
                    clearLCD();
                    printStringLCD("Enter");

                    if (addNewPw(temp, passWord) == 0) { // if password is not in list 
                        //if(match == 0){           // if password is not in list 
                        strcpy(passWord[pwStoreIndex], temp); // add it
                        pwStoreIndex++; // increment itterator 
                    }
                    modeState = dispEnter;
                    clearBuff(wordLen, temp); // clear the temp string 
                    pwItt = 0; // reset the pw itterator
                    modeStateEnable = 0; //wait for new key to be pressed 
                    enableInterrupts();
                    break;

                case dispInvalid://-
                    printOutput("Invalid ");
                    clearLCD();
                    printStringLCD("Enter");
                    modeState = dispEnter;
                    clearBuff(wordLen, temp); // clear the temp string 
                    pwItt = 0; // reset the pw itterator 
                    modeStateEnable = 0; //wait for new key to be pressed 
                    enableInterrupts();
                    break;

                case set:

                    printCharLCD(keyScanned); // might work better to press key pressed 
                    if (keyScanned == '*' || keyScanned == '#') { // if an invalid key was entered 
                        modeState = dispInvalid; // if new pw is not valid 
                        modeStateEnable = 1; //next state needs to be executed
                        disableInterrupts();
                    } else if (pwItt == 6) { // pw == "**xxxx"...
                        temp[0] = temp[2];
                        temp[1] = temp[3]; // remove leading "**" 
                        temp[2] = temp[4];
                        temp[3] = temp[5];
                        temp[4] = NULL;
                        temp[5] = NULL;

                        modeState = dispValid;
                        modeStateEnable = 1; //next state needs to be executed
                        disableInterrupts();
                    } else {
                        modeStateEnable = 0; //next state needs to be executed
                        enableInterrupts();
                    }

                    break;
            }
        }
        //<><><><> END Mode setting state machine <><><><><><><><><><><><><><><><><><><><><><>   

    }

    return 0;
}
コード例 #13
0
void main(void) 
{
    int an3now;
    int an3max = 0;
    char key;
    char eepromloc = 0;
    int eepromval;
    int eeprommax;
    char graphclear = 1;

    Glcd_Init();
    
    Sm_Glcd_Out2(57, 67, "H.S.B V1.0");
    Sm_Glcd_Out2(51, 1, "AN3:");
    Sm_Glcd_Out2(37, 1, "ROM:");
    Sm_Glcd_Out2(0, 1, "MIN");
    Sm_Glcd_Out2(10, 1, "MAX");
    Sm_Glcd_Out2(20, 1, "MEAN");
    
    //Lines for graph
    Glcd_H_Line(62, 126, 32, 1);
    Glcd_V_Line(0,32,62,1);
    
    ADCON1 = 0;
    
    eepromval = getAn3(eepromloc);
    showEepromval(eepromval,eeprommax);
    an3now = ADC_Read(3);
    intOut(26, 51, an3now);

    while(1)
    {
        an3max = an3now;
        eeprommax = eepromval;
        an3now = ADC_Read(3);

        key = scanKeypad();

        if(key == 1) //store an3 value
        {
            storeAn3(eepromloc, an3now);
            eepromval = an3now;
        }
        if(key == 2) //switch eeprom bank and return value
        {
            if(eepromloc == 0) eepromloc = 16;
            else if(eepromloc == 16) eepromloc = 32;
            else if(eepromloc == 32) eepromloc = 0;
            eepromval = getan3(eepromloc);
        }
        if(key == 4 && graphclear == 1) //run graph if it is clear
        {
            graphclear = 0;
            runGraph();
        }
        if(key == 5 && graphclear == 0) //clear graph if it has been run
        {
            graphclear = 1;
            resetGraph();
        }
        key = 0;
        
        if(an3now != an3max)
        {
            intOut(26, 51, an3now);
            printBar(0, 59, an3now, an3max);
        }
        
        showEepromval(eepromval, eeprommax);
    }
}