OK, so I took working example from Arduino UNO. I’ve cut off everything from library that I don’t need and from rest I have created function in simple sketch. On Arduino UNO it is working perfectly and now it is not relying on any external library. But …. there is always some but at the end … I’m getting compilation errors.
sketch:
// Touch screen library with X Y and Z (pressure) readings as well
// as oversampling to avoid 'bouncing'
// This demo code returns raw readings, public domain
//for STM32
#define YP PB3 // must be an analog pin, use "An" notation!
#define XM PB4 // must be an analog pin, use "An" notation!
#define YM PB5 // can be a digital pin
#define XP PB6 // can be a digital pin
//for arduino
//#define YP A2 // must be an analog pin, use "An" notation!
//#define XM A3 // must be an analog pin, use "An" notation!
//#define YM 8 // can be a digital pin
//#define XP 9 // can be a digital pin
#define pressureThreshhold 20
#define platform 1 //1 for arduino, 2 for STM32
void setup(void) {
Serial.begin(115200);
}
void loop(void) {
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms (rxplate) across the X plate
getPoint_new(XP, YP, XM, YM, 300);
}
void getPoint_new(byte xp, byte yp, byte xm, byte ym, int rxplate) {
int x, y, z;
int samples[2];
byte i, valid;
byte xp_port = digitalPinToPort(xp);
byte yp_port = digitalPinToPort(yp);
byte xm_port = digitalPinToPort(xm);
byte ym_port = digitalPinToPort(ym);
byte xp_pin = digitalPinToBitMask(xp);
byte yp_pin = digitalPinToBitMask(yp);
byte xm_pin = digitalPinToBitMask(xm);
byte ym_pin = digitalPinToBitMask(ym);
valid = 1;
pinMode(yp, INPUT);
pinMode(ym, INPUT);
*portOutputRegister(yp_port) &= ~yp_pin;
*portOutputRegister(ym_port) &= ~ym_pin;
pinMode(xp, OUTPUT);
pinMode(xm, OUTPUT);
*portOutputRegister(xp_port) |= xp_pin;
*portOutputRegister(xm_port) &= ~xm_pin;
delayMicroseconds(20); // Fast ARM chips need to allow voltages to settle
for (i=0; i<2; i++) {
samples[i] = analogRead(yp);
}
// Allow small amount of measurement noise, because capacitive
// coupling to a TFT display's signals can induce some noise.
if (samples[0] - samples[1] < -4 || samples[0] - samples[1] > 4) {
valid = 0;
} else {
samples[1] = (samples[0] + samples[1]) >> 1; // average 2 samples
}
x = (1023-samples[1]);
pinMode(xp, INPUT);
pinMode(xm, INPUT);
*portOutputRegister(xp_port) &= ~xp_pin;
pinMode(yp, OUTPUT);
*portOutputRegister(yp_port) |= yp_pin;
pinMode(ym, OUTPUT);
delayMicroseconds(20); // Fast ARM chips need to allow voltages to settle
for (i=0; i<2; i++) {
samples[i] = analogRead(xm);
}
// Allow small amount of measurement noise, because capacitive
// coupling to a TFT display's signals can induce some noise.
if (samples[0] - samples[1] < -4 || samples[0] - samples[1] > 4) {
valid = 0;
} else {
samples[1] = (samples[0] + samples[1]) >> 1; // average 2 samples
}
y = (1023-samples[1]);
// Set X+ to ground
pinMode(xp, OUTPUT);
*portOutputRegister(xp_port) &= ~xp_pin;
// Set Y- to VCC
*portOutputRegister(ym_port) |= ym_pin;
// Hi-Z X- and Y+
*portOutputRegister(yp_port) &= ~yp_pin;
pinMode(yp, INPUT);
int z1 = analogRead(xm);
int z2 = analogRead(yp);
if (rxplate != 0) {
// now read the x
float rtouch;
rtouch = z2;
rtouch /= z1;
rtouch -= 1;
rtouch *= x;
rtouch *= rxplate;
rtouch /= 1024;
z = rtouch;
} else {
z = (1023-(z2-z1));
}
if (! valid) {
z = 0;
}
if (z > pressureThreshhold && z < 1000){
Serial.print("X = "); Serial.print(x);
Serial.print("\tY = "); Serial.print(y);
Serial.print("\tPressure = "); Serial.println(z);
}
}