IoAbstraction
Loading...
Searching...
No Matches
StateMachineEncoder.h
Go to the documentation of this file.
1/*
2* Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3 * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4 */
5
15#ifndef IOA_STATEMACHINE_ENCODER_H
16#define IOA_STATEMACHINE_ENCODER_H
17
18#include <TaskPlatformDeps.h>
19#include "PlatformDetermination.h"
20#include "SwitchInput.h"
21
22//
23// DO NOT REMOVE THE ISR_ATTR and DRAM_ATTR ANNOTATIONS
24//
25
26
40
46
63private:
64 pinid_t pinA = 2;
65 pinid_t pinB = 3;
66 pinid_t interruptPin = -1;
67 EncoderCallbackFn callback = nullptr;
68 EncoderListener* listener = nullptr;
70 HWAccelerationMode accelerationMode = HWACCEL_REGULAR;
71 EncoderType encoderType = FULL_CYCLE;
72 bool needPullup = true;
73 uint8_t encoderNum = 0;
74public:
80 StateRotaryEncoderBuilder& withEncoderPins(const pinid_t l_pinA, const pinid_t l_pinB) {
81 pinA = l_pinA;
82 pinB = l_pinB;
83 return *this;
84 }
85
92 StateRotaryEncoderBuilder& withEncoderNum(const uint8_t l_encoderNum) {
93 this->encoderNum = l_encoderNum;
94 return *this;
95 }
96
102 StateRotaryEncoderBuilder& withPullUpInput(const bool l_needPullup) {
103 this->needPullup = l_needPullup;
104 return *this;
105 }
106
112 callback = l_callback;
113 return *this;
114 }
115
121 listener = l_listener;
122 return *this;
123 }
124
131 StateRotaryEncoderBuilder& interruptOnSinglePin(const pinid_t l_interruptPin) {
132 interruptMode = EncoderInterruptMode::SINGLE_PIN;
133 interruptPin = l_interruptPin;
134 return *this;
135 }
136
141 interruptMode = EncoderInterruptMode::ALL_PINS;
142 return *this;
143 }
144
152 interruptMode = EncoderInterruptMode::TIMER;
153 return *this;
154 }
155
161 accelerationMode = l_accelerationMode;
162 return *this;
163 }
164
170 encoderType = l_encoderType;
171 return *this;
172 }
173
179 [[nodiscard]] bool configurationValid() const {
180 if (listener == nullptr && callback == nullptr) {
181 return false; // no notification has been configured
182 }
183
184 if (interruptMode == EncoderInterruptMode::SINGLE_PIN && (interruptPin == pinA || interruptPin == pinB || interruptPin < 0)) {
185 return false; // The single pin must not be A or B, it should be in hardware as A or B.
186 }
187
188 // A and B must not be the same.
189 return (pinA != pinB) && interruptMode != EncoderInterruptMode::NONE;
190 }
191
197 bool build() const; // NOLINT(*-use-nodiscard) because it is not a getter it is optional error handling.
198};
199
209 return builder.build();
210}
211
212#endif
EncoderInterruptMode
Definition StateMachineEncoder.h:30
@ ALL_PINS
Definition StateMachineEncoder.h:34
@ TIMER
Definition StateMachineEncoder.h:36
@ NONE
Definition StateMachineEncoder.h:38
@ SINGLE_PIN
Definition StateMachineEncoder.h:32
ISR_ATTR void encoderInterruptHandler()
bool setupStateMachineRotaryEncoder(const StateRotaryEncoderBuilder &builder)
Definition StateMachineEncoder.h:208
Switch input provides the button and rotary encoder input capabilities provided by this library....
EncoderType
Definition SwitchInput.h:320
@ FULL_CYCLE
Definition SwitchInput.h:326
void(* EncoderCallbackFn)(int newValue)
Definition SwitchInput.h:127
HWAccelerationMode
Definition SwitchInput.h:307
@ HWACCEL_REGULAR
Definition SwitchInput.h:311
Definition SwitchInput.h:197
Definition StateMachineEncoder.h:62
StateRotaryEncoderBuilder & withEncoderNum(const uint8_t l_encoderNum)
Definition StateMachineEncoder.h:92
bool configurationValid() const
Definition StateMachineEncoder.h:179
StateRotaryEncoderBuilder & withCallback(const EncoderCallbackFn l_callback)
Definition StateMachineEncoder.h:111
StateRotaryEncoderBuilder & withAccelerationMode(const HWAccelerationMode l_accelerationMode)
Definition StateMachineEncoder.h:160
StateRotaryEncoderBuilder & withEncoderPins(const pinid_t l_pinA, const pinid_t l_pinB)
Definition StateMachineEncoder.h:80
StateRotaryEncoderBuilder & withPullUpInput(const bool l_needPullup)
Definition StateMachineEncoder.h:102
StateRotaryEncoderBuilder & withListener(EncoderListener *l_listener)
Definition StateMachineEncoder.h:120
StateRotaryEncoderBuilder & interruptOnTimer()
Definition StateMachineEncoder.h:151
StateRotaryEncoderBuilder & interruptOnSinglePin(const pinid_t l_interruptPin)
Definition StateMachineEncoder.h:131
StateRotaryEncoderBuilder & interruptOnBothPins()
Definition StateMachineEncoder.h:140
StateRotaryEncoderBuilder & withEncoderType(const EncoderType l_encoderType)
Definition StateMachineEncoder.h:169