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