// #include "main.h"
#include "display_controls.h"
#include "CampervanStructureLib.h"
#include <PinButton.h>
//#include <Tone32.h>
#define PAGE_DEVICES 1
#define PAGE_PRESETS 2
#define PAGE_COLOUR 3
#define PAGE_DEBUG 0
Setting currentState;
Setting importedState;
int currentPage = PAGE_DEVICES;
int maxPage = 3;
// Create a new button object, listening on pin 5.
PinButton btnWaterPump(PIN_BTN_WATER_PUMP);
PinButton btnKitchen12v(PIN_BTN_KITCHEN_12v);
PinButton btnUsbFastCharge(PIN_BTN_USB_FASTCHARGE);
PinButton btnLedStripPS(PIN_BTN_LED_STRIP_PS);
PinButton btnLedStripDs(PIN_BTN_LED_STRIP_DS);
PinButton btnLedLights(PIN_BTN_LED_LIGHTS);
PinButton btnBoot(PIN_BTN_BOOT);
PinButton btnFan(PIN_BTN_FAN);
PinButton btnScreenToggle(PIN_BTN_SCREEN_OFF);
PinButton btnPageNext(PIN_BTN_NEXT_PAGE);
HardwareSerial softSerial(1);
/// is true if there is a warning to display.
bool hasWarning = false;
char warningMessage[500];
/// Stores the last time the warning buzzed, this should only occurr if
/// hasWarning is true.
unsigned long lastBuzzTime;
/// if true, then the screen should be turned off
/// if false then the screen should display.
bool screenOff;
/// Can be set to true by any other methods if
/// a change has been made to the settings
/// of the screen.
bool screenUpdate = true;
void readFromSerial(HardwareSerial *readSerial)
{
if (readSerial->available())
{
char number = readSerial->read();
if (number == '0')
{
// digitalWrite(LED, LOW);
// MsgButtonPress btn;
// btn.buttonType = 10;
// btn.press = 4;
// sendMessage(&Serial, &btn);
}
if (number == '1')
{
// digitalWrite(LED, HIGH);
}
Serial.println("");
}
}
/// checks to see if there is a warning, and if there is
/// then it will clear the warning and return true, otherwise false.
/// to be used in button click methods.
bool checkIfClearWarning()
{
if (hasWarning)
{
hasWarning = false;
return true;
}
return false;
}
/// This will determine if the screen toggle on/off button has been pressed
/// and if so will update the appropriate variables.
void handleScreenOffClick()
{
btnScreenToggle.update();
if (btnScreenToggle.isSingleClick())
{
if (checkIfClearWarning())
{
return;
}
screenOff = !screenOff;
screenUpdate = true;
}
}
void drawCurrentPage()
{
Serial.print("drawCurrentPage() :");
Serial.println(currentPage);
if (currentPage == PAGE_DEVICES)
{
drawPageDevices(¤tState, false);
}
else if (currentPage == PAGE_PRESETS)
{
drawPagePreset(¤tState, false);
}
else if (currentPage == PAGE_COLOUR)
{
drawPageColour(¤tState, false);
}
else if (currentPage == PAGE_DEBUG)
{
drawPageDebug(¤tState, false);
}
else
{
drawNoPage();
}
}
void handlePreviousPage()
{Serial.print("handlePreviousPage() :");
if (currentPage == 1)
{
currentPage = maxPage;
}
else
{
currentPage = currentPage - 1;
}
drawCurrentPage();
}
void handleNextPage()
{
Serial.print("handleNextPage() :");
if (currentPage == maxPage)
{
currentPage = 1;
}
else
{
currentPage = currentPage + 1;
}
drawCurrentPage();
}
void handlePage(int page)
{
currentPage = page;
drawCurrentPage();
}
/// This will determine if the screen toggle on/off button has been pressed
/// and if so will update the appropriate variables.
void handleBtnPageNext()
{
btnPageNext.update();
if (btnPageNext.isSingleClick())
{
Serial.println("handleBtnPageNext(): Single Click");
handleNextPage();
}
else if (btnPageNext.isDoubleClick())
{
Serial.println("handleBtnPageNext(): Double Click");
handlePreviousPage();
}
else if (btnPageNext.isLongClick())
{
handlePage(PAGE_DEBUG);
// if (checkIfClearWarning())
// {
// return;
// }
// screenOff = !screenOff;
// screenUpdate = true;
}
}
/// This will determine if the buzz needs to be made to
/// warn the user of a warning.
void handleBuzz()
{
if (hasWarning)
{
if ((millis() - lastBuzzTime) > 5000)
{
tone(PIN_BUZZ, 262, 500); // Plays 262Hz tone for 0.250 seconds
Serial.println("Buzz Started");
lastBuzzTime = millis();
}
}
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("init - display .....");
delay(100);
SPI.begin(DISPLAY_CLK, DISPLAY_MISO, DISPLAY_MOSI);
displaySetup();
Serial.println(" complete");
//Serial.println("Display Test Starting...");
//displayTest();
Serial.println("Welcome Page...");
displayWelcomePage();
delay(2000);
//drawPageDevices(¤tState, false);
pinMode(ENCODER_BTN, INPUT_PULLUP);
pinMode(PIN_BUZZ, OUTPUT);
softSerial.begin(9600); // Start the serial module with a baud rate of 9600 bps and the default configuration
drawCurrentPage();
}
void drawState(Setting *setting, bool forceUpdate)
{
bool displayPartial = false;
if (forceUpdate || importedState.hasUsbFastChargeChanged(¤tState))
{
drawButtonUsb(currentState.usbFastCharge, displayPartial);
}
if (forceUpdate || importedState.hasBoot12Vhanged(¤tState))
{
drawButtonBoot12v(currentState.boot12V, displayPartial);
}
if (forceUpdate || importedState.hasBootLightChanged(¤tState))
{
drawButtonBootLight(currentState.bootLight, displayPartial);
}
if (forceUpdate || importedState.hasWaterPumpChanged(¤tState))
{
drawButtonWater(currentState.waterPump, displayPartial);
}
if (forceUpdate || importedState.hasKitchen12VChanged(¤tState))
{
drawButton12V(currentState.kitchen12V, displayPartial);
}
if (forceUpdate || importedState.hasLedLightsChanged(¤tState))
{
drawButtonLight(currentState.ledLights, displayPartial);
}
if (forceUpdate || importedState.hasLedStripDsChanged(¤tState))
{
drawButtonLEDDs(currentState.ledStripDs, displayPartial);
}
if (forceUpdate || importedState.hasLedStripPsChanged(¤tState))
{
drawButtonLEDPs(currentState.ledStripPs, displayPartial);
}
if (forceUpdate || importedState.hasFanChanged(¤tState))
{
drawButtonFan(currentState.fan, displayPartial);
}
if (forceUpdate || importedState.hasWaterLevelGreyChanged(¤tState))
{
drawGuageWater(currentState.waterLevelGrey, displayPartial);
}
if (forceUpdate || importedState.hasBatteryLevelChanged(¤tState))
{
drawGuageBattery(currentState.waterLevelFresh, displayPartial);
}
// Copy to current settings
currentState = importedState;
}
void readSerialTestInput()
{
while (Serial.available())
{
char r = Serial.read();
Serial.print("Have recieved: ");
Serial.println(r);
if (r == 'q') // q
{
Serial.println("Processing q");
importedState.usbFastCharge = !importedState.usbFastCharge;
drawButtonUsb(importedState.usbFastCharge, true);
}
else if (r == 'w') // w
{
importedState.boot12V = !importedState.boot12V;
drawButtonBoot12v(importedState.boot12V, true);
}
else if (r == 'r') // r
{
importedState.waterPump = !importedState.waterPump;
drawButtonWater(importedState.waterPump, true);
}
else if (r == 'e') // e
{
importedState.bootLight = !importedState.bootLight;
drawButton12V(importedState.bootLight, true);
}
else if (r == 'a') // a
{
importedState.ledLights = !importedState.ledLights;
drawButtonLight(importedState.ledLights, true);
}
else if (r == 's') // s
{
importedState.ledStripDs = !importedState.ledStripDs;
drawButtonLEDDs(importedState.ledStripDs, true);
}
else if (r == 'd') // d
{
importedState.ledStripPs = !importedState.ledStripPs;
drawButtonLEDPs(importedState.ledStripPs, true);
}
else if (r == 'f') // f
{
importedState.fan = !importedState.fan;
drawButtonFan(importedState.fan, true);
}
else if (r == 'c') // c
{
displayClearScreen();
}
else if (r == 'v') // v
{
drawPageDevices(&importedState, false);
}
else if (r == 'o') // o
{
displayScreenOff();
}
else if (r >= 48 && r <= 53) // 1-5
{
importedState.batteryLevel = (r - 48) * 20;
drawGuageBattery(importedState.batteryLevel, true);
}
else if (r >= 54 && r <= 57) // 5-9
{
importedState.waterLevelFresh = (r - 54) * 20;
drawGuageWater(importedState.waterLevelFresh, true);
}
else if (r == ',') // f
{
handlePreviousPage();
}
else if (r == '.') // f
{
handleNextPage();
}
}
}
void loop()
{
readSerialTestInput();
delay(10);
// Handle turning the screen on and off
handleScreenOffClick();
handleBtnPageNext();
// Serial.print(("Current Page="));
// Serial.println(currentPage);
if (currentPage == PAGE_DEVICES)
{
// Serial.println(("Read devices button click"));
// Handle button clicks from the devices
handleButtonClick(softSerial, btnWaterPump, actionWaterPumpToggle, 0, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnKitchen12v, actionKitchen12VToggle, 0, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnUsbFastCharge, actionUsbFastChargeToggle, 0, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnLedStripPS, actionLEDStripPSToggle, 0, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnLedStripDs, actionLEDStripDSToggle, 0, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnLedLights, actionLightToggle, 0, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnBoot, actionBoot12VToggle, 0, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnFan, actionFanToggle, 0, actionNone, 0, actionMasterOff, 0, true);
}
else if (currentPage == PAGE_PRESETS)
{
handleButtonClick(softSerial, btnWaterPump, actionSelectPreset, 1, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnKitchen12v, actionSelectPreset, 2, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnUsbFastCharge, actionSelectPreset, 3, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnLedStripPS, actionSelectPreset, 4, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnLedStripDs, actionSelectPreset, 5, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnLedLights, actionSelectPreset, 6, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnBoot, actionSelectPreset, 7, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnFan, actionSelectPreset, 8, actionNone, 0, actionMasterOff, 0, true);
}
else if (currentPage == PAGE_COLOUR)
{
handleButtonClick(softSerial, btnWaterPump, actionLEDStripColour, 1, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnKitchen12v, actionLEDStripColour, 2, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnUsbFastCharge, actionLEDStripColour, 3, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnLedStripPS, actionLEDStripColour, 4, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnLedStripDs, actionLEDStripColour, 5, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnLedLights, actionLEDStripColour, 6, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnBoot, actionLEDStripColour, 7, actionNone, 0, actionMasterOff, 0, true);
handleButtonClick(softSerial, btnFan, actionLEDStripColour, 8, actionNone, 0, actionMasterOff, 0, true);
}
else
{
// TODO handle other PAGES
// handleButtonClick(softSerial, btnWaterPump, action, 0, actionNone, 0, actionMasterOff, 0, true);
// handleButtonClick(softSerial, btnKitchen12v, actionKitchen12VToggle, 0, actionNone, 0, actionMasterOff, 0, true);
// handleButtonClick(softSerial, btnUsbFastCharge, actionUsbFastChargeToggle, 0, actionNone, 0, actionMasterOff, 0, true);
// handleButtonClick(softSerial, btnLedStripPS, actionLEDStripPSToggle, 0, actionNone, 0, actionMasterOff, 0, true);
// handleButtonClick(softSerial, btnLedStripDs, actionLEDStripDSToggle, 0, actionNone, 0, actionMasterOff, 0, true);
// handleButtonClick(softSerial, btnLedLights, actionLightToggle, 0, actionNone, 0, actionMasterOff, 0, true);
// handleButtonClick(softSerial, btnBoot, actionBoot12VToggle, 0, actionNone, 0, actionMasterOff, 0, true);
// handleButtonClick(softSerial, btnFan, actionFanToggle, 0, actionNone, 0, actionMasterOff, 0, true);
}
readFromSerial(&Serial);
readFromSerial(&softSerial);
// see if we need to buzz the buzzer
handleBuzz();
}Loading
epaper-2in9
epaper-2in9
Main