Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Jikstra committed Feb 25, 2019
1 parent da644cc commit 4956ce0
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 35 deletions.
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
platform = atmelavr
board = megaatmega2560
framework = arduino
monitor_speed = 115200
lib_deps =
MIDI Library
Rotary
30 changes: 15 additions & 15 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ int getChannelFromDeck(bool deck) {
}
}

bool isBouncing(unsigned long* lastFlakeMillis) {
bool isBouncing(unsigned long* last_flake_millis) {
unsigned long current_flake = millis();
//p("current_flake %lu last_flake %lu", current_flake, *lastFlakeMillis);
bool returnValue = current_flake - *lastFlakeMillis < 50;
*lastFlakeMillis = current_flake;
//p("current_flake %lu last_flake %lu", current_flake, *last_flake_millis);
bool returnValue = current_flake - *last_flake_millis < 50;
*last_flake_millis = current_flake;
return returnValue;
}


ButtonState buttonState(int pinValue, bool* wasPressed, unsigned long* lastFlakeMillis) {
if(pinValue == LOW && *wasPressed == false) {
if(isBouncing(lastFlakeMillis)) return;
*wasPressed = true;
ButtonState buttonState(int pin_value, bool* was_pressed, unsigned long* last_flake_millis) {
if(pin_value == LOW && *was_pressed == false) {
if(isBouncing(last_flake_millis)) return;
*was_pressed = true;
return ButtonState::Pressed;
} else if(pinValue == HIGH && *wasPressed == true){
if(isBouncing(lastFlakeMillis)) return;
*wasPressed = false;
} else if(pin_value == HIGH && *was_pressed == true){
if(isBouncing(last_flake_millis)) return;
*was_pressed = false;
return ButtonState::Unpressed;
} else {
return ButtonState::Unchanged;
}
}

char* buttonStateToString(ButtonState buttonState) {
switch(buttonState) {
char* buttonStateToString(ButtonState button_state) {
switch(button_state) {
case ButtonState::Pressed:
return "Pressed";
case ButtonState::Unchanged:
Expand All @@ -57,8 +57,8 @@ char* buttonStateToString(ButtonState buttonState) {
}
}

bool buttonToggle(ButtonState buttonState, bool* toggle) {
if(buttonState == ButtonState::Pressed) {
bool buttonToggle(ButtonState button_state, bool* toggle) {
if(button_state == ButtonState::Pressed) {
*toggle = !*toggle;
return true;
}
Expand Down
29 changes: 14 additions & 15 deletions src/components/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Button::Button(int pin, int control_number, bool deck) :
control_number(control_number),
deck(deck),
last_flake(0),
switch_is_up(true),
switch_was_up(true),
was_pressed(false),
toggle(false),
pin(pin) {}


Expand All @@ -18,30 +18,29 @@ void Button::process() {
_process(state);
}

void Button::_process(int state) {
if(state == LOW && switch_was_up == false) {
if(isBouncing(&last_flake)) return;
switch_is_up = !switch_is_up;
switch_was_up = true;
} else if(state == HIGH && switch_was_up == true){
if(isBouncing(&last_flake)) return;
switch_was_up = false;
return;
} else {
return;
void Button::_process(int pin_value) {
ButtonState button_state = buttonState(pin_value, &was_pressed, &last_flake);

handleButtonState(button_state);
}

void Button::handleButtonState(ButtonState button_state) {
if(buttonToggle(button_state, &toggle)) {
handleButtonToggle(toggle);
}
}

void Button::handleButtonToggle(bool toggle) {
int value_to_send = 1;
int channel = getChannelFromDeck(deck);

IFDEBUG(p(
"Button: %i:%i %s %i",
control_number,
channel,
value_to_send ? "Up" : "Down",
toggle ? "Toggled" : "Untoggled",
value_to_send
));

IFNDEBUG(midiOut.sendNoteOn(control_number, value_to_send, channel));

}
6 changes: 4 additions & 2 deletions src/components/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ class Button {
int control_number;
const bool deck;
unsigned long last_flake;
bool switch_is_up;
bool switch_was_up;
bool was_pressed;
bool toggle;
public:
int pin;

Button(int pin, int control_number, bool deck);
void setup();
void process();
void _process(int state);
virtual void handleButtonState(ButtonState button_state);
virtual void handleButtonToggle(bool toggle);
};

#endif
2 changes: 1 addition & 1 deletion src/components/CountingRotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void CountingRotaryEncoder::handleButtonToggle(bool toggle) {
int value_to_send = toggle;

IFDEBUG(
p("Button: %i:%i %s %i", control_number_mute, channel, toggle ? "Pressed" : "Released", value_to_send)
p("Button: %i:%i %s %i", control_number_mute, channel, toggle ? "Toggled" : "Untoggled", value_to_send)
);

IFNDEBUG(midiOut.sendNoteOn(control_number_mute, value_to_send, channel));
Expand Down
2 changes: 1 addition & 1 deletion src/components/LRRotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void LRRotaryEncoder::handleButtonToggle(bool toggle) {
int control_number = toggle ? control_number_press : control_number_release;

IFDEBUG(
p("Button: %i:%i %s %i", control_number, channel, toggle ? "Pressed" : "Released", value_to_send)
p("Button: %i:%i %s %i", control_number, channel, toggle ? "Toggled" : "Untoggled", value_to_send)
);

IFNDEBUG(midiOut.sendNoteOn(control_number, value_to_send, channel));
Expand Down
2 changes: 1 addition & 1 deletion src/debug.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef DEBUG_H
#define DEBUG_H

#define DEBUG // Uncomment to enable debugging
//#define DEBUG // Uncomment to enable debugging

#ifdef DEBUG
#define IFDEBUG(x) x
Expand Down

0 comments on commit 4956ce0

Please sign in to comment.