I'm making a touchscreen controlled radio. Everything works fine until I want to print some information , using tft.print. The main loop stops after calling the function displayInfo() The shared pins are restored after reading the touchscreen so I can't figure out what's wrong. Anyone can tell me what I'm doing wrong ?
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <TouchScreen.h>
#include <VMA11.h>
#include <Wire.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000
const int XP = 8, XM = A2, YP = A3, YM = 9; //320x480 ID=0x9486
const int TS_LEFT = 123, TS_RT = 908, TS_TOP = 944, TS_BOT = 91;
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button freq_btn, vol_btn, mute_btn, rds_btn, fup_btn, vup_btn, fdown_btn, vdown_btn;
int pixel_x, pixel_y; //Touch_getXY() updates global vars
int resetPin = 2;
int SDIO = 20;
int SCLK = 21;
VMA11 radio(resetPin, SDIO, SCLK);
int channel;
int volume;
char rdsname[9];
char rdsrt[65];
char previousRadioText[65];
uint8_t lastChar;
void displayInfo()
{
tft.setCursor(25, 230);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.print("Frequentie : "); tft.print(channel);
tft.print(" Volume : "); tft.print(volume);
}
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed)
{
pixel_x = map(p.y, TS_TOP, TS_BOT, 0, tft.width()); //Landscape
pixel_y = map(p.x, TS_RT, TS_LEFT, 0, tft.height());
}
return pressed;
}
void setup()
{
uint16_t ID = tft.readID();
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(1); //PORTRAIT
tft.fillScreen(BLACK);
tft.setTextColor(WHITE); //set text color white
tft.setTextSize(2); //set text size to 2 (1-6)
tft.println(" JoMoSoft FM Radio"); //print header to screen
tft.drawRoundRect(10, 20, 460, 300, 6, WHITE); //draw screen outline
freq_btn.initButton(&tft, 150, 60, 260, 50, WHITE, RED, WHITE, "FREQUENCY", 2);
fup_btn.initButton(&tft, 375, 60, 50, 50, WHITE, RED, WHITE, "", 2);
fdown_btn.initButton(&tft, 435, 60, 50, 50, WHITE, RED, WHITE, "", 2);
vol_btn.initButton(&tft, 150, 120, 260, 50, WHITE, BLUE, WHITE, "VOLUME", 2);
vup_btn.initButton(&tft, 375, 120, 50, 50, WHITE, BLUE, WHITE, "", 2);
vdown_btn.initButton(&tft, 435, 120, 50, 50, WHITE, BLUE, WHITE, "", 2);
mute_btn.initButton(&tft, 310, 120, 40, 50, WHITE, BLUE, WHITE, "X", 3);
freq_btn.drawButton(false);
vol_btn.drawButton(false);
mute_btn.drawButton(false);
fup_btn.drawButton(false);
vup_btn.drawButton(false);
fdown_btn.drawButton(false);
vdown_btn.drawButton(false);
tft.fillTriangle(375, 41, 354, 75, 395, 75, WHITE); //draw up triangle for station
tft.fillTriangle(435, 75, 416, 41, 452, 41, WHITE); //draw down triangle for station
tft.fillTriangle(375, 100, 354, 134, 395, 134, WHITE); //draw up triangle for volume
tft.fillTriangle(435, 135, 416, 100, 452, 100, WHITE); //draw down triangle for volume
tft.fillRoundRect(20, 230, 440, 75, 6, YELLOW); //info box
radio.powerOn();
radio.setVolume(1);
volume = 1;
channel = 1010;
radio.setChannel(channel);
memset(previousRadioText, 0, 65);
memset(rdsrt, 0, 65);
Serial.begin(9600);
}
void loop()
{
bool down = Touch_getXY();
vup_btn.press(down && vup_btn.contains(pixel_x, pixel_y));
vdown_btn.press(down && vdown_btn.contains(pixel_x, pixel_y));
mute_btn.press(down && mute_btn.contains(pixel_x, pixel_y));
fup_btn.press(down && fup_btn.contains(pixel_x, pixel_y));
fdown_btn.press(down && fdown_btn.contains(pixel_x, pixel_y));
if (vup_btn.justReleased())
{
volume ++;
if (volume >= 16) volume = 15;
radio.setVolume(volume);
}
if (vdown_btn.justReleased())
{
volume --;
if (volume < 0) volume = 0;
radio.setVolume(volume);
}
if (mute_btn.justReleased())
{
volume = 0;
radio.setVolume(volume);
}
if (fup_btn.justReleased())
{
channel = radio.seekUp();
}
if (fdown_btn.justReleased())
{
channel = radio.seekDown();
}
if (radio.readRDSRadioText(rdsrt))
{
if (strcmp(rdsrt, previousRadioText))
{
Serial.println(rdsrt);
strcpy(previousRadioText, rdsrt);
}
}
Serial.println(channel);
displayInfo();
}