|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + * @file core_callback.c |
| 4 | + * @author WI6LABS |
| 5 | + * @version V1.0.0 |
| 6 | + * @date 8-September-2017 |
| 7 | + * @brief Provides methods to add callback to call inside the main loop. |
| 8 | + ****************************************************************************** |
| 9 | + * @attention |
| 10 | + * |
| 11 | + * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
| 12 | + * |
| 13 | + * Redistribution and use in source and binary forms, with or without modification, |
| 14 | + * are permitted provided that the following conditions are met: |
| 15 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer. |
| 17 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 18 | + * this list of conditions and the following disclaimer in the documentation |
| 19 | + * and/or other materials provided with the distribution. |
| 20 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 21 | + * may be used to endorse or promote products derived from this software |
| 22 | + * without specific prior written permission. |
| 23 | + * |
| 24 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 28 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 30 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 31 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 32 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 33 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | + * |
| 35 | + ****************************************************************************** |
| 36 | + */ |
| 37 | + |
| 38 | +/* |
| 39 | + * @NOTE |
| 40 | + * This file provides methods to add callback in the main() function loop. |
| 41 | + * If you need to call as often as possible a function to update your system and |
| 42 | + * you want to be sure this function to be called, you can add it to the callback |
| 43 | + * list. Otherwise, your function should be called inside the loop() function of |
| 44 | + * the sketch. |
| 45 | + */ |
| 46 | + |
| 47 | +#ifdef __cplusplus |
| 48 | + extern "C" { |
| 49 | +#endif |
| 50 | + |
| 51 | +/* Includes ------------------------------------------------------------------*/ |
| 52 | +#include "core_callback.h" |
| 53 | + |
| 54 | +// List of callback to call |
| 55 | +static void (*callbackList[CALLBACK_LIST_SIZE])(void); |
| 56 | + |
| 57 | +/** |
| 58 | + * @brief Adds a callback pointer |
| 59 | + * @param func: callback pointer |
| 60 | + * @retval None |
| 61 | + */ |
| 62 | +void registerCoreCallback(void (*func)(void)) |
| 63 | +{ |
| 64 | + if(func == NULL) |
| 65 | + return; |
| 66 | + |
| 67 | + for(uint8_t i = 0; i < CALLBACK_LIST_SIZE; i++) { |
| 68 | + if(callbackList[i] == NULL) { |
| 69 | + callbackList[i] = func; |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * @brief Removes a callback pointer |
| 77 | + * @param func: callback pointer |
| 78 | + * @retval None |
| 79 | + */ |
| 80 | +void unregisterCoreCallback(void (*func)(void)) |
| 81 | +{ |
| 82 | + if(func == NULL) |
| 83 | + return; |
| 84 | + |
| 85 | + for(uint8_t i = 0; i < CALLBACK_LIST_SIZE; i++) { |
| 86 | + if(callbackList[i] == func) { |
| 87 | + callbackList[i] = NULL; |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * @brief Calls callback of the list. There is no priority. First added first |
| 95 | + * called. This function must be called only in main() function loop. |
| 96 | + * @param None |
| 97 | + * @retval None |
| 98 | + */ |
| 99 | +void CoreCallback(void) |
| 100 | +{ |
| 101 | + for(uint8_t i = 0; i < CALLBACK_LIST_SIZE; i++) { |
| 102 | + if(callbackList[i] != NULL) |
| 103 | + callbackList[i](); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +#ifdef __cplusplus |
| 108 | +} |
| 109 | +#endif |
| 110 | + |
| 111 | +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
0 commit comments