Skip to content

Commit

Permalink
Add UT and regroup sources
Browse files Browse the repository at this point in the history
  • Loading branch information
olvap80 committed Jul 28, 2024
1 parent 9cd11bd commit 05c0999
Show file tree
Hide file tree
Showing 33 changed files with 2,103 additions and 240 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Build folders
build/

# Prerequisites
*.d

Expand Down
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"ms-vscode.cmake-tools",
"ms-vscode.cpptools-extension-pack",
"vscode.cpptools",
"josetr.cmake-language-support-vscode"
]
}
88 changes: 88 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"files.associations": {
"charconv": "cpp",
"chrono": "cpp",
"system_error": "cpp",
"xlocale": "cpp",
"limits": "cpp",
"tuple": "cpp",
"algorithm": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"csignal": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"format": "cpp",
"forward_list": "cpp",
"fstream": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"set": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"string": "cpp",
"thread": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"utility": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xhash": "cpp",
"xiosbase": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp"
},
"editor.tabSize": 4,
"editor.detectIndentation": false,
"editor.insertSpaces": true,
"cSpell.words": [
"funarg",
"INSTANTMEMORY",
"Pavlo",
"SUBCASE",
"Unstorable"
],
"C_Cpp.errorSquiggles": "enabled",
"C_Cpp.loggingLevel": "Debug"
}
74 changes: 74 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# This file is added just for convenience for those projects
# that already use CMake in their activities.
#
# This library is header only, so no need to build it
# and threre in NO REQUIREMENT to use CMake:
# one can just copy headers to their project
# and use any build system one likes.


cmake_minimum_required(VERSION 3.27)
project(InstantRTOS VERSION 0.1.0 LANGUAGES CXX)

# Turn ALLOW_INSTANTRTOS_DEVELOPMENT ON once modifying InstantRTOS
# (remember this is cached variable,
# so it will stay the same until explicitly changed.
# For VSCode you can go F1 of Ctrl+Shift+P, then "CMake: Edit CMake Cache"
# or "CMake: Edit CMake Cache (UI)" and change the value there,
# or just delete CMakeCache.txt file in the build directory to start from scratch)
option(
ALLOW_INSTANTRTOS_DEVELOPMENT
"Used when developing/testing InstantRTOS"
ON
)


