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>
#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> ret;
std::string priceString;
if (std::to_string(price).length() >= NUM_SCREENS || useSuffixFormat) {
priceString = currencySymbol + formatNumberWithSuffix(price, NUM_SCREENS-2);
} else {
priceString = currencySymbol + std::to_string(price);
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);
}
std::uint32_t firstIndex = 0;
if (priceString.length() < (NUM_SCREENS))
{
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
if (currencySymbol == '[')
{
ret[0] = "BTC/EUR";
}
else
{
ret[0] = "BTC/USD";
}
ret[0] = "BTC/" + getCurrencyCode(currencySymbol);
firstIndex = 1;
}
@ -36,7 +85,7 @@ std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char cu
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::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(), ' ');
if (currencySymbol == '[')
{
ret[0] = "SATS/EUR";
}
else
{
if (currencySymbol != CURRENCY_USD)
ret[0] = "SATS/" + getCurrencyCode(currencySymbol);
else
ret[0] = "MSCW/TIME";
}
firstIndex = 1;
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];
}
if (withSatsSymbol) {
if (withSatsSymbol)
{
ret[insertSatSymbol] = "STS";
}
}
@ -91,7 +137,8 @@ std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight)
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::string blockFeesString = std::to_string(blockFees);
std::uint32_t firstIndex = 0;
@ -103,12 +150,12 @@ std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees) {
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[NUM_SCREENS-1] = "sat/vB";
ret[NUM_SCREENS - 1] = "sat/vB";
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 minutesToHalving = nextHalvingBlock * 10;
if (asBlocks) {
if (asBlocks)
{
std::string blockNrString = std::to_string(nextHalvingBlock);
std::uint32_t firstIndex = 0;
@ -134,9 +182,9 @@ std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHe
{
ret[i] = blockNrString[i];
}
} else {
}
else
{
const int years = floor(minutesToHalving / 525600);
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;
// Serial.print("Market cap: ");
// 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(), ' ');
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__
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();
for (const auto& str : arr) {
for (const auto &str : arr)
{
jsArray.call<void>("push", str);
}
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();
for (size_t i = 0; i < vec.size(); ++i) {
for (size_t i = 0; i < vec.size(); ++i)
{
jsArray.set(i, vec[i]);
}
return jsArray;
}
emscripten::val parseBlockHeightArray(std::uint32_t blockHeight) {
emscripten::val parseBlockHeightArray(std::uint32_t 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));
}
emscripten::val parseHalvingCountdownArray(std::uint32_t blockHeight, bool asBlocks) {
emscripten::val parseHalvingCountdownArray(std::uint32_t blockHeight, bool 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));
}
emscripten::val parseBlockFeesArray(std::uint16_t blockFees) {
emscripten::val parseBlockFeesArray(std::uint16_t 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));
}
EMSCRIPTEN_BINDINGS(my_module) {
// emscripten::register_vector<std::string>("StringList");
EMSCRIPTEN_BINDINGS(my_module)
{
// emscripten::register_vector<std::string>("StringList");
emscripten::function("parseBlockHeight", &parseBlockHeightArray);
emscripten::function("parseBlockHeight", &parseBlockHeightArray);
emscripten::function("parseHalvingCountdown", &parseHalvingCountdownArray);
emscripten::function("parseMarketCap", &parseMarketCapArray);
emscripten::function("parseBlockFees", &parseBlockFeesArray);

View File

@ -5,9 +5,20 @@
#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> parseBlockHeight(std::uint32_t blockHeight);
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> 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;
case TASK_PRICE_UPDATE: {
uint price = getPrice();
char priceSymbol = '$';
u_char priceSymbol = '$';
if (preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE)) {
priceSymbol = '[';
}