From f3c6faa447f60c1709831d9f574ac95cd37064a4 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 6 Feb 2025 08:54:51 +0100 Subject: [PATCH 01/18] Add TimedAttempt --- examples/timedBlink/timedBlink.ino | 25 +++ extras/test/CMakeLists.txt | 9 +- extras/test/src/arduino/Arduino.cpp | 21 +++ extras/test/src/arduino/Arduino.h | 18 +++ extras/test/src/time/test_TimedAttempt.cpp | 173 +++++++++++++++++++++ src/Arduino_TimedAttempt.h | 14 ++ src/time/TimedAttempt.cpp | 77 +++++++++ src/time/TimedAttempt.h | 39 +++++ 8 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 examples/timedBlink/timedBlink.ino create mode 100644 extras/test/src/arduino/Arduino.cpp create mode 100644 extras/test/src/arduino/Arduino.h create mode 100644 extras/test/src/time/test_TimedAttempt.cpp create mode 100644 src/Arduino_TimedAttempt.h create mode 100644 src/time/TimedAttempt.cpp create mode 100644 src/time/TimedAttempt.h diff --git a/examples/timedBlink/timedBlink.ino b/examples/timedBlink/timedBlink.ino new file mode 100644 index 0000000..6bc59f7 --- /dev/null +++ b/examples/timedBlink/timedBlink.ino @@ -0,0 +1,25 @@ +/* + 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 + +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