Need help with USB host shield and tft screen

I am waiting for an input from two devices my tft and my usb host shield. I dont know how to know if the usb was triggered but for my tft (i am using adafruit's ra8875) I think its touched(); I really need help please

I really need help please

But, I'm not going to post any code, so get your crystal balls polished up.

That's not going to work. Mine are brass.

#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include “Adafruit_RA8875.h”
#include <Adafruit_STMPE610.h>
#include <hidboot.h>
#include <usbhub.h>
#include “Adafruit_GFX.h”

#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif

const int tft_cs = 5;
const int usb_cs = 10;
const int RA8875_INT = 7;
const int RA8875_RESET = 6;
const int MAX_INT = 9;

Adafruit_RA8875 tft = Adafruit_RA8875(tft_cs, RA8875_RESET);

class KbdRptParser : public KeyboardReportParser
{

protected:
virtual void OnKeyDown (uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
};

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
uint8_t c = OemToAscii(mod, key);

if (c)
OnKeyPressed(c);
}

/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)
{
if(key != 19){
hold += ((char)key);
}
else{
FLAG = true;
return;
}

};

USB Usb;
//USBHub Hub(&Usb);
HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);

uint32_t next_time;
KbdRptParser Prs;

void setup() {
Serial.begin(115200);

digitalWrite(usb_cs, HIGH);
digitalWrite(tft_cs, LOW);

Serial.println(“RA8875 start”);
if (!tft.begin(RA8875_800x480)) {
Serial.println(“RA8875 Not Found!”);
while (1);
}
Serial.println(“Found RA8875”);
tft.displayOn(true);
tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
tft.PWM1out(255);
tft.touchEnable(true);

digitalWrite(tft_cs, HIGH);
digitalWrite(usb_cs, LOW);

#if !defined(MIPSEL)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
Serial.println(“Start USB”);
if (Usb.Init() == -1){
Serial.println(“USB initialize failed”);
}
else{
Serial.println(“USB ready!”);
}

}

void loop(){
float xScale = 1024.0F/tft.width();
float yScale = 1024.0F/tft.height();
uint16_t tx, ty;

tft.graphicsMode();

while(digitalRead(RA8875_INT)){}
if (tft.touched())
{
Serial.print("Touch: “);
tft.touchRead(&tx, &ty);
Serial.print(tx); Serial.print(”, "); Serial.println(ty);
}
}

I am sorry :frowning:

I dont know how to know if the usb was triggered

Isn't that what the OnKeyDown() and OnKeyPressed() methods are for?

#include <spi4teensy3.h>
#include <SPI.h>

You sure you want -both- of these in there?

-jim lee

I run the Usb.task() inside the while loop of the RA8875_INT

#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include “Adafruit_RA8875.h”
#include <Adafruit_STMPE610.h>
#include <hidboot.h>
#include <usbhub.h>
#include “Adafruit_GFX.h”

#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif

const int tft_cs = 5;
const int usb_cs = 10;
const int RA8875_INT = 7;
const int RA8875_RESET = 6;
const int MAX_INT = 9;

Adafruit_RA8875 tft = Adafruit_RA8875(tft_cs, RA8875_RESET);

class KbdRptParser : public KeyboardReportParser
{

protected:
virtual void OnKeyDown (uint8_t mod, uint8_t key);
virtual void OnKeyPressed(uint8_t key);
};

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
uint8_t c = OemToAscii(mod, key);

if (c)
OnKeyPressed(c);
}

/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)
{
if(key != 19){
hold += ((char)key);
}
else{
FLAG = true;
return;
}

};

USB Usb;
//USBHub Hub(&Usb);
HIDBoot<HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);

uint32_t next_time;
KbdRptParser Prs;

void setup() {
Serial.begin(115200);

digitalWrite(usb_cs, HIGH);
digitalWrite(tft_cs, LOW);

Serial.println(“RA8875 start”);
if (!tft.begin(RA8875_800x480)) {
Serial.println(“RA8875 Not Found!”);
while (1);
}
Serial.println(“Found RA8875”);
tft.displayOn(true);
tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
tft.PWM1out(255);
tft.touchEnable(true);

digitalWrite(tft_cs, HIGH);
digitalWrite(usb_cs, LOW);

#if !defined(MIPSEL)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
Serial.println(“Start USB”);
if (Usb.Init() == -1){
Serial.println(“USB initialize failed”);
}
else{
Serial.println(“USB ready!”);
}

}

void loop(){
float xScale = 1024.0F/tft.width();
float yScale = 1024.0F/tft.height();
uint16_t tx, ty;

tft.graphicsMode();

while(digitalRead(RA8875_INT)){Usb.Task();}
if (tft.touched())
{
Serial.print("Touch: “);
tft.touchRead(&tx, &ty);
Serial.print(tx); Serial.print(”, "); Serial.println(ty);
}
}

This code works but there are times the usb host stop working

This code works but there are times the usb host stop working

Are you following union rules, and allowing adequate coffee breaks and leisurely lunch times?

I can't really align the "sometimes it doesn't" part with the "this code works" part.

What I mean is it work for a while and then it just stop working, sorry for constructing wrong sentence always. Filipino kid here.

  float xScale = 1024.0F/tft.width();
  float yScale = 1024.0F/tft.height();
  uint16_t tx, ty;

Are the physical characteristics of the screen going to change? Do you really need to read the height and width of the screen on EVERY pass through loop()?

  while(digitalRead(RA8875_INT)){Usb.Task();}

Why does executing Usb.Task() depend on the setting of the RA8875_INT pin? Under what conditions is that pin HIGH?

for the 2nd question the RA8875_INT is high while the tft is not touch, but when its touch it will proceed to the if(tft.touched()), I cant seem to understand the first question sir

I cant seem to understand the first question sir

Do you know what setup() does? Do you know how many times it is called?

Do you know what loop() does? Do you know how many times it is called?

How many times do you need to get the height and width of a device whose size can not possibly change?

How many times do you use that data?

Yess sir, setup() only called once and loop() is where I loop my code right? okayy sir now I understand I just make the

float xScale = 1024.0F/tft.width();
float yScale = 1024.0F/tft.height();
uint16_t tx, ty;

from the loop and make it an global variable.

I just make the...from the loop and make it an global variable.

You would make xScale and yScale global variables, and initialize them by calling the width() and height() methods in setup().

But, you'd only do that if you actually needed the values. You don't use them anywhere.

So, they belong in the bit bucket, not in setup() or as global variables.

Oh I need them for locating the touched part on my screen like this code draw a circle on every part you touch

if (tft.touched())
{
Serial.print("Touch: “);
tft.touchRead(&tx, &ty);
Serial.print(tx); Serial.print(”, "); Serial.println(ty);
/* Draw a circle */
tft.fillCircle((uint16_t)(tx/xScale), (uint16_t)(ty/yScale), 4, RA8875_WHITE);
}
}