Skip to content

modified code to check if touch pad is connected... #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tbillion opened this issue Mar 9, 2023 · 1 comment
Open

modified code to check if touch pad is connected... #3

tbillion opened this issue Mar 9, 2023 · 1 comment

Comments

@tbillion
Copy link

tbillion commented Mar 9, 2023

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 ..

#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif

#ifndef TTP229_h
#define TTP229_h

#define TTP229_CTRL_REG0 0x00
#define TTP229_CTRL_REG1 0x01
#define TTP229_CTRL_REG2 0x02
#define TTP229_CTRL_REG3 0x03

#define TTP229_CTRL_REG0_DEFAULT 0x8F
#define TTP229_CTRL_REG1_DEFAULT 0x0F
#define TTP229_CTRL_REG2_DEFAULT 0x00
#define TTP229_CTRL_REG3_DEFAULT 0xFF

#define TTP229_KEY_REG_START_ADDR 0x04
#define TTP229_KEY_REG_END_ADDR 0x11


class TTP229
{
	public:
		TTP229(uint8_t sclPin, uint8_t sdoPin);
		uint8_t
			ReadKey8(),
			GetKey8(),
			ReadKeys8(),
			GetKeys8(),
			ReadKey16(),
			GetKey16();
			
		uint16_t
			ReadKeys16(),
			GetKeys16();
		bool
			IsAlive();
			
	private:
		void
			WaitForTouch(),
			Key8(),
			Keys8(),
			Key16(),
			Keys16();
		bool
			IsTouch(),
			GetBit();
		uint8_t
			ReadRegister(uint8_t regAddr);
			
		uint8_t _sclPin, _sdoPin;
		uint8_t _key8, _keys8, _key16;
		uint16_t _keys16;
};

#endif


#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

@arduino12
Copy link
Owner

Hi,

Thanks @tbillion !

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants