bool is16Button = true) { DEBUG_BUTTON16(Serial.println("Configure : Enter")); g_prop.SclPin = sclPin; g_prop.SdoPin = sdoPin; g_prop.Is16Button = is16Button; g_prop.PreviousButtonValue = 0; // Configure clock as output and hold it high pinMode( sclPin, OUTPUT ); digitalWrite(sclPin, HIGH); // Configure data pin as input pinMode( sdoPin, INPUT); DEBUG_BUTTON16(Serial.print("Button Configuration\n\rSCL Pin : ")); DEBUG_BUTTON16(Serial.println(sclPin)); DEBUG_BUTTON16(Serial.print("SDO Pin : ")); DEBUG_BUTTON16(Serial.println(sdoPin)); DEBUG_BUTTON16(Serial.print("Number of Keys : ")); DEBUG_BUTTON16(Serial.println(is16Button ? 16 : 8)); #if defined(INT0_PIN) || defined(INT1_PIN) g_prop.UnhandledButtonPresses = 0; g_prop.HasPendingInterrupt = false; g_prop.IgnoreNextEvent = false; SetInterruptHandler(); #endif DEBUG_BUTTON16(Serial.println("Configure : Exit")); } // // Get the current status from the 16 button touch device // // Return Value : Returns the bitflag of the keys pressed. returns 0, if no key is pressed. // static uint16_t GetButtonStatus() { #if defined(INT0_PIN) || defined(INT1_PIN) g_prop.HasPendingInterrupt = 0; #endif uint16_t returnValue = GetPressedButton(); #if defined(INT0_PIN) || defined(INT1_PIN) returnValue |= g_prop.UnhandledButtonPresses; // and also include any data that was received that we have not sent yet. g_prop.UnhandledButtonPresses = 0; #endif g_prop.PreviousButtonValue = returnValue; return returnValue; } // // Gets the event from the button. This is useful for monitoring press and release only. // Each button press will generate max 2 events, one for press and another for release. When the button is press and held, this method will return no event. // If the calls were not made often enough, the events could be missed. For instance, you might get 2 pressed, followed by 4 pressed, which automatically means 2 released in single key mode. // // Return Value : if ButtonNumber is 0, then no event // static CTtp229ButtonEvent GetButtonEvent() { CTtp229ButtonEvent returnValue = {0, 0}; uint8_t buttonNumber; DEBUG_BUTTON16(Serial.print("Old Value : ")); DEBUG_BUTTON16(Serial.println(g_prop.PreviousButtonValue)); #if defined(INT0_PIN) || defined(INT1_PIN) if( #if defined(INT0_PIN) INT0_PIN == g_prop.SdoPin #endif #if defined(INT0_PIN) && defined(INT1_PIN) || #endif #if defined(INT1_PIN) INT1_PIN == g_prop.SdoPin #endif ) { // Interrupts are used. Check if we have interrupt if( g_prop.HasPendingInterrupt ) { RemoveInterruptHandler(); // From this point upto SetInterruptHandler is called, ensure there is no return path that will leave without SetInterruptHandler } else { DEBUG_BUTTON16(Serial.println("GetButtonEvent: No interrupt pending")); return returnValue; } } #endif uint16_t currValue = GetPressedButton(); #if defined(INT0_PIN) || defined(INT1_PIN) currValue |= g_prop.UnhandledButtonPresses; // Get any previously returned but not returned now values also into the mix #endif uint16_t changes = g_prop.PreviousButtonValue ^ currValue; uint16_t pressed = (changes & currValue); uint16_t released = (changes & g_prop.PreviousButtonValue); // First check if any key is that is pressed and generate press event if( 0 != pressed ) { buttonNumber = GetButtonNumberFromFlag(pressed); returnValue.ButtonNumber = (buttonNumber & 0x7F); uint16_t mask = (1 << (returnValue.ButtonNumber -1)); // set the new notified button into prev g_prop.PreviousButtonValue |= mask; #if defined(INT0_PIN) || defined(INT1_PIN) g_prop.UnhandledButtonPresses = currValue; g_prop.UnhandledButtonPresses = currValue & ~g_prop.PreviousButtonValue; // clear unhandled for this bit, just in case #endif } else if(0 != released) { buttonNumber = GetButtonNumberFromFlag(released); returnValue.ButtonNumber = (buttonNumber & 0x7F); // The unmatching bit whose previous value of 1 means, it is released returnValue.IsButtonReleased = true; // clear the notified release button g_prop.PreviousButtonValue &= ~(1 << (returnValue.ButtonNumber -1)); } #if defined(INT0_PIN) || defined(INT1_PIN) if(((!returnValue.IsButtonReleased || (0 == pressed)) // We handle release but