btclock_v3/lib/btclock/data_handler.cpp

334 lines
9.6 KiB
C++
Raw Permalink Normal View History

2023-11-28 01:30:36 +01:00
#include "data_handler.hpp"
2024-05-09 15:43:44 +02:00
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/bind.h>
#endif
2023-11-28 01:30:36 +01:00
2024-08-15 00:37:50 +02:00
char getCurrencySymbol(char input)
{
switch (input)
{
case CURRENCY_EUR:
return '[';
break;
case CURRENCY_GBP:
return ']';
2024-08-15 00:37:50 +02:00
break;
case CURRENCY_JPY:
return '^';
2024-08-15 00:37:50 +02:00
break;
case CURRENCY_AUD:
case CURRENCY_CAD:
case CURRENCY_USD:
return '$';
break;
default:
return input;
}
}
std::string getCurrencyCode(char input)
{
switch (input)
{
case CURRENCY_EUR:
return CURRENCY_CODE_EUR;
2024-08-15 00:37:50 +02:00
break;
case CURRENCY_GBP:
return CURRENCY_CODE_GBP;
2024-08-15 00:37:50 +02:00
break;
case CURRENCY_JPY:
return CURRENCY_CODE_JPY;
2024-08-15 00:37:50 +02:00
break;
case CURRENCY_AUD:
return CURRENCY_CODE_AUD;
2024-08-15 00:37:50 +02:00
break;
case CURRENCY_CAD:
return CURRENCY_CODE_CAD;
2024-08-15 00:37:50 +02:00
break;
default:
return CURRENCY_CODE_USD;
2024-08-15 00:37:50 +02:00
}
}
char getCurrencyChar(const std::string& input)
{
if (input == "EUR")
return CURRENCY_EUR;
else if (input == "GBP")
return CURRENCY_GBP;
else if (input == "JPY")
return CURRENCY_JPY;
else if (input == "AUD")
return CURRENCY_AUD;
else if (input == "CAD")
return CURRENCY_CAD;
else
return CURRENCY_USD; // Assuming USD is the default for unknown inputs
}
2024-03-11 21:21:15 +01:00
std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char currencySymbol, bool useSuffixFormat)
2023-11-28 01:30:36 +01:00
{
std::array<std::string, NUM_SCREENS> ret;
2023-11-28 02:05:04 +01:00
std::string priceString;
2024-08-15 00:37:50 +02:00
if (std::to_string(price).length() >= NUM_SCREENS || useSuffixFormat)
{
priceString = getCurrencySymbol(currencySymbol) + formatNumberWithSuffix(price, NUM_SCREENS - 2);
}
else
{
priceString = getCurrencySymbol(currencySymbol) + std::to_string(price);
2023-11-28 02:05:04 +01:00
}
std::uint32_t firstIndex = 0;
2023-11-28 01:30:36 +01:00
if (priceString.length() < (NUM_SCREENS))
{
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
2024-08-15 00:37:50 +02:00
ret[0] = "BTC/" + getCurrencyCode(currencySymbol);
2023-11-28 01:30:36 +01:00
firstIndex = 1;
}
2023-11-28 02:05:04 +01:00
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
2023-11-28 01:30:36 +01:00
{
ret[i] = priceString[i];
}
return ret;
}
2024-08-15 00:37:50 +02:00
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price,char currencySymbol, bool withSatsSymbol)
2023-11-28 01:30:36 +01:00
{
std::array<std::string, NUM_SCREENS> ret;
std::string priceString = std::to_string(int(round(1 / float(price) * 10e7)));
2023-11-28 02:05:04 +01:00
std::uint32_t firstIndex = 0;
2024-05-09 15:43:44 +02:00
std::uint8_t insertSatSymbol = NUM_SCREENS - priceString.length() - 1;
2023-11-28 01:30:36 +01:00
if (priceString.length() < (NUM_SCREENS))
{
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
2024-08-15 00:37:50 +02:00
if (currencySymbol != CURRENCY_USD)
ret[0] = "SATS/" + getCurrencyCode(currencySymbol);
else
2023-11-28 01:30:36 +01:00
ret[0] = "MSCW/TIME";
2024-08-15 00:37:50 +02:00
2023-11-28 01:30:36 +01:00
firstIndex = 1;
2023-11-28 02:05:04 +01:00
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
2023-11-28 01:30:36 +01:00
{
ret[i] = priceString[i];
}
2024-08-15 00:37:50 +02:00
if (withSatsSymbol)
{
ret[insertSatSymbol] = "STS";
}
2023-11-28 01:30:36 +01:00
}
return ret;
}
2023-11-28 02:05:04 +01:00
std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight)
2023-11-28 01:30:36 +01:00
{
std::array<std::string, NUM_SCREENS> ret;
std::string blockNrString = std::to_string(blockHeight);
2023-11-28 02:05:04 +01:00
std::uint32_t firstIndex = 0;
2023-11-28 01:30:36 +01:00
if (blockNrString.length() < NUM_SCREENS)
{
blockNrString.insert(blockNrString.begin(), NUM_SCREENS - blockNrString.length(), ' ');
ret[0] = "BLOCK/HEIGHT";
firstIndex = 1;
}
2023-11-28 02:05:04 +01:00
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
2023-11-28 01:30:36 +01:00
{
ret[i] = blockNrString[i];
}
return ret;
}
2024-08-15 00:37:50 +02:00
std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees)
{
std::array<std::string, NUM_SCREENS> ret;
std::string blockFeesString = std::to_string(blockFees);
std::uint32_t firstIndex = 0;
if (blockFeesString.length() < NUM_SCREENS)
{
blockFeesString.insert(blockFeesString.begin(), NUM_SCREENS - blockFeesString.length() - 1, ' ');
ret[0] = "FEE/RATE";
firstIndex = 1;
}
2024-08-15 00:37:50 +02:00
for (std::uint8_t i = firstIndex; i < NUM_SCREENS - 1; i++)
{
ret[i] = blockFeesString[i];
}
2024-08-15 00:37:50 +02:00
ret[NUM_SCREENS - 1] = "sat/vB";
return ret;
}
std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHeight, bool asBlocks)
2023-11-28 01:30:36 +01:00
{
std::array<std::string, NUM_SCREENS> ret;
2023-11-28 02:05:04 +01:00
const std::uint32_t nextHalvingBlock = 210000 - (blockHeight % 210000);
const std::uint32_t minutesToHalving = nextHalvingBlock * 10;
2023-11-28 01:30:36 +01:00
2024-08-15 00:37:50 +02:00
if (asBlocks)
{
std::string blockNrString = std::to_string(nextHalvingBlock);
std::uint32_t firstIndex = 0;
if (blockNrString.length() < NUM_SCREENS)
{
blockNrString.insert(blockNrString.begin(), NUM_SCREENS - blockNrString.length(), ' ');
ret[0] = "HAL/VING";
firstIndex = 1;
}
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
{
ret[i] = blockNrString[i];
}
2024-08-15 00:37:50 +02:00
}
else
{
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));
ret[0] = "BIT/COIN";
ret[1] = "HAL/VING";
ret[(NUM_SCREENS - 5)] = std::to_string(years) + "/YRS";
ret[(NUM_SCREENS - 4)] = std::to_string(days) + "/DAYS";
ret[(NUM_SCREENS - 3)] = std::to_string(hours) + "/HRS";
ret[(NUM_SCREENS - 2)] = std::to_string(mins) + "/MINS";
ret[(NUM_SCREENS - 1)] = "TO/GO";
}
2023-11-28 01:30:36 +01:00
return ret;
}
2023-11-28 02:05:04 +01:00
std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars)
2023-11-28 01:30:36 +01:00
{
std::array<std::string, NUM_SCREENS> ret;
2023-11-28 02:05:04 +01:00
std::uint32_t firstIndex = 0;
2023-11-28 01:30:36 +01:00
double supply = getSupplyAtBlock(blockHeight);
uint64_t marketCap = static_cast<std::uint64_t>(supply * double(price));
ret[0] = getCurrencyCode(currencySymbol) + "/MCAP";
2023-11-28 01:30:36 +01:00
if (bigChars)
{
firstIndex = 1;
// Serial.print("Market cap: ");
// Serial.println(marketCap);
2024-08-15 00:37:50 +02:00
std::string priceString = currencySymbol + formatNumberWithSuffix(marketCap, (NUM_SCREENS - 2));
2023-11-28 01:30:36 +01:00
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
2023-11-28 02:05:04 +01:00
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
2023-11-28 01:30:36 +01:00
{
ret[i] = priceString[i];
}
}
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;
2023-11-28 02:05:04 +01:00
std::uint32_t groups = (mcLength + leadingSpaces) / 3;
2023-11-28 01:30:36 +01:00
if (groups < NUM_SCREENS)
{
firstIndex = 1;
}
for (int i = firstIndex; i < NUM_SCREENS - groups - 1; i++)
{
ret[i] = "";
}
ret[NUM_SCREENS - groups - 1] = std::string(" ") + currencySymbol + " ";
2023-11-28 02:05:04 +01:00
for (std::uint32_t i = 0; i < groups; i++)
2023-11-28 01:30:36 +01:00
{
ret[(NUM_SCREENS - groups + i)] = stringValue.substr(i * 3, 3).c_str();
}
}
return ret;
2024-05-09 15:43:44 +02:00
}
#ifdef __EMSCRIPTEN__
2024-08-15 00:37:50 +02:00
emscripten::val arrayToStringArray(const std::array<std::string, NUM_SCREENS> &arr)
{
2024-05-09 15:43:44 +02:00
emscripten::val jsArray = emscripten::val::array();
2024-08-15 00:37:50 +02:00
for (const auto &str : arr)
{
2024-05-09 15:43:44 +02:00
jsArray.call<void>("push", str);
}
return jsArray;
}
2024-08-15 00:37:50 +02:00
emscripten::val vectorToStringArray(const std::vector<std::string> &vec)
{
2024-05-09 15:43:44 +02:00
emscripten::val jsArray = emscripten::val::array();
2024-08-15 00:37:50 +02:00
for (size_t i = 0; i < vec.size(); ++i)
{
2024-05-09 15:43:44 +02:00
jsArray.set(i, vec[i]);
}
return jsArray;
}
2024-08-15 00:37:50 +02:00
emscripten::val parseBlockHeightArray(std::uint32_t blockHeight)
{
2024-05-09 15:43:44 +02:00
return arrayToStringArray(parseBlockHeight(blockHeight));
}
2024-08-15 00:37:50 +02:00
emscripten::val parsePriceDataArray(std::uint32_t price, const std::string &currencySymbol, bool useSuffixFormat = false)
{
2024-05-09 15:43:44 +02:00
return arrayToStringArray(parsePriceData(price, currencySymbol[0], useSuffixFormat));
}
2024-08-15 00:37:50 +02:00
emscripten::val parseHalvingCountdownArray(std::uint32_t blockHeight, bool asBlocks)
{
2024-05-10 01:09:07 +02:00
return arrayToStringArray(parseHalvingCountdown(blockHeight, asBlocks));
}
2024-08-15 00:37:50 +02:00
emscripten::val parseMarketCapArray(std::uint32_t blockHeight, std::uint32_t price, const std::string &currencySymbol, bool bigChars)
{
2024-05-10 01:09:07 +02:00
return arrayToStringArray(parseMarketCap(blockHeight, price, currencySymbol[0], bigChars));
}
2024-08-15 00:37:50 +02:00
emscripten::val parseBlockFeesArray(std::uint16_t blockFees)
{
2024-05-10 01:09:07 +02:00
return arrayToStringArray(parseBlockFees(blockFees));
}
2024-08-15 00:37:50 +02:00
emscripten::val parseSatsPerCurrencyArray(std::uint32_t price, const std::string &currencySymbol, bool withSatsSymbol)
{
2024-05-10 01:09:07 +02:00
return arrayToStringArray(parseSatsPerCurrency(price, currencySymbol[0], withSatsSymbol));
}
2024-08-15 00:37:50 +02:00
EMSCRIPTEN_BINDINGS(my_module)
{
// emscripten::register_vector<std::string>("StringList");
2024-05-09 15:43:44 +02:00
2024-08-15 00:37:50 +02:00
emscripten::function("parseBlockHeight", &parseBlockHeightArray);
2024-05-10 01:09:07 +02:00
emscripten::function("parseHalvingCountdown", &parseHalvingCountdownArray);
emscripten::function("parseMarketCap", &parseMarketCapArray);
emscripten::function("parseBlockFees", &parseBlockFeesArray);
emscripten::function("parseSatsPerCurrency", &parseSatsPerCurrencyArray);
2024-05-09 15:43:44 +02:00
emscripten::function("parsePriceData", &parsePriceDataArray);
emscripten::function("arrayToStringArray", &arrayToStringArray);
emscripten::function("vectorToStringArray", &vectorToStringArray);
}
#endif