Skip to content

Commit 67c2353

Browse files
authored
Merge pull request #1561 from kattni/rotary-trinkey
Adding Rotary Trinkey examples.
2 parents a2ec738 + cfb50cc commit 67c2353

9 files changed

Lines changed: 355 additions & 0 deletions

File tree

Rotary_Trinkey/Arduino_NeoPixel_Example/.rotarytrinkey_m0.test.only

Whitespace-only changes.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <Adafruit_NeoPixel.h>
2+
#include "Adafruit_FreeTouch.h"
3+
#include <RotaryEncoder.h>
4+
5+
// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL
6+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
7+
int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255)
8+
9+
RotaryEncoder encoder(PIN_ENCODER_A, PIN_ENCODER_B, RotaryEncoder::LatchMode::FOUR3);
10+
// This interrupt will do our encoder reading/checking!
11+
void checkPosition() {
12+
encoder.tick(); // just call tick() to check the state.
13+
}
14+
15+
uint8_t wheel_offset = 99;
16+
int last_rotary = 0;
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
//while (!Serial);
21+
22+
// start neopixels
23+
strip.begin();
24+
strip.setBrightness(neo_brightness);
25+
strip.show(); // Initialize all pixels to 'off'
26+
27+
attachInterrupt(PIN_ENCODER_A, checkPosition, CHANGE);
28+
attachInterrupt(PIN_ENCODER_B, checkPosition, CHANGE);
29+
30+
// set up the encoder switch, which is separate from the encoder
31+
pinMode(PIN_ENCODER_SWITCH, INPUT_PULLDOWN);
32+
}
33+
34+
35+
void loop() {
36+
// read encoder
37+
int curr_rotary = encoder.getPosition();
38+
RotaryEncoder::Direction direction = encoder.getDirection();
39+
40+
if (curr_rotary != last_rotary) {
41+
Serial.print("Encoder value: ");
42+
Serial.print(curr_rotary);
43+
Serial.print(" direction: ");
44+
Serial.print((int)direction);
45+
46+
// behavior differs if switch is pressed
47+
if (!digitalRead(PIN_ENCODER_SWITCH)) {
48+
// update color
49+
if (direction == RotaryEncoder::Direction::CLOCKWISE) {
50+
wheel_offset++;
51+
}
52+
if (direction == RotaryEncoder::Direction::COUNTERCLOCKWISE) {
53+
wheel_offset--;
54+
}
55+
} else {
56+
// update brightness
57+
if (direction == RotaryEncoder::Direction::CLOCKWISE) {
58+
neo_brightness += 10;
59+
}
60+
if (direction == RotaryEncoder::Direction::COUNTERCLOCKWISE) {
61+
neo_brightness -= 10;
62+
}
63+
// ranges between 0 and 255
64+
if (neo_brightness > 255) neo_brightness = 255;
65+
if (neo_brightness < 0) neo_brightness = 0;
66+
}
67+
Serial.print(" wheel color: ");
68+
Serial.print(wheel_offset);
69+
Serial.print(" brightness: ");
70+
Serial.println(neo_brightness);
71+
72+
last_rotary = curr_rotary;
73+
74+
// update pixels!
75+
strip.setBrightness(neo_brightness);
76+
strip.setPixelColor(0, Wheel(wheel_offset));
77+
strip.show();
78+
}
79+
}
80+
81+
// Input a value 0 to 255 to get a color value.
82+
// The colours are a transition r - g - b - back to r.
83+
uint32_t Wheel(byte WheelPos) {
84+
if(WheelPos < 85) {
85+
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
86+
} else if(WheelPos < 170) {
87+
WheelPos -= 85;
88+
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
89+
} else {
90+
WheelPos -= 170;
91+
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
92+
}
93+
}

Rotary_Trinkey/Arduino_SurfaceDial_Example/.rotarytrinkey_m0.test.only

Whitespace-only changes.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <Adafruit_NeoPixel.h>
2+
#include <RotaryEncoder.h>
3+
#include "HID-Project.h" // https://github.com/NicoHood/HID
4+
5+
// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL
6+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
7+
int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255)
8+
9+
RotaryEncoder encoder(PIN_ENCODER_A, PIN_ENCODER_B, RotaryEncoder::LatchMode::FOUR3);
10+
// This interrupt will do our encoder reading/checking!
11+
void checkPosition() {
12+
encoder.tick(); // just call tick() to check the state.
13+
}
14+
15+
int last_rotary = 0;
16+
bool last_button = false;
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
//while (!Serial);
21+
delay(100);
22+
23+
Serial.println("Rotary Trinkey Surface Dial");
24+
25+
// start neopixels
26+
strip.begin();
27+
strip.setBrightness(neo_brightness);
28+
strip.show(); // Initialize all pixels to 'off'
29+
30+
attachInterrupt(PIN_ENCODER_A, checkPosition, CHANGE);
31+
attachInterrupt(PIN_ENCODER_B, checkPosition, CHANGE);
32+
33+
// set up the encoder switch, which is separate from the encoder
34+
pinMode(PIN_ENCODER_SWITCH, INPUT_PULLDOWN);
35+
36+
// Sends a clean report to the host. This is important on any Arduino type.
37+
SurfaceDial.begin();
38+
}
39+
40+
41+
void loop() {
42+
// read encoder
43+
int curr_rotary = encoder.getPosition();
44+
RotaryEncoder::Direction direction = encoder.getDirection();
45+
// read switch
46+
bool curr_button = !digitalRead(PIN_ENCODER_SWITCH);
47+
48+
if (direction != RotaryEncoder::Direction::NOROTATION) {
49+
Serial.print("Encoder value: ");
50+
Serial.print(curr_rotary);
51+
Serial.print(" direction: ");
52+
Serial.print((int)direction);
53+
54+
if (direction == RotaryEncoder::Direction::CLOCKWISE) {
55+
Serial.println(" Rotate+");
56+
SurfaceDial.rotate(40);
57+
}
58+
if (direction == RotaryEncoder::Direction::COUNTERCLOCKWISE) {
59+
Serial.println(" Rotate-");
60+
SurfaceDial.rotate(-40);
61+
}
62+
63+
last_rotary = curr_rotary;
64+
}
65+
66+
if (curr_button && !last_button) { // switch pressed!
67+
Serial.println("Press");
68+
SurfaceDial.press();
69+
}
70+
if (!curr_button && last_button) { // switch released!
71+
Serial.println("Release");
72+
SurfaceDial.release();
73+
}
74+
last_button = curr_button;
75+
76+
delay(10); // debounce
77+
}

Rotary_Trinkey/Arduino_Volume_Mute_Example/.rotarytrinkey_m0.test.only

