Adding Rotary Trinkey examples.

This commit is contained in:
Kattni Rembor 2021-05-28 15:45:07 -04:00
parent 5c077fd2a6
commit cfb50cc921
9 changed files with 355 additions and 0 deletions

View file

@ -0,0 +1,93 @@
#include <Adafruit_NeoPixel.h>
#include "Adafruit_FreeTouch.h"
#include <RotaryEncoder.h>
// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255)
RotaryEncoder encoder(PIN_ENCODER_A, PIN_ENCODER_B, RotaryEncoder::LatchMode::FOUR3);
// This interrupt will do our encoder reading/checking!
void checkPosition() {
encoder.tick(); // just call tick() to check the state.
}
uint8_t wheel_offset = 99;
int last_rotary = 0;
void setup() {
Serial.begin(115200);
//while (!Serial);
// start neopixels
strip.begin();
strip.setBrightness(neo_brightness);
strip.show(); // Initialize all pixels to 'off'
attachInterrupt(PIN_ENCODER_A, checkPosition, CHANGE);
attachInterrupt(PIN_ENCODER_B, checkPosition, CHANGE);
// set up the encoder switch, which is separate from the encoder
pinMode(PIN_ENCODER_SWITCH, INPUT_PULLDOWN);
}
void loop() {
// read encoder
int curr_rotary = encoder.getPosition();
RotaryEncoder::Direction direction = encoder.getDirection();
if (curr_rotary != last_rotary) {
Serial.print("Encoder value: ");
Serial.print(curr_rotary);
Serial.print(" direction: ");
Serial.print((int)direction);
// behavior differs if switch is pressed
if (!digitalRead(PIN_ENCODER_SWITCH)) {
// update color
if (direction == RotaryEncoder::Direction::CLOCKWISE) {
wheel_offset++;
}
if (direction == RotaryEncoder::Direction::COUNTERCLOCKWISE) {
wheel_offset--;
}
} else {
// update brightness
if (direction == RotaryEncoder::Direction::CLOCKWISE) {
neo_brightness += 10;
}
if (direction == RotaryEncoder::Direction::COUNTERCLOCKWISE) {
neo_brightness -= 10;
}
// ranges between 0 and 255
if (neo_brightness > 255) neo_brightness = 255;
if (neo_brightness < 0) neo_brightness = 0;
}
Serial.print(" wheel color: ");
Serial.print(wheel_offset);
Serial.print(" brightness: ");
Serial.println(neo_brightness);
last_rotary = curr_rotary;
// update pixels!
strip.setBrightness(neo_brightness);
strip.setPixelColor(0, Wheel(wheel_offset));
strip.show();
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
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);
}
}

View file

@ -0,0 +1,77 @@
#include <Adafruit_NeoPixel.h>
#include <RotaryEncoder.h>
#include "HID-Project.h" // https://github.com/NicoHood/HID
// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255)
RotaryEncoder encoder(PIN_ENCODER_A, PIN_ENCODER_B, RotaryEncoder::LatchMode::FOUR3);
// This interrupt will do our encoder reading/checking!
void checkPosition() {
encoder.tick(); // just call tick() to check the state.
}
int last_rotary = 0;
bool last_button = false;
void setup() {
Serial.begin(115200);
//while (!Serial);
delay(100);
Serial.println("Rotary Trinkey Surface Dial");
// start neopixels
strip.begin();
strip.setBrightness(neo_brightness);
strip.show(); // Initialize all pixels to 'off'
attachInterrupt(PIN_ENCODER_A, checkPosition, CHANGE);
attachInterrupt(PIN_ENCODER_B, checkPosition, CHANGE);
// set up the encoder switch, which is separate from the encoder
pinMode(PIN_ENCODER_SWITCH, INPUT_PULLDOWN);
// Sends a clean report to the host. This is important on any Arduino type.
SurfaceDial.begin();
}
void loop() {
// read encoder
int curr_rotary = encoder.getPosition();
RotaryEncoder::Direction direction = encoder.getDirection();
// read switch
bool curr_button = !digitalRead(PIN_ENCODER_SWITCH);
if (direction != RotaryEncoder::Direction::NOROTATION) {
Serial.print("Encoder value: ");
Serial.print(curr_rotary);
Serial.print(" direction: ");
Serial.print((int)direction);
if (direction == RotaryEncoder::Direction::CLOCKWISE) {
Serial.println(" Rotate+");
SurfaceDial.rotate(40);
}
if (direction == RotaryEncoder::Direction::COUNTERCLOCKWISE) {
Serial.println(" Rotate-");
SurfaceDial.rotate(-40);
}
last_rotary = curr_rotary;
}
if (curr_button && !last_button) { // switch pressed!
Serial.println("Press");
SurfaceDial.press();
}
if (!curr_button && last_button) { // switch released!
Serial.println("Release");
SurfaceDial.release();
}
last_button = curr_button;
delay(10); // debounce
}

View file

