#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "TouchScreen.h"
#include "Buttons.h"
#include <SPI.h>
#include <stdint.h>
#define BACK_CLR ILI9341_BLACK
#define numButt 4
Button b[numButt];
Button bSingle[numButt]; // single screen
int bSize = sizeof(Button);
#define TFT_CS_0 10
#define TFT_DC_0 6
#define TFT_RST 0
#define numScr 1
#define XP 3
#define YP 1
#define XM 2
#define YM 4
Adafruit_ILI9341 tft = { Adafruit_ILI9341(TFT_CS_0, TFT_DC_0, TFT_RST)}; // screen
// measured 282 ohms accross the X plate
TouchScreen ts = { TouchScreen(XP, YP, XM, YM, 298)}; // touch screen
void setup() {
Serial.begin(9600);
tft.begin();
tft.fillScreen(BACK_CLR);
// initialise buttons on screen
int xOffset = 30; // offset from x edge
int yOffset = 30; // offset from y edge
int numBX = 2; // number of buttons along the x axis per screen
int numBY = 3; // number of buttons along the y axis per screen
int xDiff = (tft.width()) / numBX; // distance between buttons on x axis per screen
int yDiff = (tft.height()) / numBY; // distance between buttons on y axis per screen
for (int i = 0; i < numBX; i++) {
for (int j = 0; j < numBY; j++) {
b[j+numBY * i] = B_Init_Def(b[j + numBY * i],xOffset + i * xDiff,yOffset + j * yDiff);
}
}
for (int i = 0; i < numButt; i++) {
tft.fillRoundRect(b[i].x, b[i].y, b[i].width, b[i].height, b[i].r, b[i].clr);
}
}
void loop() {
static boolean set = false;
if (!set) {
set = true;
setupSingle();
}
singleScreen();
}
void setupSingle() {
digitalWrite(LED_BUILTIN,1);
// fill other screens with grey, this may not be possible
for (int i=0; i < numScr; i++)
if (i != 1)
tft.fillScreen(0x8410);
tft.fillScreen(BACK_CLR);
// initialise buttons on left screen
int bHeight = DEF_B_HEIGHT - 20; // height of buttons
int bWidth = DEF_B_WIDTH - 42; // width of buttons
int xOffset = 20; // offset from x edge
int yOffset = 30; // offset from y edge
int numBX = 2; // number of buttons along the x axis per screen
int numBY = 3; // number of buttons along the y axis per screen
int xDiff = (tft.width()) / numBX; // distance between buttons on x axis per screen
int yDiff = (tft.height()) / numBY; // distance between buttons on y axis per screen
// place buttons in an even grid formation
bSingle[0] = B_Init(bSingle[0], xOffset + 1 *xDiff, yOffset + 1 * yDiff, bHeight, bWidth, 5, 0x001F, 1);
bSingle[1] = B_Init(bSingle[1], xOffset + 0 *xDiff, yOffset + 0 * yDiff, bHeight, bWidth, 5, DEF_B_CLR, 1);
bSingle[2] = B_Init(bSingle[2], xOffset + 0 *xDiff, yOffset + 1 * yDiff, bHeight, bWidth, 5, 0x07FE, 1);
bSingle[3] = B_Init(bSingle[3], xOffset + 0 *xDiff, yOffset + 2 * yDiff, bHeight, bWidth, 5, DEF_B_CLR, 1);
// Draw to screens
for (int i = 0; i < numButt; i++) {
tft.fillRoundRect(bSingle[i].x, bSingle[i].y, bSingle[i].width, bSingle[i].height, bSingle[i].r, bSingle[i].clr);
}
}
void singleScreen() {
static int led = 1;
static int cnt = 0;
if (cnt++ > 200) {
digitalWrite(LED_BUILTIN, led ^= 1);
cnt = 0;
}
// If touchscreen pressed
static int fCnt = 0;
static int lastX = 0, lastY = 0;
// Read from touchscreens
TSPoint p = ts.getPoint();
// When touch detected
if (p.z > ts.pressureThreshhold) {
// scale from 0->1023 to tft.width
p.x = map(p.x, 920, 120, tft.width(), 0);
p.y = map(p.y, 935, 75, tft.height(), 0);
// button press to change color
if (!digitalRead(5))
{
// only when pressed, not continuously
if (fCnt > 30) {
for (int i = 0; i < numButt; i++) {
if (isPressed(bSingle[i], p.x, p.y)) {
// if cyan, change to green
if (bSingle[i].clr == 0xF81F) {
bSingle[i].clr = ILI9341_GREEN;
Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" connected");
// all other green buttons change magenta
for (int j = 0; j < numButt; j++) {
if (j != i) {
if (bSingle[j].clr == ILI9341_GREEN) {
bSingle[j].clr = 0xF81F;
tft.fillRoundRect(bSingle[j].x, bSingle[j].y, bSingle[j].width, bSingle[j].height, bSingle[j].r, bSingle[j].clr);
Serial.print("Channel "); Serial.print(j, DEC); Serial.println(" disconnected");
}
}
}
}
else if (bSingle[i].clr == ILI9341_GREEN) {
// change to magenta if green
bSingle[i].clr = 0xF81F;//random(0xFFFF);
Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" disconnected");
for (int j = 0; j < numButt; j++) {
if (j != i) {
if (bSingle[j].clr == ILI9341_GREEN) {
bSingle[j].clr = 0xF81F;
tft.fillRoundRect(bSingle[j].x, bSingle[j].y, bSingle[j].width, bSingle[j].height, bSingle[j].r, bSingle[j].clr);
Serial.print("Channel "); Serial.print(j, DEC); Serial.println(" disconnected");
}
else if (bSingle[j].clr == 0x07FF) {
bSingle[j].clr = 0x001F;
tft.fillRoundRect(bSingle[j].x, bSingle[j].y, bSingle[j].width, bSingle[j].height, bSingle[j].r, bSingle[j].clr);
Serial.print("Channel "); Serial.print(j, DEC); Serial.println(" disconnected");
}
}
}
}
else if (bSingle[i].clr == 0x001F) {
bSingle[i].clr = 0x07FF;
Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" disconnected");
}
else if (bSingle[i].clr == 0x07FF) {
bSingle[i].clr = 0x001F;
Serial.print("Channel "); Serial.print(i, DEC); Serial.println(" disconnected");
}
// redraw pressed button
tft.fillRoundRect(bSingle[i].x, bSingle[i].y, bSingle[i].width, bSingle[i].height, bSingle[i].r, bSingle[i].clr);
}
}
}
}
fCnt = 0;
} else {
fCnt++;
}
}