75 lines
1.8 KiB
C++
75 lines
1.8 KiB
C++
// Board bring-up and the loop. Everything about running a Lua app -- the state,
|
|
// the bindings, app loading, navigation history -- lives in the shared runtime
|
|
// behind LuaHost.
|
|
|
|
#include <SD.h>
|
|
#include <SPI.h>
|
|
#include <TFT_eSPI.h>
|
|
#include <XPT2046_Touchscreen.h>
|
|
|
|
#include "host/lua_host.h"
|
|
#include "net.h"
|
|
#include "settings.h"
|
|
|
|
static constexpr int SD_CS = 5;
|
|
static constexpr int SD_SCK = 18;
|
|
static constexpr int SD_MOSI = 23;
|
|
static constexpr int SD_MISO = 19;
|
|
static constexpr int TOUCH_CS = 33;
|
|
|
|
SET_LOOP_TASK_STACK_SIZE(16 * 1024);
|
|
|
|
TFT_eSPI tft;
|
|
SPIClass touchSpi(HSPI);
|
|
SPIClass &sdSpi = SPI;
|
|
XPT2046_Touchscreen touch(TOUCH_CS);
|
|
LuaHost host(tft, touch);
|
|
|
|
static bool halted = false;
|
|
|
|
static void fallbackScreen(const char *message) {
|
|
tft.resetViewport(); // no app, no status bar: this message owns the panel
|
|
tft.setRotation(0);
|
|
tft.fillScreen(TFT_WHITE);
|
|
tft.setTextSize(2);
|
|
tft.setTextColor(TFT_RED, TFT_WHITE);
|
|
tft.drawString(message, 10, 10);
|
|
tft.setTextColor(TFT_BLACK, TFT_WHITE);
|
|
tft.drawString("expected /.lua/apps/Home/main.lua", 10, 34);
|
|
Serial.printf("halted: %s\n", message);
|
|
halted = true;
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
tft.begin();
|
|
tft.setRotation(0);
|
|
tft.fillScreen(TFT_WHITE);
|
|
touchSpi.begin(14, 12, 13, TOUCH_CS);
|
|
touch.begin(touchSpi);
|
|
|
|
sdSpi.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
|
|
if (!SD.begin(SD_CS, sdSpi)) {
|
|
fallbackScreen("SD card mount failed");
|
|
return;
|
|
}
|
|
|
|
settings.load(); // absent file keeps the built-in defaults
|
|
net::begin();
|
|
if (!host.begin())
|
|
fallbackScreen("home failed to start");
|
|
}
|
|
|
|
void loop() {
|
|
if (halted) {
|
|
delay(100);
|
|
return;
|
|
}
|
|
net::loop();
|
|
host.loop();
|
|
// The launcher failing to start is the one error no app can recover from.
|
|
if (!host.running())
|
|
fallbackScreen("home failed to start");
|
|
delay(1);
|
|
}
|