-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77: Issue 072: Support Rotary Encoder KY-040 for …
…setting the volume
- Loading branch information
Showing
6 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "rotary_encoder.hpp" | ||
|
||
#include "constants.hpp" | ||
|
||
#ifdef ROTARY_ENCODER | ||
#include "logger.hpp" | ||
|
||
#ifndef ALLinONE_Plus | ||
static_assert(false, "The rotary encoder only works with AiOplus"); | ||
#endif | ||
|
||
volatile int8_t RotaryEncoder::pos = 0; | ||
|
||
void RotaryEncoder::changed() { | ||
const uint8_t dt = digitalRead(rotaryEncoderDtPin); | ||
if (dt == 0) | ||
--pos; | ||
else | ||
++pos; | ||
LOG(button_log, s_debug, F("dt: "), dt, F(" pos: "), pos); | ||
} | ||
|
||
RotaryEncoder::RotaryEncoder(const Settings& settings) | ||
: CommandSource() | ||
, settings(settings) | ||
{ | ||
pinMode(rotaryEncoderClkPin, INPUT_PULLUP); | ||
pinMode(rotaryEncoderDtPin , INPUT_PULLUP); | ||
|
||
attachInterrupt(digitalPinToInterrupt(rotaryEncoderClkPin), RotaryEncoder::changed, FALLING); | ||
} | ||
|
||
commandRaw RotaryEncoder::getCommandRaw() { | ||
commandRaw ret = commandRaw::none; | ||
|
||
const int8_t new_pos = pos; | ||
|
||
if (new_pos == old_pos) | ||
return ret; | ||
|
||
bool cw = (new_pos > old_pos); | ||
if (abs(new_pos - old_pos) > 200) // overflow | ||
cw = !cw; | ||
|
||
if (cw) { | ||
#ifdef FIVEBUTTONS | ||
ret = commandRaw::four; | ||
|
||
#else | ||
ret = settings.invertVolumeButtons? commandRaw::up : commandRaw::upLong; | ||
#endif | ||
} | ||
else { | ||
#ifdef FIVEBUTTONS | ||
ret = commandRaw::five; | ||
#else | ||
ret = settings.invertVolumeButtons? commandRaw::down : commandRaw::downLong; | ||
#endif | ||
} | ||
old_pos = new_pos; | ||
|
||
return ret; | ||
} | ||
|
||
#endif // ROTARY_ENCODER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef SRC_ROTARY_ENCODER_HPP_ | ||
#define SRC_ROTARY_ENCODER_HPP_ | ||
|
||
#include <Arduino.h> | ||
|
||
#include "commands.hpp" | ||
|
||
class RotaryEncoder: public CommandSource { | ||
public: | ||
|
||
RotaryEncoder(const Settings& settings); | ||
virtual ~RotaryEncoder() {} | ||
commandRaw getCommandRaw() override; | ||
|
||
static void changed(); | ||
private: | ||
const Settings& settings; | ||
volatile static int8_t pos; | ||
int8_t old_pos = 0; | ||
}; | ||
|
||
#endif /* SRC_ROTARY_ENCODER_HPP_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters