diff --git a/examples/cpp11_tasks/Makefile b/examples/cpp11_tasks/Makefile new file mode 100644 index 0000000..4e20218 --- /dev/null +++ b/examples/cpp11_tasks/Makefile @@ -0,0 +1,37 @@ +# --------------------------------------------------------------------------- # +# Copyright 2017 Florian Eich +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE./ limitations under the License. +# --------------------------------------------------------------------------- # + +# Makefile for cpp11 support example +PROGRAM=cpp11_tasks + +EXTRA_COMPONENTS=extras/cpp11 + +include ../../common.mk + diff --git a/examples/cpp11_tasks/cpp11_tasks.cpp b/examples/cpp11_tasks/cpp11_tasks.cpp new file mode 100644 index 0000000..214930c --- /dev/null +++ b/examples/cpp11_tasks/cpp11_tasks.cpp @@ -0,0 +1,126 @@ +/* --------------------------------------------------------------------------*\ + * Copyright 2017 Florian Eich + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE./ limitations under the License. +\* --------------------------------------------------------------------------*/ + +#include "queue.hpp" +#include "task.hpp" +#include "util.hpp" + +#include "esp/uart.h" +#include "espressif/esp_common.h" + + +// +// Declare queue for both tasks to work on. +// +cxx::queue comq{10}; + +// +// Instantiate "sender" task using the make_task generator function. +// +// +// The first parameter is the name. +// +// Second parameter is the stack depth for the task (use 512 as default). +// +// Third is the priority of the task (use 2 as default). +// +// Fourth can optionally be a pointer to a parameter passed to the lambda, +// which then must be recasted inside the lambda function via +// "static_cast(pvParameter)" to be used. +// +// Fifth is a lambda defining the function (must have "void * pvParameter"). +// +auto task1 = cxx::make_task("sender", 256, 2, &comq, [](void * pvParameter) { + printf("sender: start.\n"); + + uint32_t count{0}; + + // + // Cast "pvParameter" to the type you need it to be - must remain pointer. + // + auto my_comq = static_cast *>(pvParameter); + + while (true) { + cxx::sleep(10); + + // + // Now, the "->" must be used sinve "my_comq" is a pointer. + // + my_comq->send(count); + + ++count; + } +}); + +// +// Instantiate "receiver" task. Same syntax as above. +// +// Here, the simpler syntax is shown. Since "comq" is defined globally +// context, it is automatically captured by the lambda and does not need to be +// passed in. +// +auto task2 = cxx::make_task("receiver", [](void * pvParameter) { + printf("receiver: start.\n"); + + while (true) { + uint32_t count; + comq.receive(count, 15) ? printf("receiver: received %u\n", count) + : printf("receiver: no message received.\n"); + } +}); + +// +// The following syntaxes are also allowed: +// +// +// Only specify name and stack depth: +// +auto task3 = cxx::make_task("task3", 256, [](void *) {}); +// +// +// Only specify name, stack depth and priority: +// +auto task4 = cxx::make_task("task4", 256, 2, [](void *) {}); +// + +// +// Let the show begin. +// +extern "C" void user_init(void) { + uart_set_baud(0, 115200); + printf("SDK version: %s\n", sdk_system_get_sdk_version()); + + // + // Call "run()" on all your tasks. + // + task1.run(); + task2.run(); +} + diff --git a/extras/cpp11/component.mk b/extras/cpp11/component.mk new file mode 100644 index 0000000..eb45f04 --- /dev/null +++ b/extras/cpp11/component.mk @@ -0,0 +1,33 @@ +# --------------------------------------------------------------------------- # +# Copyright 2017 Florian Eich +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from this +# software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE./ limitations under the License. +# --------------------------------------------------------------------------- # + +# Component makefile for extras/cpp11 +INC_DIRS += $(ROOT)extras/cpp11/include + diff --git a/extras/cpp11/include/countdown.hpp b/extras/cpp11/include/countdown.hpp new file mode 100644 index 0000000..94370b3 --- /dev/null +++ b/extras/cpp11/include/countdown.hpp @@ -0,0 +1,95 @@ +/* --------------------------------------------------------------------------*\ + * Copyright 2017 Florian Eich + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE./ limitations under the License. +\* --------------------------------------------------------------------------*/ + +#ifndef ESP_OPEN_RTOS_EXTRAS_CPP11_COUNTDOWN_HPP_ +#define ESP_OPEN_RTOS_EXTRAS_CPP11_COUNTDOWN_HPP_ + + +#include "FreeRTOS.h" + +#include "util.hpp" + + +namespace cxx { + + +/*! + * \brief Wrapper for FreeRTOS TickType_t. + */ +class countdown +{ +public: + /*! + * \brief Default constructor for countdown class. + */ + countdown() : _interval_end_ms(0L) {} + + /*! + * \brief Constructor for countdown class with milliseconds parameter. + * \param ms Specify milliseconds for countdown timer. + */ + countdown(const int ms) : _interval_end_ms(millis() + ms) {} + + /*! + * \brief Function to check if countdown has expired. + * \returns boolean + */ + bool expired() { + return (_interval_end_ms > 0L) && (millis() >= _interval_end_ms); + } + + /*! + * \brief Setter for countdown. + * \param ms Number of milliseconds. + */ + void countdown_ms(const unsigned long ms) { + _interval_end_ms = ms + millis(); + } + + /*! + * \brief Setter for countdown. + * \param s Number of seconds. + */ + void countdown_s(const unsigned long s) { countdown_ms(s * 1000L); } + + /*! + * \brief Function to check time left. + * \returns Remaining time in milliseconds. + */ + int left_ms() { return _interval_end_ms - millis(); } + +private: + TickType_t _interval_end_ms; +}; + +} // namespace cxx + +#endif // ESP_OPEN_RTOS_EXTRAS_CPP11_COUNTDOWN_HPP_ + diff --git a/extras/cpp11/include/mutex.hpp b/extras/cpp11/include/mutex.hpp new file mode 100644 index 0000000..8f39f5c --- /dev/null +++ b/extras/cpp11/include/mutex.hpp @@ -0,0 +1,96 @@ +/* --------------------------------------------------------------------------*\ + * Copyright 2017 Florian Eich + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE./ limitations under the License. +\* --------------------------------------------------------------------------*/ + +#ifndef ESP_OPEN_RTOS_EXTRAS_CPP11_MUTEX_HPP_ +#define ESP_OPEN_RTOS_EXTRAS_CPP11_MUTEX_HPP_ + + +#include "FreeRTOS.h" +#include "semphr.h" + + +namespace cxx { + + +/*! + * \brief Wrapper for FreeRTOS SemaphoreHande_t. + */ +class mutex +{ +public: + /*! + * \brief Default constructor for mutex class. + */ + mutex() : _mutex(xSemaphoreCreateMutex()) {} + + // Disable copy construction and assigment. + mutex(const mutex &) = delete; + mutex & operator=(const mutex &) = delete; + + /*! + * \brief Destructor for mutex class releases underlying resource actively. + */ + ~mutex() { vQueueDelete(_mutex); } + + /*! + * \brief Check if mutex is usable. + * \returns boolean: true if usable. + */ + bool usable() const { return _mutex != nullptr; } + + /*! + * \brief Try locking mutex with no timeout. + * \returns boolean: true if successfully locked. + */ + bool try_lock() { return xSemaphoreTake(_mutex, portMAX_DELAY) == pdTRUE; } + + /*! + * \brief Try locking mutex with timeout. + * \param ms Timeout delay in milliseconds. + * \returns boolean: true if successfully locked. + */ + bool try_lock(const unsigned long ms) { + return xSemaphoreTake(_mutex, ms / portTICK_PERIOD_MS) == pdTRUE; + } + + /*! + * \brief Unlock mutex. + * \returns boolean: true if successfully unlocked. + */ + bool unlock() { return xSemaphoreGive(_mutex) == pdTRUE; } + +private: + SemaphoreHandle_t _mutex; +}; + +} // namespace cxx + +#endif // ESP_OPEN_RTOS_EXTRAS_CPP11_MUTEX_HPP_ + diff --git a/extras/cpp11/include/queue.hpp b/extras/cpp11/include/queue.hpp new file mode 100644 index 0000000..fd44562 --- /dev/null +++ b/extras/cpp11/include/queue.hpp @@ -0,0 +1,99 @@ +/* --------------------------------------------------------------------------*\ + * Copyright 2017 Florian Eich + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE./ limitations under the License. +\* --------------------------------------------------------------------------*/ + +#ifndef ESP_OPEN_RTOS_EXTRAS_CPP11_QUEUE_HPP_ +#define ESP_OPEN_RTOS_EXTRAS_CPP11_QUEUE_HPP_ + + +#include "FreeRTOS.h" +#include "queue.h" + + +namespace cxx { + + +/*! + * \brief Wrapper for FreeRTOS QueueHandle_t. + */ +template +class queue +{ +public: + /*! + * \brief Constructor for queue class. + * \param uxQueueLength Length of queue. + */ + queue(const unsigned portBASE_TYPE uxQueueLength) + : _queue(xQueueCreate(uxQueueLength, sizeof(D))) {} + + // Disable default constructor, copy constructor and assignment operator. + queue() = delete; + queue(const queue &) = delete; + queue & operator=(const queue &) = delete; + + /*! + * \brief Destructor for queue class releases underlying resource actively. + */ + ~queue() { vQueueDelete(_queue); } + + /*! + * \brief Check if queue is usable. + * \returns boolean: true if usable. + */ + bool usable() const { return _queue != nullptr; } + + /*! + * \brief Send function. + * \param data Data container. + * \param ms Timeout delay, default: 0. + * \returns boolean: true if successful. + */ + bool send(const D & data, unsigned long ms = 0) { + return xQueueSend(_queue, &data, ms / portTICK_PERIOD_MS) == pdTRUE; + } + + /*! + * \brief Receive function. + * \param data Data container. + * \param ms Timeout delay, default: 0. + * \returns boolean: true if successful. + */ + bool receive(D & data, unsigned long ms = 0) { + return xQueueReceive(_queue, &data, ms / portTICK_PERIOD_MS) == pdTRUE; + } + +private: + QueueHandle_t _queue; +}; + +} // namespace cxx + +#endif // ESP_OPEN_RTOS_EXTRAS_CPP11_QUEUE_HPP_ + diff --git a/extras/cpp11/include/task.hpp b/extras/cpp11/include/task.hpp new file mode 100644 index 0000000..3207426 --- /dev/null +++ b/extras/cpp11/include/task.hpp @@ -0,0 +1,175 @@ +/* --------------------------------------------------------------------------*\ + * Copyright 2017 Florian Eich + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE./ limitations under the License. +\* --------------------------------------------------------------------------*/ + +#ifndef ESP_OPEN_RTOS_EXTRAS_CPP11_TASK_HPP_ +#define ESP_OPEN_RTOS_EXTRAS_CPP11_TASK_HPP_ + +#include + +#include "FreeRTOS.h" +#include "task.h" + +namespace cxx { + + +/*! + * \brief Wrapper for the FreeRTOS task. + * \param F Template parameter specifying the type of the function, which is + * some form of a lambda. Do *NOT* specify this by hand - use the + * `make_task` function to instantiate tasks. + */ +template +class task +{ +public: + /*! + * \brief Constructor for the task class. + * \param name Name of the task. + * \param stack_depth Stack depth for the task (use 256 as default). + * \param priority FreeRTOS priority of the task (use 2 as default). + * \param param Parameter passed to task execution (use nullptr as default). + * \param thread Function executed by the task. + */ + task(const char * name, + const unsigned short stack_depth, + const unsigned portBASE_TYPE priority, + void * param, + F thread) + : _name(name), + _stack_depth(stack_depth), + _priority(priority), + _param(param), + _thread(thread) {} + + /*! + * \brief Destructor for task class deletes FreeRTOS task. + */ + ~task() { + if (_active) { vTaskDelete(_handle); } + } + + // Disable default construction, copy construction and assignment. + task() = delete; + task(const task &) = delete; + task & operator=(const task &) = delete; + + // Default move semantics + task(task && other) = default; + + void run() { + _active = + xTaskCreate( + +_thread, _name, _stack_depth, _param, _priority, &_handle) == pdPASS; + } + + /*! + * \brief Check if task is currently active. + * \returns boolean: true if task is active. + */ + bool active() const { return _active; } + +private: + const char * _name; + const unsigned short _stack_depth; + const unsigned portBASE_TYPE _priority; + void * _param; + F _thread; + + TaskHandle_t _handle; + bool _active{false}; +}; + + +/*! + * \brief Function to generate task object. + * \param name Name of the task. + * \param stack_depth Stack depth for the task. + * \param priority FreeRTOS priority of the task. + * \param thread Function executed by the task. + * \param param Parameters passed to task execution, nullptr by default. + * \returns task object. + */ +template +constexpr task make_task(const char * name, + const unsigned short stack_depth, + const unsigned portBASE_TYPE priority, + void * param, + F thread) { + return std::move(task(name, stack_depth, priority, param, thread)); +} + + +/*! + * \brief Function to generate task object. + * \param name Name of the task. + * \param stack_depth Stack depth for the task. + * \param priority FreeRTOS priority of the task. + * \param thread Function executed by the task. + * \returns task object. + */ +template +constexpr task make_task(const char * name, + const unsigned short stack_depth, + const unsigned portBASE_TYPE priority, + F thread) { + return std::move(task(name, stack_depth, priority, nullptr, thread)); +} + + +/*! + * \brief Function to generate task object. + * \param name Name of the task. + * \param stack_depth Stack depth for the task. + * \param thread Function executed by the task. + * \returns task object. + */ +template +constexpr task make_task(const char * name, + const unsigned short stack_depth, + F thread) { + return std::move(task(name, stack_depth, 2, nullptr, thread)); +} + + +/*! + * \brief Function to generate task object. + * \param name Name of the task. + * \param thread Function executed by the task. + * \returns task object. + */ +template +constexpr task make_task(const char * name, F thread) { + return std::move(task(name, 256, 2, nullptr, thread)); +} + +} // namespace cxx + +#endif // ESP_OPEN_RTOS_EXTRAS_CPP11_TASK_HPP_ + diff --git a/extras/cpp11/include/util.hpp b/extras/cpp11/include/util.hpp new file mode 100644 index 0000000..3b19e8c --- /dev/null +++ b/extras/cpp11/include/util.hpp @@ -0,0 +1,44 @@ +// Copyright 2017 Florian Eich +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef ESP_OPEN_RTOS_EXTRAS_CPP11_UTIL_HPP_ +#define ESP_OPEN_RTOS_EXTRAS_CPP11_UTIL_HPP_ + +#include "FreeRTOS.h" + + +namespace cxx { + + +/*! + * \brief Function to determine current task time. + * \returns Time spent on task in milliseconds. + */ +unsigned long millis() { + return xTaskGetTickCount() * portTICK_PERIOD_MS; +} + + +/*! + * \brief Function to sleep for some time. + * \param ms Time to sleep in milliseconds. + */ +void sleep(const unsigned long ms) { + vTaskDelay(ms / portTICK_PERIOD_MS); +} + +} // namespace cxx + +#endif // ESP_OPEN_RTOS_EXTRAS_CPP11_UTIL_HPP_ + diff --git a/parameters.mk b/parameters.mk index e1f019e..72ff096 100644 --- a/parameters.mk +++ b/parameters.mk @@ -90,7 +90,7 @@ C_CXX_FLAGS ?= -Wall -Wl,-EL -nostdlib $(EXTRA_C_CXX_FLAGS) # Flags for C only CFLAGS ?= $(C_CXX_FLAGS) -std=gnu99 $(EXTRA_CFLAGS) # Flags for C++ only -CXXFLAGS ?= $(C_CXX_FLAGS) -fno-exceptions -fno-rtti $(EXTRA_CXXFLAGS) +CXXFLAGS ?= $(C_CXX_FLAGS) -std=gnu++11 -fno-exceptions -fno-rtti $(EXTRA_CXXFLAGS) # these aren't all technically preprocesor args, but used by all 3 of C, C++, assembler CPPFLAGS += -mlongcalls -mtext-section-literals