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