Currently, this function only supports one key being pressed. Multi-key press needs to be added later. // // Return Value : Bit field of buttons pressed // static uint16_t GetPressedButton() { DEBUG_BUTTON16(Serial.println("GetPressedButton : Enter ")); uint16_t buttonsPressed = 0; // Need to generate the LOW and then HIGH on the clock and read the value from data when clock is back high. // As per the specification, the TTP229 chip can support 512Khz. This translates to approximately 2us for a cycle. So introducing clock with fall and raise each of 1us. uint8_t maxCnt = g_prop.Is16Button ? 16 : 8; for(uint8_t ndx = 0; ndx < maxCnt; ndx++ ) { digitalWrite(g_prop.SclPin, LOW); delayMicroseconds(1); digitalWrite(g_prop.SclPin, HIGH); int val = digitalRead(g_prop.SdoPin); delayMicroseconds(1); // Technically this can be moved after the if for most atmel microcontrollers. But if there is a really fast one (now/future) and the next call to GetPressedButton made immediately, we might overclock TTP229. Being safe here if( LOW == val ) { buttonsPressed |= (1 << ndx); } } DEBUG_BUTTON16(Serial.print("GetPressedButton : Exit. Return Value : ")); DEBUG_BUTTON16(Serial.println(buttonsPressed)); return buttonsPressed; } #if defined(INT0_PIN) || defined(INT1_PIN) // Detaching the interrupt after receiving the data can cause problem in sleeping. If the interrupt is not properly dispatched, it can lead to permanent sleep and can't wake up from button static void HandleButtonEvent() { if( g_prop.IgnoreNextEvent ) { // We ignored an event. Now we will accept the event g_prop.IgnoreNextEvent = false; } else { g_prop.HasPendingInterrupt = true; g_intCount++; } } static void SetInterruptHandler() { #ifdef INT0_PIN if( INT0_PIN == g_prop.SdoPin ) { DEBUG_BUTTON16(Serial.println("Configure : With interrupt 0")); EIFR = 0x01; // Clear INTF0 flag attachInterrupt(0, HandleButtonEvent, RISING); // The pin goes down for 93us and then raises that is when the device is ready (technically after 10us) } #endif #ifdef INT1_PIN if( INT1_PIN == g_prop.SdoPin ) { DEBUG_BUTTON16(Serial.println("Configure : With interrupt 1")); EIFR = 0x02; // Clear INTF1 flag attachInterrupt(1, HandleButtonEvent, RISING); // The pin goes down for 93us and then raises that is when the device is ready (technically after 10us) } #endif } static void RemoveInterruptHandler() { #ifdef INT0_PIN if( INT0_PIN == g_prop.SdoPin ) { detachInterrupt(0); } #endif #ifdef INT1_PIN if( INT1_PIN == g_prop.SdoPin ) { detachInterrupt(1); } #endif } #endif // // Returns button number being pressed. High bit indicates more changes present // static uint8_t GetButtonNumberFromFlag(uint16_t buttonsChanged) { uint16_t flag = 1; for(uint8_t ndx = 1; ndx <=16; ndx++, flag <<= 1) { if( (buttonsChanged & flag) != 0 ) { if( (buttonsChanged & ~flag) != 0 ) { // Some other bit is present ndx |= 0x80; } return ndx; } } return 0; } public: // // Setup the TTP229 Touch button on this input. // // Inputs: // sclPin - Clock Pin of the button (3rd from left on button, connected to arduino's digital pin number) // sdoPin - Data pin to read from the button (4th pin from left on button, connected to arduino's digital pin number) // is16Button - true = 16 buttons board. false = 8 button board // static void Configure(int sclPin, int sdoPin,