Skip to content

Instantly share code, notes, and snippets.

@ThePendulum
Created July 14, 2019 23:51
Show Gist options
  • Save ThePendulum/fdd58367774cf1ca54470928403d9c64 to your computer and use it in GitHub Desktop.
Save ThePendulum/fdd58367774cf1ca54470928403d9c64 to your computer and use it in GitHub Desktop.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <Adafruit_GFX.h>
#include <Enrf24.h>
#include <nRF24L01.h>
#define ARDUINOJSON_ENABLE_PROGMEM 0
#include <ArduinoJson.h>
// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();
SPIClass SPI_2(2);
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS PB12
#define TFT_DC PA8
Adafruit_ILI9341 tft = Adafruit_ILI9341(&SPI_2, TFT_CS, TFT_DC);
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, PB15, PB13, -1, PB14);
Enrf24 radio(PA0, PA1, PA2); // CE, CSN, IRQ
const uint8_t txaddr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x01 };
// Size of the color selection boxes and the paintbrush size
#define BOXSIZE 40
#define PENRADIUS 3
int oldcolor, currentcolor;
int mode = 0;
uint8_t hue = 0;
uint8_t sat = 255;
uint8_t val = 255;
uint8_t speed = 1;
int timeLast = millis();
void dump_radio_status_to_serialport(uint8_t status) {
Serial.print("Enrf24 radio transceiver status: ");
switch (status) {
case ENRF24_STATE_NOTPRESENT:
Serial.println("NO TRANSCEIVER PRESENT");
break;
case ENRF24_STATE_DEEPSLEEP:
Serial.println("DEEP SLEEP <1uA power consumption");
break;
case ENRF24_STATE_IDLE:
Serial.println("IDLE module powered up w/ oscillators running");
break;
case ENRF24_STATE_PTX:
Serial.println("Actively Transmitting");
break;
case ENRF24_STATE_PRX:
Serial.println("Receive Mode");
break;
default:
Serial.println("UNKNOWN STATUS CODE");
}
}
void transmit() {
StaticJsonDocument<200> data;
JsonArray hsv = data.createNestedArray("c");
hsv.add(hue);
hsv.add(sat);
hsv.add(val);
data["m"] = mode;
data["s"] = speed;
String serialData;
serializeMsgPack(data, serialData);
radio.print(serialData);
radio.flush(); // Force transmit (don't wait for any more data)
// dump_radio_status_to_serialport(radio.radioState()); // Should report IDLE
}
void setup(void) {
Serial.begin(115200);
// SPI.setModule(2);
tft.begin();
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
tft.fillScreen(ILI9341_BLACK);
// make the color selection boxes
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
// select the current color 'red'
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
currentcolor = ILI9341_RED;
// transmit();
}
void loop() {
// Wait for a touch
if (! ctp.touched()) {
int timeNow = millis();
if (timeNow - timeLast > 1000) {
Serial.println("beat!");
timeLast = timeNow;
}
return;
}
// Retrieve a point
TS_Point p = ctp.getPoint();
/*
// Print out raw data from screen touch controller
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print(" -> ");
*/
// flip it around to match the screen.
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
// Print out the remapped (rotated) coordinates
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
if (p.y < BOXSIZE) {
oldcolor = currentcolor;
if (p.x < BOXSIZE) {
currentcolor = ILI9341_RED;
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
} else if (p.x < BOXSIZE*2) {
currentcolor = ILI9341_YELLOW;
tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
} else if (p.x < BOXSIZE*3) {
currentcolor = ILI9341_GREEN;
tft.drawRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
} else if (p.x < BOXSIZE*4) {
currentcolor = ILI9341_CYAN;
tft.drawRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
} else if (p.x < BOXSIZE*5) {
currentcolor = ILI9341_BLUE;
tft.drawRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
} else if (p.x <= BOXSIZE*6) {
currentcolor = ILI9341_MAGENTA;
tft.drawRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_WHITE);
}
if (oldcolor != currentcolor) {
if (oldcolor == ILI9341_RED)
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, ILI9341_RED);
if (oldcolor == ILI9341_YELLOW)
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, ILI9341_YELLOW);
if (oldcolor == ILI9341_GREEN)
tft.fillRect(BOXSIZE*2, 0, BOXSIZE, BOXSIZE, ILI9341_GREEN);
if (oldcolor == ILI9341_CYAN)
tft.fillRect(BOXSIZE*3, 0, BOXSIZE, BOXSIZE, ILI9341_CYAN);
if (oldcolor == ILI9341_BLUE)
tft.fillRect(BOXSIZE*4, 0, BOXSIZE, BOXSIZE, ILI9341_BLUE);
if (oldcolor == ILI9341_MAGENTA)
tft.fillRect(BOXSIZE*5, 0, BOXSIZE, BOXSIZE, ILI9341_MAGENTA);
}
}
if (((p.y-PENRADIUS) > BOXSIZE) && ((p.y+PENRADIUS) < tft.height())) {
tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment