UdonLibrary
1.0.0
機械システム研究部 C++ ライブラリ
Toggle main menu visibility
読み取り中…
検索中…
一致する文字列を見つけられません
CanReader.hpp
[詳解]
1
//
2
// CAN通信 受信クラス
3
//
4
// Copyright (c) 2022 udonrobo
5
//
6
7
#pragma once
8
9
#include "
ICanBus.hpp
"
10
11
#include <
Udon/Serializer/Serializer.hpp
>
12
#include <
Udon/Serializer/SerializerTraits.hpp
>
13
#include <
Udon/Utility/Show.hpp
>
14
#include <
Udon/Utility/Time.hpp
>
15
#include <
Udon/Utility/Printf.hpp
>
16
17
namespace
Udon
18
{
19
22
template
<
typename
Message>
23
class
CanReader
24
{
25
26
static_assert
(
Traits::IsSerializable<Message>::value
,
"Message must be parsable."
);
27
28
public
:
30
using
MessageType
=
Message
;
31
33
static
constexpr
size_t
Size
=
Udon::SerializedSize<Message>
();
34
38
CanReader
(
ICanBus
& bus,
const
uint32_t
id
)
39
: node{ bus.createRx(id,
Size
) }
40
{
41
node->onReceive = [](
void
* p)
42
{
43
auto
self
=
static_cast<
CanReader
*
>
(p);
44
self
->message =
Udon::Deserialize<Message>
({
self
->node->data });
45
};
46
node->param =
this
;
47
}
48
51
CanReader
(
const
CanReader
&) =
delete
;
52
54
CanReader
(
CanReader
&& other)
55
: node{ other.node }
56
{
57
node->param =
this
;
// ここでotherに登録されていたthisポインタを上書きするため、コピーすることができない
58
}
59
62
explicit
operator
bool()
const
63
{
64
return
Millis() - node->transmitMs < 100;
65
}
66
69
Udon::Optional<MessageType>
getMessage
()
const
70
{
71
if
(*
this
)
72
{
73
return
message;
74
}
75
else
76
{
77
return
Udon::nullopt
;
78
}
79
}
80
82
void
show
()
const
83
{
84
Udon::Printf
(
"0x%03x "
, node->id);
85
if
(
const
auto
m =
getMessage
())
86
{
87
Udon::Show
(*m);
88
}
89
else
90
{
91
Udon::Show
(
F
(
"receive failed!"
));
92
}
93
}
94
96
void
showRaw
()
const
97
{
98
Udon::Printf
(
"0x%03x "
, node->id);
99
for
(
const
auto
n : node->data)
100
{
101
Udon::Show
(n);
102
}
103
}
104
105
private
:
106
CanRxNode
* node;
107
108
Udon::Optional<MessageType>
message;
109
};
110
111
}
// namespace Udon
ICanBus.hpp
Printf.hpp
SerializerTraits.hpp
Serializer.hpp
Show.hpp
F
#define F(x)
Definition
Show.hpp:17
Time.hpp
Udon::CanReader::CanReader
CanReader(ICanBus &bus, const uint32_t id)
コンストラクタ
Definition
CanReader.hpp:38
Udon::CanReader::show
void show() const
受信内容を表示
Definition
CanReader.hpp:82
Udon::CanReader::CanReader
CanReader(CanReader &&other)
コピーコンストラクタ
Definition
CanReader.hpp:54
Udon::CanReader::getMessage
Udon::Optional< MessageType > getMessage() const
メッセージ構造体を取得
Definition
CanReader.hpp:69
Udon::CanReader::Size
static constexpr size_t Size
受信バッファサイズ
Definition
CanReader.hpp:33
Udon::CanReader::MessageType
Message MessageType
受信メッセージ型
Definition
CanReader.hpp:30
Udon::CanReader::showRaw
void showRaw() const
受信バッファを表示
Definition
CanReader.hpp:96
Udon::CanReader::CanReader
CanReader(const CanReader &)=delete
コピーコンストラクタ
Udon::ICanBus
CANバス管理クラス インターフェース
Definition
ICanBus.hpp:49
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::Printf
void Printf(const char *format, Args... args)
Definition
Printf.hpp:44
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
Udon::CanRxNode
CAN受信ノード
Definition
ICanBus.hpp:28
Udon::Traits::IsSerializable
T が シリアライズ可能か判定する
Definition
SerializerTraits.hpp:98