DIY Remote XBee - Going to Teensy 3.1

I was trying to run the UTouch demo. I can get the screen display but no touch response. I think I’m misunderstanding which pins to use.

// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield		  : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield		  :  6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due	  :  6, 5, 4, 3, 2
// Teensy 3.x TFT Test Board			    : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//*********************************************
// UTouch myTouch(TCLK, TCS, TDIN, TDOUT, IRQ);
//TCLK:  Pin for Touch Clock (D_CLK)
//TCS:   Pin for Touch Chip Select (D_CS)
//TDIN:  Pin for Touch Data Input (D_DIN)
//TDOUT: Pin for Touch Data Output (D_OUT) 
//IRQ:   Pin for Touch IRQ (DPenirq)
//*********************************************
UTouch  myTouch( 15,8,6,7,2);

I Got the data from the manual on the UTFT Website

Note: I have an updated version of Adafruit_STMPE610 library which I put up on github
( github.com/KurtE/Adafruit_STMPE610 )
If you look at the shield page: adafruit.com/product/1651
You will see that:

The touch display uses the standard SPI pins (11-13) and it uses pin 8 for the Chip select.

I believe the archived sketch I included here is the one that was working on mine… But the important things is probably simply the
following 2 lines:

#define STMPE_CS 8 Adafruit_STMPE610 touch = Adafruit_STMPE610(STMPE_CS);
Hope that helps
Kurt
TouchTest-140521a.zip (1.34 KB)

Sorry if I didn’t explain properly. The Adafruit_STMPE610 library works fine. I was using the UTouch library from henningkarlsen.com/electronics/library.php?id=55
Its an add-on to the UTFT library
using the default settings for ArduinoUTFT myGLCD(ILI9341_S5P,11,13,10,0,9); // Remember to change the model parameter to suit your display module! UTouch myTouch(15,10,14,9,8; I get garbled display


I found its because of Pin 9 being assign above in myGLCD. When I change the code to read UTouch myTouch(15,8,14,12,10); I get the normal display but no touch response.

I must be assigning the pins incorrectly. the only pin I comfortable with is pin 8 as the chip select pin. But as I read the data sheet I’m not that sure anymore

// UTouch myTouch(TCLK, TCS, TDIN, TDOUT, IRQ);
//TCLK: Pin for Touch Clock (D_CLK)
//TCS: Pin for Touch Chip Select (D_CS)
//TDIN: Pin for Touch Data Input (D_DIN)
//TDOUT: Pin for Touch Data Output (D_OUT)
//IRQ: Pin for Touch IRQ (DPenirq)
//*********************************************

The problem is that these libraries are not compatible with each other (yet). Currently when installed on Arm including Teensy, the read and write to the SPI is done by bit twiddling. i.e.

[code]void UTouch::touch_WriteData(byte data)
{
byte temp;

temp=data;
cbi(P_CLK, B_CLK);

for(byte count=0; count<8; count++)
{
if(temp & 0x80)
digitalWrite(T_DIN, HIGH);
else
digitalWrite(T_DIN, LOW);
temp = temp << 1;
digitalWrite(T_CLK, LOW);
digitalWrite(T_CLK, HIGH);
}
}

word UTouch::touch_ReadData()
{
word data = 0;

for(byte count=0; count<12; count++)
{
data <<= 1;
digitalWrite(T_CLK, HIGH);
digitalWrite(T_CLK, LOW);
if (digitalRead(T_DOUT))
data++;
}
return(data);
}

[/code]
But the display code is using the hardware SPI. As the CS pin in this case pin 8 is not one of the IO pins that can be directly controlled by the hardware SPI CS processing (2=10, 9=6, 20=23, 21=22, 15), note the ones with = are alternative pins, so only for most part only one of the two can be used, unless you do fancy footwork. Note: the ILI9341 driver is using the high speed SPI where the SPI hardware itself controls the CS and DC signals for the display and as part and as such it updates the Pin MUX for the SPI pins to mode 2 (SPI). But the bitblitting code assumes it is in mode 1… So one will stomp on the other…

