Skip to content

Commit

Permalink
added touch driver and improved buttons for M5Core2
Browse files Browse the repository at this point in the history
  • Loading branch information
hpsaturn committed Oct 7, 2023
1 parent c5f4652 commit 9c1cb81
Show file tree
Hide file tree
Showing 6 changed files with 871 additions and 8 deletions.
5 changes: 4 additions & 1 deletion player/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ build_flags =
-D CORE_DEBUG_LEVEL=0 # For debugging set to 3 and enable debug mode in the app
-Ofast # maximum speed!
lib_deps =
SPI
bodmer/TFT_eSPI
bitbank2/JPEGDEC
z3t0/IRremote
Expand Down Expand Up @@ -184,7 +185,7 @@ build_flags =

[env:tdisplay]
extends = esp32_common
platform = espressif32
platform = espressif32 @ 4.4.0
board = esp32dev
build_flags =
${common.build_flags}
Expand All @@ -206,6 +207,7 @@ build_flags =
-DSPI_FREQUENCY=40000000
-DSPI_READ_FREQUENCY=6000000
; buttons
-DBUTTONS
-DBUTTON_L=0
-DBUTTON_R=35
; HAL misc
Expand All @@ -227,6 +229,7 @@ board = esp32dev
build_flags =
${common.build_flags}
-DM5CORE2
-DBUTTONS
-DUSE_DMA
; TFT_eSPI setup
-DUSER_SETUP_LOADED
Expand Down
50 changes: 43 additions & 7 deletions player/src/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,50 @@
#ifdef M5CORE2
#include "M5Touch.h"
M5Touch Touch;
TouchPoint_t pos;
#endif

int _btn_left;
int _btn_right;

bool buttonRight(){
#ifdef M5CORE2
TouchPoint_t pos = Touch.getPressPoint();
if(pos.x>100) return true;
if (pos.x > 200 && pos.y > 90 && pos.y < 180) return true;
#endif
#ifdef BUTTON_R
return (digitalRead(BUTTON_R) == 0);
return (_btn_right == 0);
#endif
return false;
}

bool buttonLeft(){
#ifdef M5CORE2
TouchPoint_t pos = Touch.getPressPoint();
if(pos.x > 0 && pos.x<100) return true;
if (pos.x > 0 && pos.x < 100 && pos.y > 90 && pos.y < 180) return true;
#endif
#ifdef BUTTON_L
return (digitalRead(BUTTON_L) == 0);
return (_btn_left == 0);
#endif
return false;
}

bool buttonUp(){
#ifdef M5CORE2
if (pos.x > 106 && pos.x < 212 && pos.y > 0 && pos.y < 90) return true;
#endif
return false;
}

bool buttonDown(){
#ifdef M5CORE2
if (pos.x > 106 && pos.x < 212 && pos.y > 180 && pos.y < 280) return true;
#endif
return false;
}

