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