#include <pins_arduino.h>
#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
// PIN_SERIAL1_RX
// PIN_SERIAL1_TX
#define CW90 0
#define CW180 1
#define CW270 2
#define CW270 3
#define INPUT_GROUNDED LOW
#define INPUT_FLOATING HIGH
int counter = 0;
bool clearScreen = false;
bool waitForEndOfMessage = false;
bool forceInitialTurnOff = true; // force an initial turn-off
bool beltStateOn = true; // force an initial turn-off
String BELT_OFF_MSG = “<007B0>”;
String BELT_ON_MSG = “<007B1>”;
void setup()
{
pinMode(WIO_KEY_A, INPUT_PULLUP);
pinMode(WIO_KEY_B, INPUT_PULLUP);
pinMode(WIO_KEY_C, INPUT_PULLUP);
pinMode(D0, INPUT_PULLUP);
// Open serial communications and wait for port to open:
// set the data rate for the Serial1 port
Serial1.begin(115200); // Tx ok, Rx ok
tft.init();
tft.setRotation(CW270);
clearTFTDisplay();
tft.println(“Bell and Howell, LLC”);
tft.println(“DIO<=>DP01 Interface”);
tft.println(“115200, N, 8, 1”);
turnBeltOff();
}
void loop() // run over and over//
{
if (clearScreen || (digitalRead(WIO_KEY_C) == LOW)) {
clearScreen = false;
clearTFTDisplay();
tft.println(“Serial Port OK”);
tft.println(“115200,N,8,1”);
delay(500);
} // if (clearScreen || (digitalRead(WIO_KEY_C) == LOW))
// read the digital input signals
switch(digitalRead(D0)) {
case INPUT_GROUNDED: { // active low logic
turnBeltOn();
break;
} // case INPUT_GROUNDED
case INPUT_FLOATING: { // goes HIGH due to internal pullup
turnBeltOff();
break;
} // case INPUT_FLOATING
} // switch(digitalRead(D0))
checkForResponse();
} // void loop()
void clearTFTDisplay()
{
tft.fillScreen(0x0);
tft.setCursor(0, 0, 2);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextFont(4);
}
void sendData(String text)
{
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("Sending " + text);
Serial1.write(text.c_str());
}
void turnBeltOff()
{
if(beltStateOn || forceInitialTurnOff) {
clearTFTDisplay();
tft.println(“Turning Belt OFF”);
sendData(BELT_OFF_MSG);
beltStateOn = false;
forceInitialTurnOff = false;
} // if(beltStateOn) {
} // void turnBeltOff()
void turnBeltOn()
{
if(!beltStateOn) {
clearTFTDisplay();
tft.println(“Turning Belt ON”);
sendData(BELT_ON_MSG);
beltStateOn = true;
} // if(!beltStateOn) {
} // void turnBeltOn()
void checkForResponse()
{
if (Serial1.available() > 0) {
if(!waitForEndOfMessage) {
tft.println(“”);
waitForEndOfMessage = true;
}
char c = Serial1.read();
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.print(c);
if (c == ‘>’) {
tft.println(“”);
waitForEndOfMessage = false;
} // if(c==‘>’)
} // if(Serial1.available()>0)
if(!waitForEndOfMessage) {
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.print(“.”);
delay(200);
} // if(!waitForEndOfMessage)
}