PDA

View Full Version : 2.8 in ili9341_touch display code differences vs ili9341_t3 library



jshooks
06-27-2016, 12:45 AM
Just wondering if anyone has modified the Audio Workshop Tutorial Part 3-3:"Add a TFT Display" code to work with a 2.8 IN ILI9341_TOUCH display (sold by PJRC) use the touch screen functions. It seems the present code is just for the non-touch ILI9341_t3 display version. The following web link is not too clear about if there is specific libraries that use the extra five pins called T_CLK, T_CS, T_DIN, T_DG and T_IRQ (normally not used). I presume that without connecting these five pins, the touch functions do not work. Has anyone used this 14-pin display? If so, can you point me to the library that supports it? Much thanks!

defragster
06-27-2016, 01:10 AM
I have done the Audio tutorial with the PJRC board, the breadboard doesn't offer room for the touch pins as configured so I have not yet used that.

There is a touch library in the TeensyDuino install : XPT2046_Touchscreen. It is linked from the PJRC store and should work - though with a unique pin for the touch IRQ as #2 is used for the one button. If you search the forum I've left a thread and links to some github work I did on the XPT2046_Touchscreen, very cool device. The SPI pins will need to be adjusted for touch to map to those used by the ILI9341 and you'll need to arrange a unique pin for the Touch CS, but it should work. Note: external search like BING is sometimes/often the best way to find stuff in forum threads. Post back with your findings - that should get you started.

jshooks
01-10-2017, 09:13 PM
In the XPT2046_Touch_Example code on GitHub list the following pin definitions:

#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#include <Wire.h>
#include <ILI9341_t3.h>

// SPI COMMON assumed :: MOSI=11, MISO=12, SCK=13
#define CS_PIN 8
XPT2046_Touchscreen ts(CS_PIN);
#define TFT_CS 10
#define TFT_DC 9
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC);

However, the Part_3_03_TFT_Display that is part of the audio project files lists and does not use touch functions lists the following connections:

#define TFT_DC 20
#define TFT_CS 21
#define TFT_RST 255 // 255 = unused, connect to 3.3V
#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);ILI931_t3

If I redefine the SPI pins to the XPT2046_Touch_Example will there be any pin compatibilty issues between the two library used (i.e. XPT2046 vs. ILI9341_t3)? For example: CS_PIN 8 and TFT_CS 10 in the XPT2046 vs. TFT_CS 21 in the ILI9241_t3. Not sure if the libraries are expecting a specific pin number or if this is all dependant on the way you interconnect the Teensy and XPT2046 based on top level .ino configuration. Thanks much!

