UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
EncoderPico.hpp
[詳解]
1//
2// Raspberry Pi Pico 用エンコーダークラス
3//
4// Copyright (c) 2022-2024 udonrobo
5//
6
7#pragma once
8
9#ifdef ARDUINO_ARCH_RP2040
10
13
15
16namespace Udon
17{
18
20 class EncoderPico
21 {
22 uint8_t pinA;
23 uint8_t pinB;
24
25 Pio::StateMachine stateMachine; // PIO使用時
26
27 public:
31 EncoderPico(uint8_t pinA, uint8_t pinB)
32 : pinA(pinA)
33 , pinB(pinB)
34 , stateMachine()
35 {
36 }
37
41 bool begin()
42 {
43 if (abs(pinA - pinB) != 1)
44 {
45 return false; // A相とB相のピン番号が隣り合っていない場合はPIOを使用できない
46 }
47
48 // 使用可能なPIOにプログラムをロードする
49 if (const auto sm = Pio::AllocateStateMachine(Pio::Encoder::quadrature_encoder_program))
50 {
51 stateMachine = *sm;
52 }
53 else
54 {
55 return false; // 割り当て失敗
56 }
57
58 // 小さいほうのピン番号を使用する
59 const auto pin = min(pinA, pinB);
60
61 // 計測開始
62 Pio::Encoder::quadrature_encoder_program_init(stateMachine.pio, stateMachine.index, stateMachine.offset, pin, 0);
63
64 return true;
65 }
66
69 int32_t read() const
70 {
71 return Pio::Encoder::quadrature_encoder_get_count(stateMachine.pio, stateMachine.index);
72 }
73
75 void show() const
76 {
77 Serial.print(read());
78 }
79 };
80} // namespace Udon
81
82#endif // defined(ARDUINO_ARCH_RP2040)
Definition Bit.hpp:12