UdonLibrary
1.0.0
機械システム研究部 C++ ライブラリ
Toggle main menu visibility
読み取り中…
検索中…
一致する文字列を見つけられません
I2cMasterReader.hpp
[詳解]
1
//
2
// I2c マスター側受信クラス
3
//
4
// Copyright (c) 2022 udonrobo
5
//
6
7
//
8
// master <--[I2C]-- slave
9
// ^^^^^^
10
//
11
12
#pragma once
13
14
#include "
I2cBus.hpp
"
15
16
#include <
Udon/Serializer/Serializer.hpp
>
17
#include <
Udon/Utility/Show.hpp
>
18
19
namespace
Udon
20
{
21
24
template
<
typename
Message>
25
class
I2cMasterReader
26
{
27
public
:
29
using
MessageType
=
Message
;
30
32
static
constexpr
size_t
Size
=
Udon::SerializedSize<Message>
();
33
37
I2cMasterReader
(
Udon::II2cBus
& bus, uint8_t address)
38
: bus(bus)
39
, address(address)
40
, buffer()
41
{
42
}
43
45
void
update
()
46
{
47
bus.requestFrom(address,
Size
);
48
while
(bus.available())
49
{
50
for
(
auto
&& it : buffer)
51
{
52
it = bus.read();
53
}
54
}
55
}
56
59
Udon::Optional<Message>
getMessage
()
const
60
{
61
if
(bus)
62
{
63
return
Udon::Deserialize<Message>
(buffer);
64
}
65
else
66
{
67
return
Udon::nullopt
;
68
}
69
}
70
72
void
show
()
const
73
{
74
if
(
const
auto
message =
getMessage
())
75
{
76
Udon::Show
(*message);
77
}
78
else
79
{
80
Udon::Show
(
F
(
"receive failed!"
));
81
}
82
}
83
85
void
showRaw
()
const
86
{
87
Udon::Show
(buffer);
88
}
89
90
private
:
91
Udon::II2cBus
& bus;
92
93
uint8_t address;
94
95
uint8_t buffer[
Size
];
96
};
97
98
}
// namespace Udon
I2cBus.hpp
Serializer.hpp
Show.hpp
F
#define F(x)
Definition
Show.hpp:17
Udon::I2cMasterReader::update
void update()
更新
Definition
I2cMasterReader.hpp:45
Udon::I2cMasterReader::MessageType
Message MessageType
受信メッセージ型
Definition
I2cMasterReader.hpp:29
Udon::I2cMasterReader::getMessage
Udon::Optional< Message > getMessage() const
受信したメッセージを取得
Definition
I2cMasterReader.hpp:59
Udon::I2cMasterReader::Size
static constexpr size_t Size
受信バッファサイズ
Definition
I2cMasterReader.hpp:32
Udon::I2cMasterReader::showRaw
void showRaw() const
受信バッファを表示
Definition
I2cMasterReader.hpp:85
Udon::I2cMasterReader::I2cMasterReader
I2cMasterReader(Udon::II2cBus &bus, uint8_t address)
コンストラクタ
Definition
I2cMasterReader.hpp:37
Udon::I2cMasterReader::show
void show() const
受信内容を表示
Definition
I2cMasterReader.hpp:72
Udon::II2cBus
I2cBus クラスのインターフェース
Definition
I2cBus.hpp:21
Udon::Optional
オプショナル型
Definition
Optional.hpp:62
Udon::Message
Definition
AirCylinder.hpp:16
Udon
Definition
Bit.hpp:12
Udon::Deserialize
Udon::Optional< T > Deserialize(ArrayView< const uint8_t > buffer)
バイト列からオブジェクトに逆シリアル化します
Definition
Serializer.hpp:87
Udon::Show
void Show(Args &&... args)
区切り文字ありで出力する
Definition
Show.hpp:339
Udon::SerializedSize
constexpr size_t SerializedSize() noexcept
Tをシリアライズした際のバイト列の要素数を取得する
Definition
Serializer.hpp:22
Udon::nullopt
constexpr NulloptT nullopt
無効値
Definition
Optional.hpp:52