Compare commits

...

2 commits

Author SHA1 Message Date
ladyada
26d562785f don't rerun calib every time 2023-10-21 14:51:48 -04:00
ladyada
683fabbcae compiles and runs (but appcode is wrong) 2023-10-21 13:37:51 -04:00
4 changed files with 70 additions and 35 deletions

View file

@ -1,44 +1,50 @@
#include "TouchControllerWS.h"
#if defined(TOUCH_CS)
TouchControllerWS::TouchControllerWS(Adafruit_STMPE610 *touchScreen) {
#else
TouchControllerWS::TouchControllerWS(Adafruit_TSC2007 *touchScreen) {
#endif
this->touchScreen = touchScreen;
}
bool TouchControllerWS::loadCalibration() {
// always use this to "mount" the filesystem
bool result = SPIFFS.begin();
Serial.println("SPIFFS opened: " + result);
// this opens the file "f.txt" in read-mode
Serial.print("SPIFFS opened: ");
Serial.println(result);
if (!result) return false;
// this opens the file "calibration.txt" in read-mode
File f = SPIFFS.open("/calibration.txt", "r");
if (!f) {
return false;
} else {
//Lets read line by line from the file
String dxStr = f.readStringUntil('\n');
String dyStr = f.readStringUntil('\n');
String axStr = f.readStringUntil('\n');
String ayStr = f.readStringUntil('\n');
dx = dxStr.toFloat();
dy = dyStr.toFloat();
ax = axStr.toInt();
ay = ayStr.toInt();
}
f.close();
//Lets read line by line from the file
String dxStr = f.readStringUntil('\n');
String dyStr = f.readStringUntil('\n');
String axStr = f.readStringUntil('\n');
String ayStr = f.readStringUntil('\n');
dx = dxStr.toFloat();
dy = dyStr.toFloat();
ax = axStr.toInt();
ay = ayStr.toInt();
f.close();
return true;
}
bool TouchControllerWS::saveCalibration() {
bool result = SPIFFS.begin();
if (!result) return false;
// open the file in write mode
File f = SPIFFS.open("/calibration.txt", "w");
if (!f) {
Serial.println("file creation failed");
return false;
}
// now write two lines in key/value style with end-of-line characters
f.println(dx);
@ -47,6 +53,7 @@ bool TouchControllerWS::saveCalibration() {
f.println(ay);
f.close();
return true;
}
void TouchControllerWS::startCalibration(CalibrationCallback *calibrationCallback) {
@ -59,7 +66,7 @@ void TouchControllerWS::continueCalibration() {
if (state == 0) {
(*calibrationCallback)(10, 10);
if (touchScreen->touched()) {
if (isTouched()) {
p1 = p;
state++;
lastStateChange = millis();
@ -67,7 +74,7 @@ void TouchControllerWS::continueCalibration() {
} else if (state == 1) {
(*calibrationCallback)(230, 310);
if (touchScreen->touched() && (millis() - lastStateChange > 1000)) {
if (isTouched() && (millis() - lastStateChange > 1000)) {
p2 = p;
state++;
@ -85,16 +92,23 @@ bool TouchControllerWS::isCalibrationFinished() {
}
bool TouchControllerWS::isTouched() {
touchScreen->touched();
#if defined(TOUCH_CS)
return touchScreen->touched();
#else
TS_Point p = touchScreen->getPoint();
return (p.z > 50);
#endif
}
bool TouchControllerWS::isTouched(int16_t debounceMillis) {
if (touchScreen->touched() && millis() - lastTouched > debounceMillis) {
if (isTouched() && millis() - lastTouched > debounceMillis) {
lastTouched = millis();
return true;
}
return false;
}
TS_Point TouchControllerWS::getPoint() {
TS_Point p = touchScreen->getPoint();
int x = (p.y - ax) * dx;

View file

@ -1,14 +1,23 @@
#include <FS.h>
#include <Adafruit_STMPE610.h>
#ifndef _TOUCH_CONTROLLERWSH_
#define _TOUCH_CONTROLLERWSH_
#ifdef TOUCH_CS
#include <Adafruit_STMPE610.h>
#else // use I2C
#include <Adafruit_TSC2007.h>
#endif
typedef void (*CalibrationCallback)(int16_t x, int16_t y);
class TouchControllerWS {
public:
#if defined(TOUCH_CS)
TouchControllerWS(Adafruit_STMPE610 *touchScreen);
#else
TouchControllerWS(Adafruit_TSC2007 *touchScreen);
#endif
bool loadCalibration();
bool saveCalibration();
void startCalibration(CalibrationCallback *callback);
@ -19,7 +28,11 @@ class TouchControllerWS {
TS_Point getPoint();
private:
#if defined(TOUCH_CS)
Adafruit_STMPE610 *touchScreen;
#else
Adafruit_TSC2007 *touchScreen;
#endif
float dx = 0.0;
float dy = 0.0;
int ax = 0;

View file

@ -27,9 +27,7 @@ See more at https://blog.squix.org
#include <Arduino.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <Adafruit_STMPE610.h>
#include "TouchControllerWS.h"
#include "TouchControllerWS.h"
/***
* Install the following libraries through Arduino Library Manager
@ -83,7 +81,11 @@ MiniGrafx gfx = MiniGrafx(&tft, BITS_PER_PIXEL, palette);
Carousel carousel(&gfx, 0, 0, 240, 100);
#ifdef HAVE_TOUCHPAD
#ifdef TOUCH_CS
Adafruit_STMPE610 ts(TOUCH_CS);
#else
Adafruit_TSC2007 ts = Adafruit_TSC2007();
#endif
TouchControllerWS touchController(&ts);
void calibrationCallback(int16_t x, int16_t y);
@ -157,25 +159,29 @@ void setup() {
// The LED pin needs to set HIGH
// Use this pin to save energy
// Turn on the background LED
#ifdef TFT_LED
Serial.println(TFT_LED);
pinMode(TFT_LED, OUTPUT);
digitalWrite(TFT_LED, HIGH); // HIGH to Turn on;
#endif
delay(100);
gfx.init();
gfx.fillBuffer(MINI_BLACK);
gfx.commit();
connectWifi();
#ifdef HAVE_TOUCHPAD
Serial.println("Initializing touch screen...");
if (!ts.begin()) {
if (!ts.begin(0x48, &Wire)) {
Serial.println("Couldn't start touchscreen controller");
while (1);
}
#endif
connectWifi();
Serial.println("Mounting file system...");
bool isFSMounted = SPIFFS.begin();
if (!isFSMounted) {
@ -186,7 +192,9 @@ void setup() {
drawProgress(100,"Formatting done");
#ifdef HAVE_TOUCHPAD
SPIFFS.remove("/calibration.txt");
// uncomment to re-run calibration
//SPIFFS.remove("/calibration.txt");
boolean isCalibrationAvailable = touchController.loadCalibration();
if (!isCalibrationAvailable) {
Serial.println("Calibration not available");

View file

@ -21,9 +21,9 @@ See more at http://blog.squix.ch
#include <simpleDSTadjust.h>
// Setup
#define WIFI_SSID "the_ssid"
#define WIFI_PASS "your_password"
#define WIFI_HOSTNAME "ThingPulse-weather-station-color"
#define WIFI_SSID "my-ssid"
#define WIFI_PASS "my-password"
#define WIFI_HOSTNAME "ThingPulseWeather"
const int UPDATE_INTERVAL_SECS = 15 * 60; // Update every 10 minutes
const int SLEEP_INTERVAL_SECS = 0; // Going to Sleep after idle times, set 0 for dont sleep
@ -33,11 +33,11 @@ const int SLEEP_INTERVAL_SECS = 0; // Going to Sleep after idle times, set 0 f
// Pins for the ILI9341
#define TFT_DC 15
#define TFT_CS 0
#define TFT_LED 5
//#define TFT_LED 5 // pins 4 and 5 are used for I2C so we can't have that plus touchpad
#define HAVE_TOUCHPAD
#define TOUCH_CS 16
//#define TOUCH_IRQ 4
//#define TOUCH_CS 16
//#define TOUCH_IRQ 16