Whitespace-only changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <Adafruit_NeoPixel.h>
2+
#include <RotaryEncoder.h>
3+
#include "HID-Project.h" // https://github.com/NicoHood/HID
4+
5+
// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL
6+
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
7+
int16_t neo_brightness = 20; // initialize with 20 brightness (out of 255)
8+
9+
RotaryEncoder encoder(PIN_ENCODER_A, PIN_ENCODER_B, RotaryEncoder::LatchMode::FOUR3);
10+
// This interrupt will do our encoder reading/checking!
11+
void checkPosition() {
12+
encoder.tick(); // just call tick() to check the state.
13+
}
14+
15+
int last_rotary = 0;
16+
bool last_button = false;
17+
18+
void setup() {
19+
Serial.begin(115200);
20+
//while (!Serial);
21+
delay(100);
22+
23+
Serial.println("Rotary Trinkey Volume Knob");
24+
25+
// start neopixels
26+
strip.begin();
27+
strip.setBrightness(neo_brightness);
28+
strip.show(); // Initialize all pixels to 'off'
29+
30+
attachInterrupt(PIN_ENCODER_A, checkPosition, CHANGE);
31+
attachInterrupt(PIN_ENCODER_B, checkPosition, CHANGE);
32+
33+
// set up the encoder switch, which is separate from the encoder
34+
pinMode(PIN_ENCODER_SWITCH, INPUT_PULLDOWN);
35+
36+
// Sends a clean report to the host. This is important on any Arduino type.
37+
Consumer.begin();
38+
}
39+
40+
41+
void loop() {
42+
// read encoder
43+
int curr_rotary = encoder.getPosition();
44+
RotaryEncoder::Direction direction = encoder.getDirection();
45+
// read switch
46+
bool curr_button = !digitalRead(PIN_ENCODER_SWITCH);
47+
48+
if (direction != RotaryEncoder::Direction::NOROTATION) {
49+
Serial.print("Encoder value: ");
50+
Serial.print(curr_rotary);
51+
Serial.print(" direction: ");
52+
Serial.print((int)direction);
53+
54+
if (direction == RotaryEncoder::Direction::CLOCKWISE) {
55+
Serial.println(" Vol +");
56+
Consumer.write(MEDIA_VOLUME_UP);
57+
}
58+
if (direction == RotaryEncoder::Direction::COUNTERCLOCKWISE) {
59+
Serial.println(" Vol -");
60+
Consumer.write(MEDIA_VOLUME_DOWN);
61+
}
62+
63+
last_rotary = curr_rotary;
64+
}
65+
66+
if (curr_button && !last_button) { // switch pressed!
67+
Serial.println("Play/Pause");
68+
Consumer.write(MEDIA_PLAY_PAUSE);
69+
}
70+
last_button = curr_button;
71+
72+
delay(10); // debounce
73+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Rotary Trinkey NeoPixel color picker example"""
2+
import rotaryio
3+
import digitalio
4+
import board
5+
import neopixel
6+
import _pixelbuf
7+
8+
print("Rotary Trinkey color picker example")
9+
10+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.5)
11+
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
12+
switch = digitalio.DigitalInOut(board.SWITCH)
13+
switch.switch_to_input(pull=digitalio.Pull.DOWN)
14+
15+
last_position = -1
16+
color = 0 # start at red
17+
while True:
18+
position = encoder.position
19+
if last_position is None or position != last_position:
20+
print(position)
21+
if not switch.value:
22+
# change the color
23+
if position > last_position: # increase
24+
color += 1
25+
else:
26+
color -= 1
27+
color = (color + 256) % 256 # wrap around to 0-256
28+
pixel.fill(_pixelbuf.colorwheel(color))
29+
else:
30+
# change the brightness
31+
if position > last_position: # increase
32+
pixel.brightness = min(1.0, pixel.brightness + 0.1)
33+
else:
34+
pixel.brightness = max(0, pixel.brightness - 0.1)
35+
last_position = position
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Rotary Trinkey Volume and Mute HID example"""
2+
import rotaryio
3+
import board
4+
import usb_hid
5+
import digitalio
6+
from adafruit_hid.consumer_control import ConsumerControl
7+
from adafruit_hid.consumer_control_code import ConsumerControlCode
8+
9+
print("Rotary Trinkey volume and mute example")
10+
11+
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
12+
switch = digitalio.DigitalInOut(board.SWITCH)
13+
switch.switch_to_input(pull=digitalio.Pull.DOWN)
14+
15+
cc = ConsumerControl(usb_hid.devices)
16+
17+
switch_state = None
18+
last_position = encoder.position
19+
20+
while True:
21+
current_position = encoder.position
22+
position_change = current_position - last_position
23+
if position_change > 0:
24+
for _ in range(position_change):
25+
cc.send(ConsumerControlCode.VOLUME_INCREMENT)
26+
print(current_position)
27+
elif position_change < 0:
28+
for _ in range(-position_change):
29+
cc.send(ConsumerControlCode.VOLUME_DECREMENT)
30+
print(current_position)
31+
last_position = current_position
32+
if not switch.value and switch_state is None:
33+
switch_state = "pressed"
34+
if switch.value and switch_state == "pressed":
35+
print("switch pressed.")
36+
cc.send(ConsumerControlCode.PLAY_PAUSE)
37+
switch_state = None
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Rotary Trinkey YouTube Frame-by-Frame Example"""
2+
import time
3+
import rotaryio
4+
import board
5+
import digitalio
6+
import usb_hid
7+
from adafruit_hid.keyboard import Keyboard
8+
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
9+
10+
print("Rotary Trinkey YouTube Frame-by-Frame example")
11+
12+
encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
13+
switch = digitalio.DigitalInOut(board.SWITCH)
14+
switch.switch_to_input(pull=digitalio.Pull.DOWN)
15+
16+
time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
17+
keyboard = Keyboard(usb_hid.devices)
18+
keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
19+
20+
switch_state = None
21+
last_position = encoder.position
22+
23+
while True:
24+
current_position = encoder.position
25+
position_change = current_position - last_position
26+
if position_change > 0:
27+
for _ in range(position_change):
28+
keyboard_layout.write('.')
29+
print(current_position)
30+
elif position_change < 0:
31+
for _ in range(-position_change):
32+
keyboard_layout.write(',')
33+
print(current_position)
34+
last_position = current_position
35+
if not switch.value and switch_state is None:
36+
switch_state = "pressed"
37+
if switch.value and switch_state == "pressed":
38+
print("switch pressed.")
39+
keyboard_layout.write(' ')
40+
switch_state = None

0 commit comments

Comments
 (0)