OSDN Git Service

update libMiMic
[mimic/MiMicSDK.git] / lib / src / os / mbedrtos / NyLPC_cStopwatch.cpp
1 /*********************************************************************************
2  * PROJECT: MiMic
3  * --------------------------------------------------------------------------------
4  *
5  * This file is part of MiMic
6  * Copyright (C)2011 Ryo Iizuka
7  *
8  * MiMic is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * For further information please contact.
22  *  http://nyatla.jp/
23  *  <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
24  *
25  *********************************************************************************/
26 #include "../NyLPC_cStopwatch.h"
27 #if NyLPC_ARCH==NyLPC_ARCH_MBEDRTOS
28 #include "mbed.h"
29 #include "rtos.h"
30
31 static Timer _timer;
32 static NyLPC_TBool _is_start=NyLPC_TBool_FALSE;
33 /**
34  * 現在のtickCountを返します。
35  */
36 NyLPC_TUInt32 NyLPC_cStopwatch_now(void)
37 {
38     return (NyLPC_TUInt32)(_timer. read_ms());
39 }
40
41 /**
42  * インスタンスを生成します。
43  */
44 void NyLPC_cStopwatch_initialize(NyLPC_TcStopwatch_t* i_inst)
45 {
46     if(!_is_start){
47         _timer.start();
48         _is_start=NyLPC_TBool_TRUE;
49     }
50     return;
51 }
52 /**
53  * 基準時刻をセットします。
54  * 値は、システム依存のtick値です。
55  */
56 void NyLPC_cStopwatch_set(NyLPC_TcStopwatch_t* i_inst,NyLPC_TUInt32 i_initial)
57 {
58     i_inst->_tick=i_initial;
59     return;
60 }
61
62 /**
63  * 基準時刻に、現在時刻をセットします。
64  */
65 void NyLPC_cStopwatch_setNow(NyLPC_TcStopwatch_t* i_inst)
66 {
67     i_inst->_tick=NyLPC_cStopwatch_now();
68 }
69 /**
70  * タイムアウト計測を開始します。
71  * この関数は、基準時刻に現在の時刻をセットします。
72  */
73 void NyLPC_cStopwatch_startExpire(NyLPC_TcStopwatch_t* i_inst,NyLPC_TUInt32 i_timeout)
74 {
75     NyLPC_cStopwatch_setNow(i_inst);
76     i_inst->_ex_timeout=i_timeout;
77 }
78
79 /**
80  * NyLPC_cStopwatch_startExpireで設定した時間を経過したかを返します。
81  */
82 NyLPC_TBool NyLPC_cStopwatch_isExpired(NyLPC_TcStopwatch_t* i_inst)
83 {
84     //経過時間の判定
85     if(NyLPC_cStopwatch_elapseInMsec(i_inst)>i_inst->_ex_timeout){
86         i_inst->_ex_timeout=0;
87     }
88     return (i_inst->_ex_timeout==0);
89 }
90
91 /**
92  * 基準時刻と現在時刻の差を計算して、経過時間をミリ秒で返します。
93  */
94 NyLPC_TUInt32 NyLPC_cStopwatch_elapseInMsec(const NyLPC_TcStopwatch_t* i_inst)
95 {
96     return (NyLPC_cStopwatch_now()-i_inst->_tick);
97 }
98
99 #endif