Last active
July 14, 2018 12:07
-
-
Save AberrantWolf/ccdfc0df83196e73b36faa7c0cbdc0f0 to your computer and use it in GitHub Desktop.
Code to run my media keys controller project.
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
// Uses Consumer API for media keys: https://github.com/NicoHood/HID/wiki/Consumer-API | |
#include <HID-Project.h> | |
const int mutePin = 5; | |
const int prevPin = 15; | |
const int playPin = 14; | |
const int nextPin = 16; | |
const uint64_t debounce_delay = 5; | |
typedef struct { | |
int a; | |
int b; | |
} Pair; | |
const Pair volume { 2, 4 }; | |
typedef struct { | |
int32_t value; | |
int32_t last_value; | |
uint64_t change_time; | |
bool changed; | |
} DebounceData; | |
void debounce_int32(DebounceData& data, int32_t val) { | |
uint64_t now = millis(); | |
if (val != data.last_value) { | |
data.change_time = now; | |
} | |
if (now - data.change_time > debounce_delay) { | |
if (val != data.value) { | |
data.value = val; | |
data.changed = true; | |
} | |
} | |
data.last_value = val; | |
} | |
void setup() { | |
pinMode(mutePin, INPUT_PULLUP); | |
pinMode(prevPin, INPUT_PULLUP); | |
pinMode(playPin, INPUT_PULLUP); | |
pinMode(nextPin, INPUT_PULLUP); | |
pinMode(volume.a, INPUT_PULLUP); | |
pinMode(volume.b, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(2), doEncoder, CHANGE); | |
Consumer.begin(); | |
} | |
int32_t rotary_val; | |
DebounceData mute_db, rotary_db, prev_db, play_db, next_db; | |
void doEncoder() { | |
cli(); | |
if (digitalRead(volume.a) == digitalRead(volume.b)) { | |
rotary_val = 1; | |
} else { | |
rotary_val = -1; | |
} | |
debounce_int32(rotary_db, rotary_val); | |
sei(); | |
} | |
void loop() { | |
debounce_int32(mute_db, digitalRead(mutePin)); | |
debounce_int32(rotary_db, rotary_val); | |
debounce_int32(prev_db, digitalRead(prevPin)); | |
debounce_int32(play_db, digitalRead(playPin)); | |
debounce_int32(next_db, digitalRead(nextPin)); | |
if (mute_db.changed) { | |
if (mute_db.value == LOW) { | |
Consumer.write(MEDIA_VOLUME_MUTE); | |
} | |
mute_db.changed = false; | |
} | |
if (rotary_db.changed) { | |
if (rotary_db.value == 1) { | |
Consumer.write(MEDIA_VOLUME_UP); | |
} else if (rotary_db.value == -1) { | |
Consumer.write(MEDIA_VOLUME_DOWN); | |
} | |
rotary_db.value = rotary_db.last_value = 0; | |
rotary_db.changed = false; | |
rotary_val = 0; | |
} | |
if (prev_db.changed) { | |
if (prev_db.value == LOW) { | |
Consumer.write(MEDIA_PREVIOUS); | |
} | |
prev_db.changed = false; | |
} | |
if (play_db.changed) { | |
if (play_db.value == LOW) { | |
Consumer.write(MEDIA_PLAY_PAUSE); | |
} | |
play_db.changed = false; | |
} | |
if (next_db.changed) { | |
if (next_db.value == LOW) { | |
Consumer.write(MEDIA_NEXT); | |
} | |
next_db.changed = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment