diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 6e4d438..1de2167 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -26,6 +26,7 @@ jobs: - examples/crc32 - examples/crc16 - examples/sha256 + - examples/timedBlink SKETCHES_REPORTS_PATH: sketches-reports strategy: diff --git a/examples/timedBlink/timedBlink.ino b/examples/timedBlink/timedBlink.ino new file mode 100644 index 0000000..686e5ed --- /dev/null +++ b/examples/timedBlink/timedBlink.ino @@ -0,0 +1,29 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include + +#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) +static int const LED_BUILTIN = 2; +#endif + +TimedAttempt blink(500, 1000); + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); +} + +void loop() { + if(blink.isExpired()) { + blink.retry(); + digitalWrite(LED_BUILTIN, blink.getRetryCount() % 2); + } + +} diff --git a/extras/test/CMakeLists.txt b/extras/test/CMakeLists.txt index 2fb0dd0..7877717 100644 --- a/extras/test/CMakeLists.txt +++ b/extras/test/CMakeLists.txt @@ -20,6 +20,7 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) include_directories(../../src) +include_directories(src/arduino) set(TEST_SRCS src/lzss/test_decoder.cpp @@ -27,6 +28,7 @@ set(TEST_SRCS src/crc16/test_crc16.cpp src/sha256/test_sha256.cpp src/hex/test_hex.cpp + src/time/test_TimedAttempt.cpp ) set(TEST_DUT_SRCS @@ -34,6 +36,11 @@ set(TEST_DUT_SRCS ../../src/crc/crc16.cpp ../../src/sha256/sha2.c ../../src/hex/chex.h + ../../src/time/TimedAttempt.cpp +) + +set(TEST_STUB_SRCS + src/arduino/Arduino.cpp ) ########################################################################## @@ -45,7 +52,7 @@ add_compile_options(-Wno-cast-function-type) set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage") set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage -Wno-deprecated-copy") -add_executable( ${TEST_TARGET} ${TEST_SRCS} ${TEST_DUT_SRCS} ) +add_executable( ${TEST_TARGET} ${TEST_SRCS} ${TEST_DUT_SRCS} ${TEST_STUB_SRCS}) target_compile_definitions( ${TEST_TARGET} PUBLIC SOURCE_DIR="${CMAKE_SOURCE_DIR}" ) target_link_libraries( ${TEST_TARGET} Catch2WithMain ) diff --git a/extras/test/src/arduino/Arduino.cpp b/extras/test/src/arduino/Arduino.cpp new file mode 100644 index 0000000..cf277de --- /dev/null +++ b/extras/test/src/arduino/Arduino.cpp @@ -0,0 +1,21 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include + +static unsigned long current_millis = 0; + +void set_millis(unsigned long const millis) { + current_millis = millis; +} + +unsigned long millis() { + return current_millis; +} diff --git a/extras/test/src/arduino/Arduino.h b/extras/test/src/arduino/Arduino.h new file mode 100644 index 0000000..91ef626 --- /dev/null +++ b/extras/test/src/arduino/Arduino.h @@ -0,0 +1,18 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#ifndef min + #define min(a,b) ((a)<(b)?(a):(b)) +#endif + +void set_millis(unsigned long const millis); +unsigned long millis(); diff --git a/extras/test/src/time/test_TimedAttempt.cpp b/extras/test/src/time/test_TimedAttempt.cpp new file mode 100644 index 0000000..8009492 --- /dev/null +++ b/extras/test/src/time/test_TimedAttempt.cpp @@ -0,0 +1,173 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include + +#include