UdonLibrary
1.0.0
機械システム研究部 C++ ライブラリ
Toggle main menu visibility
読み取り中…
検索中…
一致する文字列を見つけられません
I2cSlaveReader.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
I2cSlaveReader
26
{
27
public
:
29
using
MessageType
=
Message
;
30
32
static
constexpr
size_t
Size
=
Udon::SerializedSize<MessageType>
();
33
36
I2cSlaveReader
(
Udon::II2cBus
& bus)
37
: bus(bus)
38
, buffer()
39
{
40
self =
this
;
41
}
42
45
I2cSlaveReader
(
const
I2cSlaveReader
&) =
delete
;
46
47
49
I2cSlaveReader
(
I2cSlaveReader
&& other)
50
: bus(other.bus)
51
, buffer()
52
{
53
self =
this
;
54
}
55
57
void
begin
()
58
{
59
bus.onReceive(
60
[](
int
)
61
{
62
for
(
auto
& buf : self->buffer)
63
{
64
buf = Wire.read();
65
}
66
});
67
}
68
71
Udon::Optional<MessageType>
getMessage
()
const
72
{
73
if
(bus)
74
{
75
return
Udon::Deserialize<MessageType>
(buffer);
76
}
77
else
78
{
79
return
Udon::nullopt
;
80
}
81
}
82
84
void
show
()
const
85
{
86
if
(
const
auto
message =
getMessage
())
87
{
88
Udon::Show
(*message);
89
}
90
else
91
{
92
Udon::Show
(
F
(
"receive failed!"
));
93
}
94
}
95
97
void
showRaw
()
const
98
{
99
Udon::Show
(buffer);
100
}
101
102
private
:
103
Udon::II2cBus
& bus;
104
105
uint8_t buffer[
Size
];
106
107
static
I2cSlaveReader
*
self
;
108
};
109
110
template
<
typename
Message>
111
I2cSlaveReader<Message>
* I2cSlaveReader<Message>::self;
112
113
}
// namespace Udon
I2cBus.hpp
Serializer.hpp
Show.hpp
F
#define F(x)
Definition
Show.hpp:17
Udon::I2cSlaveReader
I2c スレーブ側受信クラス
Definition
I2cSlaveReader.hpp:26
Udon::I2cSlaveReader::show
void show() const
受信内容を表示
Definition
I2cSlaveReader.hpp:84
Udon::I2cSlaveReader::showRaw
void showRaw() const
受信バッファを表示
Definition
I2cSlaveReader.hpp:97
Udon::I2cSlaveReader::MessageType
Message MessageType
受信メッセージ型
Definition
I2cSlaveReader.hpp:29
Udon::I2cSlaveReader::Size
static constexpr size_t Size
受信バッファサイズ
Definition
I2cSlaveReader.hpp:32
Udon::I2cSlaveReader::begin
void begin()
受信開始
Definition
I2cSlaveReader.hpp:57
Udon::I2cSlaveReader::I2cSlaveReader
I2cSlaveReader(const I2cSlaveReader &)=delete
コピーコンストラクタ
Udon::I2cSlaveReader::I2cSlaveReader
I2cSlaveReader(I2cSlaveReader &&other)
ムーブコンストラクタ
Definition
I2cSlaveReader.hpp:49
Udon::I2cSlaveReader::getMessage
Udon::Optional< MessageType > getMessage() const
受信したメッセージを取得
Definition
I2cSlaveReader.hpp:71
Udon::I2cSlaveReader::I2cSlaveReader
I2cSlaveReader(Udon::II2cBus &bus)
コンストラクタ
Definition
I2cSlaveReader.hpp:36
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::self
CanBusTeensy< Bus > * CanBusTeensy< Bus >::self
Definition
CanBusTeensy.hpp:275
Udon::nullopt
constexpr NulloptT nullopt
無効値
Definition
Optional.hpp:52