CheeseBoard
A library for use with the CheeseBoard Cheddar platform
CbRotaryInput.cpp
1 #include "CbRotaryInput.h"
2 #include "MutilaDebug.h"
3 #include "CheeseboardConfig.h"
4 
5 CbRotaryInputClass CbRotaryInput(ROT_PUSH_PIN, ROT_A_PIN, ROT_B_PIN);
6 
7 CbRotaryInputClass::CbRotaryInputClass(uint8_t buttonPin, uint8_t aPin, uint8_t bPin) :
8 #ifdef BOARD_V0
9  _button(buttonPin, false),
10 #else
11  _button(buttonPin),
12 #endif
13  _encoder(aPin, bPin),
14  _buttonCb(NULL),
15  _rotatyCb(NULL)
16 {
17 }
18 
20 {
21  _button.begin();
22  _position = _encoder.read()/4;
23  _buttonCb = buttonCb;
24  _rotatyCb = rotaryCb;
25 }
26 
28 {
29  // Update the button and handle presses
30  _button.update();
31  uint16_t tapDuration = _button.tapped();
32  if (tapDuration > 0) {
33  // DBLN(F("CbRotaryInputClass::update button press"));
34  if (_buttonCb != NULL) {
35  _buttonCb(tapDuration);
36  }
37  }
38 
39  int32_t newPosition = _encoder.read()/4;
40  if (newPosition != _position) {
41  int8_t diff = newPosition - _position;
42  if (_rotatyCb != NULL) {
43  _rotatyCb(diff, newPosition);
44  }
45  _position = newPosition;
46  }
47 }
48 
50 {
51  return _position;
52 }
53 
55 {
56  return _button.on();
57 }
58 
void(* t_rotaryCb)(int8_t, int32_t)
Definition: CbRotaryInput.h:34
void(* t_buttonCb)(uint16_t)
Definition: CbRotaryInput.h:25
int32_t getEncoderPosition()
CbRotaryInputClass(uint8_t buttonPin, uint8_t aPin, uint8_t bPin)
void begin(t_buttonCb buttonCb=NULL, t_rotaryCb rotaryCb=NULL)