Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
aziz111
Date:
Fri Mar 08 17:15:02 2019 +0000
Revision:
5:569a4894abc1
Parent:
3:78f223d34f36
Final

Who changed what in which revision?

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