Code for controlling mbed hardware (LED's, motors), as well as code for the Raspberry Pi to run a Support Vector Machine that identifies objects using the Pi camera

Dependencies:   mbed Motordriver mbed-rtos PololuLedStrip

Committer:
arogliero3
Date:
Fri Dec 06 00:58:02 2019 -0500
Revision:
3:a3ed7ff99772
Parent:
0:e0dbd261724a
update img6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arogliero3 0:e0dbd261724a 1 /* mbed Microcontroller Library
arogliero3 0:e0dbd261724a 2 * Copyright (c) 2006-2012 ARM Limited
arogliero3 0:e0dbd261724a 3 *
arogliero3 0:e0dbd261724a 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
arogliero3 0:e0dbd261724a 5 * of this software and associated documentation files (the "Software"), to deal
arogliero3 0:e0dbd261724a 6 * in the Software without restriction, including without limitation the rights
arogliero3 0:e0dbd261724a 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
arogliero3 0:e0dbd261724a 8 * copies of the Software, and to permit persons to whom the Software is
arogliero3 0:e0dbd261724a 9 * furnished to do so, subject to the following conditions:
arogliero3 0:e0dbd261724a 10 *
arogliero3 0:e0dbd261724a 11 * The above copyright notice and this permission notice shall be included in
arogliero3 0:e0dbd261724a 12 * all copies or substantial portions of the Software.
arogliero3 0:e0dbd261724a 13 *
arogliero3 0:e0dbd261724a 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
arogliero3 0:e0dbd261724a 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
arogliero3 0:e0dbd261724a 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
arogliero3 0:e0dbd261724a 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
arogliero3 0:e0dbd261724a 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
arogliero3 0:e0dbd261724a 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
arogliero3 0:e0dbd261724a 20 * SOFTWARE.
arogliero3 0:e0dbd261724a 21 */
arogliero3 0:e0dbd261724a 22 #ifndef MEMORYPOOL_H
arogliero3 0:e0dbd261724a 23 #define MEMORYPOOL_H
arogliero3 0:e0dbd261724a 24
arogliero3 0:e0dbd261724a 25 #include <stdint.h>
arogliero3 0:e0dbd261724a 26 #include <string.h>
arogliero3 0:e0dbd261724a 27
arogliero3 0:e0dbd261724a 28 #include "cmsis_os.h"
arogliero3 0:e0dbd261724a 29
arogliero3 0:e0dbd261724a 30 namespace rtos {
arogliero3 0:e0dbd261724a 31 /** \addtogroup rtos */
arogliero3 0:e0dbd261724a 32 /** @{*/
arogliero3 0:e0dbd261724a 33
arogliero3 0:e0dbd261724a 34 /** Define and manage fixed-size memory pools of objects of a given type.
arogliero3 0:e0dbd261724a 35 @tparam T data type of a single object (element).
arogliero3 0:e0dbd261724a 36 @tparam queue_sz maximum number of objects (elements) in the memory pool.
arogliero3 0:e0dbd261724a 37 */
arogliero3 0:e0dbd261724a 38 template<typename T, uint32_t pool_sz>
arogliero3 0:e0dbd261724a 39 class MemoryPool {
arogliero3 0:e0dbd261724a 40 public:
arogliero3 0:e0dbd261724a 41 /** Create and Initialize a memory pool. */
arogliero3 0:e0dbd261724a 42 MemoryPool() {
arogliero3 0:e0dbd261724a 43 #ifdef CMSIS_OS_RTX
arogliero3 0:e0dbd261724a 44 memset(_pool_m, 0, sizeof(_pool_m));
arogliero3 0:e0dbd261724a 45 _pool_def.pool = _pool_m;
arogliero3 0:e0dbd261724a 46
arogliero3 0:e0dbd261724a 47 _pool_def.pool_sz = pool_sz;
arogliero3 0:e0dbd261724a 48 _pool_def.item_sz = sizeof(T);
arogliero3 0:e0dbd261724a 49 #endif
arogliero3 0:e0dbd261724a 50 _pool_id = osPoolCreate(&_pool_def);
arogliero3 0:e0dbd261724a 51 }
arogliero3 0:e0dbd261724a 52
arogliero3 0:e0dbd261724a 53 /** Allocate a memory block of type T from a memory pool.
arogliero3 0:e0dbd261724a 54 @return address of the allocated memory block or NULL in case of no memory available.
arogliero3 0:e0dbd261724a 55 */
arogliero3 0:e0dbd261724a 56 T* alloc(void) {
arogliero3 0:e0dbd261724a 57 return (T*)osPoolAlloc(_pool_id);
arogliero3 0:e0dbd261724a 58 }
arogliero3 0:e0dbd261724a 59
arogliero3 0:e0dbd261724a 60 /** Allocate a memory block of type T from a memory pool and set memory block to zero.
arogliero3 0:e0dbd261724a 61 @return address of the allocated memory block or NULL in case of no memory available.
arogliero3 0:e0dbd261724a 62 */
arogliero3 0:e0dbd261724a 63 T* calloc(void) {
arogliero3 0:e0dbd261724a 64 return (T*)osPoolCAlloc(_pool_id);
arogliero3 0:e0dbd261724a 65 }
arogliero3 0:e0dbd261724a 66
arogliero3 0:e0dbd261724a 67 /** Return an allocated memory block back to a specific memory pool.
arogliero3 0:e0dbd261724a 68 @param address of the allocated memory block that is returned to the memory pool.
arogliero3 0:e0dbd261724a 69 @return status code that indicates the execution status of the function.
arogliero3 0:e0dbd261724a 70 */
arogliero3 0:e0dbd261724a 71 osStatus free(T *block) {
arogliero3 0:e0dbd261724a 72 return osPoolFree(_pool_id, (void*)block);
arogliero3 0:e0dbd261724a 73 }
arogliero3 0:e0dbd261724a 74
arogliero3 0:e0dbd261724a 75 private:
arogliero3 0:e0dbd261724a 76 osPoolId _pool_id;
arogliero3 0:e0dbd261724a 77 osPoolDef_t _pool_def;
arogliero3 0:e0dbd261724a 78 #ifdef CMSIS_OS_RTX
arogliero3 0:e0dbd261724a 79 uint32_t _pool_m[3+((sizeof(T)+3)/4)*(pool_sz)];
arogliero3 0:e0dbd261724a 80 #endif
arogliero3 0:e0dbd261724a 81 };
arogliero3 0:e0dbd261724a 82
arogliero3 0:e0dbd261724a 83 }
arogliero3 0:e0dbd261724a 84 #endif
arogliero3 0:e0dbd261724a 85
arogliero3 0:e0dbd261724a 86 /** @}*/