Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pinguino32X + keypad librairie
16-06-2018, 05:21 PM,
#16
RE: Pinguino32X + keypad librairie
Code:
#include <digitalw.c>
#include <millis.c>
#include "keypad.h"
//--------------------
// Consts and Defines
//--------------------
//const char NO_KEY = '\0';
//#define KEY_RELEASED NO_KEY
// this is a macro that converts 2D arrays to pou8ers:
#define makeKeymap(x) ((char *)x)
void Keypad_transitionTo(KeypadState newState);
void Keypad_initializePins();
void Keypad_init(char *keypadmap, u8 *rowp, u8 *colp, u8 rows, u8 cols);
void Keypad_setDebounceTime( unsigned int debounce);
void Keypad_setHoldTime( unsigned int hold);
//-------------------
// Library variables
//-------------------
u8 keypad_rows;
u8 keypad_columns;
char *userKeymap;
u8 *rowPins;
u8 *columnPins;
KeypadState state;
char currentKey;
unsigned long lastUpdate;
unsigned int debounceTime;
unsigned int holdTime;
const u8 rows = 4;
const u8 cols = 4;

//-----------------
// Funtions
//-----------------
// create keypad matrix
char keypadmap[4][4] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
u8 *rowp[4] = {24, 23, 22, 21}; //connect to the row pinouts of the keypad
u8 *colp [4]= {28, 27, 26, 25};
KeypadState Keypad_getState(){
return state;
}
void Keypad_setDebounceTime(unsigned int debounce){
debounceTime = debounce;
}
void Keypad_setHoldTime(unsigned int hold){
holdTime = hold;
}
//void Keypad_addEventListener(void (*listener)(char)){
//    keypadEventListener = listener;
//}
//private function
void Keypad_transitionTo(KeypadState newState){
if (state!=newState){
state = newState;
//        if (keypadEventListener!=NULL){
//            keypadEventListener(currentKey);
//        }
}
}
//private function
void Keypad_initializePins(){
u8 c,r;
for (r=0; r<keypad_rows; r++){
for (c=0; c<keypad_columns; c++){
pinmode(columnPins[c],OUTPUT);
digitalwrite(columnPins[c],HIGH);
}
}
//configure row pin modes and states
for(r=21;r<25;r++){
pinmode(rowPins[r],INPUT);
digitalwrite(rowPins[r],HIGH);
}
}
// Initialization of Allows custom keymap. pin configuration and keypad size
void Keypad_init(char *keypadmap, u8 *rowp, u8 *colp, u8 rows, u8 cols){
userKeymap = keypadmap;
rowPins = rowp;
columnPins = colp;
keypad_rows = rows;
keypad_columns = cols;        
lastUpdate = 0;
debounceTime = 50;
holdTime = 1000;

//    keypadEventListener = 0;
currentKey = NO_KEY;
state = IDLE;
Keypad_initializePins();
}
// Returns the keykode of the pressed key, or NO_KEY if no key is pressed
char Keypad_getKey(){
char key = NO_KEY; // Assume that no key is pressed, this is the default return for getKey()
u8 c,r;    
for (c=0; c<keypad_columns; c++){
digitalwrite(columnPins[c],LOW);    // Activate the current column.
for (r=0; r<keypad_rows; r++){    // Scan all the rows for a key press.
//  The user pressed a button for more then debounceTime microseconds.
if (currentKey == userKeymap[c+(r*keypad_columns)]){
// Button hold
if (((millis()-lastUpdate) >= holdTime) && (digitalread(rowPins[r])) == LOW){
Keypad_transitionTo(HOLD);


}
// Button release
if (((millis()-lastUpdate) >= debounceTime) && digitalread(rowPins[r]) == HIGH){
Keypad_transitionTo(RELEASED);
currentKey = NO_KEY;
}
}
// Button pressed event.  The user pressed a button.
else if (((millis()-lastUpdate) >= debounceTime) && digitalread(rowPins[r]) == LOW){
digitalwrite(columnPins[c],HIGH);    // De-activate the current column.
key = userKeymap[c+(r*keypad_columns)];
lastUpdate = millis();
goto EVALUATE_KEY;             // Save resources and do not attempt to parse two keys at a time
}
}
digitalwrite(columnPins[c],HIGH);    // De-activate the current column.
}
EVALUATE_KEY:
    //if (key != NO_KEY && key != currentKey ){ //Error: keeps repeating keys in the same column
if (key != NO_KEY && key != currentKey && state!=PRESSED){ //Correction for simultaneous pressed keys on the same row
        // if(state ==HOLD && key != currentKey) //try to disable several keys pressed at the same time
        //     return NO_KEY;
    
currentKey = key;
Keypad_transitionTo(PRESSED);        
return currentKey;
    }
    
    else{
return NO_KEY;
    }
}

el código !
Reply


Messages In This Thread
Pinguino32X + keypad librairie - by j3r0m3 - 08-11-2011, 01:10 AM
RE: Pinguino32X + keypad librairie - by j3r0m3 - 08-11-2011, 10:29 PM
RE: Pinguino32X + keypad librairie - by regis - 08-11-2011, 10:51 PM
RE: Pinguino32X + keypad librairie - by j3r0m3 - 09-11-2011, 01:20 AM
RE: Pinguino32X + keypad librairie - by regis - 09-11-2011, 10:00 AM
RE: Pinguino32X + keypad librairie - by j3r0m3 - 10-11-2011, 12:44 PM
RE: Pinguino32X + keypad librairie - by j3r0m3 - 11-11-2011, 11:44 AM
RE: Pinguino32X + keypad librairie - by j3r0m3 - 11-11-2011, 06:19 PM
RE: Pinguino32X + keypad librairie - by j3r0m3 - 12-11-2011, 02:40 PM
RE: Pinguino32X + keypad librairie - by malagas - 22-01-2013, 04:18 PM
RE: Pinguino32X + keypad librairie - by Cristobal - 16-06-2018, 04:44 PM
RE: Pinguino32X + keypad librairie - by Cristobal - 16-06-2018, 05:21 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)