Added C++ base components and example

This commit is contained in:
Michael Jacobsen 2015-08-19 08:38:15 +02:00 committed by Angus Gratton
parent 7d480876fa
commit 1282e11c08
11 changed files with 586 additions and 2 deletions

View file

@ -0,0 +1,3 @@
# Component makefile for extras/cplusplus
INC_DIRS += $(ROOT)extras

View file

@ -0,0 +1,68 @@
/*
* ESP8266 FreeRTOS Firmware
* Copyright (C) 2015 Michael Jacobsen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* https://github.com/SuperHouse/esp-open-rtos
*
*/
#ifndef COM_THOLUSI_ESP_OPEN_RTOS_CPLUSPLUS_HPP
#define COM_THOLUSI_ESP_OPEN_RTOS_CPLUSPLUS_HPP
#include <stdlib.h>
/******************************************************************************************************************
* C++ new and delete operators
*
*/
/**
*
* @param size
* @return
*/
inline void *operator new(size_t size)
{
return malloc(size);
}
/**
*
* @param size
* @return
*/
inline void *operator new[](size_t size)
{
return malloc(size);
}
/**
*
* @param ptr
*/
inline void operator delete(void * ptr)
{
free(ptr);
}
/**
*
* @param ptr
*/
inline void operator delete[](void * ptr)
{
free(ptr);
}
#endif /* COM_THOLUSI_ESP_OPEN_RTOS_CPLUSPLUS_HPP */

View file

@ -0,0 +1,3 @@
# Component makefile for extras/thread
INC_DIRS += $(ROOT)extras

90
extras/thread/mutex.hpp Normal file
View file

@ -0,0 +1,90 @@
/*
* ESP8266 FreeRTOS Firmware
* Copyright (C) 2015 Michael Jacobsen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* https://github.com/SuperHouse/esp-open-rtos
*
*/
#ifndef COM_THOLUSI_ESP_OPEN_RTOS_MUTEX_HPP
#define COM_THOLUSI_ESP_OPEN_RTOS_MUTEX_HPP
#include "semphr.h"
namespace esp_open_rtos {
namespace thread {
/******************************************************************************************************************
* class mutex_t
*
*/
class mutex_t
{
public:
/**
*
*/
inline mutex_t()
{
mutex = xSemaphoreCreateMutex();
}
/**
*
*/
inline ~mutex_t()
{
vQueueDelete(mutex);
}
/**
*
* @return
*/
inline int lock()
{
return (xSemaphoreTake(mutex, portMAX_DELAY) == pdTRUE) ? 0 : -1;
}
/**
*
* @param ms
* @return
*/
inline int try_lock(unsigned long ms)
{
return (xSemaphoreTake(mutex, ms / portTICK_RATE_MS) == pdTRUE) ? 0 : -1;
}
/**
*
* @return
*/
inline int unlock()
{
return (xSemaphoreGive(mutex) == pdTRUE) ? 0 : -1;
}
private:
xSemaphoreHandle mutex;
// Disable copy construction and assignment.
mutex_t (const mutex_t&);
const mutex_t &operator = (const mutex_t&);
};
} //namespace thread {
} //namespace esp_open_rtos {
#endif /* COM_THOLUSI_ESP_OPEN_RTOS_MUTEX_HPP */

120
extras/thread/queue.hpp Normal file
View file

@ -0,0 +1,120 @@
/*
* ESP8266 FreeRTOS Firmware
* Copyright (C) 2015 Michael Jacobsen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* https://github.com/SuperHouse/esp-open-rtos
*
*/
#ifndef COM_THOLUSI_ESP_OPEN_RTOS_QUEUE_HPP
#define COM_THOLUSI_ESP_OPEN_RTOS_QUEUE_HPP
#include "FreeRTOS.h"
#include "queue.h"
namespace esp_open_rtos {
namespace thread {
/******************************************************************************************************************
* class queue_t
*
*/
template<class Data>
class queue_t
{
public:
/**
*
*/
inline queue_t()
{
queue = 0;
}
/**
*
* @param uxQueueLength
* @param uxItemSize
* @return
*/
inline int create(unsigned portBASE_TYPE uxQueueLength)
{
queue = xQueueCreate(uxQueueLength, sizeof(Data));
if(queue == NULL) {
return -1;
}
else {
return 0;
}
}
/**
*
* @param data
* @param ms
* @return
*/
inline void destroy()
{
vQueueDelete(queue);
queue = 0;
}
/**
*
* @param data
* @param ms
* @return
*/
inline int post(const Data& data, unsigned long ms = 0)
{
return (xQueueSend(queue, &data, ms / portTICK_RATE_MS) == pdTRUE) ? 0 : -1;
}
/**
*
* @param data
* @param ms
* @return
*/
inline int receive(Data& data, unsigned long ms = 0)
{
return (xQueueReceive(queue, &data, ms / portTICK_RATE_MS) == pdTRUE) ? 0 : -1;
}
/**
*
* @param other
* @return
*/
const queue_t &operator = (const queue_t& other)
{
if(this != &other) { // protect against invalid self-assignment
queue = other.queue;
}
return *this;
}
private:
xQueueHandle queue;
// Disable copy construction.
queue_t (const queue_t&);
};
} //namespace thread {
} //namespace esp_open_rtos {
#endif /* COM_THOLUSI_ESP_OPEN_RTOS_QUEUE_HPP */

