NerdNos-Firmware/src/NerdMinerV2.ino.cpp
2023-09-22 01:35:31 +02:00

153 lines
3.9 KiB
C++

#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
#include <esp_task_wdt.h>
#include <OneButton.h>
#include "mbedtls/md.h"
#include "wManager.h"
#include "mining.h"
#include "monitor.h"
#include "drivers/displays/display.h"
#include "drivers/storage/SDCard.h"
//3 seconds WDT
#define WDT_TIMEOUT 3
//15 minutes WDT for miner task
#define WDT_MINER_TIMEOUT 900
#ifdef PIN_BUTTON_1
OneButton button1(PIN_BUTTON_1);
#endif
#ifdef PIN_BUTTON_2
OneButton button2(PIN_BUTTON_2);
#endif
extern monitor_data mMonitor;
SDCard SDCrd = SDCard();
/**********************⚡ GLOBAL Vars *******************************/
unsigned long start = millis();
const char* ntpServer = "pool.ntp.org";
//void runMonitor(void *name);
/********* INIT *****/
void setup()
{
#ifdef MONITOR_SPEED
Serial.begin(MONITOR_SPEED);
#else
Serial.begin(115200);
#endif //MONITOR_SPEED
Serial.setTimeout(0);
delay(100);
esp_task_wdt_init(WDT_MINER_TIMEOUT, true);
// Idle task that would reset WDT never runs, because core 0 gets fully utilized
disableCore0WDT();
//disableCore1WDT();
// Setup the buttons
#if defined(PIN_BUTTON_1) && !defined(PIN_BUTTON_2) //One button device
button1.setPressTicks(5000);
button1.attachClick(switchToNextScreen);
button1.attachDoubleClick(alternateScreenRotation);
button1.attachLongPressStart(reset_configuration);
button1.attachMultiClick(alternateScreenState);
#endif
#if defined(PIN_BUTTON_1) && defined(PIN_BUTTON_2) //Button 1 of two button device
button1.setPressTicks(5000);
button1.attachClick(alternateScreenState);
button1.attachDoubleClick(alternateScreenRotation);
#endif
#if defined(PIN_BUTTON_2) //Button 2 of two button device
button2.setPressTicks(5000);
button2.attachClick(switchToNextScreen);
button2.attachLongPressStart(reset_configuration);
#endif
/******** INIT NERDMINER ************/
Serial.println("NerdMiner v2 starting......");
/******** INIT DISPLAY ************/
initDisplay();
/******** PRINT INIT SCREEN *****/
drawLoadingScreen();
delay(2000);
/******** SHOW LED INIT STATUS (devices without screen) *****/
mMonitor.NerdStatus = NM_waitingConfig;
doLedStuff(0);
/******** INIT WIFI ************/
init_WifiManager();
/******** CREATE TASK TO PRINT SCREEN *****/
//tft.pushImage(0, 0, MinerWidth, MinerHeight, MinerScreen);
// Higher prio monitor task
Serial.println("");
Serial.println("Initiating tasks...");
char *name = (char*) malloc(32);
sprintf(name, "(%s)", "Monitor");
BaseType_t res1 = xTaskCreatePinnedToCore(runMonitor, "Monitor", 10000, (void*)name, 4, NULL,1);
/******** CREATE STRATUM TASK *****/
sprintf(name, "(%s)", "Stratum");
BaseType_t res2 = xTaskCreatePinnedToCore(runStratumWorker, "Stratum", 15000, (void*)name, 3, NULL,1);
/******** CREATE MINER TASKS *****/
//for (size_t i = 0; i < THREADS; i++) {
// char *name = (char*) malloc(32);
// sprintf(name, "(%d)", i);
// Start mining tasks
//BaseType_t res = xTaskCreate(runWorker, name, 35000, (void*)name, 1, NULL);
TaskHandle_t minerTask1, minerTask2 = NULL;
xTaskCreate(runMiner, "Miner0", 6000, (void*)0, 1, &minerTask1);
xTaskCreate(runMiner, "Miner1", 6000, (void*)1, 1, &minerTask2);
esp_task_wdt_add(minerTask1);
esp_task_wdt_add(minerTask2);
/******** MONITOR SETUP *****/
setup_monitor();
}
void app_error_fault_handler(void *arg) {
// Get stack errors
char *stack = (char *)arg;
// Print the stack errors in the console
esp_log_write(ESP_LOG_ERROR, "APP_ERROR", "Pila de errores:\n%s", stack);
// restart ESP32
esp_restart();
}
void loop() {
// keep watching the push buttons:
#ifdef PIN_BUTTON_1
button1.tick();
#endif
#ifdef PIN_BUTTON_2
button2.tick();
#endif
wifiManagerProcess(); // avoid delays() in loop when non-blocking and other long running code
vTaskDelay(50 / portTICK_PERIOD_MS);
}