Skip to content

Commit 9c708c2

Browse files
Add re-calibration and navigation (#150)
- Allow user to force a screen re-calibration - Break up the screen into 4 sections for navigation Co-authored-by: Marcel Stör <[email protected]>
1 parent cff1060 commit 9c708c2

File tree

2 files changed

+89
-30
lines changed

2 files changed

+89
-30
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ ThingPulse runs a support forum for its customers that is better suited to answe
3434
## Wiring
3535

3636
The [wiring diagram](https://docs.thingpulse.com/specs/wifi-color-display-kit/#wiring) is only needed when you do _not_ buy the self-contained kit from ThingPulse but rather assemble the components yourself. The kit provides a custom PCB that solidly connects microcontroller and display.
37+
38+
## Operation
39+
40+
See ["Operation" in the official documentation](https://docs.thingpulse.com/guides/wifi-color-display-kit/#operation).

esp8266-weather-station-color.ino

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ uint16_t palette[] = {ILI9341_BLACK, // 0
6969
0x7E3C
7070
}; //3
7171

72-
int SCREEN_WIDTH = 240;
73-
int SCREEN_HEIGHT = 320;
7472
// Limited to 4 colors due to memory constraints
7573
int BITS_PER_PIXEL = 2; // 2^2 = 4 colors
7674

@@ -122,7 +120,11 @@ int frameCount = 3;
122120
int screenCount = 5;
123121
long lastDownloadUpdate = millis();
124122

125-
uint16_t screen = 0;
123+
uint8_t screen = 0;
124+
// divide screen into 4 quadrants "< top", "> bottom", " < middle "," > middle "
125+
uint16_t dividerTop, dividerBottom, dividerMiddle;
126+
uint8_t changeScreen(TS_Point p, uint8_t screen);
127+
126128
long timerPress;
127129
bool canBtnPress;
128130
bool sleep_mode();
@@ -189,8 +191,6 @@ void setup() {
189191
gfx.fillBuffer(MINI_BLACK);
190192
gfx.commit();
191193

192-
connectWifi();
193-
194194
Serial.println("Initializing touch screen...");
195195
ts.begin();
196196

@@ -202,9 +202,31 @@ void setup() {
202202
SPIFFS.format();
203203
}
204204
drawProgress(100, "Formatting done");
205+
206+
/* Allow user to force a screen re-calibration */
207+
gfx.fillBuffer(MINI_BLACK);
208+
gfx.drawString(120, 160, F("Press and hold\nto initiate touch screen\ncalibration"));
209+
gfx.commit();
210+
delay(3000);
211+
yield();
205212
boolean isCalibrationAvailable = touchController.loadCalibration();
213+
if(ts.touched()) {
214+
isCalibrationAvailable = false;
215+
gfx.fillBuffer(MINI_YELLOW);
216+
gfx.drawString(120, 160, F("Calibration initiated\nnow release screen"));
217+
gfx.commit();
218+
219+
// Wait for release otherwise touch becomes first calibration point
220+
while(ts.touched()) {
221+
delay(10);
222+
yield();
223+
}
224+
delay(100); // debounce
225+
touchController.getPoint(); // throw away last point
226+
}
227+
206228
if (!isCalibrationAvailable) {
207-
Serial.println("Calibration not available");
229+
Serial.println("Calibration data not available or force calibration initiated");
208230
touchController.startCalibration(&calibration);
209231
while (!touchController.isCalibrationFinished()) {
210232
gfx.fillBuffer(0);
@@ -218,6 +240,12 @@ void setup() {
218240
touchController.saveCalibration();
219241
}
220242

243+
dividerTop = 64;
244+
dividerBottom = gfx.getHeight() - dividerTop;
245+
dividerMiddle = gfx.getWidth() / 2;
246+
247+
connectWifi();
248+
221249
carousel.setFrames(frames, frameCount);
222250
carousel.disableAllIndicators();
223251

@@ -236,19 +264,20 @@ TS_Point points[10];
236264
uint8_t currentTouchPoint = 0;
237265

238266
void loop() {
239-
static bool asleep = false; // asleep used to stop screen change after touch for wake-up
267+
static bool asleep = false; // asleep used to stop screen change after touch for wake-up
240268
gfx.fillBuffer(MINI_BLACK);
241-
if (touchController.isTouched(0)) {
269+
270+
/* Break up the screen into 4 sections a touch in section:
271+
* - Top changes the time format
272+
* - Left back one page
273+
* - Right forward one page
274+
* - Bottom jump to page 0
275+
*/
276+
if (touchController.isTouched(500)) {
242277
TS_Point p = touchController.getPoint();
243278
timerPress = millis();
244-
245-
Serial.printf("Touch point detected at %d/%d.\n", p.x, p.y);
246-
if (!asleep) { // no need to change screens;
247-
if (p.y < 80) {
248-
IS_STYLE_12HR = !IS_STYLE_12HR;
249-
} else {
250-
screen = (screen + 1) % screenCount;
251-
}
279+
if (!asleep) { // no need to update or change screens;
280+
screen = changeScreen(p, screen);
252281
}
253282
} // isTouched()
254283

@@ -604,14 +633,12 @@ void drawForecastTable(uint8_t start) {
604633
gfx.setTextAlignment(TEXT_ALIGN_CENTER);
605634
time_t time = forecasts[i].observationTime;
606635
struct tm * timeinfo = localtime (&time);
607-
// Added 24hr / 12hr conversion //
608-
if(IS_STYLE_12HR){
636+
if (IS_STYLE_12HR) {
609637
gfx.drawString(120, y - 15, WDAY_NAMES[timeinfo->tm_wday] + " " + String(make12_24(timeinfo->tm_hour)));
610638
} else {
611639
gfx.drawString(120, y - 15, WDAY_NAMES[timeinfo->tm_wday] + " " + String(timeinfo->tm_hour) + ":00");
612640
}
613641

614-
615642
gfx.drawPalettedBitmapFromPgm(0, 5 + y, getMiniMeteoconIconFromProgmem(forecasts[i].icon));
616643
gfx.setTextAlignment(TEXT_ALIGN_LEFT);
617644
gfx.setColor(MINI_YELLOW);
@@ -690,33 +717,32 @@ void calibrationCallback(int16_t x, int16_t y) {
690717
gfx.fillCircle(x, y, 10);
691718
}
692719

693-
// Added 24hr / 12hr conversion //
694720
String getTime(time_t *timestamp) {
695721
struct tm *timeInfo = localtime(timestamp);
696722

697-
//char buf[6];
698723
char buf[9]; // "12:34 pm\0"
699-
char ampm[3]; ampm[0]='\0'; //Ready for 24hr clock
724+
char ampm[3];
725+
ampm[0]='\0'; //Ready for 24hr clock
700726
uint8_t hour = timeInfo->tm_hour;
701727

702-
if(IS_STYLE_12HR){
703-
if(hour > 12){
728+
if (IS_STYLE_12HR) {
729+
if (hour > 12) {
704730
hour = hour - 12;
705-
sprintf(ampm,"pm");
731+
sprintf(ampm, "pm");
706732
} else {
707-
sprintf(ampm,"am");
733+
sprintf(ampm, "am");
708734
}
709735
sprintf(buf, "%2d:%02d %s", hour, timeInfo->tm_min, ampm);
710-
} else {
736+
} else {
711737
sprintf(buf, "%02d:%02d %s", hour, timeInfo->tm_min, ampm);
712-
}
738+
}
713739
return String(buf);
714740
}
715741

716742
/*
717-
* Convert hour from 24 hr time to 12 hr time
718-
* @return cString with 2 digit hour + am or pm
743+
* Convert hour from 24 hr time to 12 hr time.
719744
*
745+
* @return cString with 2 digit hour + am or pm
720746
*/
721747
char* make12_24(int hour){
722748
static char hr[6];
@@ -780,3 +806,32 @@ void loadPropertiesFromSpiffs() {
780806
Serial.println("SPIFFS mount failed.");
781807
}
782808
}
809+
810+
/*
811+
* Change screen based on touchpoint location.
812+
*/
813+
uint8_t changeScreen(TS_Point p, uint8_t screen) {
814+
uint8_t page = screen;
815+
816+
// Serial.printf("Touch point detected at %d/%d.\n", p.x, p.y);
817+
// From the screen's point of view commented values for the 240 X 320 touch screen
818+
// if (p.y < dividerTop) Serial.print(" top "); // < 80
819+
// if (p.y > dividerBottom) Serial.print(" bottom "); // > 240
820+
// if (p.x > dividerMiddle) Serial.print(" left "); // > 120
821+
// if (p.x <= dividerMiddle) Serial.print(" right "); // <= 120
822+
// Serial.println();
823+
824+
if (p.y < dividerTop) { // top -> change 12/24h style
825+
IS_STYLE_12HR = !IS_STYLE_12HR;
826+
} else if (p.y > dividerBottom) { // bottom -> go to screen 0
827+
page = 0;
828+
} else if (p.x > dividerMiddle) { // left -> previous page
829+
if (page == 0) { // Note type is unsigned
830+
page = screenCount; // Last screen is max -1
831+
}
832+
page--;
833+
} else { // right -> next screen
834+
page = (page + 1) % screenCount;
835+
}
836+
return page;
837+
}

0 commit comments

Comments
 (0)