FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
tasklet.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
4  *
5  * This file is part of the FreeRTOS Add-ons project.
6  *
7  * Source Code:
8  * https://github.com/michaelbecker/freertos-addons
9  *
10  * Project Page:
11  * http://michaelbecker.github.io/freertos-addons/
12  *
13  * On-line Documentation:
14  * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files
18  * (the "Software"), to deal in the Software without restriction, including
19  * without limitation the rights to use, copy, modify, merge, publish,
20  * distribute, sublicense, and/or sell copies of the Software, and to
21  * permit persons to whom the Software is furnished to do so,subject to the
22  * following conditions:
23  *
24  * + The above copyright notice and this permission notice shall be included
25  * in all copies or substantial portions of the Software.
26  * + Credit is appreciated, but not required, if you find this project
27  * useful enough to include in your application, product, device, etc.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36  *
37  ***************************************************************************/
38 
39 
40 #ifndef TASKLET_HPP_
41 #define TASKLET_HPP_
42 
43 
51 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
52 #include <exception>
53 #include <string>
54 #include <cstdio>
55 #ifdef CPP_FREERTOS_NO_CPP_STRINGS
56 #error "FreeRTOS-Addons require C++ Strings if you are using exceptions"
57 #endif
58 #endif
59 #include "FreeRTOS.h"
60 #include "timers.h"
61 #include "semphr.h"
62 
63 
64 namespace cpp_freertos {
65 
66 
67 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
68 
71 class TaskletCreateException : public std::exception {
72 
73  public:
78  {
79  sprintf(errorString, "Tasklet Constructor Failed");
80  }
81 
85  explicit TaskletCreateException(const char *info)
86  {
87  snprintf(errorString, sizeof(errorString),
88  "Tasklet Constructor Failed %s", info);
89  }
90 
95  virtual const char *what() const throw()
96  {
97  return errorString;
98  }
99 
100  private:
104  char errorString[80];
105 };
106 #endif
107 
108 
119 class Tasklet {
120 
122  //
123  // Public API
124  //
126  public:
132  Tasklet();
133 
139  virtual ~Tasklet();
140 
150  bool Schedule( uint32_t parameter,
151  TickType_t CmdTimeout = portMAX_DELAY);
152 
164  bool ScheduleFromISR( uint32_t parameter,
165  BaseType_t *pxHigherPriorityTaskWoken);
166 
168  //
169  // Protected API
170  // Available from inside your Tasklet implementation.
171  // You should make sure that you are only calling these methods
172  // from within your Run() method, or that your Run() method is on the
173  // callstack.
174  //
176  protected:
183  virtual void Run(uint32_t parameter) = 0;
184 
189  void CheckForSafeDelete();
190 
192  //
193  // Private API
194  // The internals of this wrapper class.
195  //
197  private:
204  static void TaskletAdapterFunction(void *ref, uint32_t parameter);
205 
209  SemaphoreHandle_t DtorLock;
210 };
211 
212 }
213 #endif
214 
TaskletCreateException(const char *info)
Definition: tasklet.hpp:85
virtual const char * what() const
Definition: tasklet.hpp:95
SemaphoreHandle_t DtorLock
Definition: tasklet.hpp:209