UdonLibrary
1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
LoopCycleController.hpp
[詳解]
1
//
2
// ループ周期制御クラス
3
//
4
// Copyright (c) 2016-2023 谷川 豊章
5
// Copyright (c) 2016-2023 udonrobo
6
//
7
8
#ifndef DEF_LoopCycleController_H
9
#define DEF_LoopCycleController_H
10
11
#ifdef ARDUINO
12
13
# include <Arduino.h>
14
15
namespace
Udon
16
{
17
19
class
LoopCycleController
20
{
21
const
unsigned
long
CYCLE_US;
22
unsigned
long
lastTime;
23
unsigned
long
Time;
24
25
bool
isError;
26
27
public
:
30
LoopCycleController(
unsigned
long
cycle_us) noexcept
31
: CYCLE_US(cycle_us)
32
, lastTime()
33
, isError()
34
{
35
}
36
39
void
update() noexcept
40
{
41
if
(micros() - lastTime > CYCLE_US)
42
{
// 指定されたループ周期より処理に時間がかかっている
43
44
if
(lastTime != 0)
45
{
// 初回のループでなければエラーフラグを立てる
46
isError =
true
;
47
}
48
lastTime = micros();
49
}
50
else
51
{
52
53
// 一定周期になるように待つ
54
isError =
false
;
55
while
(micros() - lastTime < CYCLE_US)
56
;
57
lastTime += CYCLE_US;
58
}
59
}
60
63
unsigned
long
cycleUs() const noexcept
64
{
65
return
CYCLE_US;
66
}
67
71
explicit
operator
bool() const noexcept
72
{
73
return
not isError;
74
}
75
78
long
getCycle() noexcept
79
{
80
unsigned
long
Time_ = micros() - Time;
81
Time = micros();
82
return
Time_;
83
}
84
};
85
86
}
// namespace Udon
87
88
#endif
// ARDUINO
89
#endif
// DEF_LoopCycleController_H
Udon
Definition
Bit.hpp:12