bool buttonPowerOff() {
#ifdef BUTTON_L
#ifdef BUTTON_R
return (digitalRead(BUTTON_L) == 0 && digitalRead(BUTTON_R) == 0);
return (_btn_left == 0 && _btn_right == 0);
#endif
#endif
return false;
Expand All @@ -41,7 +57,27 @@ void buttonInit(){
pinMode(BUTTON_L, INPUT_PULLUP);
pinMode(BUTTON_R, INPUT);
#endif
#endif
#ifdef M5CORE2
Touch.begin();
#endif
}

void buttonLoop(){
static uint_fast64_t buttonTimeStamp = 0;
if (millis() - buttonTimeStamp > 80) {
buttonTimeStamp = millis();
#ifdef M5CORE2
pos = Touch.getPressPoint();
// Serial.printf("(%i,%i)",pos.x,pos.y);
#endif
#ifdef BUTTON_L
#ifdef BUTTON_R
_btn_right = digitalRead(BUTTON_R);
_btn_left = digitalRead(BUTTON_L);
#endif
#endif
}
}


181 changes: 181 additions & 0 deletions player/src/M5PointAndZone.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#include "M5PointAndZone.h"

// Point class

Point::Point(int16_t x_ /* = INVALID_VALUE */,
int16_t y_ /* = INVALID_VALUE */) {
x = x_;
y = y_;
}

bool Point::operator==(const Point& p) { return (Equals(p)); }

bool Point::operator!=(const Point& p) { return (!Equals(p)); }

Point::operator char*() {
if (valid()) {
sprintf(_text, "(%d, %d)", x, y);
} else {
strncpy(_text, "(invalid)", 12);
}
return _text;
}

Point::operator bool() { return !(x == INVALID_VALUE && y == INVALID_VALUE); }

void Point::set(int16_t x_ /* = INVALID_VALUE */,
int16_t y_ /* = INVALID_VALUE */) {
x = x_;
y = y_;
}

bool Point::Equals(const Point& p) { return (x == p.x && y == p.y); }

bool Point::in(Zone& z) { return (z.contains(x, y)); }

bool Point::valid() { return !(x == INVALID_VALUE && y == INVALID_VALUE); }

uint16_t Point::distanceTo(const Point& p) {
int16_t dx = x - p.x;
int16_t dy = y - p.y;
return sqrt(dx * dx + dy * dy) + 0.5;
}

uint16_t Point::directionTo(const Point& p, bool rot1 /* = false */) {
// 57.29578 =~ 180/pi, see https://stackoverflow.com/a/62486304
uint16_t a = (uint16_t)(450.5 + (atan2(p.y - y, p.x - x) * 57.29578));
#ifdef TFT
if (rot1) {
if (TFT->rotation < 4) {
a = (((TFT->rotation + 3) % 4) * 90) + a;
} else {
a = (((TFT->rotation + 1) % 4) * 90) - a;
}
}
#endif /* TFT */
a = (360 + a) % 360;
return a;
}

bool Point::isDirectionTo(const Point& p, int16_t wanted,
uint8_t plusminus /* = PLUSMINUS */,
bool rot1 /* = false */) {
uint16_t a = directionTo(p, rot1);
return (min(abs(wanted - a), 360 - abs(wanted - a)) <= plusminus);
}

void Point::rotate(uint8_t m) {
if (m == 1 || !valid()) return;
int16_t normal_x = x;
int16_t normal_y = y;
int16_t inv_x = HIGHEST_X - x;
int16_t inv_y = HIGHEST_Y - y;
// inv_y can be negative for area below screen of M5Core2
switch (m) {
case 0:
x = inv_y;
y = normal_x;
break;
case 2:
x = normal_y;
y = inv_x;
break;
case 3:
x = inv_x;
y = inv_y;
break;
// rotations 4-7 are mirrored
case 4:
x = inv_y;
y = inv_x;
break;
case 5:
x = normal_x;
y = inv_y;
break;
case 6:
x = normal_y;
y = normal_x;
break;
case 7:
x = inv_x;
y = normal_y;
break;
}
}

// Zone class

Zone::Zone(int16_t x_ /* = INVALID_VALUE */, int16_t y_ /* = INVALID_VALUE */,
int16_t w_ /* = 0 */, int16_t h_ /* = 0 */, bool rot1_ /* = false */
) {
set(x_, y_, w_, h_, rot1_);
}

Zone::operator bool() { return !(x == INVALID_VALUE && y == INVALID_VALUE); }

void Zone::set(int16_t x_ /* = INVALID_VALUE */,
int16_t y_ /* = INVALID_VALUE */, int16_t w_ /* = 0 */,
int16_t h_ /* = 0 */, bool rot1_ /* = false */) {
x = x_;
y = y_;
w = w_;
h = h_;
rot1 = rot1_;
}

bool Zone::valid() { return !(x == INVALID_VALUE && y == INVALID_VALUE); }

bool Zone::contains(const Point& p) { return contains(p.x, p.y); }

bool Zone::contains(int16_t x_, int16_t y_) {
#ifdef TFT
if (rot1 && TFT->rotation != 1) {
Zone t = *this;
t.rotate(TFT->rotation);
return (y_ >= t.y && y_ <= t.y + t.h && x_ >= t.x && x_ <= t.x + t.w);
}
#endif /* TFT */

return (y_ >= y && y_ <= y + h && x_ >= x && x_ <= x + w);
}

void Zone::rotate(uint8_t m) {
if (m == 1) return;
int16_t normal_x = x;
int16_t normal_y = y;
int16_t inv_x = TFT_WIDTH - 1 - x - w;
int16_t inv_y = TFT_HEIGHT - 1 - y - h; // negative for area below screen
switch (m) {
case 0:
x = inv_y;
y = normal_x;
break;
case 2:
x = normal_y;
y = inv_x;
break;
case 3:
x = inv_x;
y = inv_y;
break;
// rotations 4-7 are mirrored
case 4:
x = inv_y;
y = inv_x;
break;
case 5:
x = normal_x;
y = inv_y;
break;
case 6:
x = normal_y;
y = normal_x;
break;
case 7:
x = inv_x;
y = normal_y;
break;
}
if (!(m % 2)) std::swap(w, h);
}
Loading

0 comments on commit 9c1cb81

Please sign in to comment.