But code can be udated to work like what I did the STMPE library, where if the MISO, MOSI, SCLK are the right pins for the hardware, it can use the standard hardware SPI library. Which would get you part way there. BUT then you run into the next issue that I had with my test program. Both the ILI9341 driver and the SPI library both muck with some of the SPI registers. Details about these registers are in the datasheet for the processor (pjrc.com/teensy/K20P64M72SF1RM.pdf)
So in my test program, I had to detect when I was switching gears between the two SPI devices and restore registers. There are still parts of this I wish to figure out better. Also wondering about how some of this code is setup. Will probably ask some questions up on Teensy forum.

Note: Our problem about multiple devices conflicting is not unique. In fact Paul (PJRC - Teensy), is working on some ideas on this, along with some of the main Arduino people. Hopefully they will come up with a standardized way to deal with some of this.

If I get a chance maybe I will muck up the UTouch library the same way I did with the other one and also hack up some calls to do the mode switching and see if I can get their example to work.

Kurt

Also: Looking at their library, they are assuming that you have the Interrupt pin of the STMPE chip connected, which looks like is a possibility with the Adafruit WIKI learn.adafruit.com/adafruit-2-8 … -touch-irq

That is, would need to do solder connection on the back of the shield to connect it up to Pin 7. I am thinking I may not do that, but instead, update their code that if the Interrupt is -1, to ask through SPI for the data…

Note: I am also wondering about punting here… That is there is no reason can not simply use the STMPE library to do this. May hack up a new version of the UTouch_Buttontest that uses the other library. Still have to do the SPI register save/restore code base but would need to do this regardless…

Kurt

P.S. - I wonder if more of this conversation would be better up on Teensy forum.

Quick update: on the UTouch… I was wrong, actually both of these libraries. UTFT/UTouch both use software bit whacking code to talk to the display (i.e. slow method).

The one thing that was at first screwing me up is the Utouch library, the parameters for the constructor for input(DIN) and output(DOUT). I noticed in the init code they set they: did pinMode(din, OUTPUT)…

So I now have:

UTFT myGLCD(ILI9341_S5P,11,13,10,0,9); // Remember to change the model parameter to suit your display module! UTouch myTouch( 13, 8, 11, 12, 7);

The buttons now display properly, but I don’t think I am getting valid inputs from the touch yet.

Not sure if I will try hooking up Pin 7 of my display, or see about hacking it to do query for queue empty instead of trying to use interrupt pin…

I too really dont want to connect pin 7. As you mentioned earlier,if the the UTFT library would work together with the Adafruit_STMPE610 library. I was using the original UTFT and Adafruit_STMPE610 library and I have been able to get some
touch response. by selecting a color block. I’m getting a bit confused setting the individual block parameters.
I was able to do this with a PIC before by using youtube.com/watch?v=jTYpGIAb5E4

[code]
if(x >= X_POSITION && x <= X_POSITION ) // To select the row
{
if(y >= Y_POSITION && Y <= X_POSITION ) // to select the col
{
tft.drawRect(Block code); outline selected color block}

}
[/code] Will post it later when i get home

Sounds good. FYI - I believe that the utouch library is specific to some of the displays mentioned through the web pointer you mentioned and my quick check at one of them, I believe that they may be using a different touch controller XPT2046, which does not look like it has the same interface as the STMPE controller on the adafruit. Could go a variety of ways here.

  1. Punt - The other libraries I already have working are using the faster communications. But it does not have the better fonts…

  2. I think there is another version of the gfx library that does run faster.

  3. Update UTFT/UTouch -
    a) Speed up the libraries, to a minimum of SPI library.
    b) Build a version of UTouch interface to talk to the STMPE chip

  4. add option to STMPE library to run in slow motion… May try that anyway…

All for now
Kurt

Edit: 4) was already in place, just had to specify the actual pins. However clock did not work properly. I made a simple mod to the library and updated it on my github account.
Sample program converted over to STMPE library is starting to work. Included in case you want to look:
UTouch_STMPE_Button_Test-140609a.zip (2.33 KB)

Kurt This is using the first libraries you posted seems to get some good response SCREEN_TEST.zip (1.36 KB)

Yep - as I mentioned I am having good luck with my two libraries, except as your test case shows (on my machine), that if you use both at the same time, the display does not work… Will hack your up and make it work again, by remembering some of the SPI library registers and restoring them at the right time for the two hardware SPI’s

Kurt

