Add unit tests

This commit is contained in:
Djuri 2023-11-28 01:30:36 +01:00
parent 8d2edc40ca
commit 98c036f9e3
14 changed files with 263 additions and 126 deletions

View file

@ -605,4 +605,15 @@ void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
default:
break;
}
}
String getMyHostname() {
uint8_t mac[6];
//WiFi.macAddress(mac);
esp_efuse_mac_get_default(mac);
char hostname[15];
String hostnamePrefix = preferences.getString("hostnamePrefix", "btclock");
snprintf(hostname, sizeof(hostname), "%s-%02x%02x%02x",
hostnamePrefix, mac[3], mac[4], mac[5]);
return hostname;
}

View file

@ -42,6 +42,7 @@ void tryImprovSetup();
void setupTimers();
void finishSetup();
void setupMcp();
String getMyHostname();
std::vector<std::string> getScreenNameMap();
std::vector<std::string> getLocalUrl();

View file

@ -173,6 +173,16 @@ void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent)
setEpdContent(newEpdContent, false);
}
void setEpdContent(std::array<std::string, NUM_SCREENS> newEpdContent) {
std::array<String, NUM_SCREENS> conv;
for (size_t i = 0; i < newEpdContent.size(); ++i) {
conv[i] = String(newEpdContent[i].c_str());
}
return setEpdContent(conv);
}
void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent, bool forceUpdate)
{
std::lock_guard<std::mutex> lock(epdUpdateMutex);

View file

@ -44,5 +44,8 @@ void renderQr(const uint dispNum, const String& text, bool partial);
void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent, bool forceUpdate);
void setEpdContent(std::array<String, NUM_SCREENS> newEpdContent);
void setEpdContent(std::array<std::string, NUM_SCREENS> newEpdContent);
std::array<String, NUM_SCREENS> getCurrentEpdContent();
void waitUntilNoneBusy();

View file

