Skip to content

Commit

Permalink
Basic functions working
Browse files Browse the repository at this point in the history
  • Loading branch information
Romkabouter committed Dec 14, 2020
1 parent 79ee73e commit 235029f
Show file tree
Hide file tree
Showing 9 changed files with 853 additions and 360 deletions.
49 changes: 0 additions & 49 deletions PlatformIO/src/Application.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions PlatformIO/src/Application.h

This file was deleted.

166 changes: 39 additions & 127 deletions PlatformIO/src/MatrixVoiceAudioServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,142 +69,54 @@
- Added configuration webserver
- Improved stability for MQTT stream
v7.0:
- Complete rewrite
- Complete rewrite using StateMachine
* ************************************************************************ */

#include <Arduino.h>
#include <ArduinoOTA.h>
#include <WiFi.h>
// extern "C" {
// #include "freertos/FreeRTOS.h"
// #include "freertos/event_groups.h"
// #include "freertos/timers.h"
// }
#include "Application.h"
#include <StateMachine.hpp>
#include "M5Atom.h"

bool isUpdateInProgess = false;
bool wifi_connected = false;
TimerHandle_t wifiReconnectTimer;
TaskHandle_t applicationTaskHandle;
int retryCount = 0;

void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
retryCount = 0;
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
retryCount++;
if (retryCount > 2) {
Serial.println("Connection Failed! Rebooting...");
ESP.restart();
}
}
}

void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_STA_START:
WiFi.setHostname(HOSTNAME);
break;
case SYSTEM_EVENT_STA_GOT_IP:
wifi_connected = true;
//xTaskNotify(applicationTaskHandle, 1, eSetBits);
Serial.println("Connected to Wifi with IP: " + WiFi.localIP().toString());
xTimerStop(wifiReconnectTimer, 0); // Stop the reconnect timer
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
wifi_connected = false;
Serial.println("Disconnected from Wifi!");
xTimerStart(wifiReconnectTimer, 0); // Start the reconnect timer
break;
default:
break;
}
}

void applicationTask(void *param)
{
Application *application = static_cast<Application *>(param);

// const TickType_t xMaxBlockTime = pdMS_TO_TICKS(100);
// uint32_t ulNotificationValue = 1;
while (true)
{
//uint32_t ulNotificationValue = ulTaskNotifyTake(pdTRUE, xMaxBlockTime);

// if (ulNotificationValue > 0)
// {
application->run();
// ulNotificationValue = 0;
// }
}
}

/* ************************************************************************ *
SETUP
* ************************************************************************ */
void setup() {
Serial.begin(115200);
Serial.println("Booting");
//Application *application = new Application();

wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdTRUE, (void *)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));

WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
retryCount++;
if (retryCount > 2) {
Serial.println("Connection Failed! Rebooting...");
ESP.restart();
} else {
Serial.println("Connection Failed! Retry...");
}
}

// xTaskCreatePinnedToCore(applicationTask, "Application Task", 8192, application, 1, &applicationTaskHandle, 0);
//xTaskNotify(applicationTaskHandle, 1, eIncrement);

// ---------------------------------------------------------------------------
// ArduinoOTA
// ---------------------------------------------------------------------------
ArduinoOTA.setPasswordHash(OTA_PASS_HASH);

ArduinoOTA
.onStart([]() {
// vTaskSuspend(applicationTaskHandle);
isUpdateInProgess = true;
Serial.println("Uploading...");
xTimerStop(wifiReconnectTimer, 0);
})
.onEnd([]() {
isUpdateInProgess = false;
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR)
Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR)
Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR)
Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.begin(115200);
Serial.println("Booting");
M5.begin(true,true,true);
// ---------------------------------------------------------------------------
// ArduinoOTA
// ---------------------------------------------------------------------------
ArduinoOTA.setPasswordHash(OTA_PASS_HASH);

ArduinoOTA
.onStart([]() {
Serial.println("Uploading...");
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR)
Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR)
Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR)
Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed");
});

fsm_list::start();
}

/* ************************************************************************ *
MAIN LOOP
* ************************************************************************ */
void loop() {
if (WiFi.isConnected()) {
ArduinoOTA.handle();
}
fsm_list::run();
}
Loading

0 comments on commit 235029f

Please sign in to comment.