From 0a08c5f9eaa2dab3f47cfe73879c7d7bebde844c Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Thu, 15 Aug 2024 00:37:50 +0200 Subject: [PATCH] Begin support for other currencies --- lib/btclock/data_handler.cpp | 145 ++++++++++++++++++++++++----------- lib/btclock/data_handler.hpp | 13 +++- src/lib/screen_handler.cpp | 2 +- 3 files changed, 115 insertions(+), 45 deletions(-) diff --git a/lib/btclock/data_handler.cpp b/lib/btclock/data_handler.cpp index 7d8e9d0..6592807 100644 --- a/lib/btclock/data_handler.cpp +++ b/lib/btclock/data_handler.cpp @@ -4,27 +4,76 @@ #include #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 parsePriceData(std::uint32_t price, char currencySymbol, bool useSuffixFormat) { std::array 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 parsePriceData(std::uint32_t price, char cu return ret; } -std::array parseSatsPerCurrency(std::uint32_t price, char currencySymbol, bool withSatsSymbol) +std::array parseSatsPerCurrency(std::uint32_t price,char currencySymbol, bool withSatsSymbol) { std::array ret; std::string priceString = std::to_string(int(round(1 / float(price) * 10e7))); @@ -47,15 +96,11 @@ std::array 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 parseSatsPerCurrency(std::uint32_t price, c ret[i] = priceString[i]; } - if (withSatsSymbol) { + if (withSatsSymbol) + { ret[insertSatSymbol] = "STS"; } } @@ -91,7 +137,8 @@ std::array parseBlockHeight(std::uint32_t blockHeight) return ret; } -std::array parseBlockFees(std::uint16_t blockFees) { +std::array parseBlockFees(std::uint16_t blockFees) +{ std::array ret; std::string blockFeesString = std::to_string(blockFees); std::uint32_t firstIndex = 0; @@ -103,12 +150,12 @@ std::array 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 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 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 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 parseMarketCap(std::uint32_t blockHeight, s } #ifdef __EMSCRIPTEN__ -emscripten::val arrayToStringArray(const std::array& arr) { +emscripten::val arrayToStringArray(const std::array &arr) +{ emscripten::val jsArray = emscripten::val::array(); - for (const auto& str : arr) { + for (const auto &str : arr) + { jsArray.call("push", str); } return jsArray; } -emscripten::val vectorToStringArray(const std::vector& vec) { +emscripten::val vectorToStringArray(const std::vector &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 ¤cySymbol, 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 ¤cySymbol, 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 ¤cySymbol, bool withSatsSymbol) +{ return arrayToStringArray(parseSatsPerCurrency(price, currencySymbol[0], withSatsSymbol)); } -EMSCRIPTEN_BINDINGS(my_module) { -// emscripten::register_vector("StringList"); +EMSCRIPTEN_BINDINGS(my_module) +{ + // emscripten::register_vector("StringList"); - emscripten::function("parseBlockHeight", &parseBlockHeightArray); + emscripten::function("parseBlockHeight", &parseBlockHeightArray); emscripten::function("parseHalvingCountdown", &parseHalvingCountdownArray); emscripten::function("parseMarketCap", &parseMarketCapArray); emscripten::function("parseBlockFees", &parseBlockFeesArray); diff --git a/lib/btclock/data_handler.hpp b/lib/btclock/data_handler.hpp index b31cfe7..226758f 100644 --- a/lib/btclock/data_handler.hpp +++ b/lib/btclock/data_handler.hpp @@ -5,9 +5,20 @@ #include "utils.hpp" -std::array 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 parsePriceData(std::uint32_t price, char currency, bool useSuffixFormat = false); std::array parseSatsPerCurrency(std::uint32_t price, char currencySymbol, bool withSatsSymbol); std::array parseBlockHeight(std::uint32_t blockHeight); std::array parseHalvingCountdown(std::uint32_t blockHeight, bool asBlocks); std::array parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars); std::array parseBlockFees(std::uint16_t blockFees); + +char getCurrencySymbol(char input); +std::string getCurrencyCode(char input); \ No newline at end of file diff --git a/src/lib/screen_handler.cpp b/src/lib/screen_handler.cpp index c91c096..269d2a7 100644 --- a/src/lib/screen_handler.cpp +++ b/src/lib/screen_handler.cpp @@ -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 = '['; }