@ -8,7 +8,7 @@ TaskHandle_t workerTaskHandle;
esp_timer_handle_t screenRotateTimer;
esp_timer_handle_t minuteTimer;
std::array<String, NUM_SCREENS> taskEpdContent = {"", "", "", "", "", "", ""};
std::array<std::string, NUM_SCREENS> taskEpdContent = {"", "", "", "", "", "", ""};
std::string priceString;
// typedef enum
@ -45,7 +45,6 @@ void workerTask(void *pvParameters)
{
case TASK_PRICE_UPDATE:
{
firstIndex = 0;
uint price = getPrice();
char priceSymbol = '$';
if (preferences.getBool("fetchEurPrice", false))
@ -54,92 +53,15 @@ void workerTask(void *pvParameters)
}
if (getCurrentScreen() == SCREEN_BTC_TICKER)
{
priceString = (priceSymbol + String(price)).c_str();
if (priceString.length() < (NUM_SCREENS))
{
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
if (preferences.getBool("fetchEurPrice", false))
{
taskEpdContent[0] = "BTC/EUR";
}
else
{
taskEpdContent[0] = "BTC/USD";
}
firstIndex = 1;
}
taskEpdContent = parsePriceData(price, priceSymbol);
}
else if (getCurrentScreen() == SCREEN_MSCW_TIME)
{
priceString = String(int(round(1 / float(price) * 10e7))).c_str();
if (priceString.length() < (NUM_SCREENS))
{
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
if (preferences.getBool("fetchEurPrice", false))
{
taskEpdContent[0] = "SATS/EUR";
} else {
taskEpdContent[0] = "MSCW/TIME";
}
firstIndex = 1;
}
taskEpdContent = parseSatsPerCurrency(price, priceSymbol);
}
else
{
double supply = getSupplyAtBlock(getBlockHeight());
int64_t marketCap = static_cast<std::int64_t>(supply * double(price));
if (preferences.getBool("fetchEurPrice", false))
{
taskEpdContent[0] = "EUR/MCAP";
}
else
{
taskEpdContent[0] = "USD/MCAP";
}
if (preferences.getBool("mcapBigChar", true))
{
firstIndex = 1;
priceString = priceSymbol + formatNumberWithSuffix(marketCap);
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
}
else
{
std::string stringValue = std::to_string(marketCap);
size_t mcLength = stringValue.length();
size_t leadingSpaces = (3 - mcLength % 3) % 3;
stringValue = std::string(leadingSpaces, ' ') + stringValue;
uint groups = (mcLength + leadingSpaces) / 3;
if (groups < NUM_SCREENS)
{
firstIndex = 1;
}
for (int i = firstIndex; i < NUM_SCREENS - groups - 1; i++)
{
taskEpdContent[i] = "";
}
taskEpdContent[NUM_SCREENS - groups - 1] = " $ ";
for (uint i = 0; i < groups; i++)
{
taskEpdContent[(NUM_SCREENS - groups + i)] = stringValue.substr(i * 3, 3).c_str();
}
}
}
if (!(getCurrentScreen() == SCREEN_MARKET_CAP && !preferences.getBool("mcapBigChar", true)))
{
for (uint i = firstIndex; i < NUM_SCREENS; i++)
{
taskEpdContent[i] = priceString[i];
}
taskEpdContent = parseMarketCap(getBlockHeight(), price, priceSymbol, preferences.getBool("mcapBigChar", true));
}
setEpdContent(taskEpdContent);
@ -147,39 +69,13 @@ void workerTask(void *pvParameters)
}
case TASK_BLOCK_UPDATE:
{
std::string blockNrString = String(getBlockHeight()).c_str();
firstIndex = 0;
if (getCurrentScreen() != SCREEN_HALVING_COUNTDOWN)
{
if (blockNrString.length() < NUM_SCREENS)
{
blockNrString.insert(blockNrString.begin(), NUM_SCREENS - blockNrString.length(), ' ');
taskEpdContent[0] = "BLOCK/HEIGHT";
firstIndex = 1;
}
for (uint i = firstIndex; i < NUM_SCREENS; i++)
{
taskEpdContent[i] = blockNrString[i];
}
taskEpdContent = parseBlockHeight(getBlockHeight());
}
else
{
const uint nextHalvingBlock = 210000 - (getBlockHeight() % 210000);
const uint minutesToHalving = nextHalvingBlock * 10;
const int years = floor(minutesToHalving / 525600);
const int days = floor((minutesToHalving - (years * 525600)) / (24 * 60));
const int hours = floor((minutesToHalving - (years * 525600) - (days * (24 * 60))) / 60);
const int mins = floor(minutesToHalving - (years * 525600) - (days * (24 * 60)) - (hours * 60));
taskEpdContent[0] = "BIT/COIN";
taskEpdContent[1] = "HALV/ING";
taskEpdContent[(NUM_SCREENS - 5)] = String(years) + "/YRS";
taskEpdContent[(NUM_SCREENS - 4)] = String(days) + "/DAYS";
taskEpdContent[(NUM_SCREENS - 3)] = String(hours) + "/HRS";
taskEpdContent[(NUM_SCREENS - 2)] = String(mins) + "/MINS";
taskEpdContent[(NUM_SCREENS - 1)] = "TO/GO";
taskEpdContent = parseHalvingCountdown(getBlockHeight());
}
if (getCurrentScreen() == SCREEN_HALVING_COUNTDOWN || getCurrentScreen() == SCREEN_BLOCK_HEIGHT)
@ -206,7 +102,7 @@ void workerTask(void *pvParameters)
timeString = std::to_string(timeinfo.tm_hour) + ":" + minute.c_str();
timeString.insert(timeString.begin(), NUM_SCREENS - timeString.length(), ' ');
taskEpdContent[0] = String(timeinfo.tm_mday) + "/" + String(timeinfo.tm_mon + 1);
taskEpdContent[0] = std::to_string(timeinfo.tm_mday) + "/" + std::to_string(timeinfo.tm_mon + 1);
for (uint i = 1; i < NUM_SCREENS; i++)
{

View file

@ -4,6 +4,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <data_handler.hpp>
#include "price_fetch.hpp"
#include "shared.hpp"

View file

@ -45,3 +45,4 @@ struct SpiRamAllocator {
};
using SpiRamJsonDocument = BasicJsonDocument<SpiRamAllocator>;

View file

@ -1,59 +0,0 @@
#include "utils.hpp"
int modulo(int x, int N)
{
return (x % N + N) % N;
}
String getMyHostname() {
uint8_t mac[6];
//WiFi.macAddress(mac);
esp_efuse_mac_get_default(mac);
char hostname[15];
String hostnamePrefix = preferences.getString("hostnamePrefix", "btclock");
snprintf(hostname, sizeof(hostname), "%s-%02x%02x%02x",
hostnamePrefix, mac[3], mac[4], mac[5]);
return hostname;
}
double getSupplyAtBlock(uint blockNr) {
if (blockNr >= 33 * 210000) {
return 20999999.9769;
}
const int initialBlockReward = 50; // Initial block reward
const int halvingInterval = 210000; // Number of blocks before halving
int halvingCount = blockNr / halvingInterval;
double totalBitcoinInCirculation = 0;
for (int i = 0; i < halvingCount; ++i) {
totalBitcoinInCirculation += halvingInterval * initialBlockReward * std::pow(0.5, i);
}
totalBitcoinInCirculation += (blockNr % halvingInterval) * initialBlockReward * std::pow(0.5, halvingCount);
return totalBitcoinInCirculation;
}
std::string formatNumberWithSuffix(int64_t num) {
const long long quadrillion = 1000000000000000LL;
const long long trillion = 1000000000000LL;
const long long billion = 1000000000;
const long long million = 1000000;
const long long thousand = 1000;
if (num >= quadrillion) {
return std::to_string(num / quadrillion) + "Q";
} else if (num >= trillion) {
return std::to_string(num / trillion) + "T";
} else if (num >= billion) {
return std::to_string(num / billion) + "B";
} else if (num >= million) {
return std::to_string(num / million) + "M";
} else if (num >= thousand) {
return std::to_string(num / thousand) + "K";
} else {
return std::to_string(num);
}
}

View file

@ -1,11 +0,0 @@
#pragma once
#include <WiFi.h>
#include "shared.hpp"
int modulo(int x,int N);
double getSupplyAtBlock(uint blockNr);
String getMyHostname();
std::string formatNumberWithSuffix(int64_t num);

View file

@ -313,7 +313,7 @@ void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json)
}
}
String uintSettings[] = {"minSecPriceUpd", "fullRefreshMin", "ledBrightness", "mcapBigChar"};
String uintSettings[] = {"minSecPriceUpd", "fullRefreshMin", "ledBrightness"};
for (String setting : uintSettings)
{