Skip to content

Instantly share code, notes, and snippets.

@mostley
Created December 11, 2021 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mostley/da966940246e018220a432265f47284f to your computer and use it in GitHub Desktop.
Save mostley/da966940246e018220a432265f47284f to your computer and use it in GitHub Desktop.
Arduino Code for a hardware mute button built with an Adafruit Neokey Trinkey
#include <Adafruit_NeoPixel.h>
#include "Adafruit_FreeTouch.h"
#include "HID-Project.h"
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
Adafruit_FreeTouch qt = Adafruit_FreeTouch(PIN_TOUCH, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
int16_t neo_brightness = 255;
bool last_switch = true;
bool last_touch = true;
bool is_muted = false;
bool is_color_wheel_on = true;
void setup() {
Serial.begin(9600);
strip.begin();
strip.setBrightness(neo_brightness);
strip.show();
if (! qt.begin()) {
Serial.println("Failed to begin qt");
}
pinMode(PIN_SWITCH, INPUT_PULLDOWN);
//Consumer.begin();
Keyboard.begin();
}
void onTouched() {
Serial.println("Touched");
is_color_wheel_on = !is_color_wheel_on;
}
void onTouchReleased() {
Serial.println("Touch Released");
}
void onPressed() {
Serial.println("Pressed");
if (is_muted) {
is_muted = false;
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('u');
delay(100);
Keyboard.releaseAll();
} else {
is_muted = true;
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('m');
delay(100);
Keyboard.releaseAll();
}
}
void onReleased() {
Serial.println("Released");
}
uint8_t j = 0;
void loop() {
// check touch
bool curr_touched = qt.measure() > 500;
if (curr_touched != last_touch) {
if (curr_touched) {
onTouched();
} else {
onTouchReleased();
}
last_touch = curr_touched;
}
// check mechswitch
bool curr_switch = digitalRead(PIN_SWITCH);
if (curr_switch != last_switch) {
if (curr_switch) {
onPressed();
} else {
onReleased();
}
last_switch = curr_switch;
}
if (is_muted) {
strip.setBrightness(neo_brightness);
strip.setPixelColor(0, strip.Color(255, 0, 0));
} else if (is_color_wheel_on) {
strip.setBrightness(neo_brightness);
strip.setPixelColor(0, Wheel(j++));
} else {
strip.setBrightness(0);
}
strip.show();
delay(10);
}
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment