Begin support for other currencies

This commit is contained in:
Djuri Baars 2024-08-15 00:37:50 +02:00
parent b13c7242a6
commit 0a08c5f9ea
3 changed files with 115 additions and 45 deletions

View File

@ -4,27 +4,76 @@
#include <emscripten/bind.h> #include <emscripten/bind.h>
#endif #endif
char getCurrencySymbol(char input)
{
switch (input)
{
case CURRENCY_EUR:
return '[';
break;
case CURRENCY_GBP:
return '\\';
break;
case CURRENCY_JPY:
return ']';
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 "EUR";
break;
case CURRENCY_GBP:
return "GBP";
break;
case CURRENCY_JPY:
return "YEN";
break;
case CURRENCY_AUD:
return "AUD";
break;
case CURRENCY_CHF:
return "CHF";
break;
case CURRENCY_CAD:
return "CAD";
break;
default:
return "USD";
}
}
std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char currencySymbol, bool useSuffixFormat) std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char currencySymbol, bool useSuffixFormat)
{ {
std::array<std::string, NUM_SCREENS> ret; std::array<std::string, NUM_SCREENS> ret;
std::string priceString; std::string priceString;
if (std::to_string(price).length() >= NUM_SCREENS || useSuffixFormat) { if (std::to_string(price).length() >= NUM_SCREENS || useSuffixFormat)
priceString = currencySymbol + formatNumberWithSuffix(price, NUM_SCREENS-2); {
} else { priceString = getCurrencySymbol(currencySymbol) + formatNumberWithSuffix(price, NUM_SCREENS - 2);
priceString = currencySymbol + std::to_string(price); }
else
{
priceString = getCurrencySymbol(currencySymbol) + std::to_string(price);
} }
std::uint32_t firstIndex = 0; std::uint32_t firstIndex = 0;
if (priceString.length() < (NUM_SCREENS)) if (priceString.length() < (NUM_SCREENS))
{ {
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' '); priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
if (currencySymbol == '[')
{ ret[0] = "BTC/" + getCurrencyCode(currencySymbol);
ret[0] = "BTC/EUR";
}
else
{
ret[0] = "BTC/USD";
}
firstIndex = 1; firstIndex = 1;
} }
@ -36,7 +85,7 @@ std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char cu
return ret; return ret;
} }
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price, char currencySymbol, bool withSatsSymbol) std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price,char currencySymbol, bool withSatsSymbol)
{ {
std::array<std::string, NUM_SCREENS> ret; std::array<std::string, NUM_SCREENS> ret;
std::string priceString = std::to_string(int(round(1 / float(price) * 10e7))); std::string priceString = std::to_string(int(round(1 / float(price) * 10e7)));
@ -47,15 +96,11 @@ std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price, c
{ {
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' '); priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
if (currencySymbol != CURRENCY_USD)
if (currencySymbol == '[') ret[0] = "SATS/" + getCurrencyCode(currencySymbol);
{ else
ret[0] = "SATS/EUR";
}
else
{
ret[0] = "MSCW/TIME"; ret[0] = "MSCW/TIME";
}
firstIndex = 1; firstIndex = 1;
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++) for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
@ -63,7 +108,8 @@ std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price, c
ret[i] = priceString[i]; ret[i] = priceString[i];
} }
if (withSatsSymbol) { if (withSatsSymbol)
{
ret[insertSatSymbol] = "STS"; ret[insertSatSymbol] = "STS";
} }
} }
@ -91,7 +137,8 @@ std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight)
return ret; return ret;
} }
std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees) { std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees)
{
std::array<std::string, NUM_SCREENS> ret; std::array<std::string, NUM_SCREENS> ret;
std::string blockFeesString = std::to_string(blockFees); std::string blockFeesString = std::to_string(blockFees);
std::uint32_t firstIndex = 0; std::uint32_t firstIndex = 0;
@ -103,12 +150,12 @@ std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees) {
firstIndex = 1; firstIndex = 1;
} }
for (std::uint8_t i = firstIndex; i < NUM_SCREENS-1; i++) for (std::uint8_t i = firstIndex; i < NUM_SCREENS - 1; i++)
{ {
ret[i] = blockFeesString[i]; ret[i] = blockFeesString[i];
} }
ret[NUM_SCREENS-1] = "sat/vB"; ret[NUM_SCREENS - 1] = "sat/vB";
return ret; return ret;
} }
@ -119,7 +166,8 @@ std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHe
const std::uint32_t nextHalvingBlock = 210000 - (blockHeight % 210000); const std::uint32_t nextHalvingBlock = 210000 - (blockHeight % 210000);
const std::uint32_t minutesToHalving = nextHalvingBlock * 10; const std::uint32_t minutesToHalving = nextHalvingBlock * 10;
if (asBlocks) { if (asBlocks)
{
std::string blockNrString = std::to_string(nextHalvingBlock); std::string blockNrString = std::to_string(nextHalvingBlock);
std::uint32_t firstIndex = 0; std::uint32_t firstIndex = 0;
@ -134,9 +182,9 @@ std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHe
{ {
ret[i] = blockNrString[i]; ret[i] = blockNrString[i];
} }
}
} else { else
{
const int years = floor(minutesToHalving / 525600); const int years = floor(minutesToHalving / 525600);
const int days = floor((minutesToHalving - (years * 525600)) / (24 * 60)); const int days = floor((minutesToHalving - (years * 525600)) / (24 * 60));
@ -174,7 +222,7 @@ std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, s
firstIndex = 1; firstIndex = 1;
// Serial.print("Market cap: "); // Serial.print("Market cap: ");
// Serial.println(marketCap); // Serial.println(marketCap);
std::string priceString = currencySymbol + formatNumberWithSuffix(marketCap, (NUM_SCREENS-2)); std::string priceString = currencySymbol + formatNumberWithSuffix(marketCap, (NUM_SCREENS - 2));
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' '); priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++) for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
@ -212,50 +260,61 @@ std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, s
} }
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
emscripten::val arrayToStringArray(const std::array<std::string, NUM_SCREENS>& arr) { emscripten::val arrayToStringArray(const std::array<std::string, NUM_SCREENS> &arr)
{
emscripten::val jsArray = emscripten::val::array(); emscripten::val jsArray = emscripten::val::array();
for (const auto& str : arr) { for (const auto &str : arr)
{
jsArray.call<void>("push", str); jsArray.call<void>("push", str);
} }
return jsArray; return jsArray;
} }
emscripten::val vectorToStringArray(const std::vector<std::string>& vec) { emscripten::val vectorToStringArray(const std::vector<std::string> &vec)
{
emscripten::val jsArray = emscripten::val::array(); emscripten::val jsArray = emscripten::val::array();
for (size_t i = 0; i < vec.size(); ++i) { for (size_t i = 0; i < vec.size(); ++i)
{
jsArray.set(i, vec[i]); jsArray.set(i, vec[i]);
} }
return jsArray; return jsArray;
} }
emscripten::val parseBlockHeightArray(std::uint32_t blockHeight) { emscripten::val parseBlockHeightArray(std::uint32_t blockHeight)
{
return arrayToStringArray(parseBlockHeight(blockHeight)); return arrayToStringArray(parseBlockHeight(blockHeight));
} }
emscripten::val parsePriceDataArray(std::uint32_t price, const std::string& currencySymbol, bool useSuffixFormat = false) { emscripten::val parsePriceDataArray(std::uint32_t price, const std::string &currencySymbol, bool useSuffixFormat = false)
{
return arrayToStringArray(parsePriceData(price, currencySymbol[0], useSuffixFormat)); return arrayToStringArray(parsePriceData(price, currencySymbol[0], useSuffixFormat));
} }
emscripten::val parseHalvingCountdownArray(std::uint32_t blockHeight, bool asBlocks) { emscripten::val parseHalvingCountdownArray(std::uint32_t blockHeight, bool asBlocks)
{
return arrayToStringArray(parseHalvingCountdown(blockHeight, asBlocks)); return arrayToStringArray(parseHalvingCountdown(blockHeight, asBlocks));
} }
emscripten::val parseMarketCapArray(std::uint32_t blockHeight, std::uint32_t price, const std::string& currencySymbol, bool bigChars) { emscripten::val parseMarketCapArray(std::uint32_t blockHeight, std::uint32_t price, const std::string &currencySymbol, bool bigChars)
{
return arrayToStringArray(parseMarketCap(blockHeight, price, currencySymbol[0], bigChars)); return arrayToStringArray(parseMarketCap(blockHeight, price, currencySymbol[0], bigChars));
} }
emscripten::val parseBlockFeesArray(std::uint16_t blockFees) { emscripten::val parseBlockFeesArray(std::uint16_t blockFees)
{
return arrayToStringArray(parseBlockFees(blockFees)); return arrayToStringArray(parseBlockFees(blockFees));
} }
emscripten::val parseSatsPerCurrencyArray(std::uint32_t price, const std::string& currencySymbol, bool withSatsSymbol) { emscripten::val parseSatsPerCurrencyArray(std::uint32_t price, const std::string &currencySymbol, bool withSatsSymbol)
{
return arrayToStringArray(parseSatsPerCurrency(price, currencySymbol[0], withSatsSymbol)); return arrayToStringArray(parseSatsPerCurrency(price, currencySymbol[0], withSatsSymbol));
} }
EMSCRIPTEN_BINDINGS(my_module) { EMSCRIPTEN_BINDINGS(my_module)
// emscripten::register_vector<std::string>("StringList"); {
// emscripten::register_vector<std::string>("StringList");
emscripten::function("parseBlockHeight", &parseBlockHeightArray); emscripten::function("parseBlockHeight", &parseBlockHeightArray);
emscripten::function("parseHalvingCountdown", &parseHalvingCountdownArray); emscripten::function("parseHalvingCountdown", &parseHalvingCountdownArray);
emscripten::function("parseMarketCap", &parseMarketCapArray); emscripten::function("parseMarketCap", &parseMarketCapArray);
emscripten::function("parseBlockFees", &parseBlockFeesArray); emscripten::function("parseBlockFees", &parseBlockFeesArray);

View File

@ -5,9 +5,20 @@
#include "utils.hpp" #include "utils.hpp"
std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char currencySymbol, bool useSuffixFormat = false); const char CURRENCY_USD = '$';
const char CURRENCY_EUR = '[';
const char CURRENCY_GBP = '\\';
const char CURRENCY_JPY = ']';
const char CURRENCY_AUD = '^';
const char CURRENCY_CHF = '_';
const char CURRENCY_CAD = '`';
std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char currency, bool useSuffixFormat = false);
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price, char currencySymbol, bool withSatsSymbol); std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price, char currencySymbol, bool withSatsSymbol);
std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight); std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight);
std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHeight, bool asBlocks); std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHeight, bool asBlocks);
std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars); std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars);
std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees); std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees);
char getCurrencySymbol(char input);
std::string getCurrencyCode(char input);

View File

@ -41,7 +41,7 @@ void workerTask(void *pvParameters) {
break; break;
case TASK_PRICE_UPDATE: { case TASK_PRICE_UPDATE: {
uint price = getPrice(); uint price = getPrice();
char priceSymbol = '$'; u_char priceSymbol = '$';
if (preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE)) { if (preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE)) {
priceSymbol = '['; priceSymbol = '[';
} }