Music Visualizer

Dependencies:   mbed SDFileSystem NeoStrip PinDetect

Committer:
spatel465
Date:
Thu Apr 30 01:45:25 2020 +0000
Revision:
0:adce77867281
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
spatel465 0:adce77867281 1 /* mbed Microcontroller Library
spatel465 0:adce77867281 2 * Copyright (c) 2006-2012 ARM Limited
spatel465 0:adce77867281 3 *
spatel465 0:adce77867281 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
spatel465 0:adce77867281 5 * of this software and associated documentation files (the "Software"), to deal
spatel465 0:adce77867281 6 * in the Software without restriction, including without limitation the rights
spatel465 0:adce77867281 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
spatel465 0:adce77867281 8 * copies of the Software, and to permit persons to whom the Software is
spatel465 0:adce77867281 9 * furnished to do so, subject to the following conditions:
spatel465 0:adce77867281 10 *
spatel465 0:adce77867281 11 * The above copyright notice and this permission notice shall be included in
spatel465 0:adce77867281 12 * all copies or substantial portions of the Software.
spatel465 0:adce77867281 13 *
spatel465 0:adce77867281 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
spatel465 0:adce77867281 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
spatel465 0:adce77867281 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
spatel465 0:adce77867281 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
spatel465 0:adce77867281 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
spatel465 0:adce77867281 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
spatel465 0:adce77867281 20 * SOFTWARE.
spatel465 0:adce77867281 21 */
spatel465 0:adce77867281 22 #ifndef MAIL_H
spatel465 0:adce77867281 23 #define MAIL_H
spatel465 0:adce77867281 24
spatel465 0:adce77867281 25 #include <stdint.h>
spatel465 0:adce77867281 26 #include <string.h>
spatel465 0:adce77867281 27
spatel465 0:adce77867281 28 #include "cmsis_os.h"
spatel465 0:adce77867281 29
spatel465 0:adce77867281 30 namespace rtos {
spatel465 0:adce77867281 31 /** \addtogroup rtos */
spatel465 0:adce77867281 32 /** @{*/
spatel465 0:adce77867281 33
spatel465 0:adce77867281 34 /** The Mail class allow to control, send, receive, or wait for mail.
spatel465 0:adce77867281 35 A mail is a memory block that is send to a thread or interrupt service routine.
spatel465 0:adce77867281 36 @tparam T data type of a single message element.
spatel465 0:adce77867281 37 @tparam queue_sz maximum number of messages in queue.
spatel465 0:adce77867281 38 */
spatel465 0:adce77867281 39 template<typename T, uint32_t queue_sz>
spatel465 0:adce77867281 40 class Mail {
spatel465 0:adce77867281 41 public:
spatel465 0:adce77867281 42 /** Create and Initialise Mail queue. */
spatel465 0:adce77867281 43 Mail() {
spatel465 0:adce77867281 44 #ifdef CMSIS_OS_RTX
spatel465 0:adce77867281 45 memset(_mail_q, 0, sizeof(_mail_q));
spatel465 0:adce77867281 46 _mail_p[0] = _mail_q;
spatel465 0:adce77867281 47
spatel465 0:adce77867281 48 memset(_mail_m, 0, sizeof(_mail_m));
spatel465 0:adce77867281 49 _mail_p[1] = _mail_m;
spatel465 0:adce77867281 50
spatel465 0:adce77867281 51 _mail_def.pool = _mail_p;
spatel465 0:adce77867281 52 _mail_def.queue_sz = queue_sz;
spatel465 0:adce77867281 53 _mail_def.item_sz = sizeof(T);
spatel465 0:adce77867281 54 #endif
spatel465 0:adce77867281 55 _mail_id = osMailCreate(&_mail_def, NULL);
spatel465 0:adce77867281 56 }
spatel465 0:adce77867281 57
spatel465 0:adce77867281 58 /** Allocate a memory block of type T
spatel465 0:adce77867281 59 @param millisec timeout value or 0 in case of no time-out. (default: 0).
spatel465 0:adce77867281 60 @return pointer to memory block that can be filled with mail or NULL in case error.
spatel465 0:adce77867281 61 */
spatel465 0:adce77867281 62 T* alloc(uint32_t millisec=0) {
spatel465 0:adce77867281 63 return (T*)osMailAlloc(_mail_id, millisec);
spatel465 0:adce77867281 64 }
spatel465 0:adce77867281 65
spatel465 0:adce77867281 66 /** Allocate a memory block of type T and set memory block to zero.
spatel465 0:adce77867281 67 @param millisec timeout value or 0 in case of no time-out. (default: 0).
spatel465 0:adce77867281 68 @return pointer to memory block that can be filled with mail or NULL in case error.
spatel465 0:adce77867281 69 */
spatel465 0:adce77867281 70 T* calloc(uint32_t millisec=0) {
spatel465 0:adce77867281 71 return (T*)osMailCAlloc(_mail_id, millisec);
spatel465 0:adce77867281 72 }
spatel465 0:adce77867281 73
spatel465 0:adce77867281 74 /** Put a mail in the queue.
spatel465 0:adce77867281 75 @param mptr memory block previously allocated with Mail::alloc or Mail::calloc.
spatel465 0:adce77867281 76 @return status code that indicates the execution status of the function.
spatel465 0:adce77867281 77 */
spatel465 0:adce77867281 78 osStatus put(T *mptr) {
spatel465 0:adce77867281 79 return osMailPut(_mail_id, (void*)mptr);
spatel465 0:adce77867281 80 }
spatel465 0:adce77867281 81
spatel465 0:adce77867281 82 /** Get a mail from a queue.
spatel465 0:adce77867281 83 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
spatel465 0:adce77867281 84 @return event that contains mail information or error code.
spatel465 0:adce77867281 85 */
spatel465 0:adce77867281 86 osEvent get(uint32_t millisec=osWaitForever) {
spatel465 0:adce77867281 87 return osMailGet(_mail_id, millisec);
spatel465 0:adce77867281 88 }
spatel465 0:adce77867281 89
spatel465 0:adce77867281 90 /** Free a memory block from a mail.
spatel465 0:adce77867281 91 @param mptr pointer to the memory block that was obtained with Mail::get.
spatel465 0:adce77867281 92 @return status code that indicates the execution status of the function.
spatel465 0:adce77867281 93 */
spatel465 0:adce77867281 94 osStatus free(T *mptr) {
spatel465 0:adce77867281 95 return osMailFree(_mail_id, (void*)mptr);
spatel465 0:adce77867281 96 }
spatel465 0:adce77867281 97
spatel465 0:adce77867281 98 private:
spatel465 0:adce77867281 99 osMailQId _mail_id;
spatel465 0:adce77867281 100 osMailQDef_t _mail_def;
spatel465 0:adce77867281 101 #ifdef CMSIS_OS_RTX
spatel465 0:adce77867281 102 uint32_t _mail_q[4+(queue_sz)];
spatel465 0:adce77867281 103 uint32_t _mail_m[3+((sizeof(T)+3)/4)*(queue_sz)];
spatel465 0:adce77867281 104 void *_mail_p[2];
spatel465 0:adce77867281 105 #endif
spatel465 0:adce77867281 106 };
spatel465 0:adce77867281 107
spatel465 0:adce77867281 108 }
spatel465 0:adce77867281 109
spatel465 0:adce77867281 110 #endif
spatel465 0:adce77867281 111
spatel465 0:adce77867281 112
spatel465 0:adce77867281 113 /** @}*/