Members
I get an error when I try to display only one button at a time on the TFT. If I press 'On' then 'Off' should appear & vice versa.
/*
DUE_OnOff_multiUnits
TX &TFT &nRF
sub menu buttons
unit with 2 leds can be switched via nRF
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
//Touchscreen X+ X- Y+ Y- pins
#define YP A5
#define XM A4
#define YM 10
#define XP 11
//tested as correct for new ADAFRUIT screen
#define TS_MINX 150
#define TS_MINY 90
#define TS_MAXX 900
#define TS_MAXY 800
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
#define BLACK 0x0000
#define BLUE 0x001F
#define CYAN 0x07FF
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define DARKGREY 0x7BEF
#define GREEN 0x07E0
#define GREENYELLOW 0xB7E0
#define LIGHTGREY 0xC618
#define MAGENTA 0xF81F
#define MAROON 0x7800
#define NAVY 0x000F
#define OLIVE 0x7BE0
#define ORANGE 0xFDA0
#define PINK 0xFC9F
#define PURPLE 0x780F
#define RED 0xF800
#define WHITE 0xFFFF
#define YELLOW 0xFFE0
#define MINPRESSURE 10
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button on_btn, off_btn, on_btn1, off_btn1,
on_btn2, off_btn2, on_btn3, off_btn3,
on_btn4, off_btn4, on_btn5, off_btn5;
// DUE pins ................CHECK
#define CE_PIN 50 //confirm pin connections for nrf
#define CSN_PIN 51
RF24 radio(CE_PIN, CSN_PIN);
const byte slaveAddress1[5] = {'R', 'x', 'A', 'A', 'A'}; //need address for each slave 1
const byte slaveAddress2[5] = {'R', 'x', 'A', 'A', 'B'}; //need address for each slave 2
const byte slaveAddress3[5] = {'R', 'x', 'A', 'A', 'C'}; //need address for each slave 3
const byte slaveAddress4[5] = {'R', 'x', 'A', 'A', 'D'}; //need address for each slave 4
const byte slaveAddress5[5] = {'R', 'x', 'A', 'A', 'E'}; //need address for each slave 5
const byte slaveAddress6[5] = {'R', 'x', 'A', 'A', 'F'}; //need address for each slave 6
int msg[1];
int pixel_x, pixel_y;
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.y, TS_MAXX, TS_MINX, tft.width(), 0);
pixel_y = map(p.x, TS_MAXY, TS_MINY, 0, tft.height());
//Serial.println( pixel_x);
//Serial.println( pixel_y);
}
return pressed;
}
void setup(void) {
Serial.begin(9600);
radio.begin();
uint16_t ID = tft.readID();
tft.begin(ID);
tft.setRotation(1);
tft.fillScreen(BLACK);
OnOffGraphics ();
}
void OnOffGraphics () {
on_btn.initButton(&tft, 230, 15, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
off_btn.initButton(&tft, 285, 15, 50, 30, CYAN, RED, BLACK, "OFF", 1);
if (on_btn.drawButton(false)) else (off_btn.drawButton(true));
on_btn1.initButton(&tft, 230, 50, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
off_btn1.initButton(&tft, 285, 50, 50, 30, CYAN, RED, BLACK, "OFF", 1);
on_btn1.drawButton(false);
off_btn1.drawButton(false);
on_btn2.initButton(&tft, 230, 85, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
off_btn2.initButton(&tft, 285, 85, 50, 30, CYAN, RED, BLACK, "OFF", 1);
on_btn2.drawButton(false);
off_btn2.drawButton(false);
on_btn3.initButton(&tft, 230, 120, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
off_btn3.initButton(&tft, 285, 120, 50, 30, CYAN, RED, BLACK, "OFF", 1);
on_btn3.drawButton(false);
off_btn3.drawButton(false);
on_btn4.initButton(&tft, 230, 155, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
off_btn4.initButton(&tft, 285, 155, 50, 30, CYAN, RED, BLACK, "OFF", 1);
on_btn4.drawButton(false);
off_btn4.drawButton(false);
on_btn5.initButton(&tft, 230, 190, 50, 30, CYAN, GREEN, BLACK, "ON", 1);
off_btn5.initButton(&tft, 285, 190, 50, 30, CYAN, RED, BLACK, "OFF", 1);
on_btn5.drawButton(false);
off_btn5.drawButton(false);
}
// Array of button addresses to behave like a list
Adafruit_GFX_Button *buttons[] = {&on_btn, &off_btn, &on_btn1, &off_btn1,
&on_btn2, &off_btn2, &on_btn3, &off_btn3,
&on_btn4, &off_btn4, &on_btn5, &off_btn5 , NULL
};
bool update_button(Adafruit_GFX_Button *b, bool down)
{
b->press(down && b->contains(pixel_x, pixel_y));
if (b->justReleased())
b->drawButton(false);
if (b->justPressed())
b->drawButton(true);
return down;
}
bool update_button_list(Adafruit_GFX_Button **pb)
{
bool down = Touch_getXY();
for (int i = 0 ; pb[i] != NULL; i++) {
update_button(pb[i], down);
}
return down;
}
void loop(void) {
PressButton();
}
void PressButton() {
update_button_list(buttons); //2 buttons & leds / unit
radio.openWritingPipe(slaveAddress1);
if (on_btn.justPressed()) { // unit 1
msg[0] = 001; // LED0=on
radio.write(msg, 1);
}
if (off_btn.justPressed()) { // unit 1
msg[0] = 000; // LED0=off
radio.write(msg, 1);
}
if (on_btn1.justPressed()) { // unit 1
msg[0] = 101; // LED1=on
radio.write(msg, 1); // transmitt button two is pressed
}
if (off_btn1.justPressed()) { // unit 1
msg[0] = 100; // LED1=off
radio.write(msg, 1); // transmitt button two is pressed
}
radio.openWritingPipe(slaveAddress6);
if (on_btn2.justPressed()) { // unit 2
msg[0] = 001; // LED0=on
radio.write(msg, 1);
}
if (off_btn2.justPressed()) { // unit 2
msg[0] = 000; // LED0=off
radio.write(msg, 1);
}
if (on_btn3.justPressed()) { // unit 2
msg[0] = 101; // LED1=on
radio.write(msg, 1); // transmitt button two is pressed
}
if (off_btn3.justPressed()) { // unit 2
msg[0] = 100; // LED1=off
radio.write(msg, 1); // transmitt button two is pressed
}
}