Skip to content

Instantly share code, notes, and snippets.

@endes0
Last active December 20, 2024 17:07
Show Gist options
  • Save endes0/85346b7718b90a3818522278202aff97 to your computer and use it in GitHub Desktop.
Save endes0/85346b7718b90a3818522278202aff97 to your computer and use it in GitHub Desktop.
See https://dev.to/endes/analyzing-the-communication-protocol-of-the-decathlon-walk-by-domyos-3a36. Uses MCUFRIEND_kbv 3.0.0, based on the button_simple example.
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP=6,XM=A2,YP=A1,YM=7; //240x320 ID=0x7783
const int TS_LEFT=800,TS_RT=154,TS_TOP=971,TS_BOT=129;
bool started = false;
int vel = 0;
uint8_t recivedMsg[16];
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button plus_btn, min_btn, onoff_btn;
int pixel_x, pixel_y;
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT);
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH);
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
void update_text()
{
tft.fillRect(0, 0, 250, 150, BLACK);
tft.setTextColor(WHITE);
tft.setCursor(0, 0);
tft.setTextSize(1);
for (size_t i = 2; i < 15; i++) {
tft.print(recivedMsg[i]);
tft.print("|");
}
tft.setTextSize(7);
tft.setCursor(40, 100);
tft.print(vel);
}
void send_msg()
{
uint8_t msg[10] = {0x68, 8, 0x80, 0, 0, 0, 0, 0, 0, 0x43};
if (started) {
msg[3] = 0x50;
msg[4] = vel;
}
int checksum = 0;
for (size_t i = 1; i < 9; i++) {
checksum += msg[i];
}
msg[8] = checksum % 0x100;
Serial1.write(msg, 10);
}
void setup(void)
{
Serial.begin(9600);
Serial1.begin(2400);
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(0); //PORTRAIT
tft.fillScreen(BLACK);
plus_btn.initButton(&tft, 60, 300, 100, 40, WHITE, CYAN, BLACK, "+", 2);
min_btn.initButton(&tft, 180, 300, 100, 40, WHITE, CYAN, BLACK, "-", 2);
onoff_btn.initButton(&tft, 125, 250, 200, 40, WHITE, CYAN, BLACK, "ON", 2);
plus_btn.drawButton(false);
min_btn.drawButton(false);
onoff_btn.drawButton(false);
tft.fillRect(40, 150, 160, 80, RED);
}
void loop(void)
{
bool down = Touch_getXY();
plus_btn.press(down && plus_btn.contains(pixel_x, pixel_y));
min_btn.press(down && min_btn.contains(pixel_x, pixel_y));
onoff_btn.press(down && onoff_btn.contains(pixel_x, pixel_y));
if (plus_btn.justReleased())
plus_btn.drawButton();
if (min_btn.justReleased())
min_btn.drawButton();
if (onoff_btn.justReleased())
onoff_btn.drawButton();
if (plus_btn.justPressed()) {
plus_btn.drawButton(true);
if (vel < 100) {
vel += 1;
update_text();
}
}
if (min_btn.justPressed()) {
min_btn.drawButton(true);
if (vel > 0) {
vel -= 1;
update_text();
}
}
if (onoff_btn.justPressed()) {
started = !started;
if (started) {
tft.fillRect(40, 150, 160, 80, GREEN);
} else {
tft.fillRect(40, 150, 160, 80, RED);
}
onoff_btn.drawButton(true);
}
if (millis() % 500 == 0) {
send_msg();
}
if(Serial1.available() > 0) {
Serial1.readBytesUntil(0x43, recivedMsg, 16);
}
if (millis() % 1500 == 0) {
update_text();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment