Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for Teensy & improve performance on all boards #10

Merged
merged 3 commits into from
Dec 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 52 additions & 12 deletions TouchScreen.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// (c) ladyada / adafruit
// Code under MIT License

#include "Arduino.h"
#include "pins_arduino.h"
#include "wiring_private.h"

#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
Expand Down Expand Up @@ -57,16 +58,37 @@ TSPoint TouchScreen::getPoint(void) {
int samples[NUMSAMPLES];
uint8_t i, valid;

#if defined(ARDUINO_ARCH_SAM)
Pio* xp_port = digitalPinToPort(_xp);
Pio* yp_port = digitalPinToPort(_yp);
Pio* xm_port = digitalPinToPort(_xm);
Pio* ym_port = digitalPinToPort(_ym);

uint32_t xp_pin = digitalPinToBitMask(_xp);
uint32_t yp_pin = digitalPinToBitMask(_yp);
uint32_t xm_pin = digitalPinToBitMask(_xm);
uint32_t ym_pin = digitalPinToBitMask(_ym);
#elif defined(ARDUINO_ARCH_SAMD)
PortGroup* xp_port = digitalPinToPort(_xp);
PortGroup* yp_port = digitalPinToPort(_yp);
PortGroup* xm_port = digitalPinToPort(_xm);
PortGroup* ym_port = digitalPinToPort(_ym);

uint32_t xp_pin = digitalPinToBitMask(_xp);
uint32_t yp_pin = digitalPinToBitMask(_yp);
uint32_t xm_pin = digitalPinToBitMask(_xm);
uint32_t ym_pin = digitalPinToBitMask(_ym);
#else
uint8_t xp_port = digitalPinToPort(_xp);
uint8_t yp_port = digitalPinToPort(_yp);
uint8_t xm_port = digitalPinToPort(_xm);
uint8_t ym_port = digitalPinToPort(_ym);

uint8_t xp_port = digitalPinToPort(_xp);
uint8_t yp_port = digitalPinToPort(_yp);
uint8_t xm_port = digitalPinToPort(_xm);
uint8_t ym_port = digitalPinToPort(_ym);

uint8_t xp_pin = digitalPinToBitMask(_xp);
uint8_t yp_pin = digitalPinToBitMask(_yp);
uint8_t xm_pin = digitalPinToBitMask(_xm);
uint8_t ym_pin = digitalPinToBitMask(_ym);
uint8_t xp_pin = digitalPinToBitMask(_xp);
uint8_t yp_pin = digitalPinToBitMask(_yp);
uint8_t xm_pin = digitalPinToBitMask(_xm);
uint8_t ym_pin = digitalPinToBitMask(_ym);
#endif


valid = 1;
Expand All @@ -86,14 +108,23 @@ TSPoint TouchScreen::getPoint(void) {
*portOutputRegister(xp_port) |= xp_pin;
*portOutputRegister(xm_port) &= ~xm_pin;

#ifdef __arm__
delayMicroseconds(20); // Fast ARM chips need to allow voltages to settle
#endif
for (i=0; i<NUMSAMPLES; i++) {
samples[i] = analogRead(_yp);
}
#if NUMSAMPLES > 2
insert_sort(samples, NUMSAMPLES);
#endif
#if NUMSAMPLES == 2
if (samples[0] != samples[1]) { valid = 0; }
// 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
}
#endif
x = (1023-samples[NUMSAMPLES/2]);

Expand All @@ -107,6 +138,9 @@ TSPoint TouchScreen::getPoint(void) {
//digitalWrite(_yp, HIGH);
pinMode(_ym, OUTPUT);

#ifdef __arm__
delayMicroseconds(20); // Fast ARM chips need to allow voltages to settle
#endif
for (i=0; i<NUMSAMPLES; i++) {
samples[i] = analogRead(_xm);
}
Expand All @@ -115,7 +149,13 @@ TSPoint TouchScreen::getPoint(void) {
insert_sort(samples, NUMSAMPLES);
#endif
#if NUMSAMPLES == 2
if (samples[0] != samples[1]) { valid = 0; }
// 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
}
#endif

y = (1023-samples[NUMSAMPLES/2]);
Expand Down
Empty file modified TouchScreen.h
100644 → 100755
Empty file.