Skip to content

Commit

Permalink
Sensors to class sensorState.
Browse files Browse the repository at this point in the history
Sensors are now in class sensorState.
  • Loading branch information
CelliesProjects authored Jul 9, 2019
1 parent c903297 commit 6fa531c
Show file tree
Hide file tree
Showing 22 changed files with 2,343 additions and 1,826 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ The [moon phase library](https://github.com/CelliesProjects/MoonPhase) is adapte

By default log files are not generated.
<br>That is because log files saved on FFat could reduce the lifetime of the flash memory.
<br>To log the temperature sensor values enable `LOG_FILES` (set it to `true`) in `deviceSetup.h`.
<br>Sensor logging can be enabled in the web interface.
<br>`Task.h` and `Task.cpp` used in the sensor code are written by Neil Kolban, can be found [here](https://github.com/nkolban/esp32-snippets) and are released under the [Apache 2.0 license](https://github.com/nkolban/esp32-snippets/blob/master/LICENSE).

#### Known issues:

Expand All @@ -123,7 +124,6 @@ By default log files are not generated.

I develop Aquacontrol32 in my spare time for fun.
Although I like to code, my afk time is equally important.
A beer in a bar is around €2 where I live.
If you like the project, you could buy me a beer for some moral support.

[![paypal](https://www.paypalobjects.com/en_US/NL/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MSP53ANQ3VV6J)
130 changes: 130 additions & 0 deletions Task.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Task.cpp
*
* Created on: Mar 4, 2017
* Author: kolban
*/


#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string>

#include "Task.h"
#include "sdkconfig.h"

static const char* LOG_TAG = "Task";


/**
* @brief Create an instance of the task class.
*
* @param [in] taskName The name of the task to create.
* @param [in] stackSize The size of the stack.
* @return N/A.
*/
Task::Task(std::string taskName, uint16_t stackSize, uint8_t priority) {
m_taskName = taskName;
m_stackSize = stackSize;
m_priority = priority;
m_taskData = nullptr;
m_handle = nullptr;
m_coreId = tskNO_AFFINITY;
} // Task

Task::~Task() {
} // ~Task

/**
* @brief Suspend the task for the specified milliseconds.
*
* @param [in] ms The delay time in milliseconds.
* @return N/A.
*/

/* static */ void Task::delay(int ms) {
::vTaskDelay(ms / portTICK_PERIOD_MS);
} // delay

/**
* Static class member that actually runs the target task.
*
* The code here will run on the task thread.
* @param [in] pTaskInstance The task to run.
*/
void Task::runTask(void* pTaskInstance) {
Task* pTask = (Task*) pTaskInstance;
ESP_LOGD(LOG_TAG, ">> runTask: taskName=%s", pTask->m_taskName.c_str());
pTask->run(pTask->m_taskData);
//ESP_LOGD(LOG_TAG, "<< runTask: taskName=%s", pTask->m_taskName.c_str());
//pTask->stop();
} // runTask

/**
* @brief Start an instance of the task.
*
* @param [in] taskData Data to be passed into the task.
* @return N/A.
*/
void Task::start(void* taskData) {
if (m_handle != nullptr) {
ESP_LOGW(LOG_TAG, "Task::start - There might be a task already running!");
}
m_taskData = taskData;
::xTaskCreatePinnedToCore(&runTask, m_taskName.c_str(), m_stackSize, this, m_priority, &m_handle, m_coreId);
} // start


/**
* @brief Stop the task.
*
* @return N/A.
*/
void Task::stop() {
if (m_handle == nullptr) return;
xTaskHandle temp = m_handle;
m_handle = nullptr;
::vTaskDelete(temp);
} // stop

/**
* @brief Set the stack size of the task.
*
* @param [in] stackSize The size of the stack for the task.
* @return N/A.
*/
void Task::setStackSize(uint16_t stackSize) {
m_stackSize = stackSize;
} // setStackSize

/**
* @brief Set the priority of the task.
*
* @param [in] priority The priority for the task.
* @return N/A.
*/
void Task::setPriority(uint8_t priority) {
m_priority = priority;
} // setPriority

/**
* @brief Set the name of the task.
*
* @param [in] name The name for the task.
* @return N/A.
*/
void Task::setName(std::string name) {
m_taskName = name;
} // setName

/**
* @brief Set the core number the task has to be executed on.
* If the core number is not set, tskNO_AFFINITY will be used
*
* @param [in] coreId The id of the core.
* @return N/A.
*/
void Task::setCore(BaseType_t coreId) {
m_coreId = coreId;
}
67 changes: 67 additions & 0 deletions Task.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Task.h
*
* Created on: Mar 4, 2017
* Author: kolban
*/

#ifndef COMPONENTS_CPP_UTILS_TASK_H_
#define COMPONENTS_CPP_UTILS_TASK_H_
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string>
/**
* @brief Encapsulate a runnable task.
*
* This class is designed to be subclassed with the method:
*
* @code{.cpp}
* void run(void *data) { ... }
* @endcode
*
* For example:
*
* @code{.cpp}
* class CurlTestTask : public Task {
* void run(void *data) {
* // Do something
* }
* };
* @endcode
*
* implemented.
*/
class Task {
public:
Task(std::string taskName = "Task", uint16_t stackSize = 10000, uint8_t priority = 5);
virtual ~Task();
void setStackSize(uint16_t stackSize);
void setPriority(uint8_t priority);
void setName(std::string name);
void setCore(BaseType_t coreId);
void start(void* taskData = nullptr);
void stop();
/**
* @brief Body of the task to execute.
*
* This function must be implemented in the subclass that represents the actual task to run.
* When a task is started by calling start(), this is the code that is executed in the
* newly created task.
*
* @param [in] data The data passed in to the newly started task.
*/
virtual void run(void* data) = 0; // Make run pure virtual
static void delay(int ms);

private:
xTaskHandle m_handle;
void* m_taskData;
static void runTask(void* data);
std::string m_taskName;
uint16_t m_stackSize;
uint8_t m_priority;
BaseType_t m_coreId;

};

#endif /* COMPONENTS_CPP_UTILS_TASK_H_ */
Loading

0 comments on commit 6fa531c

Please sign in to comment.