//
// JOY-IT Raspberry Pi Pico (RBP) Explorer Board (XPLR) based Information Terminal (TERM) Sketch
//
// NOTE: this application is based on the software-structure of PUCONMP575, mixed in technical functions from RBPXPLRDEMO
//

//
// =================== configuration ======================================
//

#include "RBPXPLRTERM_config.h"
#include "softwareInfo.h"
#include "logBuffer.h"
#include "serial.h"               // serial port setup and handling
#include "display.h"              // OLED display
#include "sensor.h"               // sensors library
#include "actuator.h"             // actuators library
#include "printer.h"

#ifndef DISABLE_LEDS
#include "leds.h"                 // LED status indicator(s)
#endif

#include "loopScanner.h"

#ifdef ENA_WIFI
// #include <WiFi.h>
// #include <Dns.h>
#include "net.h"
#ifdef ENA_WEBSERVER
#include "RBPXPLRTERM_webserver.h"
#endif
#else
#error WIFI not defined
#endif

#include "textBuffer.h"
#include "timeouts.h"
#include "timeutils.h"
#include "keyboard.h"
#include "servo.h"
#include "commands.h"

/*
const char* ntpServer          = "pool.ntp.org";    // NTP server to use
const long  gmtOffset_sec      = 3600;              // offset of local time against GMT - CET is 1 hour BEFORE GMT
const int   daylightOffset_sec = 3600;              // daylight saving time is 1 hour advanced against normal time
*/

void printAliveMessage(int lsparam) {
  SERIAL_PRINTF("loop_alive: millis = %s, current time = %s\n",formatLongGrouped(millis()).c_str(),(timeValid()?getTimeString().c_str():"<no-time-available>"));
}

//
// PUCON specific definitions
//
// WiFi Credentials
//
/*
const char* ssid = "PUCONWLAN02";
const char* password = "Hadersdorf";
*/

// Set web server port number to 80
// WiFiServer server(80);

// Variable to store the HTTP request
String header;
 
// Variable to store onboard LED state
String picoLEDState = "off";
 
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
// const long timeoutTime = 2000;

unsigned long lastTickMillis = millis();

/* *****************************************************************************
 *  FUNCTION:     setup
 *  DESCRIPTION:  the central setup routine runs once when you press reset
 */
void setup() { 
  initSerial();
  initLogBuffer();
  startLog();
  initTimeouts();
  initLoopScanner();
  setupEventQueue();
  initLEDs();
  setBlinkStep(BLE_INIT);
  setEcho(false);
#ifdef WIRE_ENABLED
  Wire.begin(I2C_SDA, I2C_SCL);
#endif
  initSensor();
  initActuator();
  initDisplay();
#ifdef ENA_WIFI
  initNet();
  initWebServer();
#endif
  initServo();
  initPrinter();
  initCommands();
  serialPrintSoftwareInfo();
  addLoopScanner(60000,printAliveMessage,0,"aliveLoop");    // print keep-alive message approximately every 60000ms = 60 seconds = 1 minute 
  displayRunningSketch();
  setBlinkStep(BLE_NOTCONN);
  SERIAL_PRINTLN("setup complete, Setup Log:");
  dumpLogBuffer();
}

/* *****************************************************************************
 *  FUNCTION:     loop
 *  DESCRIPTION:  the loop routine runs over and over again forever
 */
void loop() {
	handleSerial();
  loopScanner();
}
