// GT9271_Teensy_Touch.ino
/*
* License: Refer to LICENSE file in main directory.
* Modified: For use with Teensy 4.x and MicroMod
* By: Warren Watson 2024.
*/
#include <Wire.h>
#include "Display_CPT_Touchscreen_GT9271.h"
#include "DJETH_RA8876_Config_8080.h"
#include <DJETH_RA8876_t41_p.h>
#include "_font_ComicSansMS.h"
#include "DJETH_RA8876_common.h" //added this one and may not be needed
#include "DJETH_RA8876Registers.h" //added this one and may not be needed
RA8876_t41_p tft = RA8876_t41_p(RA8876_8080_DC, RA8876_8080_CS, RA8876_8080_RESET);
#define INT_PIN 8 // was 22
#define RST_PIN 255 // 255 means do not use reset pin else reset using pin number.
#define USE_WRITERECT
int TFT_Backlight_Pin = 33;
DisplayCPTGT9271 touch = DisplayCPTGT9271(INT_PIN, RST_PIN, 0x5D); // was 0x5d
int ScreenActive = 0;
int ButtonPressGoToScreen = 0;
int XAxis = 0;
int YAxis = 0;
int TimeADDEDForButtonDebounceMillis = 300;
long unsigned int TimeForButtonDebounceMillis = 0;
void setup() {
digitalWrite(TFT_Backlight_Pin, HIGH);
Serial.begin(115200);
while (!Serial && millis() < 1000) {} //wait for Serial Monitor
// Set 8/16bit bus mode. Default is 8bit bus mode.
tft.setBusWidth(16); // RA8876_8080_BUS_WIDTH is defined in
// src/RA8876_Config_8080.h.
tft.begin(40); // RA8876_8080_BUS_WIDTH is defined in
// src/RA8876_Config_8080.h. Default is 20MHz. // 60 and 120
tft.fillScreen(BLUE);
// Setup callback.
touch.setHandler(handleTouch);
// We are using Wire2 on T41. Set this to your wire usage.
touch.setWireObject(&Wire2); // Default set to Wire on reset or power up. // was &Wire
// Start touchscreen.
if (touch.begin() != true) {
Serial.println("! Module Initialize: Failed");
} else {
Serial.println("Module Initialize: Succeded");
}
Serial.printf("\nUp to 10 contact points can be used with this driver.\n");
Serial.printf("The screen is ready to be touched...\n");
Wire2.setClock(400000);
HomeScreenDraw();
attachInterrupt(INT_PIN, InterruptRoutine, FALLING);
TimeForButtonDebounceMillis = millis();
} // End Void Setup //////////////////////////////////////////////////////////////////////
void loop() {
//touch.armIRQ();
//touch.onIRQ();
//touch.loop();
// delay(100);
} // End Void Loop/ ////////////////////////////////////////////////////////////
// Callback from irq function.
void handleTouch(int8_t contacts, GTPoint *points) {
detachInterrupt(digitalPinToInterrupt(INT_PIN));
Serial.printf("Number Of Contact Points: %d\n", contacts);
for (uint8_t i = 0; i < contacts; i++) {
// X and Y coords are inverted side to side and up and down.
// Subtract from screen width and height to get x/y origins at
// 0,0 instead of 1023,599. Also generate 16bit coords from two
// 8bit coords (big endian).
XAxis = (uint16_t)LCD_WIDTH - ((points[i].x_msb << 8) | points[i].x_lsb);
YAxis = (uint16_t)LCD_HEIGHT - ((points[i].y_msb << 8) | points[i].y_lsb);
uint16_t area = (uint16_t)((points[i].area_msb << 8) | points[i].area_lsb);
Serial.printf("Contact #%d: Track ID %d, X position %d, Y position %d, area %d\n", i, points[i].trackId, XAxis, YAxis, area);
} // End for loop
if (TimeForButtonDebounceMillis < millis()) {
ScreenButtonCheck();
TimeForButtonDebounceMillis = millis() + TimeADDEDForButtonDebounceMillis;
} // End if(TimeForButtonDebounceMillis < millis())
attachInterrupt(INT_PIN, InterruptRoutine, FALLING);
} // End void handleTouch(int8_t contacts, GTPoint *points)
void InterruptRoutine() {
// tft.setCursor(20, 20);
// tft.print("Hello");
touch.onIRQ();
}
void ScreenButtonCheck() {
if (XAxis > 400 && XAxis < 650 && YAxis > 50 && YAxis < 100) {
if (ButtonPressGoToScreen == 1) {
HomeScreenDraw();
}
else if (ButtonPressGoToScreen == 2) {
SecondScreen();
}
} // End if(XAxis > 400 && XAxis < 650 && YAxis > 50 && YAxis < 100)
} // End void ScreenButtonCheck()
void HomeScreenDraw() {
ScreenActive = 1;
ButtonPressGoToScreen = 2;
tft.fillScreen(RED);
//tft.drawCircleSquareFill(ru16 x0, ru16 y0, ru16 x1, ru16 y1, ru16 xr, ru16 yr, ru16 color)
tft.drawCircleSquareFill(400, 50, 650, 100, 10, 10, BLACK);
tft.drawCircleSquare(400, 50, 650, 100, 10, 10, BLUE);
tft.setTextColor(WHITE);
tft.setFont(ComicSansMS_18);
tft.setCursor(415, 64);
tft.print("GOTO Screen 2");
} // End void HomeScreen()
void SecondScreen() {
ScreenActive = 2;
ButtonPressGoToScreen = 1;
tft.fillScreen(YELLOW);
//tft.drawCircleSquareFill(ru16 x0, ru16 y0, ru16 x1, ru16 y1, ru16 xr, ru16 yr, ru16 color)
tft.drawCircleSquareFill(400, 50, 650, 100, 10, 10, BLUE);
tft.drawCircleSquare(400, 50, 650, 100, 10, 10, BLACK);
tft.setTextColor(WHITE);
tft.setFont(ComicSansMS_18);
tft.setCursor(415, 64);
tft.print("GOTO Screen 1");
} // End void SecondScreen()