@ -0,0 +1,73 @@
#include <Adafruit_NeoPixel.h>
#include <RotaryEncoder.h>
#include "HID-Project.h" // https://github.com/NicoHood/HID
// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255)
RotaryEncoder encoder(PIN_ENCODER_A, PIN_ENCODER_B, RotaryEncoder::LatchMode::FOUR3);
// This interrupt will do our encoder reading/checking!
void checkPosition() {
encoder.tick(); // just call tick() to check the state.
}
int last_rotary = 0;
bool last_button = false;
void setup() {
Serial.begin(115200);
//while (!Serial);
delay(100);
Serial.println("Rotary Trinkey Volume Knob");
// start neopixels
strip.begin();
strip.setBrightness(neo_brightness);
strip.show(); // Initialize all pixels to 'off'
attachInterrupt(PIN_ENCODER_A, checkPosition, CHANGE);
attachInterrupt(PIN_ENCODER_B, checkPosition, CHANGE);
// set up the encoder switch, which is separate from the encoder
pinMode(PIN_ENCODER_SWITCH, INPUT_PULLDOWN);
// Sends a clean report to the host. This is important on any Arduino type.
Consumer.begin();
}
void loop() {
// read encoder
int curr_rotary = encoder.getPosition();
RotaryEncoder::Direction direction = encoder.getDirection();
// read switch
bool curr_button = !digitalRead(PIN_ENCODER_SWITCH);
if (direction != RotaryEncoder::Direction::NOROTATION) {
Serial.print("Encoder value: ");
Serial.print(curr_rotary);
Serial.print(" direction: ");
Serial.print((int)direction);
if (direction == RotaryEncoder::Direction::CLOCKWISE) {
Serial.println(" Vol +");
Consumer.write(MEDIA_VOLUME_UP);
}
if (direction == RotaryEncoder::Direction::COUNTERCLOCKWISE) {
Serial.println(" Vol -");
Consumer.write(MEDIA_VOLUME_DOWN);
}
last_rotary = curr_rotary;
}
if (curr_button && !last_button) { // switch pressed!
Serial.println("Play/Pause");
Consumer.write(MEDIA_PLAY_PAUSE);
}
last_button = curr_button;
delay(10); // debounce
}

View file

@ -0,0 +1,35 @@
"""Rotary Trinkey NeoPixel color picker example"""
import rotaryio
import digitalio
import board
import neopixel
import _pixelbuf
print("Rotary Trinkey color picker example")
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.5)
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
switch = digitalio.DigitalInOut(board.SWITCH)
switch.switch_to_input(pull=digitalio.Pull.DOWN)
last_position = -1
color = 0 # start at red
while True:
position = encoder.position
if last_position is None or position != last_position:
print(position)
if not switch.value:
# change the color
if position > last_position: # increase
color += 1
else:
color -= 1
color = (color + 256) % 256 # wrap around to 0-256
pixel.fill(_pixelbuf.colorwheel(color))
else:
# change the brightness
if position > last_position: # increase
pixel.brightness = min(1.0, pixel.brightness + 0.1)
else:
pixel.brightness = max(0, pixel.brightness - 0.1)
last_position = position

View file

@ -0,0 +1,37 @@
"""Rotary Trinkey Volume and Mute HID example"""
import rotaryio
import board
import usb_hid
import digitalio
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
print("Rotary Trinkey volume and mute example")
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
switch = digitalio.DigitalInOut(board.SWITCH)
switch.switch_to_input(pull=digitalio.Pull.DOWN)
cc = ConsumerControl(usb_hid.devices)
switch_state = None
last_position = encoder.position
while True:
current_position = encoder.position
position_change = current_position - last_position
if position_change > 0:
for _ in range(position_change):
cc.send(ConsumerControlCode.VOLUME_INCREMENT)
print(current_position)
elif position_change < 0:
for _ in range(-position_change):
cc.send(ConsumerControlCode.VOLUME_DECREMENT)
print(current_position)
last_position = current_position
if not switch.value and switch_state is None:
switch_state = "pressed"
if switch.value and switch_state == "pressed":
print("switch pressed.")
cc.send(ConsumerControlCode.PLAY_PAUSE)
switch_state = None

View file

@ -0,0 +1,40 @@
"""Rotary Trinkey YouTube Frame-by-Frame Example"""
import time
import rotaryio
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
print("Rotary Trinkey YouTube Frame-by-Frame example")
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
switch = digitalio.DigitalInOut(board.SWITCH)
switch.switch_to_input(pull=digitalio.Pull.DOWN)
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
switch_state = None
last_position = encoder.position
while True:
current_position = encoder.position
position_change = current_position - last_position
if position_change > 0:
for _ in range(position_change):
keyboard_layout.write('.')
print(current_position)
elif position_change < 0:
for _ in range(-position_change):
keyboard_layout.write(',')
print(current_position)
last_position = current_position
if not switch.value and switch_state is None:
switch_state = "pressed"
if switch.value and switch_state == "pressed":
print("switch pressed.")
keyboard_layout.write(' ')
switch_state = None