Skip to content

Commit

Permalink
Merge pull request #77: Issue 072: Support Rotary Encoder KY-040 for …
Browse files Browse the repository at this point in the history
…setting the volume
  • Loading branch information
boerge1 authored Apr 7, 2023
2 parents d5d70c8 + a41c095 commit 69c95ee
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Die SD Karte (Ordner mp3 und advert) hat sich gegenüber der Version 3.1.2 geän

# Change Log

## Version 3.1.3 (06.04.2023)
- [Issue 072](https://github.com/tonuino/TonUINO-TNG/issues/72): Support Rotary Encoder KY-040 for setting the volume

## Version 3.1.3 (01.04.2023)
- Viele Fehlerkorrekturen und Verbesserungen
- [Issue 019](https://github.com/tonuino/TonUINO-TNG/issues/19): Implement support for the 3x3 Button board
Expand Down
2 changes: 1 addition & 1 deletion TonUINO-TNG.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void setup()
LOG(init_log, s_error, F("TonUINO Version 3.1 - refactored by Boerge1\n"));
LOG(init_log, s_error, F("created by Thorsten Voß and licensed under GNU/GPL."));
LOG(init_log, s_error, F("Information and contribution at https://tonuino.de.\n"));
LOG(init_log, s_error, F("V3.1.3 01.04.23\n"));
LOG(init_log, s_error, F("V3.1.3 06.04.23\n"));

Tonuino::getTonuino().setup();
}
Expand Down
6 changes: 6 additions & 0 deletions src/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
// um ein Shutdown via Taste (long press Play/Pause) zu unterdrücken bitte in der nächste Zeile den Kommentar entfernen
//#define DISABLE_SHUTDOWN_VIA_BUTTON

// uncomment the below line to enable the rotary encoder for volume setting (only for AiOplus)
// um den Drehgeber zu unterstützen bitte in der nächste Zeile den Kommentar entfernen (nur für AioPlus)
//#define ROTARY_ENCODER
inline constexpr uint8_t rotaryEncoderClkPin = 31;
inline constexpr uint8_t rotaryEncoderDtPin = 32;

// ####### helper for level ############################

enum class level : uint8_t {
Expand Down
65 changes: 65 additions & 0 deletions src/rotary_encoder.cpp
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
22 changes: 22 additions & 0 deletions src/rotary_encoder.hpp
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_ */
7 changes: 7 additions & 0 deletions src/tonuino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "commands.hpp"
#include "buttons.hpp"
#include "buttons3x3.hpp"
#include "rotary_encoder.hpp"
#include "serial_input.hpp"
#include "mp3.hpp"
#include "modifier.hpp"
Expand Down Expand Up @@ -57,6 +58,9 @@ class Tonuino {
#endif
#ifdef BUTTONS3X3
Buttons3x3 buttons3x3 {};
#endif
#ifdef ROTARY_ENCODER
RotaryEncoder rotaryEncoder {settings};
#endif
Commands commands {
settings
Expand All @@ -66,6 +70,9 @@ class Tonuino {
#endif
#ifdef BUTTONS3X3
, &buttons3x3
#endif
#ifdef ROTARY_ENCODER
, &rotaryEncoder
#endif
};
Chip_card chip_card {mp3};
Expand Down

0 comments on commit 69c95ee

Please sign in to comment.