Skip to content

Commit

Permalink
Fix WebUI currency converter, reorganize some code
Browse files Browse the repository at this point in the history
  • Loading branch information
dsbaars committed Sep 18, 2024
1 parent 5dd47c2 commit ff50acf
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 195 deletions.
2 changes: 1 addition & 1 deletion data
1 change: 1 addition & 0 deletions src/lib/block_notify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "lib/led_handler.hpp"
#include "lib/screen_handler.hpp"
#include "lib/timers.hpp"
#include "lib/shared.hpp"

// using namespace websockets;
Expand Down
1 change: 1 addition & 0 deletions src/lib/button_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "lib/screen_handler.hpp"
#include "lib/shared.hpp"
#include "lib/timers.hpp"

extern TaskHandle_t buttonTaskHandle;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ void setupWebsocketClients(void *pvParameters)
{
if (preferences.getBool("ownDataSource", DEFAULT_OWN_DATA_SOURCE))
{
setupV2Notify();
V2Notify::setupV2Notify();
}
else
{
Expand Down Expand Up @@ -553,7 +553,7 @@ void setupHardware()
{
Serial.println(F("Found BH1750"));
hasLuxSensor = true;
bh1750.begin(BH1750::CONTINUOUS_LOW_RES_MODE, 0x5C);
bh1750.begin(BH1750::CONTINUOUS_HIGH_RES_MODE, 0x5C);
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/lib/nostr_notify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "price_notify.hpp"
#include "block_notify.hpp"
#include "lib/timers.hpp"

void setupNostrNotify(bool asDatasource, bool zapNotify);

Expand Down
1 change: 1 addition & 0 deletions src/lib/ota.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "lib/config.hpp"
#include "lib/shared.hpp"
#include "lib/timers.hpp"

#ifndef UPDATE_MESSAGE_HPP
#define UPDATE_MESSAGE_HPP
Expand Down
92 changes: 2 additions & 90 deletions src/lib/screen_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
// TaskHandle_t timeUpdateTaskHandle;
TaskHandle_t taskScreenRotateTaskHandle;
TaskHandle_t workerTaskHandle;
esp_timer_handle_t screenRotateTimer;
esp_timer_handle_t minuteTimer;


std::array<std::string, NUM_SCREENS> taskEpdContent = {};
std::string priceString;
Expand All @@ -23,8 +22,6 @@ void workerTask(void *pvParameters) {
while (1) {
// Wait for a work item to be available in the queue
if (xQueueReceive(workQueue, &receivedItem, portMAX_DELAY)) {
uint firstIndex = 0;

// Process the work item based on its type
switch (receivedItem.type) {
case TASK_BITAXE_UPDATE: {
Expand All @@ -42,10 +39,7 @@ void workerTask(void *pvParameters) {
case TASK_PRICE_UPDATE: {
uint currency = getCurrentCurrency();
uint price = getPrice(currency);
// u_char priceSymbol = '$';
// if (preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE)) {
// priceSymbol = '[';
// }

if (getCurrentScreen() == SCREEN_BTC_TICKER) {
taskEpdContent = parsePriceData(price, currency, preferences.getBool("suffixPrice", DEFAULT_SUFFIX_PRICE));
} else if (getCurrentScreen() == SCREEN_SATS_PER_CURRENCY) {
Expand Down Expand Up @@ -121,29 +115,6 @@ void taskScreenRotate(void *pvParameters) {
}
}

void IRAM_ATTR minuteTimerISR(void *arg) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// vTaskNotifyGiveFromISR(timeUpdateTaskHandle, &xHigherPriorityTaskWoken);
WorkItem timeUpdate = {TASK_TIME_UPDATE, 0};
xQueueSendFromISR(workQueue, &timeUpdate, &xHigherPriorityTaskWoken);

if (bitaxeFetchTaskHandle != NULL) {
vTaskNotifyGiveFromISR(bitaxeFetchTaskHandle, &xHigherPriorityTaskWoken);
}

if (xHigherPriorityTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
}

void IRAM_ATTR screenRotateTimerISR(void *arg) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(taskScreenRotateTaskHandle, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
}

void setupTasks() {
workQueue = xQueueCreate(WORK_QUEUE_SIZE, sizeof(WorkItem));

Expand All @@ -159,65 +130,6 @@ void setupTasks() {
setCurrentScreen(preferences.getUInt("currentScreen", DEFAULT_CURRENT_SCREEN));
}

void setupTimeUpdateTimer(void *pvParameters) {
const esp_timer_create_args_t minuteTimerConfig = {
.callback = &minuteTimerISR, .name = "minute_timer"};

esp_timer_create(&minuteTimerConfig, &minuteTimer);

time_t currentTime;
struct tm timeinfo;
time(&currentTime);
localtime_r(&currentTime, &timeinfo);
uint32_t secondsUntilNextMinute = 60 - timeinfo.tm_sec;

if (secondsUntilNextMinute > 0)
vTaskDelay(pdMS_TO_TICKS((secondsUntilNextMinute * 1000)));

esp_timer_start_periodic(minuteTimer, usPerMinute);

WorkItem timeUpdate = {TASK_TIME_UPDATE, 0};
xQueueSend(workQueue, &timeUpdate, portMAX_DELAY);
// xTaskNotifyGive(timeUpdateTaskHandle);

vTaskDelete(NULL);
}

void setupScreenRotateTimer(void *pvParameters) {
const esp_timer_create_args_t screenRotateTimerConfig = {
.callback = &screenRotateTimerISR, .name = "screen_rotate_timer"};

esp_timer_create(&screenRotateTimerConfig, &screenRotateTimer);

if (preferences.getBool("timerActive", DEFAULT_TIMER_ACTIVE)) {
esp_timer_start_periodic(screenRotateTimer,
getTimerSeconds() * usPerSecond);
}

vTaskDelete(NULL);
}

uint getTimerSeconds() { return preferences.getUInt("timerSeconds", DEFAULT_TIMER_SECONDS); }

bool isTimerActive() { return esp_timer_is_active(screenRotateTimer); }

void setTimerActive(bool status) {
if (status) {
esp_timer_start_periodic(screenRotateTimer,
getTimerSeconds() * usPerSecond);
queueLedEffect(LED_EFFECT_START_TIMER);
preferences.putBool("timerActive", true);
} else {
esp_timer_stop(screenRotateTimer);
queueLedEffect(LED_EFFECT_PAUSE_TIMER);
preferences.putBool("timerActive", false);
}

if (eventSourceTaskHandle != NULL) xTaskNotifyGive(eventSourceTaskHandle);
}

void toggleTimerActive() { setTimerActive(!isTimerActive()); }

uint getCurrentScreen() { return currentScreen; }

void setCurrentScreen(uint newScreen) {
Expand Down
12 changes: 1 addition & 11 deletions src/lib/screen_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
extern TaskHandle_t workerTaskHandle;
extern TaskHandle_t taskScreenRotateTaskHandle;

extern esp_timer_handle_t screenRotateTimer;
extern esp_timer_handle_t minuteTimer;

extern QueueHandle_t workQueue;

typedef enum {
Expand All @@ -42,21 +39,14 @@ void previousScreen();

void showSystemStatusScreen();

void setupTimeUpdateTimer(void *pvParameters);
void setupScreenRotateTimer(void *pvParameters);

void IRAM_ATTR minuteTimerISR(void *arg);
void IRAM_ATTR screenRotateTimerISR(void *arg);

// void taskPriceUpdate(void *pvParameters);
// void taskBlockUpdate(void *pvParameters);
// void taskTimeUpdate(void *pvParameters);
void taskScreenRotate(void *pvParameters);

uint getTimerSeconds();
bool isTimerActive();
void setTimerActive(bool status);
void toggleTimerActive();


void setupTasks();
void setCurrentCurrency(char currency);
Expand Down
86 changes: 86 additions & 0 deletions src/lib/timers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "timers.hpp"

esp_timer_handle_t screenRotateTimer;
esp_timer_handle_t minuteTimer;

void setupTimeUpdateTimer(void *pvParameters) {
const esp_timer_create_args_t minuteTimerConfig = {
.callback = &minuteTimerISR, .name = "minute_timer"};

esp_timer_create(&minuteTimerConfig, &minuteTimer);

time_t currentTime;
struct tm timeinfo;
time(&currentTime);
localtime_r(&currentTime, &timeinfo);
uint32_t secondsUntilNextMinute = 60 - timeinfo.tm_sec;

if (secondsUntilNextMinute > 0)
vTaskDelay(pdMS_TO_TICKS((secondsUntilNextMinute * 1000)));

esp_timer_start_periodic(minuteTimer, usPerMinute);

WorkItem timeUpdate = {TASK_TIME_UPDATE, 0};
xQueueSend(workQueue, &timeUpdate, portMAX_DELAY);
// xTaskNotifyGive(timeUpdateTaskHandle);

vTaskDelete(NULL);
}

void setupScreenRotateTimer(void *pvParameters) {
const esp_timer_create_args_t screenRotateTimerConfig = {
.callback = &screenRotateTimerISR, .name = "screen_rotate_timer"};

esp_timer_create(&screenRotateTimerConfig, &screenRotateTimer);

if (preferences.getBool("timerActive", DEFAULT_TIMER_ACTIVE)) {
esp_timer_start_periodic(screenRotateTimer,
getTimerSeconds() * usPerSecond);
}

vTaskDelete(NULL);
}

uint getTimerSeconds() { return preferences.getUInt("timerSeconds", DEFAULT_TIMER_SECONDS); }

bool isTimerActive() { return esp_timer_is_active(screenRotateTimer); }

void setTimerActive(bool status) {
if (status) {
esp_timer_start_periodic(screenRotateTimer,
getTimerSeconds() * usPerSecond);
queueLedEffect(LED_EFFECT_START_TIMER);
preferences.putBool("timerActive", true);
} else {
esp_timer_stop(screenRotateTimer);
queueLedEffect(LED_EFFECT_PAUSE_TIMER);
preferences.putBool("timerActive", false);
}

if (eventSourceTaskHandle != NULL) xTaskNotifyGive(eventSourceTaskHandle);
}

void toggleTimerActive() { setTimerActive(!isTimerActive()); }

void IRAM_ATTR minuteTimerISR(void *arg) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// vTaskNotifyGiveFromISR(timeUpdateTaskHandle, &xHigherPriorityTaskWoken);
WorkItem timeUpdate = {TASK_TIME_UPDATE, 0};
xQueueSendFromISR(workQueue, &timeUpdate, &xHigherPriorityTaskWoken);

if (bitaxeFetchTaskHandle != NULL) {
vTaskNotifyGiveFromISR(bitaxeFetchTaskHandle, &xHigherPriorityTaskWoken);
}

if (xHigherPriorityTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
}

void IRAM_ATTR screenRotateTimerISR(void *arg) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(taskScreenRotateTaskHandle, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE) {
portYIELD_FROM_ISR();
}
}
22 changes: 22 additions & 0 deletions src/lib/timers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

#include "lib/shared.hpp"
#include "lib/screen_handler.hpp"

extern esp_timer_handle_t screenRotateTimer;
extern esp_timer_handle_t minuteTimer;

void setupTimeUpdateTimer(void *pvParameters);
void setupScreenRotateTimer(void *pvParameters);

void IRAM_ATTR minuteTimerISR(void *arg);
void IRAM_ATTR screenRotateTimerISR(void *arg);

uint getTimerSeconds();
bool isTimerActive();
void setTimerActive(bool status);
void toggleTimerActive();
Loading

0 comments on commit ff50acf

Please sign in to comment.