From d4a05e2f362559beb11cac75b6f91030ea8d7dc9 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Thu, 9 May 2024 15:43:44 +0200 Subject: [PATCH] Add Emscripten support to data handler --- lib/btclock/data_handler.cpp | 50 +++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/btclock/data_handler.cpp b/lib/btclock/data_handler.cpp index 764417f..3100ef8 100644 --- a/lib/btclock/data_handler.cpp +++ b/lib/btclock/data_handler.cpp @@ -1,4 +1,8 @@ #include "data_handler.hpp" +#ifdef __EMSCRIPTEN__ +#include +#include +#endif std::array parsePriceData(std::uint32_t price, char currencySymbol, bool useSuffixFormat) { @@ -37,7 +41,7 @@ std::array parseSatsPerCurrency(std::uint32_t price, c std::array ret; std::string priceString = std::to_string(int(round(1 / float(price) * 10e7))); std::uint32_t firstIndex = 0; - uint insertSatSymbol = NUM_SCREENS - priceString.length() - 1; + std::uint8_t insertSatSymbol = NUM_SCREENS - priceString.length() - 1; if (priceString.length() < (NUM_SCREENS)) { @@ -99,7 +103,7 @@ std::array parseBlockFees(std::uint16_t blockFees) { firstIndex = 1; } - for (uint i = firstIndex; i < NUM_SCREENS-1; i++) + for (std::uint8_t i = firstIndex; i < NUM_SCREENS-1; i++) { ret[i] = blockFeesString[i]; } @@ -205,4 +209,44 @@ std::array parseMarketCap(std::uint32_t blockHeight, s } return ret; -} \ No newline at end of file +} + +#ifdef __EMSCRIPTEN__ +emscripten::val arrayToStringArray(const std::array& arr) { + emscripten::val jsArray = emscripten::val::array(); + for (const auto& str : arr) { + jsArray.call("push", str); + } + return jsArray; +} + +emscripten::val vectorToStringArray(const std::vector& vec) { + emscripten::val jsArray = emscripten::val::array(); + for (size_t i = 0; i < vec.size(); ++i) { + jsArray.set(i, vec[i]); + } + return jsArray; +} + +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) { + return arrayToStringArray(parsePriceData(price, currencySymbol[0], useSuffixFormat)); +} + +EMSCRIPTEN_BINDINGS(my_module) { +// emscripten::register_vector("StringList"); + + emscripten::function("parseBlockHeight", &parseBlockHeightArray); + // emscripten::function("parseHalvingCountdown", &parseBlockHeightArray); + // emscripten::function("parseMarketCap", &parseBlockHeightArray); + // emscripten::function("parseBlockFees", &parseBlockHeightArray); + // emscripten::function("parseSatsPerCurrency", &parseBlockHeightArray); + emscripten::function("parsePriceData", &parsePriceDataArray); + + emscripten::function("arrayToStringArray", &arrayToStringArray); + emscripten::function("vectorToStringArray", &vectorToStringArray); +} +#endif \ No newline at end of file