Edit: I played with your program, to get it to work with the high speed version of my ILI9341 code so I hacked in some functions to restore SPI (and pin) configuration…
See what you think.
Philtkp_Screen_Test-140610a.zip (1.81 KB)

Very nice speed increase

Thanks, may want to try one additional save/restore of registers, that may improve speed and that is the MCR register which controls how the SPI works… Will try that out in a little bit.

Kurt

Edit: I updated the save/restore functions to save and restore the SPI MCR. Could upload new version or simply update the code at the end:

[code]//---------------------------------------------------------
// Some hacks for switching SPI registers
uint8_t g_icurSPIR = 0xff; // Set to something that won’t be
uint32_t g_aluSPI0_CTAR0[SPIR_CNT];
uint32_t g_alsuSPI0_PIN10_CONFIG[SPIR_CNT]; // pin 10 CS gets mucked with…
uint32_t g_aluSPI0_MCR[SPIR_CNT];

void SaveSPIRegs(uint8_t spir) {
g_aluSPI0_MCR[spir] = SPI0_MCR;
g_aluSPI0_CTAR0[spir] = SPI0_CTAR0;
g_alsuSPI0_PIN10_CONFIG[spir] = CORE_PIN10_CONFIG;
}

void RestoreSPIRegs(uint8_t spir) {
if (g_icurSPIR != spir) {
// Need to wait until all outputs are done…
// Wait until hardware queue is empty…
uint16_t wTimeout = 0xffff;
while (((SPI0.SR) & (15 << 12)) && (–wTimeout)) ; // wait until empty

// May want to wait until actual output is done? also may need to turn off SPI before change?
// But for now simply try restoring the register(s)
SPI0.MCR = SPI_MCR_MDIS | SPI_MCR_HALT;
SPI0_CTAR0 = g_aluSPI0_CTAR0[spir];
SPI0_MCR = g_aluSPI0_MCR[spir];
CORE_PIN10_CONFIG = g_alsuSPI0_PIN10_CONFIG[spir];
g_icurSPIR = spir;
}
}
//-----------------------------------------------------------
[/code]

Works like a charm. how would you go about adding say like pages that you can flip back and forth. from this example I added a right arrow box that would give you another screen.

Philtkp_Screen_Test-140610a.zip (1.81 KB)
Guess i’ll figure that out tomorrow

My other program (Teensy_Adafruit_Touch_Test) has examples of doing this when you press the Up/Dn buttons…

Not doing it as well as it could, Probably should update to only redraw the screen areas that are changing…

Not sure if you have played around with some of the Adafruit_ILI9341 examples or other examples that use the SD library, This was failing for me and while I did not need it, that bugged me, so today I finally tracked it down. Put details up on a thread on PJRC forum (Teensy), but the fix is simple:

The change is in the file /libraries/SD/Utility/Sd2Card.cpp
Look for the function: static void spiInit(uint8_t spiRate)

Look at the switch statement: switch (spiRate/2)

The /2 made no sense as the comments above talked about the different rates.

So simply change that line to: switch (spiRate)

And now the example SD example CardInfo works, also tried out the Adafruit_ILI9341 example program spitftbitmap also works, assuming you have the appropriate bitmap file…

Kurt

Kurt,
Been trying to flip between screens, but cant seem to find what I’m doing wrong.

Philtkp_Screen_Test2.rar (2.09 KB)

Not sure exactly what your test program is supposed to do between the Main and 2nd Screen code base. But I think the main issue, is some of the code organization. That is your main loop function, is simply waiting for a touch event and then it always calls off to MainScreen() to do the processing of the touch event.

My guess is you may want to do something like, break up some of the functions, maybe have some functions like: DisplayMainPage(), Display2ndPage() and then maybe some functions like: ProcessMainPageTouchEvent, Process2ndPageTouchEvent, and have your main loop have some form of flag to know which one to call.

Alternatively create a C++ object, which has some methods, like: Draw, ProcessTouch… Create an instance for each of the implementations, then have a current Object, which you call through the virtual pointers…

Hope that makes sense.

KUrt

I hacked up a version of your program to break up the displaying from the processing of the touch events. I am not going to say it is 100% there as there are some issues with displaying the “next” button when switching pages that sometimes will display with different colors… Mainly because you have pending touch events when this happens… But I think it is working OK…

For myself I may play some more with my earlier code base (Teensy_Adafruit_Touch_Test) and strip it down again. That is, I would like to make it more object orientated. In that test program, I have a header file OLEDSGC.h for examples defines a display object, which includes a rectangle, plus virtual functions to draw the object, plus process touch events. Then a display can be configured to simply contain a collection of these objects. Currently I have a simple button defined as well as a simple slider defined using this. Could extend this further, to for example create a container class, and the like, where for example could define multiple top level screens, some screens, example which may be broken up into sub regions. Example top level buttons. Could define main overlay regions, where most of the secondary stuff is displayed…

Or could look to see if another library has been created that does the job… I know a few years ago, there was another one for the 4D systems displays, but the links to it no longer work, but I think there is still some available from his site: embeddedcomputing.weebly.com/software.html I am not sure if he has made any of it work with Teensy 3.1 yet. I have seen him up on PJRC forums, so maybe. Also don’t think he has this display…

Kurt
Philtkp_Screen_Test2.ino (8.61 KB)

I knew it was something I was doing wrong. I’m not familiar with C++. I can do some C and Basic. I guess its time to brush up on some C++. any good books you know of? Thanks for the code update i’ll read through it.

Sorry, not sure of what C++ book to recommend. Maybe others have a good idea. There are lots of tutorials and the like up the web like: cplusplus.com/doc/tutorial/

For the most part I keep it to the simple stuff, with simple class inheritance, with maybe a few virtual methods,
which is more or less along the same line as what is used in Arduino in other areas.

For example: the hardware serial classes like Serial1, inherit from the Stream class, which itself inherits from the Print.

Both the Stream and Print classes have virtual functions which in most of them are these cases are pure virtual (no base implementation), so the sub-classes must implement them.
Example in Print class: virtual size_t write(uint8_t b) = 0;

Print also has at least one virtual function which is not pure: example:
virtual size_t write(const uint8_t *buffer, size_t size);
The sub-classes can simply use the default implementation, which simply loop size times calling off to write one byte at a time to the pure virtual write method mentioned earlier.

What I try to avoid is stuff like multiple class inheritance and the like, as then you need to get into issues, like dynamic casting of classes, which gets into lots of complications like potentially needing run time class information to be able to cast…

Can describe more if you like or can play more with my earlier objects: DISPOBJ, BUTTON, SLIDER… Note: my current stuff is pretty primitive. I don’t think I have many classes showing the constructor or distructor…

Kurt

I have been playing around with a slight diversion, which I am not sure I will use for anything or not…

A friend sent me a link to youtube:

So I thought I would try it out, since I had an extra kepad sitting around plus some resisters and breadboard.
Keypad-test.jpg
In the Schematic that the person in the video used, he used 4 diodes. Not sure why he needed them, but I skipped using them.
I am using resistor values(0, 1K, 2.2K, 3.3K, 0, 220, 470, 680) and 1.5K… I checked what voltages and analogRead values that I should get, versus, what values I am
actually getting. They are in the ball park…

I ran into a problem with my Teensy 3.1 breakout board with Arduino headers. For some reason the 3.3v circuit is giving me something like 4.5v which can screw things up. Not sure if I have a short, wrong Voltage regulator, something wrong in my design… But on this one I hacked off the VR and then jumpered from the 3.3v output of the Teensy to the 3.3v circuit, which works. But then have limited 3.3v to use. Which in my case is not an issue here.

Anyway after that got values that worked. I then was hacking around with a simple app (included). Note: I did not do any fancy math like using log function to map the analogRead values to an index, instead, I used simple check ranges… Works most of the time. May need a tweak or two if I were to seriously use…

Again not sure if I will use any of this as on the Teensy I have lots of IO pins to use. But might be interesting if one wanted to instead using something like an
Atmega 32u4 based processor like the Arduino Leonardo, or the Sparkfun 32u4 breakout board, as you would still have a limited number of IO pins. But the
Leonardo gives you 12 analog pins, so still could do all of the analog stuff, could use the one Usart for XBee and use SPI to talk to the TFT display… Would be a good setup… But again currently I am happy with Teensy!

Kurt
Test_Keypad_1IO-140703a.zip (966 Bytes)