18#include "TaskManager.h"
19#include <SimpleCollections.h>
25#define HOLD_THRESHOLD 20
29#ifndef SWITCHES_ACCELERATION_DIVISOR
30#define SWITCHES_ACCELERATION_DIVISOR 2
39#define MAX_KEYS DEFAULT_LIST_SIZE
46#ifndef MAX_ROTARY_ENCODERS
47#define MAX_ROTARY_ENCODERS 4
53#ifndef SWITCH_POLL_INTERVAL
54#define SWITCH_POLL_INTERVAL 20
63#ifndef REJECT_DIRECTION_CHANGE_THRESHOLD
64#define REJECT_DIRECTION_CHANGE_THRESHOLD 10000
72enum KeyPressState : uint8_t {
80#define KEY_PRESS_STATE_MASK 0x0f
81#define KEY_LISTENER_MODE_BIT 7
82#define KEY_LOGIC_IS_INVERTED 6
128 KeyPressState previousState;
131 uint8_t acceleration;
132 uint8_t repeatInterval;
141 KeyboardItem(pinid_t pin,
SwitchListener* switchListener, uint8_t repeatInterval = NO_REPEAT,
bool keyLogicIsInverted =
false);
144 void checkAndTrigger(uint8_t pin);
147 bool isDebouncing()
const {
return getState() == DEBOUNCING1 || getState() == DEBOUNCING2; }
148 bool isPressed()
const {
return getState() == PRESSED || getState() == BUTTON_HELD; }
149 bool isHeld()
const {
return getState() == BUTTON_HELD; }
150 pinid_t getPin()
const {
return pin; }
151 pinid_t getKey()
const {
return pin; }
153 void trigger(
bool held);
154 void triggerRelease(
bool held);
156 KeyPressState getState()
const {
return (KeyPressState)(stateFlags & KEY_PRESS_STATE_MASK); }
157 void setState(KeyPressState state) {
158 stateFlags &= ~KEY_PRESS_STATE_MASK;
159 stateFlags |= (state & KEY_PRESS_STATE_MASK);
161 bool isUsingListener() {
return bitRead(stateFlags, KEY_LISTENER_MODE_BIT); }
162 bool isLogicInverted() {
return bitRead(stateFlags, KEY_LOGIC_IS_INVERTED); }
205 enum EncoderFlagBits { LAST_SYNC_STATUS=0, WRAP_AROUND_MODE, OO_LISTENER_CALLBACK, LAST_ENCODER_DIRECTION_UP };
206 uint16_t maximumValue;
207 uint16_t currentReading;
227 void changePrecision(uint16_t maxValue,
int currentValue,
bool rolloverOnMax =
false,
int step = 1);
280 void runCallback(
int newVal) {
281 if(bitRead(flags, OO_LISTENER_CALLBACK)) {
282 notify.encoderListener->encoderHasChanged(newVal);
284 notify.callback(newVal);
322 unsigned long lastChange;
346 int amountFromChange(
unsigned long change);
347 void handleChangeRaw(
bool increase);
360 uint8_t pulseCounter = 0;
395 int8_t currentEncoderState = 0;
420 int8_t stateFor(uint8_t bits);
430 const pinid_t upPin, downPin, backPin, nextPin;
432 const bool canRotate;
488 void onPressed(pinid_t pin,
bool held)
override;
489 void onReleased(pinid_t pin,
bool held)
override;
498#define SW_FLAG_PULLUP_LOGIC 0
499#define SW_FLAG_INTERRUPT_DRIVEN 1
500#define SW_FLAG_INTERRUPT_DEBOUNCE 2
501#define SW_FLAG_ENCODER_IS_POLLING 3
532 BtreeList<pinid_t, KeyboardItem> keys;
533 volatile uint8_t swFlags;
587 bool addSwitch(pinid_t pin,
KeyCallbackFn callback, uint8_t repeat = NO_REPEAT,
bool invertLogic =
false);
672 void changeEncoderPrecision(uint8_t slot, uint16_t precision, uint16_t currentValue,
bool rollover,
int step=1);
696 bool pullUp = bitRead(swFlags, SW_FLAG_PULLUP_LOGIC);
698 return (pullUp && !invertedLogic) || (!pullUp && invertedLogic);
704 bool isInterruptDriven() {
return bitRead(swFlags, SW_FLAG_INTERRUPT_DRIVEN);}
740 return keys.removeByKey(pin);
744 bool internalAddSwitch(pinid_t pin,
bool invertLogic);
746 friend void onSwitchesInterrupt(pinid_t);
Using basic IoFacilities allows one to abstract away the use of IoExpanders, such that the switching ...
Definition SwitchInput.h:320
void setEncoderType(EncoderType et)
Definition SwitchInput.h:342
void setAccelerationMode(HWAccelerationMode mode)
Definition SwitchInput.h:336
Definition BasicIoAbstraction.h:38
Definition SwitchInput.h:188
virtual void encoderHasChanged(int newValue)=0
Definition SwitchInput.h:357
void encoderChanged() override
Definition SwitchInput.cpp:429
Definition SwitchInput.h:393
void encoderChanged() override
Definition SwitchInput.cpp:628
Definition SwitchInput.h:125
Definition SwitchInput.h:203
bool didLastSyncSucceed()
Definition SwitchInput.h:268
int getCurrentReading() const
Definition SwitchInput.h:244
virtual void encoderChanged()
Definition SwitchInput.h:261
void replaceCallbackListener(EncoderListener *callbackFn)
Definition SwitchInput.cpp:301
void setUserIntention(EncoderUserIntention intention)
Definition SwitchInput.cpp:306
void replaceCallback(EncoderCallbackFn callbackFn)
Definition SwitchInput.cpp:296
void increment(int8_t incVal)
Definition SwitchInput.cpp:317
void setCurrentReading(int reading)
Definition SwitchInput.h:250
bsize_t getMaximumValue()
Definition SwitchInput.h:291
void changePrecision(uint16_t maxValue, int currentValue, bool rolloverOnMax=false, int step=1)
Definition SwitchInput.cpp:287
Definition SwitchInput.h:91
virtual void onPressed(pinid_t pin, bool held)=0
virtual void onReleased(pinid_t pin, bool held)=0