esmp3/src/main.cpp

98 lines
2.3 KiB
C++
Raw Normal View History

2019-08-02 21:48:36 +00:00
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <SPIFFS.h>
2019-08-02 21:48:36 +00:00
#include "config.h"
#include "controller.h"
#include "player.h"
#include "spi_master.h"
#include "http_server.h"
#include "playlist_manager.h"
2019-08-02 21:48:36 +00:00
Controller* controller;
Player* player;
PlaylistManager* pm;
HTTPServer* http_server;
2019-11-17 13:25:22 +00:00
uint8_t SPIMaster::state = 0;
2019-08-02 21:48:36 +00:00
void setup() {
delay(500);
Serial.begin(74880);
Serial.println("Starting...");
Serial.println("Started.");
INFO("Starting.\n");
#ifdef VERSION
INFO("ESMP3 version %s\n", VERSION);
#else
INFO("ESMP3, version unknown\n");
#endif
INFO("Initializing...\n");
2019-08-13 04:16:08 +00:00
DEBUG("Setting up SPI...\n");
SPI.begin();
SPI.setHwCs(false);
SPIMaster::init();
SPIMaster* spi = new SPIMaster();
INFO("SPI initialized.\n");
DEBUG("Setting up SD card...\n");
spi->select_sd();
if (SD.begin(42, SPI, 25000000)) {
INFO("SD card initialized.\n");
} else {
2019-08-13 04:16:08 +00:00
ERROR("Could not initialize SD card.\n");
}
spi->select_sd(false);
2019-08-13 04:16:08 +00:00
DEBUG("Starting SPIFFS...\n");
SPIFFS.begin(true);
DEBUG("Initializing PlaylistManager...\n");
pm = new PlaylistManager();
DEBUG("Initializing Player and Controller...\n");
player = new Player(spi);
controller = new Controller(player, pm);
INFO("Player and controller initialized.\n");
2019-08-13 04:16:08 +00:00
DEBUG("Connecting to wifi \"%s\"...\n", WIFI_SSID);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
ERROR("Could not connect to Wifi. Rebooting.");
delay(1000);
ESP.restart();
}
INFO("WiFi connected. IP address: %s\n", WiFi.localIP().toString().c_str());
MDNS.begin("esmp3");
DEBUG("Setting up HTTP server...\n");
http_server = new HTTPServer(player, controller);
controller->register_http_server(http_server);
DEBUG("Starting NTP client...\n");
// Taken from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "europe.pool.ntp.org");
struct tm time;
if (getLocalTime(&time, 10000)) {
char buffer[100];
strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", &time);
DEBUG("Got time: %s\n", buffer);
} else {
INFO("Could not fetch current time via NTP.\n");
}
INFO("Initialization completed.\n");
2019-08-02 21:48:36 +00:00
}
void loop() {
bool more_data_needed = player->loop();
if (more_data_needed) return;
2019-08-02 21:48:36 +00:00
controller->loop();
2019-08-02 21:48:36 +00:00
}