diff --git a/lib/btclock/data_handler.cpp b/lib/btclock/data_handler.cpp index 250dbc9..28f13d0 100644 --- a/lib/btclock/data_handler.cpp +++ b/lib/btclock/data_handler.cpp @@ -1,10 +1,15 @@ #include "data_handler.hpp" -std::array parsePriceData(uint price, char currencySymbol) +std::array parsePriceData(std::uint32_t price, char currencySymbol) { std::array ret; - std::string priceString = currencySymbol + std::to_string(price); - uint firstIndex = 0; + std::string priceString; + if (std::to_string(price).length() >= NUM_SCREENS) { + priceString = formatNumberWithSuffix(price); + } else { + priceString = currencySymbol + std::to_string(price); + } + std::uint32_t firstIndex = 0; if (priceString.length() < (NUM_SCREENS)) { priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' '); @@ -19,7 +24,7 @@ std::array parsePriceData(uint price, char currencySym firstIndex = 1; } - for (uint i = firstIndex; i < NUM_SCREENS; i++) + for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++) { ret[i] = priceString[i]; } @@ -27,11 +32,11 @@ std::array parsePriceData(uint price, char currencySym return ret; } -std::array parseSatsPerCurrency(uint price, char currencySymbol) +std::array parseSatsPerCurrency(std::uint32_t price, char currencySymbol) { std::array ret; std::string priceString = std::to_string(int(round(1 / float(price) * 10e7))); - uint firstIndex = 0; + std::uint32_t firstIndex = 0; if (priceString.length() < (NUM_SCREENS)) { @@ -46,7 +51,7 @@ std::array parseSatsPerCurrency(uint price, char curre } firstIndex = 1; - for (uint i = firstIndex; i < NUM_SCREENS; i++) + for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++) { ret[i] = priceString[i]; } @@ -54,11 +59,11 @@ std::array parseSatsPerCurrency(uint price, char curre return ret; } -std::array parseBlockHeight(uint blockHeight) +std::array parseBlockHeight(std::uint32_t blockHeight) { std::array ret; std::string blockNrString = std::to_string(blockHeight); - uint firstIndex = 0; + std::uint32_t firstIndex = 0; if (blockNrString.length() < NUM_SCREENS) { @@ -67,7 +72,7 @@ std::array parseBlockHeight(uint blockHeight) firstIndex = 1; } - for (uint i = firstIndex; i < NUM_SCREENS; i++) + for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++) { ret[i] = blockNrString[i]; } @@ -75,12 +80,12 @@ std::array parseBlockHeight(uint blockHeight) return ret; } -std::array parseHalvingCountdown(uint blockHeight) +std::array parseHalvingCountdown(std::uint32_t blockHeight) { std::array ret; - const uint nextHalvingBlock = 210000 - (blockHeight % 210000); - const uint minutesToHalving = nextHalvingBlock * 10; + const std::uint32_t nextHalvingBlock = 210000 - (blockHeight % 210000); + const std::uint32_t minutesToHalving = nextHalvingBlock * 10; const int years = floor(minutesToHalving / 525600); const int days = floor((minutesToHalving - (years * 525600)) / (24 * 60)); @@ -97,10 +102,10 @@ std::array parseHalvingCountdown(uint blockHeight) return ret; } -std::array parseMarketCap(uint blockHeight, uint price, char currencySymbol, bool bigChars) +std::array parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars) { std::array ret; - uint firstIndex = 0; + std::uint32_t firstIndex = 0; double supply = getSupplyAtBlock(blockHeight); int64_t marketCap = static_cast(supply * double(price)); if (currencySymbol == '[') @@ -119,7 +124,7 @@ std::array parseMarketCap(uint blockHeight, uint price std::string priceString = currencySymbol + formatNumberWithSuffix(marketCap); priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' '); - for (uint i = firstIndex; i < NUM_SCREENS; i++) + for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++) { ret[i] = priceString[i]; } @@ -131,7 +136,7 @@ std::array parseMarketCap(uint blockHeight, uint price size_t leadingSpaces = (3 - mcLength % 3) % 3; stringValue = std::string(leadingSpaces, ' ') + stringValue; - uint groups = (mcLength + leadingSpaces) / 3; + std::uint32_t groups = (mcLength + leadingSpaces) / 3; if (groups < NUM_SCREENS) { @@ -144,7 +149,7 @@ std::array parseMarketCap(uint blockHeight, uint price } ret[NUM_SCREENS - groups - 1] = " $ "; - for (uint i = 0; i < groups; i++) + for (std::uint32_t i = 0; i < groups; i++) { ret[(NUM_SCREENS - groups + i)] = stringValue.substr(i * 3, 3).c_str(); } diff --git a/lib/btclock/data_handler.hpp b/lib/btclock/data_handler.hpp index 373d78a..3ac52c6 100644 --- a/lib/btclock/data_handler.hpp +++ b/lib/btclock/data_handler.hpp @@ -1,10 +1,12 @@ #include #include #include +#include + #include "utils.hpp" -std::array parsePriceData(uint price, char currencySymbol); -std::array parseSatsPerCurrency(uint price, char currencySymbol); -std::array parseBlockHeight(uint blockHeight); -std::array parseHalvingCountdown(uint blockHeight); -std::array parseMarketCap(uint blockHeight, uint price, char currencySymbol, bool bigChars); \ No newline at end of file +std::array parsePriceData(std::uint32_t price, char currencySymbol); +std::array parseSatsPerCurrency(std::uint32_t price, char currencySymbol); +std::array parseBlockHeight(std::uint32_t blockHeight); +std::array parseHalvingCountdown(std::uint32_t blockHeight); +std::array parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars); \ No newline at end of file diff --git a/lib/btclock/utils.cpp b/lib/btclock/utils.cpp index b68b327..2234ebb 100644 --- a/lib/btclock/utils.cpp +++ b/lib/btclock/utils.cpp @@ -5,7 +5,7 @@ int modulo(int x, int N) return (x % N + N) % N; } -double getSupplyAtBlock(uint blockNr) { +double getSupplyAtBlock(std::uint32_t blockNr) { if (blockNr >= 33 * 210000) { return 20999999.9769; } @@ -25,7 +25,7 @@ double getSupplyAtBlock(uint blockNr) { return totalBitcoinInCirculation; } -std::string formatNumberWithSuffix(int64_t num) { +std::string formatNumberWithSuffix(std::uint64_t num) { const long long quadrillion = 1000000000000000LL; const long long trillion = 1000000000000LL; const long long billion = 1000000000; diff --git a/lib/btclock/utils.hpp b/lib/btclock/utils.hpp index 0a14328..b44c978 100644 --- a/lib/btclock/utils.hpp +++ b/lib/btclock/utils.hpp @@ -2,9 +2,10 @@ #include #include +#include int modulo(int x,int N); -double getSupplyAtBlock(uint blockNr); +double getSupplyAtBlock(std::uint32_t blockNr); -std::string formatNumberWithSuffix(int64_t num); \ No newline at end of file +std::string formatNumberWithSuffix(std::uint64_t num); \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index e73d0b6..2044170 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,13 +11,14 @@ data_dir = data/build_gz [env] + + +[btclock_base] platform = https://github.com/platformio/platform-espressif32.git framework = arduino, espidf monitor_speed = 115200 monitor_filters = esp32_exception_decoder, colorize board_build.filesystem = littlefs - -[btclock_base] extra_scripts = post:scripts/extra_script.py build_flags = !python scripts/git_rev.py @@ -84,4 +85,14 @@ build_flags = -D MCP2_A1_PIN=10 -D MCP2_A2_PIN=14 build_unflags = - ${btclock_base.build_unflags} \ No newline at end of file + ${btclock_base.build_unflags} + +[env:native_test_only] +platform = native +test_framework = unity +build_flags = + ${btclock_base.build_flags} + -D MCP_INT_PIN=8 + -D NEOPIXEL_PIN=34 + -D NEOPIXEL_COUNT=4 + -D NUM_SCREENS=7 \ No newline at end of file diff --git a/test/test_datahandler/test_main.cpp b/test/test_datahandler/test_main.cpp index 922a911..ba6b011 100644 --- a/test/test_datahandler/test_main.cpp +++ b/test/test_datahandler/test_main.cpp @@ -9,7 +9,7 @@ void tearDown(void) { // clean stuff up here } -void test_sats_per_dollar(void) { +void test_CorrectSatsPerDollarConversion(void) { std::array output = parseSatsPerCurrency(37253, '$'); TEST_ASSERT_EQUAL_STRING("MSCW/TIME", output[0].c_str()); TEST_ASSERT_EQUAL_STRING("2", output[NUM_SCREENS-4].c_str()); @@ -19,42 +19,50 @@ void test_sats_per_dollar(void) { } -void test_block_height_6screens(void) { +void test_SixCharacterBlockHeight(void) { std::array output = parseBlockHeight(999999); TEST_ASSERT_EQUAL_STRING("BLOCK/HEIGHT", output[0].c_str()); TEST_ASSERT_EQUAL_STRING("9", output[1].c_str()); } -void test_block_height_7screens(void) { +void test_SevenCharacterBlockHeight(void) { std::array output = parseBlockHeight(1000000); TEST_ASSERT_EQUAL_STRING("1", output[0].c_str()); TEST_ASSERT_EQUAL_STRING("0", output[1].c_str()); } -void test_ticker_6screens(void) { +void test_PriceOf100kusd(void) { std::array output = parsePriceData(100000, '$'); TEST_ASSERT_EQUAL_STRING("$", output[0].c_str()); TEST_ASSERT_EQUAL_STRING("1", output[1].c_str()); } -void test_ticker_7screens(void) { +void test_PriceOf1MillionUsd(void) { std::array output = parsePriceData(1000000, '$'); - TEST_ASSERT_EQUAL_STRING("1", output[0].c_str()); - TEST_ASSERT_EQUAL_STRING("0", output[1].c_str()); + TEST_ASSERT_EQUAL_STRING("BTC/USD", output[0].c_str()); + for (int i = 1; i <= NUM_SCREENS-3; i++) { + TEST_ASSERT_EQUAL_STRING(" ", output[i].c_str()); + } + TEST_ASSERT_EQUAL_STRING("1", output[NUM_SCREENS-2].c_str()); + TEST_ASSERT_EQUAL_STRING("M", output[NUM_SCREENS-1].c_str()); } // not needed when using generate_test_runner.rb int runUnityTests(void) { UNITY_BEGIN(); - RUN_TEST(test_sats_per_dollar); - RUN_TEST(test_block_height_6screens); - RUN_TEST(test_block_height_7screens); - RUN_TEST(test_ticker_6screens); - RUN_TEST(test_ticker_7screens); + RUN_TEST(test_CorrectSatsPerDollarConversion); + RUN_TEST(test_SixCharacterBlockHeight); + RUN_TEST(test_SevenCharacterBlockHeight); + RUN_TEST(test_PriceOf100kusd); + RUN_TEST(test_PriceOf1MillionUsd); return UNITY_END(); } +int main(void) { + return runUnityTests(); +} + extern "C" void app_main() { runUnityTests(); }