99
extras/thread/task.hpp Normal file
View file

@ -0,0 +1,99 @@
/*
* ESP8266 FreeRTOS Firmware
* Copyright (C) 2015 Michael Jacobsen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* https://github.com/SuperHouse/esp-open-rtos
*
*/
#ifndef COM_THOLUSI_ESP_OPEN_RTOS_TASK_HPP
#define COM_THOLUSI_ESP_OPEN_RTOS_TASK_HPP
#include "FreeRTOS.h"
#include "task.h"
namespace esp_open_rtos {
namespace thread {
/******************************************************************************************************************
* task_t
*
*/
class task_t
{
public:
/**
*
*/
task_t()
{}
/**
*
* @param pcName
* @param usStackDepth
* @param uxPriority
* @return
*/
int task_create(const char* const pcName, unsigned short usStackDepth = 256, unsigned portBASE_TYPE uxPriority = 2)
{
return xTaskCreate(task_t::_task, (signed char *)pcName, usStackDepth, this, uxPriority, NULL);
}
protected:
/**
*
* @param ms
*/
void sleep(unsigned long ms)
{
vTaskDelay(ms / portTICK_RATE_MS);
}
/**
*
* @return
*/
inline unsigned long millis()
{
return xTaskGetTickCount() * portTICK_RATE_MS;
}
private:
/**
*
*/
virtual void task() = 0;
/**
*
* @param pvParameters
*/
static void _task(void* pvParameters)
{
if(pvParameters != 0) {
((task_t*)(pvParameters))->task();
}
}
// no copy and no = operator
task_t(const task_t&);
task_t &operator=(const task_t&);
};
} //namespace thread {
} //namespace esp_open_rtos {
#endif /* COM_THOLUSI_ESP_OPEN_RTOS_TASK_HPP */

View file

@ -0,0 +1,3 @@
# Component makefile for extras/timer
INC_DIRS += $(ROOT)extras

View file

@ -0,0 +1,95 @@
/*
* ESP8266 FreeRTOS Firmware
* Copyright (C) 2015 Michael Jacobsen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* https://github.com/SuperHouse/esp-open-rtos
*
*/
#ifndef COM_THOLUSI_ESP_OPEN_RTOS_TIMER_HPP
#define COM_THOLUSI_ESP_OPEN_RTOS_TIMER_HPP
#include "FreeRTOS.h"
#include "task.h"
namespace esp_open_rtos {
namespace timer {
#define __millis() (xTaskGetTickCount() * portTICK_RATE_MS)
/******************************************************************************************************************
* countdown_t
*
*/
class countdown_t
{
public:
/**
*
*/
countdown_t()
{
interval_end_ms = 0L;
}
/**
*
* @param ms
*/
countdown_t(int ms)
{
countdown_ms(ms);
}
/**
*
* @return
*/
bool expired()
{
return (interval_end_ms > 0L) && (__millis() >= interval_end_ms);
}
/**
*
* @param ms
*/
void countdown_ms(unsigned long ms)
{
interval_end_ms = __millis() + ms;
}
/**
*
* @param seconds
*/
void countdown(int seconds)
{
countdown_ms((unsigned long)seconds * 1000L);
}
/**
*
* @return
*/
int left_ms()
{
return interval_end_ms - __millis();
}
private:
portTickType interval_end_ms;
};
} // namespace timer {
} // namespace esp_open_rtos {
#endif