// Manage the list without rearranging the keys. Returns true if any keys on the list changed state. bool Keypad::updateList() { bool anyActivity = false; // Delete any IDLE keys for (byte i=0; i<LIST_MAX; i++) { if (key[i].kstate==IDLE) { key[i].kchar = NO_KEY; key[i].kcode = -1; key[i].stateChanged = false; } } // Add new keys to empty slots in the key list. for (byte r=0; r<sizeKpd.rows; r++) { for (byte c=0; c<sizeKpd.columns; c++) { boolean button = bitRead(bitMap[r],c); char keyChar = keymap[r * sizeKpd.columns + c]; int keyCode = r * sizeKpd.columns + c; int idx = findInList (keyCode); // Key is already on the list so set its next state. if (idx > -1) { nextKeyState(idx, button); } // Key is NOT on the list so add it. if ((idx == -1) && button) { for (byte i=0; i<LIST_MAX; i++) { if (key[i].kchar==NO_KEY) { // Find an empty slot or don't add key to list. key[i].kchar = keyChar; key[i].kcode = keyCode; key[i].kstate = IDLE; // Keys NOT on the list have an initial state of IDLE. nextKeyState (i, button); break; // Don't fill all the empty slots with the same key. } } } } } // Report if the user changed the state of any key. for (byte i=0; i<LIST_MAX; i++) { if (key[i].stateChanged) anyActivity = true; } return anyActivity; }
// Manage the list without rearranging the keys. Returns true if any keys on the list changed state. bool Keypad_MAX::updateList() { bool anyActivity = false; // Delete IDLE keys for (byte i=0; i<LIST_MAX; i++) { if (key[i].kstate==IDLE) { key[i].kchar = NO_KEY; key[i].kcode = -1; key[i].stateChanged = false; } } #if 0 // Add new keys to empty slots in the key list. for (byte r=0; r<sizeKpd.rows; r++) { for (byte c=0; c<sizeKpd.columns; c++) { boolean button = bitRead(bitMap[r],c); char keyChar = keymap[r * sizeKpd.columns + c]; int keyCode = r * sizeKpd.columns + c; int idx = findInList (keyCode); // Key was found on the list so set its next state. if (idx > -1) nextKeyState(idx, button); // Key is not on the list so add it. if ((idx == -1) && button) { for (byte i=0; i<LIST_MAX; i++) { if (key[i].kchar==NO_KEY) { // Find an empty slot or don't add key to list. key[i].kchar = keyChar; key[i].kcode = keyCode; key[i].kstate = IDLE; // Keys NOT on the list have an initial state of IDLE. nextKeyState (i, button); break; // Don't fill all the empty slots with the same key. } } } } } #endif // MAX7359 does not need row/column examination of bitmap. Keycodes already have the // row and column values, so just need to read from the Wire buffer filled in getKeys byte keyCode; do { keyCode = TwoWire::read( ); if( keyCode == 0x3f ) { // if key fifo is empty for( byte r=0; r<sizeKpd.rows; r++ ) { for( byte c=0; c<sizeKpd.columns; c++ ) { // check list for all codes keyCode = rowPins[r] * sizeKpd.columns + columnPins[c]; int idx = findInList( (int)keyCode ); if( idx > -1 && key[idx].kstate != PRESSED ) { nextKeyState( (byte)idx, false ); anyActivity = true; } // if( idx > -1 && (key[idx].kstate == PRESSED // || key[idx].kstate == HOLD) ) { // this gets HOLD noticed // nextKeyState( (byte)idx, CLOSED ); // but causes release if held // anyActivity = true; // } } // all columns } // all rows return anyActivity; } bool button = ( (keyCode&0x40) != 0x40 ); // bit6 = 1 ==>release byte krow = rowPins[(keyCode&0x3f)%8]; byte kcol = columnPins[(keyCode&0x3f)/8]; char keyChar = keymap[krow * sizeKpd.columns + kcol]; int idx = findInList( (int)(keyCode&0x3f) ); // key already on list if( idx > -1 ) nextKeyState( (byte)idx, button ); // key needs to be added if( (idx == -1) && button ) { for( byte i=0; i<LIST_MAX; i++ ) { if( key[i].kchar == NO_KEY ) { // find empty slot key[i].kchar = keyChar; key[i].kcode = keyCode&0x3f; key[i].kstate = IDLE; // init entry to IDLE nextKeyState( i, button ); // set to button (PRESSED) break; } // if list slot empty } // for all list } // if not on list } while( (keyCode&0x80) == 0x80 ); // last valid key read from fifo has clear bit7 // Report if the user changed the state of any key. for (byte i=0; i<LIST_MAX; i++) { if (key[i].stateChanged) anyActivity = true; } return anyActivity; }