UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
I2cMasterReader.hpp
[詳解]
1//
2// I2c マスター側受信クラス
3//
4// Copyright (c) 2022-2024 udonrobo
5//
6
7//
8// master <--[I2C]-- slave
9// ^^^^^^
10//
11
12#pragma once
13
14#include "I2cBus.hpp"
15
17#include <Udon/Utility/Show.hpp>
18
19namespace Udon
20{
21
24 template <typename Message>
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
60 {
61 if (bus)
62 {
63 return Udon::Deserialize<Message>(buffer);
64 }
65 else
66 {
67 return Udon::nullopt;
68 }
69 }
70
73 void show(char gap = '\t') const
74 {
75 if (const auto message = getMessage())
76 {
77 Udon::Show(*message, gap);
78 }
79 else
80 {
81 Udon::Show(F("receive failed!"));
82 }
83 }
84
87 void showRaw(char gap = ' ') const
88 {
89 Udon::Show(buffer, gap);
90 }
91
92 private:
93 Udon::II2cBus& bus;
94
95 uint8_t address;
96
97 uint8_t buffer[Size];
98 };
99
100} // namespace Udon
#define F(x)
Definition Show.hpp:17
I2c マスター側受信クラス
Definition I2cMasterReader.hpp:26
void showRaw(char gap=' ') const
受信バッファを表示
Definition I2cMasterReader.hpp:87
void update()
更新
Definition I2cMasterReader.hpp:45
Message MessageType
受信メッセージ型
Definition I2cMasterReader.hpp:29
Udon::Optional< Message > getMessage() const
受信したメッセージを取得
Definition I2cMasterReader.hpp:59
static constexpr size_t Size
受信バッファサイズ
Definition I2cMasterReader.hpp:32
I2cMasterReader(Udon::II2cBus &bus, uint8_t address)
コンストラクタ
Definition I2cMasterReader.hpp:37
void show(char gap='\t') const
受信内容を表示
Definition I2cMasterReader.hpp:73
I2cBus クラスのインターフェース
Definition I2cBus.hpp:21
virtual int available()=0
virtual int read()=0
virtual uint8_t requestFrom(uint8_t address, uint8_t quantity)=0
オプショナル型
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
constexpr size_t SerializedSize() noexcept
Tをシリアライズした際のバイト列の要素数を取得する
Definition Serializer.hpp:22
constexpr NulloptT nullopt
無効値
Definition Optional.hpp:52