You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in my use case i have several different input methods and the ttp229 is one of them i needed a way at runtime to check and see if the keypad existed, and if it didnt to continue the program so i added the IsAlive(); function... and a register read command, these may be useful to some people and they may not. here is the code for anyone seeking this functionality ..
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "TTP229.h"
TTP229::TTP229(uint8_t sclPin, uint8_t sdoPin)
{
_sclPin = sclPin;
_sdoPin = sdoPin;
pinMode(_sclPin , OUTPUT);
pinMode(_sdoPin , INPUT);
digitalWrite(_sclPin, HIGH);
}
bool TTP229::IsAlive()
{
return (ReadRegister(TTP229_CTRL_REG0) != 0);
}
uint8_t TTP229::ReadKey8()
{
WaitForTouch();
Key8();
return _key8;
}
uint8_t TTP229::GetKey8()
{
if (IsTouch()) Key8();
return _key8;
}
uint8_t TTP229::ReadKeys8()
{
WaitForTouch();
Keys8();
return _keys8;
}
uint8_t TTP229::GetKeys8()
{
if (IsTouch()) Keys8();
return _keys8;
}
uint8_t TTP229::ReadKey16()
{
WaitForTouch();
Key16();
return _key16;
}
uint8_t TTP229::GetKey16()
{
if (IsTouch()) Key16();
return _key16;
}
uint16_t TTP229::ReadKeys16()
{
WaitForTouch();
Keys16();
return _keys16;
}
uint16_t TTP229::GetKeys16()
{
if (IsTouch()) Keys16();
return _keys16;
}
void TTP229::Key8()
{
_key8 = 0;
for (uint8_t i = 0; i < 8; i++)
if (GetBit()) _key8 = i + 1;
delay(2); // Tout
}
void TTP229::Keys8()
{
_keys8 = 0;
for (uint8_t i = 0; i < 8; i++)
if (GetBit()) _keys8 |= 1 << i;
delay(2); // Tout
}
void TTP229::Key16()
{
_key16 = 0;
for (uint8_t i = 0; i < 16; i++)
if (GetBit()) _key16 = i + 1;
delay(2); // Tout
}
void TTP229::Keys16()
{
_keys16 = 0;
for (uint8_t i = 0; i < 16; i++)
if (GetBit()) _keys16 |= 1 << i;
delay(2); // Tout
}
bool TTP229::GetBit()
{
digitalWrite(_sclPin, LOW);
delayMicroseconds(2); // 500KHz
bool retVal = !digitalRead(_sdoPin);
digitalWrite(_sclPin, HIGH);
delayMicroseconds(2); // 500KHz
return retVal;
}
bool TTP229::IsTouch()
{
uint16_t timeout = 5000; // 50ms timeout
while (digitalRead(_sdoPin)) // DV LOW
{
if (--timeout == 0) return false;
delayMicroseconds(10);
}
while (!digitalRead(_sdoPin)) // DV HIGH
{
if (--timeout == 0) return false;
delayMicroseconds(10);
}
delayMicroseconds(10); // Tw
return true;
}
void TTP229::WaitForTouch()
{
while (digitalRead(_sdoPin)); // DV LOW
while (!digitalRead(_sdoPin)); // DV HIGH
delayMicroseconds(10); // Tw
}
uint8_t TTP229::ReadRegister(uint8_t regAddr)
{
uint8_t regValue = 0;
digitalWrite(_sclPin, LOW);
delayMicroseconds(2); // 500KHz
for (int i = 0; i < 8; i++)
{
digitalWrite(_sclPin, LOW);
delayMicroseconds(2); // 500KHz
digitalWrite(_sdoPin, (regAddr & (1 << i)));
digitalWrite(_sclPin, HIGH);
delayMicroseconds(2); // 500KHz
}
pinMode(_sdoPin, INPUT);
digitalWrite(_sclPin, LOW);
delayMicroseconds(2); // 500KHz
for (int i = 0; i < 8; i++)
{
digitalWrite(_sclPin, LOW);
delayMicroseconds(2); // 500KHz
regValue |= (digitalRead(_sdoPin) << i);
digitalWrite(_sclPin, HIGH);
delayMicroseconds(2); // 500KHz
}
pinMode(_sdoPin, OUTPUT);
return regValue;
}
and finally an example to test functionality, if its hooked up right it will print to serial terminal that it is connected while this is running you can pull the ground connection from the ttp229 and it will say is not connected. if you reattach the ground it will say it is connected again, this may be helpful for anyone trying to diagnose connection issues as well ... just a thought
#include <TTP229.h>
const int SCL_PIN = 21; // The pin number of the clock pin.
const int SDO_PIN = 20; // The pin number of the data pin.
TTP229 ttp229(SCL_PIN, SDO_PIN); // TTP229(sclPin, sdoPin)
void setup()
{
Serial.begin(115200);
}
void loop()
{
// uint8_t key = ttp229.ReadKey16(); // Blocking
// if (key) Serial.println(key);
//uint16_t key = ttp229.GetKeys16(); // Non Blocking
//Serial.println(key,BIN);
if(ttp229.IsAlive()){
Serial.println("Is Present");
}else{
Serial.println("Is not Present");
}
}
i hope this helps someone along the way ... if arduino12 wants to post a patch of the code that would be great too... i just didnt want to mess with the pull request.
heres some added information if anyone is looking for it
The TTP229 is a 16-key touchpad detector IC, which has a set of control and configuration registers. The registers are accessed via a serial interface, and their values determine the behavior of the touchpad.
Here are the register values for the TTP229:
Control Register 0 (Address 0x00): This register controls the mode of the touchpad, the number of keys, and the scan frequency. The default value is 0x8F.
Control Register 1 (Address 0x01): This register controls the key-press status output mode and the key-release status output mode. The default value is 0x0F.
Control Register 2 (Address 0x02): This register controls the pull-up resistor configuration for the touchpad inputs. The default value is 0x00.
Control Register 3 (Address 0x03): This register controls the key-press debounce time and the key-release debounce time. The default value is 0xFF.
Key Data Registers (Address 0x04 to 0x11): These registers store the status of the touchpad keys, with one bit per key. A high bit indicates that the corresponding key is pressed.
-tbillion
The text was updated successfully, but these errors were encountered:
This is a very old library I wrote long time ago when I just start playing with my first Arduino :)
The timeout you have added to IsTouch() is very good, also the IsAlive functionality!
I will keep this issue open so others can use your code.
When I have the time and hardware I will write a new version with your improvements and more 👍
firstly your library is great!
in my use case i have several different input methods and the ttp229 is one of them i needed a way at runtime to check and see if the keypad existed, and if it didnt to continue the program so i added the IsAlive(); function... and a register read command, these may be useful to some people and they may not. here is the code for anyone seeking this functionality ..
and finally an example to test functionality, if its hooked up right it will print to serial terminal that it is connected while this is running you can pull the ground connection from the ttp229 and it will say is not connected. if you reattach the ground it will say it is connected again, this may be helpful for anyone trying to diagnose connection issues as well ... just a thought
i hope this helps someone along the way ... if arduino12 wants to post a patch of the code that would be great too... i just didnt want to mess with the pull request.
heres some added information if anyone is looking for it
-tbillion
The text was updated successfully, but these errors were encountered: