Davidelvig
Well-known member
This code simplifies the problem as much as I can.
It need to use two SPI peripherals:
While it's unlikely you have the latter "in stock"...
...can anyone identify where I'm going wrong using these libraries
Paul, you had a post here instructing to use SPI.usingInterrupt() before attachInterrupt().
I don't see any of the libraries using SPI.usingInterrupt(), though the touch library uses attachInterrupt().
So I tried adding SPI.usingInterrupt() in this test code... to no avail.
Here's the code:
It need to use two SPI peripherals:
Teensy Touch Display: here
- HP Pressure Sensor: here
While it's unlikely you have the latter "in stock"...
...can anyone identify where I'm going wrong using these libraries
#include <SPI.h>
#include <ILI9341_t3.h>
#include <XPT2046_Touchscreen.h>
#include <ILI9341_t3.h>
#include <XPT2046_Touchscreen.h>
Paul, you had a post here instructing to use SPI.usingInterrupt() before attachInterrupt().
I don't see any of the libraries using SPI.usingInterrupt(), though the touch library uses attachInterrupt().
So I tried adding SPI.usingInterrupt() in this test code... to no avail.
Here's the code:
Code:
#include <SPI.h>
#include <ILI9341_t3.h>
#include <XPT2046_Touchscreen.h>
// pressure sensor uses SPISettings(800000, MSBFIRST, SPI_MODE3) below
// TFT uses SPISettings(clock, MSBFIRST, SPI_MODE0) in ILI9341_t3.h where clock is 2000000 for reads and 30000000 for writes
// touchscreen uses SPISettings(2000000, MSBFIRST, SPI_MODE0) in XPT2046_Touchscreen.cpp
#define TOUCH_CS_PIN 8
#define TOUCH_IRQ_PIN 2
#define TFT_CS_PIN 10
#define TFT_DC_PIN 9
#define PRESSURE_CS_PIN 7
ILI9341_t3 tft = ILI9341_t3(TFT_CS_PIN, TFT_DC_PIN);
//#define USE_TOUCH_IRQ // <== uncomment this line to demonstrate failure - where pressure input is intermittently scrambled
#ifdef USE_TOUCH_IRQ
XPT2046_Touchscreen ts(TOUCH_CS_PIN, TOUCH_IRQ_PIN);
#else // USE_TOUCH_IRQ
XPT2046_Touchscreen ts(TOUCH_CS_PIN);
#endif // USE_TOUCH_IRQ
void setup() {
Serial.begin(9600);
while (!Serial && (millis() <= 1000));
pressureBegin(PRESSURE_CS_PIN);
tft.begin();
tft.setRotation( 0 );
Serial.printf("TFT started with:\nTFT_CS_PIN: %2d\nTFT_DC_PIN: %2d\n", TFT_CS_PIN, TFT_DC_PIN);
if (ts.begin()) {
ts.setRotation( 2 );
Serial.printf("Touchscreen started with:\nTOUCH_CS_PIN: %2d\n", TOUCH_CS_PIN);
#ifdef USE_TOUCH_IRQ // based on https://forum.pjrc.com/threads/31634-ILI9341-and-XPT2046-for-Teensy-Touchscreen-320x240-display/page3?highlight=usingInterrupt%28
SPI.usingInterrupt(TOUCH_IRQ_PIN); // <== where should this line go? I've tried it here, and in between lines 39 & 40 of XPT2046_Touchscreen.cpp
Serial.printf("Touchscreen using IRQ on:\nTOUCH_IRQ_PIN: %2d\n", TOUCH_IRQ_PIN);
#endif // USE_TOUCH_IRQ
}
else {
Serial.println("Unable to start touchscreen.");
}
}
TS_Point p;
int x = 0, lastX = 0,
y = 0, lastY = 0,
pressure = 0, lastPressure = 0;
void loop() {
pressure = readPressure();
if (ts.tirqTouched()) { // Note this should always be true if no irq defined
if (ts.touched()) {
getTouch();
}
}
if ((x != lastX) ||
(y != lastY) ||
(abs(pressure - lastPressure) > 50)) { // the pressure transducer will vary by as much as 50 from read to read. this last condition minimizes reporting jitter
drawScreen();
Serial.printf("readPressure(): %d\t", pressure);
Serial.printf("getTouch(): %d/%d\t", x, y);
Serial.printf("drawScreen(): %d/%d\t%d\n", x, y, pressure);
lastPressure = pressure;
lastX = x;
lastY = y;
}
}
void pressureBegin(int csPin) {
pinMode (csPin, OUTPUT);
digitalWrite(csPin, HIGH);
SPI.begin();
}
uint16_t readPressure(void) { // Using Honeywell's ABPMANN060KGSA3 (60mBar, SPI, 3v)
uint16_t _p;
byte a, b, c, d, s;
SPI.beginTransaction(SPISettings(800000, MSBFIRST, SPI_MODE3));
digitalWrite(PRESSURE_CS_PIN, LOW);
a = SPI.transfer(0);
b = SPI.transfer(0);
c = SPI.transfer(0);
d = SPI.transfer(0);
s = a >> 6;
if (s > 0) {
Serial.printf("Error: pressure transducer returns %d (binary %d%d) and should return 0.\n",
s, s >> 1, s & 0b00000001);
Serial.printf("a: %d\t(%d : %d)\nb: %d\nc: %d\nd: %d\n", a, a>>6, a & 0b00111111, b, c, d);
delay(500);
}
_p = ((a & 0b00111111) << 6) + b; // 6 bits of byte 1, plus all bits of byte 2.
digitalWrite(PRESSURE_CS_PIN, HIGH);
SPI.endTransaction();
return (_p);
}
// calibration for my particular touchscreen
#define MIN_TS_X 350
#define MIN_TS_Y 310
#define MAX_TS_X 3820
#define MAX_TS_Y 3750
#define TFT_WIDTH 240
#define TFT_HEIGHT 320
void getTouch(void) {
p = ts.getPoint();
x = map(constrainInt(p.x, MIN_TS_X, MAX_TS_X),
MIN_TS_X,
MAX_TS_X,
0,
TFT_WIDTH);
y = map(constrainInt(p.y, MIN_TS_Y, MAX_TS_Y),
MIN_TS_Y,
MAX_TS_Y,
0,
TFT_HEIGHT);
}
int constrainInt(int val, int low, int high) {
if (val < low) { return(low); }
else if (val > high) { return(high); }
else { return(val); }
}
#define COLOR_BLACK 0x0000
#define COLOR_WHITE 0xFFFF
void drawScreen(void) {
tft.fillScreen(COLOR_BLACK);
tft.setTextColor(COLOR_WHITE, COLOR_BLACK);
tft.setTextSize(2);
tft.setCursor(x, y);
tft.print(x);
tft.print("/");
tft.print(y);
tft.print("/");
tft.print(pressure);
}