Include Header File: "Touch_Pad_Class.h"
Constructor: Touch_Pad_Class (SCLK,SDA)
Public Methods : void begin( )
int key( )
---------------------------
Abstract: This page describes converting an existing C program to read a capacitive serial touch keypad into a C++ class.
Possible Audience: Hardware enthusiasts interested in touch key pad and programmers converting to C++ code.
Keywords: XP4602, Touch Keypad, TTP229, C++ Class, C++ Constructor
Required Hardware: Touch Pad Keyboard Part XC4602 from Jaycar. For testing a ESP8266 with OLED was used. A simple Arduino with Serial Monitor could also be used.
Required Software: Arduino IDE with ESP8266 extensions. For testing OLED library.
------------------------------
I need to build a WiFi throttle for a model railway. I choose an ESP8266 with an OLED display. The photo shows the device as used in a previous project.
To select various options and control the model train a keypad was included in the design. A typical 4 x 4 matrix keyboard required too many pins so a serial capacitive keyboard, the XC4602 was chosen.
This keypad has many options. To select the 16 bit mode as opposed to the default 8 bit mode a jumper/link must be made as illustrated in red in the photo.
Initially I was unable to get the XC4602 to work so I went back to the default 8 bit mode and the sample code supplied by Jaycar for an Arduino and worked from there**. See page Touch_Pad_Test.
** In my testing I tried to observe all the signals using a Mixed Signal Oscilloscope. In the default mode a probe on one of the output pins gave the desired signal. Leaving that probe in place for the 16 bit mode not only showed no signal (as expected) but it also caused the XP4602 to fail. I cannot explain why this occurs but it was a major cause of my problems.
The first step is to copy the source code developed in the page Touch_Pad_Test..**
In the Arduino IDE Tools Menu select the Lilin(Wemos)D1 mini Lite++ and change the "Port" as required.
** In my example I have wired the clock to pin D8 and the data to pin D7. There were no other changes
++If the mini Lite is not available Go to Tools>Boards>Boards Manager and type 'esp' in the search box
Install ESP8266 by ESP8266 Community. (Button on lower right) This is about 150MB download and can take a while.
The program was executed and displayed the correct results on the serial monitor. The waveforms for key 11 were:
An important note here is that no OUT pin of the XC4602 was probed. As found in the page Touch_Pad_Test this caused the keypad to fail.
As shown when a key is pressed the DV (Data Valid) signal pulses LOW for approximately 93uSec**. The software waits until it returns HIGH and then waits a further 10 uSec and puts out a sequence of 16 clock pulses. As shown the total elapsed time is approximately 140 uSec.++
Since in the example key 11 was pressed the SDO (Serial Data Out) goes LOW on the 11th pulse. This will be read by the software.
In moving from the NANO to the faster ESP8266 the only issue might have been the clock speed. Expanding the trace gave the clock frequency as 400 kHz that is within the specifications**.
** Specifications TTP229 Timing
Parameter Min. Typ. Max. Unit
DV - 93 - us
Tw 10 - - us
Tout - 2 - ms
T_resp(for 16-KEYS) - 32 - ms
T_resp(for 8-KEYS) - 16 - ms
F_SCL 1K - 512K Hz
++ The software includes a further delay of 3mSec to account for Tout.
The sample code is a single C code file. With C++ there will be 3 files:
1. The application or client code. This will be the minimum code required by a programmer to use the class or library.
2. The implementation file/code that will be written once by the developer and will include all the implementation details.
3. The header file that gives the end user a passage into the implementation code.
In this example the files will be given the same names Touch_Pad_Class but different extensions
In the Arduino IDE create a new file and give it the label Touch_Pad_Class. The IDE will give it the extension *.ino.
In the above routine an object/instance touchkey of the class Touch_Pad_Class is created. This object will be attached to the pins D8 and D7. The object will need to have a method key( ) that will return the value of the key pressed. It will also have a method begin( ) that will perform any initialisations on the touch pad.
As shown the application code is very simple: an include file, the constructor, the begin( ) method and reading the key.
For testing purposes the results may be either displayed on the serial monitor via the object Serial or on the OLED via the object display of class OLED. The header file OLED.h must be included as part of the code. See page OLED. In this case the full application code will be:
The next steps will be to implement the header and implementation files.
The "bare-bones" header file that includes all the actions specified in the application file will be
The outline for the implementation file will be:
Note with the given files the project will compile. The next task is to complete the details.
The first step is to hide all variables that could accidently be accessed from outside the class. In this example these are the two parameters passed to the constructor. ie sclpin and sdopin. Typically the new hidden or private variables are given the same label starting with an underscore. The additional code becomes:
In the header file:
In the implementation file:
This step will basically involve copying the code from the previous setup( ) function into the new begin( ) method but modifying the variable sclpin and sdopin. The new method becomes:
The code is basically as the original code except that the value of the key is returned**.
The keypad class should now be operational.
** If no key is pressed the return value is "0" .
To save the library for future projects:
(i) Make a *.zip file of the Touch_Pad_Class.cpp and Touch_Pad_Class.h files. By default it will have the label Touch_Pad_Class.zip
(ii) In the Arduino IDE select sketch--> Include Library --> Add *.ZIP Libray.
(iii) Select Touch_Pad_class.zip. It will now be added to your libraries. See Files -->Preferences --> Sketch book location.
(iv) To use the library in future projects manually use "#include <Touch_Pad_Class.h" or use Sketch-->Include Library and select from contributed list.
This page has illustrated taking a working C program and generating a C++ class or library. To use the class requires 4 lines: the header file, the constructor, the initialisation or begin and the read key operation.
To use the Touch_Pad_Class include the following:
Include file: "Touch_Pad_Class.h"
Constructor: Touch_Pad_Class (SCLK,SDA)
Public Methods: void begin( )
int key( )
Included in the key( ) implementation is a 3 microsecond delay. This could be eliminated if in the user code there were other actions that took at least 3 micro seconds. That is
--------------------------------------