jshooks
01-22-2017, 05:24 AM
Hi Defrayster, I have referenced a long thread back in 2016 started by Duncabar inwhich he posted the same problem of not being able to play audio SD along with TFT 2.8 Touchscreen ili9241_t3 lib (see https://forum.pjrc.com/threads/34424-Teensy-3-2-with-Audio-Board-and-ILI9341-2-8-inch-Touch-Screen). Not sure if that is the same as the XPT2046 issue I am having, but maybe so. Anyway, several things were stated in the thread concerning the SPI mapping/connections and that is one thing I am still confussed about. In the original Tutorial Part 3-3: the TFT Display uses the MOSI=7, MISO=12 and SCLK=14. However, Duncabar change his usage of the Audio Board to the Wiz820 and used the normal MISO=12, MOSI=11 and SCK=13. So if I stay with Paul's original code using the audio board and ili9241/xpt2046 combination, do I stay with the original pin mapping?

Secondly, the partial code below starts to execute but gets stuck in the SD card while loop at the end. Do you see any reason for this? Is there a issue with the order in which the SD card is executed relative to the pin and labruary declarations? This code works with the exclusion of the XPT2036 references.

// Advanced Microcontroller-based Audio Workshop
// Commented out guitar code per defragster form post suggestion
// Part 3-1: Fourier Transform
//Last Change Date: 1/5/2016 Time: 14:13

#include <Bounce.h>
//#include "AudioSampleGuitar.h"
Bounce button0 = Bounce(0, 15);
Bounce button1 = Bounce(1, 15); // 15 = 15 ms debounce time
Bounce button2 = Bounce(3, 15);
//int ledPins[] = {6}; //Set LED for transmission indicator

///////////////////////////////////
// copy the Design Tool code here
///////////////////////////////////
#include <XPT2046_Touchscreen.h> //New
#include <ILI9341_t3.h>
#include <font_Arial.h> // from ILI9341_t3
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthWaveform waveform1; //xy=254,251
AudioPlaySdWav playSdWav1; //xy=257,155
AudioMixer4 mixer1; //xy=454,195
AudioOutputI2S i2s1; //xy=622,168
AudioAnalyzeFFT1024 fft1024_1; //xy=629,211
AudioConnection patchCord1(waveform1, 0, mixer1, 3); //Sine wave actuated by button 3
AudioConnection patchCord2(waveform1, 0, mixer1, 2); //Sine wave actuated by button 2 (random)
AudioConnection patchCord3(playSdWav1, 0, mixer1, 0); //SD card sterio input
AudioConnection patchCord4(playSdWav1, 1, mixer1, 1); //SD card sterio input
AudioConnection patchCord5(mixer1, 0, i2s1, 0);
AudioConnection patchCord6(mixer1, 0, i2s1, 1);
AudioConnection patchCord7(mixer1, fft1024_1);
AudioControlSGTL5000 sgtl5000_1; //xy=466,287
// GUItool: end automatically generated code

char freq[2]; //Create frequency bin #
char avalue[2]; //Create audio FP string array
int freqamp; //Create int variable for freq float
const int audioprc = 6; //Create processed output pin
int audval[32]; //Initialize audio int value array
int ch1audval = 0;
int ch2audval = 0;
int ch3audval = 0;
int ch4audval = 0;
int ch5audval = 0;
int ch6audval = 0;
int ch7audval = 0;
int ch8audval = 0;
int rcvflag = 0; //Set receive flag pin
int audioflag = 0; //Create audio received flag

// SPI COMMON assumed :: MOSI=11, MISO=12, SCK=13
//#define TFT_CS 10
//#define TFT_DC 9
#define TFT_DC 20
#define TFT_CS 21
#define TFT_RST 255 // 255 = unused, connect to 3.3V
#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
// set this to the hardware serial port you wish to use
#define SIZE 10 //Was 10
elapsedMillis msecs;
int height = 20; //Set mode button height
int Audmode = 0;
int Seqmode = 0;
int Ranmode = 0;
int Cnvmode = 0;
int Hltmode = 0;
int Spmode = 0;
//const int audioprc = 3; //Create processed output pin

#define CS_PIN 8
#define TIRQ_PIN 2
//XPT2046_Touchscreen ts(CS_PIN); // Param 2 - NULL - No interrupts
//XPT2046_Touchscreen ts(CS_PIN, 255); // Param 2 - 255 - No interrupts
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling

void setup() {
Serial.begin(9600); //SERIAL USB buad rate
while ( !Serial && (millis() < 3000));
pinMode(audioprc, OUTPUT); //Transmit flag
// delay(500);
ts.begin(); //Added for touch display
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setFont(Arial_10);
tft.setTextSize(3);
tft.setCursor(40, 8);
tft.println("LIGHTING CONTROL");
AudioMemory(10);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
SPI.setMOSI(7);
SPI.setSCK(14);
if (!(SD.begin(10))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}

defragster
01-22-2017, 07:50 AM
This posts shows (https://forum.pjrc.com/threads/34424-Teensy-3-2-with-Audio-Board-and-ILI9341-2-8-inch-Touch-Screen?p=104334&viewfull=1#post104334)the Audio card + TFT + (tft)SD working if I read right - when using the non_T3 adafruit version of the ILI9341 library. The earlier code was using the SD physically on the display before I got the audio tutorial hardware. Based on the posts where my stuff worked when others didn't - they had to use explicit pin declares in setup.

But this following post (https://forum.pjrc.com/threads/34424-Teensy-3-2-with-Audio-Board-and-ILI9341-2-8-inch-Touch-Screen?p=105328&viewfull=1#post105328)I claim to have played SD audio with the ILI9341 that was with #include <ILI9341_t3.h> code. That was using the Audio Tutorial base code extended for the TFT - set up on the breadboard solution from PJRC audio T_3.2 pre-assembled kit where I added display - I did it with both ILI9341 and smaller ILI9163C at that time. I'm not sure what the sketch I attached shows - a quick scan of the INO shows it doing this in setup():: playSdWav1.play("SDTEST1.WAV"); That sketch doesn't use Touch - but should work with IRQ on a usable pin ( audio hardware has a button on pin2 - I see that moved to #3 in sketch above)

If there is a conflict I didn't hit it as I used it - check wiring against that shown here store/audio_tutorial_kit.html (https://www.pjrc.com/store/audio_tutorial_kit.html) and in the PDF and code for the indicated sketch perhaps?

With the Audio adapter active pin 13 is owned by Audio Adapter for I2S IIRC and that is why SCK must be moved to pin 14.

Are you using a T_3.2? If you try my posted sketch and have trouble I can get back to that as the Audio tutorial board is still here.

jshooks
01-25-2017, 03:27 AM
I am using the Teensy 3.2, but not the pre-assemblied kit. I double checked the pin connections to the XPT2046/ILI9341 display including explicitly declaring it in the Setup loop (i.e. #define SC_PIN 8 and #define TIRQ_PIN 2 (in case that made a difference). I also checked for the playSdWav1.play("SDTEST1.WAV"); in setup and it is there (not shown in the partial code above). What I don't understand is why the above code gets stuck in the last part of the Setup loop in accessing the SD card just because the #include <XPT2046_touchscreen.h> was added:

if (!(SD.begin(10))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}

Is there any chance that adding the interrupt line for the XPT2046 is effecting the initialization of the SD card? As I said before, this does not happen if the #include <XPT2046_touchscreen.h> is not used. Has anyone actually used the touch display function with interrupts and the SD card as opposed to just the ILI9341? I supposed I could try to use polling instead, but it's not really what I need. The code I am using for the touch function is from the TouchTest example posted on either Github or the forum.pjrc (can't remember which one).

defragster
01-25-2017, 08:20 AM
I used the 2.4" non-touch so I didn't use the TIRQ_PIN when I was using audio board SD, but I got the idea from the linked posts that others did.

With audio tutorial breadboard spaced for non-touch would have to wire differently to use that board for the touch display - would use pin#3 for TIRQ_PIN and have to fit the other added SPI touch pins . . .

jshooks
01-31-2017, 04:31 AM
I tried switching the TIRQ pin from 2 to 3 as you suggested, but it didn't make any difference as far as the SD initialization being stuck in the setup routine. If I comment out all the XPT2046 library calls and code references for touch reading from the TouchTest example (see code below), the ILI9341 display part works fine, the SD card gets initialized and the music plays (Paul's FFT Peak Meter sketch). One thing I noticed is that the XPT2046 IRQ pin is contantly pulsing low when music is being played. I thought that was just when the touch screen was being pressed or reading the touch location. It appears like this pin is activiate even when writing to the ILI9341 display. That doesn't make much sense. This even happens during setup when the XPT2046_Touchscreen.h library is included and SD card is being initialized. I am wondering if this IRQ is interferring with the SD setup timing wise even though they are not using the same bus (i.e. SPI vs. I2C). Maybe I should try just polling the touch function instead of using the IRQ. Any suggestion on code for polling XPT2046?

#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#define CS_PIN 8
// MOSI=11, MISO=12, SCK=13

//XPT2046_Touchscreen ts(CS_PIN);
#define TIRQ_PIN 2 //Change to pin 3 as you suggested
//XPT2046_Touchscreen ts(CS_PIN); // Param 2 - NULL - No interrupts
//XPT2046_Touchscreen ts(CS_PIN, 255); // Param 2 - 255 - No interrupts
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN); // Param 2 - Touch IRQ Pin - interrupt enabled polling

void setup() {
Serial.begin(38400);
ts.begin();
while (!Serial && (millis() <= 1000));
}

void loopB() {
TS_Point p = ts.getPoint();
Serial.print("Pressure = ");
Serial.print(p.z);
if (ts.touched()) {
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
Serial.print("Pressure = ");
Serial.print(p.z);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
delay(30);
Serial.println();
}
}

jshooks
01-31-2017, 06:25 AM
Defragster,

I finally found the problem with the SD Card and XPT2046 touch. In setup, before the SD Card is initialized, you cannot write anything to the display. Otherwise, the interrupts producted by the XPT2046 will prevent the SD Card from completing the initialization. All write to the display most be after the following code.

if (!(SD.begin(10))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}

Frank B
01-31-2017, 06:28 AM
Do you have pullups attached to the display chipselect lines ?

defragster
01-31-2017, 06:41 AM
jshooks - that was a note to myself - needing to move T_IRQ if I tried that as the pre-made Audio board has the buttons on 0,1,2.

Are all CS pins using pullups (touch, sd, display, ...) as Frank notes? if they aren't held high - they won't stay inactive while other devices are being addressed. With CS held high the devices should be inactive unless the software pulls it down. Not having that could explain odd behavior.

KurtE
01-31-2017, 04:53 PM
Note: I have not seen any issues with the display CS/DC pins with the ILI9341 and I have never used PU resistors on these. And I don't think it is shown in the hook up guide: https://www.pjrc.com/store/display_ili9341_touch.html

The ILI9341_t3 library sets these pins to mode(2) which I believe will hold the high level except when asserted... But if there is an issue at initialization order. At the beginning of your setup code you could simply set these pins to output and set the value high... Then when the display init is called it will take it over...

So far I have not tried using the interrupts with the touch controller. I simply use the polling functions. Example of which with my ILI9341_t3n version of the library I have a Touch paint program that was converted to use this touch controller. https://github.com/KurtE/ILI9341_t3n/blob/master/examples/touchpaint_xpt2046/touchpaint_xpt2046.ino

Maybe at some point I will try using the interrupts to see what happens... But then again I am using these displays on a board with a T3.6, so normally use the built-in sd card

defragster
01-31-2017, 05:12 PM
When Paul updated OSHPark display Purple test board to Touch (https://www.oshpark.com/shared_projects/TG395I75): 10K pull-ups were part of that design, I never tried touch without them on any I set up.

KurtE
01-31-2017, 05:24 PM
Thanks defragster. Note to Paul. If there are cases where they are needed, maybe good to add to notes on Product page.

Thanks again.

Frank B
01-31-2017, 05:35 PM
In general, they are a good idea for every SPI-Slave.
They prevent floating chipselects _before_ initialization of the libraries, even _before_ setup() is run.
On Teensy and many other devices, this is a "long" time (>400ms, because of a delay(400) for USB).
They prevent that the slave interpret signals on the clock-lines as real clocks. The outputs on the master (aka Teensy) are floating too, or, maybe, the teensy initializes an other SPI device, incl. CS, first ( => wrong commands?)

It depends on the individual circumstances if the pullups are really needed, and they make sense for the startup only - but adding them makes your board more reliable.

I really don't know if missing pullups are the reason for jshooks's xpt-problems, but, as said, they are a good idea.

Frank B
01-31-2017, 06:05 PM
@Paul and all: Perhaps it would be better to initialize CS-Pins in every lib that uses SPI earlier - in the constructor - not in begin(), which is a bit late.

Michael Ward
01-31-2017, 07:13 PM
I got the touch working on a t3.6 with audio board. I found that the order of calling begin mattered. If I called ts.begin() before tft.begin() (for reasons I don't understand) no audio would be output, though the tft and touch appeared to be working.
The following code works for me. It's the audio tft tutorial slightly modified so that a touch on the screen causes it to randomly choose and then play a different file:

/************************************************** ************************************************** *********
* Advanced Microcontroller-based Audio Workshop
* Part 3-3: Add a TFT Display
* https://github.com/PaulStoffregen/AudioWorkshop2015/raw/master/workshop.pdf
* https://hackaday.io/project/8292-microcontroller-audio-workshop-had-supercon-2015
************************************************** ************************************************** *********/

#include <ILI9341_t3.h>
#include <XPT2046_Touchscreen.h>
#include <font_Arial.h> // from ILI9341_t3
#define TFT_DC 20
#define TFT_CS 21
#define TFT_RST 255 // 255 = unused, connect to 3.3V
#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12
#define BACKLIGHT 5
#define TS_CS 8 // Touchscreen CS
#define TS_TIRQ 39 // IRQ instead of polling...?
XPT2046_Touchscreen ts = XPT2046_Touchscreen(TS_CS, TS_TIRQ);
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
elapsedMillis sampleTime;

//------------------------------------------- GUItool output ------------------------------------------------+
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
Sd2Card card;
SdVolume volume;
SdFile root;
AudioPlaySdWav playSdWav1;
AudioAnalyzePeak peak2;
AudioAnalyzePeak peak1;
AudioOutputI2S i2s1;
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 0, peak1, 0);
AudioConnection patchCord3(playSdWav1, 1, i2s1, 1);
AudioConnection patchCord4(playSdWav1, 1, peak2, 0);
AudioControlSGTL5000 sgtl5000_1;

//=========================================== setup ================================================== ==+
void setup() {
randomSeed(analogRead(A10));
Serial.begin(230400);
while (!Serial && (millis() < 2000));
pinMode(BACKLIGHT, OUTPUT);
digitalWrite(BACKLIGHT, HIGH);
tft.begin();
ts.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_RED);
tft.setFont(Arial_16);
tft.setRotation(2);
tft.setCursor(63, 10);
tft.println("Peak Meter");
AudioMemory(12);
sgtl5000_1.enable();
sgtl5000_1.volume(0.3);
SPI.setMOSI(7);
SPI.setSCK(14);
if (!(SD.begin(BUILTIN_SDCARD))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
delay(500);
}

//=========================================== loop ================================================== ===+
void loop() {
const int8_t quantFiles = 4;
static int8_t wavToPlay = chooseFile(quantFiles);
boolean wasTouched = false;
if (ts.touched()) {
delay(75); // Debounce
wasTouched = true;
wavToPlay = chooseFile(quantFiles);
}
if (playSdWav1.isPlaying() && wasTouched) {
playSdWav1.stop();
wasTouched = false;
}
else if (playSdWav1.isPlaying() == false) {
if (wasTouched) wasTouched = false;
switch(wavToPlay) {
case 0:
playSdWav1.play("SDTEST1.WAV"); break;
case 1:
playSdWav1.play("SDTEST2.WAV"); break;
case 2:
playSdWav1.play("SDTEST3.WAV"); break;
case 3:
playSdWav1.play("SDTEST4.WAV"); break;
}
delay(10); // Wait for library to parse WAV info
}
if (sampleTime > 20) {
if (peak1.available() && peak2.available()) {
sampleTime = 0;
float leftPeak = peak1.read();
float rightPeak = peak2.read();
int height = leftPeak * 240; // Draw the vertical bars
tft.fillRect(60, 284 - height, 40, height, ILI9341_GREEN);
tft.fillRect(60, 284 - 240, 40, 240 - height, ILI9341_BLACK);
height = rightPeak * 240;
tft.fillRect(140, 284 - height, 40, height, ILI9341_GREEN);
tft.fillRect(140, 284 - 240, 40, 240 - height, ILI9341_BLACK); // ("Would be smarter to redraw only changed portion")
tft.setFont(Arial_14); // Draw numbers underneath each bar
tft.setTextColor(ILI9341_BLACK);
tft.fillRect(60, 284, 40, 16, ILI9341_GREEN);
tft.setCursor(60, 284);
tft.print(leftPeak);
tft.fillRect(140, 284, 40, 16, ILI9341_GREEN);
tft.setCursor(140, 284);
tft.print(rightPeak);
}
}
}
//------------------------------------------- chooseFile ----------------------------------------------------+
int8_t chooseFile(int8_t quantFiles) {
static int8_t lastFile = random(quantFiles);
int8_t choice = random(quantFiles);
if (choice == lastFile) chooseFile(quantFiles);
else {
lastFile = choice;
return choice;
}
}


best,
Michael

edit: Works without adding pullup resistors

jshooks
01-31-2017, 09:47 PM
Michael,

Wow, I must have moved the ts.begin in my setup and the audio music stop playing. Moved it back after tft.begin and it works! It must be that touch should only be initialized after ILI9341 and SD card are started. I guess to find out why, you would have to use a logic analyzer to trace code execution.

Michael Ward
01-31-2017, 10:20 PM
For me, as you can see in my code, initializing both tft and touch before the SD worked fine. (I did set the alternate SPI data and clock lines before the SD.begin.) Note that my posted example uses the SD slot on the T3.6 (SD.begin(BUILTIN_SDCARD)), but I also tried it successfully using the audio board's slot (SD.begin(10)).

--Michael

edit: I did get hung up quite a while before I noticed the superfluous SD library in my sketchbook lib folder and deleted it.

jshooks
02-01-2017, 06:03 AM
When you say "alternate SPI data and clock lines" you talking about the following, right?

#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12

That is what I have always used. Of course, on the XPT2046 you have to jumper these over to the extra touch pins on the display. I am displaying five buttons and am having a little trouble with the display touch resolution X,Y position being constant values. I probably need to make the buttons a little bigger for touch point range correlation (i.e. have to use a stylist to make it work). Do you find your delay(75) enough time for the debouncing of the touch?

defragster
02-01-2017, 06:28 AM
When you say "alternate SPI data and clock lines" you talking about the following, right?

#define TFT_MOSI 7
#define TFT_SCLK 14
#define TFT_MISO 12

That is what I have always used. Of course, on the XPT2046 you have to jumper these over to the extra touch pins on the display. I am displaying five buttons and am having a little trouble with the display touch resolution X,Y position being constant values. I probably need to make the buttons a little bigger for touch point range correlation (i.e. have to use a stylist to make it work). Do you find your delay(75) enough time for the debouncing of the touch?

The alternate pins noted are set with these lines as I see it:

SPI.setMOSI(7);
SPI.setSCK(14);
if (!(SD.begin(BUILTIN_SDCARD))) {

How does the TouchPaint demo work with drawing a line and touching the color buttons? The raw touch values X&Y needs to be mapped to TFT display pixels - AFAIK that sketch should work unless the touch hardware has changed. I posted some samples of buttons in use on the XPT2046 (https://forum.pjrc.com/threads/31634-ILI9341-and-XPT2046-for-Teensy-Touchscreen-320x240-display?highlight=XPT2046)and had a map function for any orientation wasn't included when I had IRQ code PULL'ed in - Though KurtE did something similar recently

Michael Ward
02-01-2017, 03:07 PM
Yes, what defragster said about alternate lines. The "non-alternates" are the default pins that are the same for Arduino Uno etc.
As for my debounce delay--that's not an ideal way to do it. It's just good enough for my simple application, where a brief hiccup is not noticeable between file switches.
--Michael

edit: I played a little with the touchpoint demo and it worked. I recommend checking out defragster's examples re mapping.

KurtE
02-01-2017, 03:24 PM
The alternate pins noted are set with these lines as I see it:


How does the TouchPaint demo work with drawing a line and touching the color buttons? The raw touch values X&Y needs to be mapped to TFT display pixels - AFAIK that sketch should work unless the touch hardware has changed. I posted some samples of buttons in use on the XPT2046 (https://forum.pjrc.com/threads/31634-ILI9341-and-XPT2046-for-Teensy-Touchscreen-320x240-display?highlight=XPT2046)and had a map function for any orientation wasn't included when I had IRQ code PULL'ed in - Though KurtE did something similar recently
Yep - I have it in my Example program touchpaint for the XPT... It is up at: https://github.com/KurtE/ILI9341_t3n/blob/master/examples/touchpaint_xpt2046/touchpaint_xpt2046.ino

It might be good to standardize on what are the valid ranges for the touch and come up with a set of mapping functions that works correctly for several of the displays...

Yesterday I tried turning on the interrupt code of the touch driver and it appeared to work ok on my board which has it going to pin 3.

It is also interesting that I am not sure what all triggers the IO line, but I know that if I simply add something like:
pinMode(3, INPUT);
You will see lots of transisitions going on when/if your code calls functions like:
TS_Point p = ts.getPoint();

But it appeared to work fine where you do not get a transition on that pin when the interrupt code is configured (unless again you call the functions like ts.getPoint();

But with it on in the Touch paint code, the call I make at the start of the function:

// See if there's any touch data for us
if (ts.bufferEmpty()) {
return;
}
the return will be taken and nothing happens on the IO pin until you touch the screen. At which point again you will see lots of transitions on the IO pin until you are no longer touching the display and stop calling ts.getpoint...

defragster
02-01-2017, 04:05 PM
Of course it works :) If you read Paul's Caveat ( if still posted ) - testing interrupt is great for trouble - the int fires on each touch call made during touch. My solution is just an enable flag set on int - and removed after touch goes away - ended up at like 20 lines of code with header stuff. So you can attempt to poll all day long and will be quickly ignored saving SPI traffic - unless there was a prior int.

defragster
02-01-2017, 07:17 PM
As far as debounce - I had something I found to be effective in my linked sample (https://github.com/Defragster/XPT2046_Touch_Examples/blob/master/onoffbuttonMark3/ButtonMap.cpp#L62). There was no delay - but it only takes a button touch if two samples are seen as the same (pixel and time) within 3 pixels as coded. And falling out of that I detected what was needed for a slider button not just a toggle. That code got to functional in my sample - but not pretty as I got pulled off to other things.

Follow up to my quick post #24 above - is there a benefit to detaching the interrupt after it was detected and then re-attachinterrupt when the touch is detected to have stopped? I assumed that would add as much overhead as letting this code just get hit on each command issued during touch - but never tested it { ...\hardware\teensy\avr\libraries\XPT2046_Touchscr een\XPT2046_Touchscreen.cpp }:


void isrPin( void )
{
XPT2046_Touchscreen *o = isrPinptr;
o->isrWake = true;
}

It would be easy to detach there when setting true, and then do the attach here when set false and should be otherwise unaffected:


if (z < Z_THRESHOLD_INT) { // if ( !touched ) {
if (255 != tirqPin) isrWake = false;
}

jshooks
02-03-2017, 06:22 PM
Thanks for the interrupt idea. I think that will help to reduce any erratic response to the touch function. On another note, I am having trouble with the original external button input function of Paul's Audio Workshop (Part_03_02_Fourier_Transform) listed below.

button0.update();
button1.update();
button2.update();

// Left button starts playing a new song
if (button0.fallingEdge()) {
mixer1.gain(2, 0.0);
mixer1.gain(3, 0.0);
fileNumber = fileNumber + 1;
if (fileNumber >= 4) fileNumber = 0;
playMem1.stop();
playSdWav1.play(filenames[fileNumber]);
mixer1.gain(0, 0.5);
mixer1.gain(1, 0.5);
}

I am now transferring these mode inputs to the XPT2046 display touch mode buttons with different variable names (i.e. button0 = audmode, button1 = seqmode, etc.). The only renamed mode that does not work is button0 with executing "playSdWav1.play(filenames[fileNumber]);". What I am wondering is if the SD.h or audio.h library expects the specific "button0" variable to be passed instead of my new variable name "audmode". If not, maybe it has more to do with the button0.update(); function as it is not being used due to no external switch input. Any ideas on this?

defragster
02-03-2017, 07:30 PM
That looks like the existing code - triggering only when button0.fallingEdge is detected - How does the replacement code look?

jshooks
02-03-2017, 08:05 PM
The new code is shown below with no reference to the button0.update(); requirement, just Audmode display button going low.

// Audio button starts playing a new song
if (Audmode == (0)) {
mixer1.gain(2, 0.0);
mixer1.gain(3, 0.0);
fileNumber = fileNumber + 1;
if (fileNumber >= 4) fileNumber = 0;
// playMem1.stop();
playSdWav1.play(filenames[fileNumber]);
mixer1.gain(0, 0.5);
mixer1.gain(1, 0.5);
} //End automode sampling

The following code was used from the TouchTest example:

void loopB() {
TS_Point p = ts.getPoint();
Serial.print("Pressure = ");
Serial.print(p.z);
if (ts.touched()) {
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
}
Serial.println();
// delay(100);
delay(30);
}

The Audmode is set low in the touch screen code qualified by x and y as follows (main loop):

//Check for mode touch
if (ts.touched()) {
TS_Point p = ts.getPoint(); //Get touch points
Serial.print("Pressure = "); //Print position, pressure
Serial.print(p.z);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
delay(30);
Serial.println();

// draw the verticle mode buttons and detect mode selection
//void fillRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color);

if (2500 < p.x && p.x < 3650) { //If touch is on mode button line
tft.fillRect(5, 200 - height, 20, 290, ILI9341_BLACK);
tft.fillRect(45, 200 - height, 20, 290, ILI9341_BLACK);
tft.fillRect(85, 200 - height, 20, 290, ILI9341_BLACK);
tft.fillRect(125, 200 - height, 20, 290, ILI9341_BLACK);
tft.fillRect(165, 200 - height, 20, 290, ILI9341_BLACK);
tft.fillRect(205, 280 - height, 20, height, ILI9341_WHITE);

if (3050 < p.y && p.y < 3700){ //Set audio mode button 1 if in range
tft.fillRect(5, 290 - height, 20, height, ILI9341_WHITE);
Audmode = 0;
Seqmode = 1;
Ranmode = 1;
Cnvmode = 1;
Hltmode = 1;
Serial.print("Audio mode=");
Serial.println(Audmode);
// delay(200);
}
}

KurtE
02-03-2017, 08:25 PM
Where is Audmode being set?

jshooks
02-03-2017, 09:03 PM
In the last portion of the code above, the line "Audmode = 0;" The other modes are set to a "1" (off).

KurtE
02-03-2017, 10:11 PM
Side comment: Sure wish code fragments were in code tags so you can better see the flow ...

Not sure what you are asking? Are you asking how to process the touch information, such that if you click on a logical button, it only is processed once?

So it looks like if you get any touch event that has the x,y within some rectangle than it turns that variable on, not sure where it gets turned off and not sure how/when it stops....

I have not yet done any touch input for my current stuff, (well monitor and also probably robot control), but I have in the past, but was using Adafruit version of display with the different touch sensor. Also the last time I did it was on Intel Edison using Eclipse... But will soon.

What my buttons typically did, was to detect the touch, and track the touch. If the touch is inside an object, such as a button, I will show the button in it's depressed state (could change color, or highlight changes.. or?), and only if when I release the touch I am still inside of the object, will I then process the command for the button.

I also used to have some things like sliders and the like, that if I clicked on it and dragged, it will track my movement, but if I moved too far outside of the object in the other direction it would release the object...

But again not sure what you are asking.

KurtE
02-04-2017, 12:28 AM
Not sure if it is interesting or not, but I hacked up an old test program that I would probably rewrite, but it had some buttons and slider like objects, and I hacked it up to use the XPT... controller. It is a multi-tab sketch. At the time I was working on maybe abstracting away which display... So the code is convoluted.
But it sort of works... Nothing is hooked up to buttons here... Except I think the UP/Down buttons sort of go to new displays...
This code was originally some stuff I was doing for my Teensy DIY XBEE remote control, where I have real sliders, keypad, 3 axis joysticks...
9591


The file display.h has the defines for which pins I am using on my display and touch...

Again it ain't much. I am sure there are much better libraries available

jshooks
02-04-2017, 01:46 AM
Sorry about the code fragments. I have to figure out how to create code tags on this form. Guess I haven't read enough of the procures. I will take a look at your code example. To answer your two questions relative to my code, they are:

Question - Not sure what you are asking? Are you asking how to process the touch information, such that if you click on a logical button, it only is processed once? Answer - I was just using a delay after selection, but your idea about waiting until the touch is released before executing the selections.

Question - So it looks like if you get any touch event that has the x,y within some rectangle than it turns that variable on, not sure where it gets turned off and not sure how/when it stops.... Answer - The selection detected turns off all other non-selected modes. That works okay.

The original issue was just trying to figure out why the audio mode did not get selected like the other modes and I thought it might have something to do with the SD card library interaction with the button0 variable instead of the new Audmode. But if executing the line "playSdWav1.play(filenames[fileNumber]);" in the original code should play the SD card music, then it must be something else. I'll keep searching the code for an answer.

Turns out I am going to use XBEE also because of the net relay feature so thanks for the example. I'm sure I have a lot to learn on that as far as formating command/data packets.

Thanks much for your help.

defragster
02-04-2017, 01:50 AM
Code Tags: the '#' hash icon or wrap like this ( I hit the comment cloud and change QUOTE to CODE on both ends ):
9593