# The only purpose here is to populate include directories
add_library(${PROJECT_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 11 # This is the minimum requirement
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
target_include_directories(${PROJECT_NAME} INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/src
)


if(ALLOW_INSTANTRTOS_DEVELOPMENT)
# While in VSCode, use recommended extentions from extensions.json,
# they add context menus and buttons into status bar.
# To see the list of targets and select the one to run
# of provide --config ConfigurationName in VSCode's
# click "Activity Bar" on the left, then go "CMake" icon.
# Find Build/Debug/Run buttons in the status bar below
# See also https://github.com/microsoft/vscode-cmake-tools/blob/main/docs/debug-launch.md

if(MSVC)
# Remember _HAS_STATIC_RTTI=0 shall also do /GR-

# uncomment below to have PDB in addition to execitable even in release
# set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "ProgramDatabase")
# add_link_options("/DEBUG:FULL")
endif()

# Optionally it is possible to run tests
include(CTest)
if( BUILD_TESTING )
# Go with doctest as the most natural choice for a C++ project ))

# The only "external" dependency is when testing with doctest
include(FetchContent)
FetchContent_Declare(
doctest
GIT_REPOSITORY https://github.com/doctest/doctest.git
GIT_TAG master # or use a specific tag instead of master
)
FetchContent_MakeAvailable(doctest)

# Remember you can eiter run tests with CTest
# of just run (debug) the executable directly from the IDE
add_subdirectory(tests)
endif()
endif()
62 changes: 62 additions & 0 deletions examples/CoroutineGenerators/CoroutineGenerators.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "InstantCoroutine.h"

// Create functor class named SequenceOfSquares for a coroutine
// producing squares starting from 0
CoroutineDefine( SequenceOfSquares ) {
//coroutine variables (state persisting between CoroutineYield calls)
int i = 0;

//specify body of the coroutine, int here is the type yielded
CoroutineBegin(int)
for ( ;; ++i){
CoroutineYield( i*i );
}
CoroutineEnd()
};

//Create functor class named Range for range producing coroutine
//(yes, it is possible for a coroutine to be template, it is just
// a class that can be instantiated multiple times!)
template<class T>
CoroutineDefine( Range ) {
T current, last;
public:
///Constructor to establish initial coroutine state
Range(T beginFrom, T endWith) : current(beginFrom), last(endWith) {}

//body of the coroutine that uses this state
CoroutineBegin(T)
for(; current < last; ++current){
CoroutineYield( current );
}
CoroutineStop(last);
CoroutineEnd()
};

//Instantiate squares generator
SequenceOfSquares sequenceOfSquares;

void setup() {
Serial.begin(9600);
}

void loop() {
//establish new range on each iteration
Range<int8_t> range(10, 20);

while( range ){
//"call" to coroutine resumes is till CoroutineYield or CoroutineStop
auto i = range();
auto x = sequenceOfSquares();

Serial.print( i );
Serial.print( ':' );
Serial.println( x );
delay( 200 );
}
Serial.println(F("Iteration finished"));

//NOTE: next iteration will start a new range again from scratch
// but sequenceOfSquares will continue where is was stopped previously
delay( 2000 );
}
110 changes: 110 additions & 0 deletions examples/InstantCallback/InstantCallback.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* @file InstantCallback
* @brief Show how to turn C++ lambda into a "plain C" callback
*/
#include "InstantCallback.h"

void invoke_simple_callback(
unsigned (*simpleFunctionPointer)(unsigned arg)
){
Serial.println(F("\nENTER invoke_simple_callback"));
unsigned res = simpleFunctionPointer(1000);
Serial.print(F("res=")); Serial.println(res);
Serial.println(F("LEAVE invoke_simple_callback\n"));
}

void invoke_multiple(
unsigned (*simpleFunctionPointer)(unsigned arg)
){
Serial.println(F("\nENTER invoke_multiple"));
unsigned res = simpleFunctionPointer(2000);
Serial.print(F("res=")); Serial.println(res);
res = simpleFunctionPointer(3000);
Serial.print(F("res=")); Serial.println(res);
res = simpleFunctionPointer(4000);
Serial.print(F("res=")); Serial.println(res);
Serial.println(F("LEAVE invoke_multiple\n"));
}

/// Class for demo purposes (illustrate how lifetime is managed)
struct Wrap{
unsigned val = 0;

Wrap(){
Serial.println(F("Wrap Default constructor"));
}
Wrap(unsigned initialVal): val(initialVal){
Serial.print(F("Wrap Constructor for")); Println();
}
~Wrap(){
Serial.print(F("Wrap Destructor for")); Println();
}

Wrap(const Wrap& other): val(other.val){
Serial.print(F("Wrap Copy for")); Println();
}
Wrap(Wrap&& other): val(other.val){
other.val += 10000; //mark "other" as "moved from"
Serial.print(F("Wrap Move from "));
Serial.print((unsigned)&other);
Serial.print(F(" to "));
Println();
}
Wrap& operator=(const Wrap& other){
val = other.val;
Serial.print(F("Wrap Assignment for")); Println();
return *this;
}
Wrap& operator=(Wrap&& other){
val = other.val;
other.val += 20000; //mark "other" as "moved from"
Serial.print(F("Wrap Move assignment for")); Println();
return *this;
}
void Println(){
Serial.print(F(" val=")); Serial.print(val);
Serial.print(F(" at ")); Serial.println((unsigned)this);
}
};

void setup() {
Serial.begin(115200);
Serial.println(F("Start ==================================="));
}

void loop() {
Serial.println(F("\nIteration ==============================="));

unsigned var = rand() & 0xF;
Wrap wrap = rand() & 0xFF;
//demo for "single shot" callback
invoke_simple_callback(CallbackFrom<1>(
[=](unsigned arg){
Serial.print(F("Lambda-1 called var=")); Serial.print(var);
Serial.print(F(", wrap=")); Serial.print(wrap.val);
Serial.print(F(", arg=")); Serial.println(arg);
return var + wrap.val + arg;
}
));

//refresh captured variables for new values
var = rand() & 0xF;
wrap.val = rand() & 0xFF;
//demo for multi shot callback
invoke_multiple(CallbackFrom<1>(
[=](
CallbackExtendLifetime& lifetime,
unsigned arg
){
if( 4000 == arg ){
//this will free memory after lambda exits
lifetime.Dispose();
}
Serial.print(F("Lambda-2 called var=")); Serial.print(var);
Serial.print(F(", wrap=")); Serial.print(wrap.val);
Serial.print(F(", arg=")); Serial.println(arg);
return var + wrap.val + arg;
}
));

delay(1000);
}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=InstantRTOS
version=0.1.0
author=Pavlo M
maintainer=Pavlo M
sentence=A real-time operating system for Arduino (and other embedded platforms).
paragraph=InstantRTOS is a simple and efficient, lightweight real-time operating system (RTOS) for Arduino boards (and other embedded platforms). It allows you to create tasks, manage task scheduling etc...
category=Timing
url=https://github.com/olvap80/InstantRTOS
architectures=*
File renamed without changes.
Loading

0 comments on commit 05c0999

Please sign in to comment.