Samantha Wang / MbedModules

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
ywang627
Date:
Fri Dec 26 13:38:44 2014 +0000
Revision:
0:6dff7b3b161d
Battleship Game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ywang627 0:6dff7b3b161d 1 /* mbed Microcontroller Library
ywang627 0:6dff7b3b161d 2 * Copyright (c) 2006-2012 ARM Limited
ywang627 0:6dff7b3b161d 3 *
ywang627 0:6dff7b3b161d 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
ywang627 0:6dff7b3b161d 5 * of this software and associated documentation files (the "Software"), to deal
ywang627 0:6dff7b3b161d 6 * in the Software without restriction, including without limitation the rights
ywang627 0:6dff7b3b161d 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ywang627 0:6dff7b3b161d 8 * copies of the Software, and to permit persons to whom the Software is
ywang627 0:6dff7b3b161d 9 * furnished to do so, subject to the following conditions:
ywang627 0:6dff7b3b161d 10 *
ywang627 0:6dff7b3b161d 11 * The above copyright notice and this permission notice shall be included in
ywang627 0:6dff7b3b161d 12 * all copies or substantial portions of the Software.
ywang627 0:6dff7b3b161d 13 *
ywang627 0:6dff7b3b161d 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ywang627 0:6dff7b3b161d 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ywang627 0:6dff7b3b161d 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ywang627 0:6dff7b3b161d 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ywang627 0:6dff7b3b161d 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ywang627 0:6dff7b3b161d 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
ywang627 0:6dff7b3b161d 20 * SOFTWARE.
ywang627 0:6dff7b3b161d 21 */
ywang627 0:6dff7b3b161d 22 #ifndef THREAD_H
ywang627 0:6dff7b3b161d 23 #define THREAD_H
ywang627 0:6dff7b3b161d 24
ywang627 0:6dff7b3b161d 25 #include <stdint.h>
ywang627 0:6dff7b3b161d 26 #include "cmsis_os.h"
ywang627 0:6dff7b3b161d 27
ywang627 0:6dff7b3b161d 28 namespace rtos {
ywang627 0:6dff7b3b161d 29
ywang627 0:6dff7b3b161d 30 /** The Thread class allow defining, creating, and controlling thread functions in the system. */
ywang627 0:6dff7b3b161d 31 class Thread {
ywang627 0:6dff7b3b161d 32 public:
ywang627 0:6dff7b3b161d 33 /** Create a new thread, and start it executing the specified function.
ywang627 0:6dff7b3b161d 34 @param task function to be executed by this thread.
ywang627 0:6dff7b3b161d 35 @param argument pointer that is passed to the thread function as start argument. (default: NULL).
ywang627 0:6dff7b3b161d 36 @param priority initial priority of the thread function. (default: osPriorityNormal).
ywang627 0:6dff7b3b161d 37 @param stack_size stack size (in bytes) requirements for the thread function. (default: DEFAULT_STACK_SIZE).
ywang627 0:6dff7b3b161d 38 @param stack_pointer pointer to the stack area to be used by this thread (default: NULL).
ywang627 0:6dff7b3b161d 39 */
ywang627 0:6dff7b3b161d 40 Thread(void (*task)(void const *argument), void *argument=NULL,
ywang627 0:6dff7b3b161d 41 osPriority priority=osPriorityNormal,
ywang627 0:6dff7b3b161d 42 uint32_t stack_size=DEFAULT_STACK_SIZE,
ywang627 0:6dff7b3b161d 43 unsigned char *stack_pointer=NULL);
ywang627 0:6dff7b3b161d 44
ywang627 0:6dff7b3b161d 45 /** Terminate execution of a thread and remove it from Active Threads
ywang627 0:6dff7b3b161d 46 @return status code that indicates the execution status of the function.
ywang627 0:6dff7b3b161d 47 */
ywang627 0:6dff7b3b161d 48 osStatus terminate();
ywang627 0:6dff7b3b161d 49
ywang627 0:6dff7b3b161d 50 /** Set priority of an active thread
ywang627 0:6dff7b3b161d 51 @param priority new priority value for the thread function.
ywang627 0:6dff7b3b161d 52 @return status code that indicates the execution status of the function.
ywang627 0:6dff7b3b161d 53 */
ywang627 0:6dff7b3b161d 54 osStatus set_priority(osPriority priority);
ywang627 0:6dff7b3b161d 55
ywang627 0:6dff7b3b161d 56 /** Get priority of an active thread
ywang627 0:6dff7b3b161d 57 @return current priority value of the thread function.
ywang627 0:6dff7b3b161d 58 */
ywang627 0:6dff7b3b161d 59 osPriority get_priority();
ywang627 0:6dff7b3b161d 60
ywang627 0:6dff7b3b161d 61 /** Set the specified Signal Flags of an active thread.
ywang627 0:6dff7b3b161d 62 @param signals specifies the signal flags of the thread that should be set.
ywang627 0:6dff7b3b161d 63 @return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
ywang627 0:6dff7b3b161d 64 */
ywang627 0:6dff7b3b161d 65 int32_t signal_set(int32_t signals);
ywang627 0:6dff7b3b161d 66
ywang627 0:6dff7b3b161d 67 /** State of the Thread */
ywang627 0:6dff7b3b161d 68 enum State {
ywang627 0:6dff7b3b161d 69 Inactive, /**< Not created or terminated */
ywang627 0:6dff7b3b161d 70 Ready, /**< Ready to run */
ywang627 0:6dff7b3b161d 71 Running, /**< Running */
ywang627 0:6dff7b3b161d 72 WaitingDelay, /**< Waiting for a delay to occur */
ywang627 0:6dff7b3b161d 73 WaitingInterval, /**< Waiting for an interval to occur */
ywang627 0:6dff7b3b161d 74 WaitingOr, /**< Waiting for one event in a set to occur */
ywang627 0:6dff7b3b161d 75 WaitingAnd, /**< Waiting for multiple events in a set to occur */
ywang627 0:6dff7b3b161d 76 WaitingSemaphore, /**< Waiting for a semaphore event to occur */
ywang627 0:6dff7b3b161d 77 WaitingMailbox, /**< Waiting for a mailbox event to occur */
ywang627 0:6dff7b3b161d 78 WaitingMutex, /**< Waiting for a mutex event to occur */
ywang627 0:6dff7b3b161d 79 };
ywang627 0:6dff7b3b161d 80
ywang627 0:6dff7b3b161d 81 /** State of this Thread
ywang627 0:6dff7b3b161d 82 @return the State of this Thread
ywang627 0:6dff7b3b161d 83 */
ywang627 0:6dff7b3b161d 84 State get_state();
ywang627 0:6dff7b3b161d 85
ywang627 0:6dff7b3b161d 86 /** Wait for one or more Signal Flags to become signaled for the current RUNNING thread.
ywang627 0:6dff7b3b161d 87 @param signals wait until all specified signal flags set or 0 for any single signal flag.
ywang627 0:6dff7b3b161d 88 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
ywang627 0:6dff7b3b161d 89 @return event flag information or error code.
ywang627 0:6dff7b3b161d 90 */
ywang627 0:6dff7b3b161d 91 static osEvent signal_wait(int32_t signals, uint32_t millisec=osWaitForever);
ywang627 0:6dff7b3b161d 92
ywang627 0:6dff7b3b161d 93 /** Wait for a specified time period in millisec:
ywang627 0:6dff7b3b161d 94 @param millisec time delay value
ywang627 0:6dff7b3b161d 95 @return status code that indicates the execution status of the function.
ywang627 0:6dff7b3b161d 96 */
ywang627 0:6dff7b3b161d 97 static osStatus wait(uint32_t millisec);
ywang627 0:6dff7b3b161d 98
ywang627 0:6dff7b3b161d 99 /** Pass control to next thread that is in state READY.
ywang627 0:6dff7b3b161d 100 @return status code that indicates the execution status of the function.
ywang627 0:6dff7b3b161d 101 */
ywang627 0:6dff7b3b161d 102 static osStatus yield();
ywang627 0:6dff7b3b161d 103
ywang627 0:6dff7b3b161d 104 /** Get the thread id of the current running thread.
ywang627 0:6dff7b3b161d 105 @return thread ID for reference by other functions or NULL in case of error.
ywang627 0:6dff7b3b161d 106 */
ywang627 0:6dff7b3b161d 107 static osThreadId gettid();
ywang627 0:6dff7b3b161d 108
ywang627 0:6dff7b3b161d 109 virtual ~Thread();
ywang627 0:6dff7b3b161d 110
ywang627 0:6dff7b3b161d 111 private:
ywang627 0:6dff7b3b161d 112 osThreadId _tid;
ywang627 0:6dff7b3b161d 113 osThreadDef_t _thread_def;
ywang627 0:6dff7b3b161d 114 bool _dynamic_stack;
ywang627 0:6dff7b3b161d 115 };
ywang627 0:6dff7b3b161d 116
ywang627 0:6dff7b3b161d 117 }
ywang627 0:6dff7b3b161d 118 #endif