Merge pull request #832 from mkbel/add_Timer_remaining
Add timer remaining
This commit is contained in:
commit
73b0c65bda
19
CMakeLists.txt
Normal file
19
CMakeLists.txt
Normal file
@ -0,0 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
project(cmake_test)
|
||||
|
||||
# Prepare "Catch" library for other executables
|
||||
set(CATCH_INCLUDE_DIR Catch2)
|
||||
add_library(Catch INTERFACE)
|
||||
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
|
||||
|
||||
# Make test executable
|
||||
set(TEST_SOURCES
|
||||
Tests/tests.cpp
|
||||
Tests/Example_test.cpp
|
||||
Tests/Timer_test.cpp
|
||||
Firmware/Timer.cpp
|
||||
)
|
||||
add_executable(tests ${TEST_SOURCES})
|
||||
target_include_directories(tests PRIVATE Tests)
|
||||
target_link_libraries(tests Catch)
|
13287
Catch2/catch.hpp
Normal file
13287
Catch2/catch.hpp
Normal file
File diff suppressed because it is too large
Load Diff
@ -22,6 +22,8 @@ public:
|
||||
void stop(){m_isRunning = false;}
|
||||
bool running(){return m_isRunning;}
|
||||
bool expired(T msPeriod);
|
||||
protected:
|
||||
T started(){return m_started;}
|
||||
private:
|
||||
bool m_isRunning;
|
||||
T m_started;
|
||||
|
55
Firmware/TimerRemaining.h
Normal file
55
Firmware/TimerRemaining.h
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @file
|
||||
* @author Marek Bel
|
||||
*/
|
||||
|
||||
#ifndef TIMERREMAINING_H
|
||||
#define TIMERREMAINING_H
|
||||
|
||||
#include "Timer.h"
|
||||
#include "Arduino.h"
|
||||
#include <limits.h>
|
||||
|
||||
class TimerRemaining : public LongTimer
|
||||
{
|
||||
public:
|
||||
TimerRemaining() : m_period(){}
|
||||
void start() = delete;
|
||||
bool expired(unsigned long msPeriod) = delete;
|
||||
/**
|
||||
* @brief Start timer
|
||||
* @param msPeriod Time to expire in milliseconds
|
||||
*/
|
||||
void start(unsigned long msPeriod)
|
||||
{
|
||||
m_period = msPeriod;
|
||||
LongTimer::start();
|
||||
}
|
||||
/**
|
||||
* @brief Time remaining to expiration
|
||||
*
|
||||
* @param msPeriod timer period in milliseconds
|
||||
* @return time remaining to expiration in milliseconds
|
||||
* @retval 0 Timer has expired, or was not even started.
|
||||
*/
|
||||
unsigned long remaining()
|
||||
{
|
||||
if (!running()) return 0;
|
||||
if (expired()) return 0;
|
||||
const unsigned long now = millis();
|
||||
return (started() + m_period - now);
|
||||
}
|
||||
/**
|
||||
* @brief Timer has expired.
|
||||
* @retval true Timer has expired.
|
||||
* @retval false Timer has not expired.
|
||||
*/
|
||||
bool expired()
|
||||
{
|
||||
return LongTimer::expired(m_period);
|
||||
}
|
||||
private:
|
||||
unsigned long m_period; //!< Timer period in milliseconds.
|
||||
};
|
||||
|
||||
#endif // ifndef TIMERREMAINING_H
|
38
README.md
38
README.md
@ -51,3 +51,41 @@ or you can also save the output code to the file (in so called `HEX`-format) `"F
|
||||
`Sketch->ExportCompiledBinary`
|
||||
and then upload it to the printer using the program `"FirmwareUpdater"`
|
||||
_note: this file is created in the directory `"Firmware/"`_
|
||||
|
||||
# 3. Automated tests
|
||||
## Prerequisites
|
||||
c++11 compiler e.g. g++ 6.3.1
|
||||
|
||||
cmake
|
||||
|
||||
build system - ninja or gnu make
|
||||
|
||||
## Building
|
||||
Create folder where you want to build tests.
|
||||
|
||||
Example:
|
||||
|
||||
`cd ..`
|
||||
|
||||
`mkdir Prusa-Firmware-test`
|
||||
|
||||
Generate build scripts in target folder.
|
||||
|
||||
Example:
|
||||
|
||||
`cd Prusa-Firmware-test`
|
||||
|
||||
`cmake -G "Eclipse CDT4 - Ninja" ../Prusa-Firmware`
|
||||
|
||||
or for DEBUG build:
|
||||
|
||||
`cmake -G "Eclipse CDT4 - Ninja" -DCMAKE_BUILD_TYPE=Debug ../Prusa-Firmware`
|
||||
|
||||
Build it.
|
||||
|
||||
Example:
|
||||
|
||||
`ninja`
|
||||
|
||||
## Runing
|
||||
`./tests`
|
12
Tests/Arduino.h
Normal file
12
Tests/Arduino.h
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Mock file to allow test compilation.
|
||||
* @author Marek Bel
|
||||
*/
|
||||
|
||||
#ifndef TESTS_ARDUINO_H_
|
||||
#define TESTS_ARDUINO_H_
|
||||
|
||||
extern unsigned long millis();
|
||||
|
||||
#endif /* TESTS_ARDUINO_H_ */
|
12
Tests/Example_test.cpp
Normal file
12
Tests/Example_test.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
#include "catch.hpp"
|
||||
|
||||
unsigned int Factorial( unsigned int number ) {
|
||||
return number <= 1 ? number : Factorial(number-1)*number;
|
||||
}
|
||||
|
||||
TEST_CASE( "Factorials are computed", "[factorial]" ) {
|
||||
REQUIRE( Factorial(1) == 1 );
|
||||
REQUIRE( Factorial(2) == 2 );
|
||||
REQUIRE( Factorial(3) == 6 );
|
||||
REQUIRE( Factorial(10) == 3628800 );
|
||||
}
|
171
Tests/Timer_test.cpp
Normal file
171
Tests/Timer_test.cpp
Normal file
@ -0,0 +1,171 @@
|
||||
/**
|
||||
* @file
|
||||
* @author Marek Bel
|
||||
*/
|
||||
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "../Firmware/Timer.h"
|
||||
#include "../Firmware/TimerRemaining.h"
|
||||
|
||||
static unsigned long now = 0;
|
||||
|
||||
unsigned long millis()
|
||||
{
|
||||
return now;
|
||||
}
|
||||
|
||||
static void basicTimer()
|
||||
{
|
||||
LongTimer timer;
|
||||
CHECK( timer.running() == false);
|
||||
|
||||
timer.start();
|
||||
CHECK( timer.running() == true);
|
||||
|
||||
timer.stop();
|
||||
CHECK( timer.running() == false);
|
||||
|
||||
timer.start();
|
||||
CHECK( timer.expired(0) == true);
|
||||
CHECK( timer.expired(0) == false);
|
||||
CHECK( timer.running() == false);
|
||||
|
||||
timer.start();
|
||||
CHECK( timer.expired(1) == false);
|
||||
CHECK( timer.running() == true);
|
||||
++now;
|
||||
CHECK( timer.expired(1) == true);
|
||||
CHECK( timer.running() == false);
|
||||
--now;
|
||||
|
||||
timer.start();
|
||||
CHECK( timer.expired(ULONG_MAX - 1) == false);
|
||||
now+= ULONG_MAX - 2;
|
||||
CHECK( timer.expired(ULONG_MAX - 1) == false);
|
||||
now++;
|
||||
CHECK( timer.expired(ULONG_MAX - 1) == true);
|
||||
CHECK( timer.running() == false);
|
||||
now-= ULONG_MAX - 1;
|
||||
|
||||
timer.start();
|
||||
CHECK( timer.expired(ULONG_MAX) == false);
|
||||
now+= ULONG_MAX - 1;
|
||||
CHECK( timer.expired(ULONG_MAX) == false);
|
||||
now++;
|
||||
CHECK( timer.expired(ULONG_MAX) == true);
|
||||
CHECK( timer.running() == false);
|
||||
now-= ULONG_MAX;
|
||||
|
||||
timer.start();
|
||||
CHECK( timer.running() == true);
|
||||
CHECK( timer.expired(12*3600000ul + 38*60000ul + 15000) == false);
|
||||
now+= 12*3600000ul + 38*60000ul + 14999;
|
||||
CHECK( timer.expired(12*3600000ul + 38*60000ul + 15000) == false);
|
||||
++now;
|
||||
CHECK( timer.expired(12*3600000ul + 38*60000ul + 15000) == true);
|
||||
CHECK( timer.running() == false);
|
||||
now-= 12*3600000ul + 38*60000ul + 15000;
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE( "LongTimer test.", "[timer]" )
|
||||
{
|
||||
now = 0;
|
||||
basicTimer();
|
||||
now = 1;
|
||||
basicTimer();
|
||||
now = ULONG_MAX;
|
||||
basicTimer();
|
||||
now = ULONG_MAX - 1;
|
||||
basicTimer();
|
||||
now = 12*3600000ul + 38*60000ul + 15000;
|
||||
basicTimer();
|
||||
}
|
||||
|
||||
static void basicRemaining()
|
||||
{
|
||||
TimerRemaining timer;
|
||||
CHECK( timer.running() == false);
|
||||
CHECK( timer.remaining() == 0);
|
||||
|
||||
timer.start(100);
|
||||
CHECK( timer.running() == true);
|
||||
CHECK( timer.remaining() == 100);
|
||||
now += 99;
|
||||
CHECK( timer.running() == true);
|
||||
CHECK( timer.remaining() == 1);
|
||||
|
||||
++now;
|
||||
CHECK( timer.running() == true);
|
||||
CHECK( timer.remaining() == 0);
|
||||
CHECK( timer.running() == false);
|
||||
now -=100;
|
||||
|
||||
timer.start(1);
|
||||
timer.stop();
|
||||
CHECK( timer.remaining() == 0);
|
||||
CHECK( timer.running() == false);
|
||||
|
||||
timer.start(0);
|
||||
CHECK( timer.remaining() == 0);
|
||||
CHECK( timer.running() == false);
|
||||
|
||||
timer.start(1);
|
||||
CHECK( timer.remaining() == 1);
|
||||
CHECK( timer.running() == true);
|
||||
|
||||
++now;
|
||||
CHECK( timer.running() == true);
|
||||
CHECK( timer.remaining() == 0);
|
||||
CHECK( timer.running() == false);
|
||||
--now;
|
||||
|
||||
timer.start(ULONG_MAX - 1);
|
||||
CHECK( timer.running() == true);
|
||||
CHECK( timer.remaining() == ULONG_MAX - 1);
|
||||
now+= ULONG_MAX - 2;
|
||||
CHECK( timer.running() == true);
|
||||
CHECK( timer.remaining() == 1);
|
||||
CHECK( timer.running() == true);
|
||||
++now;
|
||||
CHECK( timer.remaining() == 0);
|
||||
CHECK( timer.running() == false);
|
||||
now-= ULONG_MAX - 1;
|
||||
|
||||
timer.start(ULONG_MAX);
|
||||
CHECK( timer.running() == true);
|
||||
CHECK( timer.remaining() == ULONG_MAX);
|
||||
now+= ULONG_MAX - 1;
|
||||
CHECK( timer.remaining() == 1);
|
||||
CHECK( timer.running() == true);
|
||||
++now;
|
||||
CHECK( timer.remaining() == 0);
|
||||
CHECK( timer.running() == false);
|
||||
|
||||
timer.start(12*3600000ul + 38*60000ul + 15000);
|
||||
CHECK( timer.running() == true);
|
||||
CHECK( timer.remaining() == 12*3600000ul + 38*60000ul + 15000);
|
||||
now+= 12*3600000ul + 38*60000ul + 14999;
|
||||
CHECK( timer.remaining() == 1);
|
||||
++now;
|
||||
CHECK( timer.remaining() == 0);
|
||||
CHECK( timer.running() == false);
|
||||
now-= 12*3600000ul + 38*60000ul + 15000;
|
||||
}
|
||||
|
||||
TEST_CASE( "TimerRemaining test.", "[timer]" )
|
||||
{
|
||||
now = 0;
|
||||
basicRemaining();
|
||||
now = 1;
|
||||
basicRemaining();
|
||||
now = ULONG_MAX;
|
||||
basicRemaining();
|
||||
now = ULONG_MAX - 1;
|
||||
basicRemaining();
|
||||
|
||||
now = 12*3600000ul + 38*60000ul + 15000;
|
||||
basicRemaining();
|
||||
|
||||
}
|
2
Tests/tests.cpp
Normal file
2
Tests/tests.cpp
Normal file
@ -0,0 +1,2 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include "catch.hpp"
|
Loading…
Reference in New Issue
Block a user