The following code is my attempt to place all HX8357 display and touchscreen code in a personal library that consists of mydisplay.h and mydisplay.cpp.
It is working, but there is a problem I have not yet solved. Some of my display projects use pins 7, 8, A2, and A3 for the Teensy to touchpad connections, and some others us A0, A1, A2, and A3.
I need to find a way to tell the library files which pins to use via the TeensyHX8357.ino, so I won’t need 2 different libraries.
Placing the pin definitions in the main sketch file, TeensyHX8357.ino, doesn’t work.
TeensyHX8357.ino
// for teensy 4.0
/***************************************************************************************************************/
#include "mydisplay.h"
#include <arduino.h>
float angle = 0;
int16_t h, v;
int16_t vLoc[SCREENWIDTH];
uint16_t fg = WHITE, bg = BLUE;
/***************************************************************************************************************/
void setup(void)
{
Serial.begin(115200);
delay(250);
startDisplay(bg);
}
/***************************************************************************************************************/
void loop(void)
{
Sine();
}
/***************************************************************************************************************/
void Sine()
{
angle += 1;
if (angle > 360)
angle = 0;
v = 160 + 128 * sin(angle * 0.0174532925); // calculate the pixel vertical location
if (h < SCREENWIDTH - 1)
{
h += 1;
drawPixel(h, v, fg); // draw the pixel
vLoc[h] = v; // move the stored pixel vertical position
}
else
Scroll();
// delay(10); // control the frequency here
}
/***************************************************************************************************************/
void Scroll()
{
drawPixel(0, vLoc[0], bg); // Erase the zero pixel
for (int x = 1; x < SCREENWIDTH; x++)
{
drawPixel(x - 1, vLoc[x], fg); // move the pixels backwards (vLoc[1 - MAX-1] to vLoc[0 - MAX-2])
drawPixel(x, vLoc[x], bg); // erase the original pixels
vLoc[x - 1] = vLoc[x]; // move the stored pixels vertical position
}
vLoc[SCREENWIDTH - 1] = v; // store last pixel vertical position
}
Mydisplay.cpp
#include "mydisplay.h" // display functions, touchscreen functions anda button class
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS_PIN, TFT_DC_PIN, TFT_RST_PIN);
TouchScreen tscr = TouchScreen(XP_PIN, YP_PIN, XM_PIN, YM_PIN, 500);
/***************************************************************************************************************/
void drawPixel(int x, int y, int clr)
{
tft.drawPixel(x, y, clr);
}
/***************************************************************************************************************/
void rotation(int r)
{
tft.setRotation(r);
}
/***************************************************************************************************************/
void startDisplay(int clr)
{
tft.begin();
delay(200);
rotation(1);
tft.fillScreen(clr);
}
Mydisplay.h
// display functions, touchscreen functions and a button class
#ifndef __DISPLAY_H__
#define __DISPLAY_H__
#include <arduino.h>
#include "Adafruit_HX8357.h" // the 480x320 display
#include <Adafruit_GFX.h> // Core graphics library
#include <TouchScreen.h> // the touch screen
void startDisplay(int clr = 0x0000); // start the display with selected color
void drawPixel(int x, int y, int clr);
// The display uses hardware SPI, plus pins 9 & 10
#define TFT_DC_PIN 9
#define TFT_CS_PIN 10
#define TFT_RST_PIN 15 // tie this to arduino RST if you like
// These are the four touchscreen pins
#define YM_PIN A0 // can be a digital pin
#define XP_PIN A1 // can be a digital pin
#define YP_PIN A2 // (pin 16) must be an analog pin, use "An" notation!
#define XM_PIN A3 // (pin 17) must be an analog pin, use "An" notation!
#define MINPRESSURE 100 // minimum touch pressure to recognize a touch
#define MAXPRESSURE 1000 // maximum touch pressure to recognize a touch
// This is calibration data for the raw touch data to align the screen coordinates
#define TS_MINX 105 // bottom
#define TS_MINY 60 // right
#define TS_MAXX 945 // top
#define TS_MAXY 930 // left
#define SCREENWIDTH 480
#define SCREENHEIGHT 480
// Colors
#define BACKGROUND 0xA514
#define FILL 0xAD75
#define BLACK 0x0000
#define BLUE 0x0019
#define YELLOW 0xFFE0
#define RED 0xF800
#define DARKRED 0xA800
#define GREEN 0x0360
#define WHITE 0xFFFF
#define GRAY 0x9CD3
#endif