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:
Thu Dec 05 20:34:10 2019 -0500
Revision:
0:e0dbd261724a
Adding code to mbed repo

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 MAIL_H
arogliero3 0:e0dbd261724a 23 #define MAIL_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 /** The Mail class allow to control, send, receive, or wait for mail.
arogliero3 0:e0dbd261724a 35 A mail is a memory block that is send to a thread or interrupt service routine.
arogliero3 0:e0dbd261724a 36 @tparam T data type of a single message element.
arogliero3 0:e0dbd261724a 37 @tparam queue_sz maximum number of messages in queue.
arogliero3 0:e0dbd261724a 38 */
arogliero3 0:e0dbd261724a 39 template<typename T, uint32_t queue_sz>
arogliero3 0:e0dbd261724a 40 class Mail {
arogliero3 0:e0dbd261724a 41 public:
arogliero3 0:e0dbd261724a 42 /** Create and Initialise Mail queue. */
arogliero3 0:e0dbd261724a 43 Mail() {
arogliero3 0:e0dbd261724a 44 #ifdef CMSIS_OS_RTX
arogliero3 0:e0dbd261724a 45 memset(_mail_q, 0, sizeof(_mail_q));
arogliero3 0:e0dbd261724a 46 _mail_p[0] = _mail_q;
arogliero3 0:e0dbd261724a 47
arogliero3 0:e0dbd261724a 48 memset(_mail_m, 0, sizeof(_mail_m));
arogliero3 0:e0dbd261724a 49 _mail_p[1] = _mail_m;
arogliero3 0:e0dbd261724a 50
arogliero3 0:e0dbd261724a 51 _mail_def.pool = _mail_p;
arogliero3 0:e0dbd261724a 52 _mail_def.queue_sz = queue_sz;
arogliero3 0:e0dbd261724a 53 _mail_def.item_sz = sizeof(T);
arogliero3 0:e0dbd261724a 54 #endif
arogliero3 0:e0dbd261724a 55 _mail_id = osMailCreate(&_mail_def, NULL);
arogliero3 0:e0dbd261724a 56 }
arogliero3 0:e0dbd261724a 57
arogliero3 0:e0dbd261724a 58 /** Allocate a memory block of type T
arogliero3 0:e0dbd261724a 59 @param millisec timeout value or 0 in case of no time-out. (default: 0).
arogliero3 0:e0dbd261724a 60 @return pointer to memory block that can be filled with mail or NULL in case error.
arogliero3 0:e0dbd261724a 61 */
arogliero3 0:e0dbd261724a 62 T* alloc(uint32_t millisec=0) {
arogliero3 0:e0dbd261724a 63 return (T*)osMailAlloc(_mail_id, millisec);
arogliero3 0:e0dbd261724a 64 }
arogliero3 0:e0dbd261724a 65
arogliero3 0:e0dbd261724a 66 /** Allocate a memory block of type T and set memory block to zero.
arogliero3 0:e0dbd261724a 67 @param millisec timeout value or 0 in case of no time-out. (default: 0).
arogliero3 0:e0dbd261724a 68 @return pointer to memory block that can be filled with mail or NULL in case error.
arogliero3 0:e0dbd261724a 69 */
arogliero3 0:e0dbd261724a 70 T* calloc(uint32_t millisec=0) {
arogliero3 0:e0dbd261724a 71 return (T*)osMailCAlloc(_mail_id, millisec);
arogliero3 0:e0dbd261724a 72 }
arogliero3 0:e0dbd261724a 73
arogliero3 0:e0dbd261724a 74 /** Put a mail in the queue.
arogliero3 0:e0dbd261724a 75 @param mptr memory block previously allocated with Mail::alloc or Mail::calloc.
arogliero3 0:e0dbd261724a 76 @return status code that indicates the execution status of the function.
arogliero3 0:e0dbd261724a 77 */
arogliero3 0:e0dbd261724a 78 osStatus put(T *mptr) {
arogliero3 0:e0dbd261724a 79 return osMailPut(_mail_id, (void*)mptr);
arogliero3 0:e0dbd261724a 80 }
arogliero3 0:e0dbd261724a 81
arogliero3 0:e0dbd261724a 82 /** Get a mail from a queue.
arogliero3 0:e0dbd261724a 83 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
arogliero3 0:e0dbd261724a 84 @return event that contains mail information or error code.
arogliero3 0:e0dbd261724a 85 */
arogliero3 0:e0dbd261724a 86 osEvent get(uint32_t millisec=osWaitForever) {
arogliero3 0:e0dbd261724a 87 return osMailGet(_mail_id, millisec);
arogliero3 0:e0dbd261724a 88 }
arogliero3 0:e0dbd261724a 89
arogliero3 0:e0dbd261724a 90 /** Free a memory block from a mail.
arogliero3 0:e0dbd261724a 91 @param mptr pointer to the memory block that was obtained with Mail::get.
arogliero3 0:e0dbd261724a 92 @return status code that indicates the execution status of the function.
arogliero3 0:e0dbd261724a 93 */
arogliero3 0:e0dbd261724a 94 osStatus free(T *mptr) {
arogliero3 0:e0dbd261724a 95 return osMailFree(_mail_id, (void*)mptr);
arogliero3 0:e0dbd261724a 96 }
arogliero3 0:e0dbd261724a 97
arogliero3 0:e0dbd261724a 98 private:
arogliero3 0:e0dbd261724a 99 osMailQId _mail_id;
arogliero3 0:e0dbd261724a 100 osMailQDef_t _mail_def;
arogliero3 0:e0dbd261724a 101 #ifdef CMSIS_OS_RTX
arogliero3 0:e0dbd261724a 102 uint32_t _mail_q[4+(queue_sz)];
arogliero3 0:e0dbd261724a 103 uint32_t _mail_m[3+((sizeof(T)+3)/4)*(queue_sz)];
arogliero3 0:e0dbd261724a 104 void *_mail_p[2];
arogliero3 0:e0dbd261724a 105 #endif
arogliero3 0:e0dbd261724a 106 };
arogliero3 0:e0dbd261724a 107
arogliero3 0:e0dbd261724a 108 }
arogliero3 0:e0dbd261724a 109
arogliero3 0:e0dbd261724a 110 #endif
arogliero3 0:e0dbd261724a 111
arogliero3 0:e0dbd261724a 112
arogliero3 0:e0dbd261724a 113 /** @}*/