forked from jfurcean/CircuitPython-MuteButton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
executable file
·109 lines (77 loc) · 3.26 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# SPDX-FileCopyrightText: 2020 John Furcean
# SPDX-License-Identifier: MIT
import time
import board
from digitalio import DigitalInOut, Direction, Pull
import usb_hid
from adafruit_hid.keyboard import Keyboard
# from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
# from adafruit_hid.keycode import Keycode
import neopixel
from config import * # User settings in config.py
from profiles import profiles # Import dictionary of buttons and lights
def button_pressed(button):
return button.value
# def set_color(led, color):
def select_profile(profiles, led, button):
all_profiles = list(profiles.keys())
all_profiles.remove('mute') # Can't select 'mute' as a profile
selected_profile = False
while not selected_profile:
for profile in all_profiles: # Iterate through chat applications
if DEBUG:
print(led)
print(profiles[profile])
start_time = time.monotonic() # Start the timer
rgb_led[0] = profiles[profile]["color"] # Display the application color
while time.monotonic() - start_time < SELECTION_TIMEOUT: # Has X time passed?
if button.value == True: # Button has been pressed
if DEBUG:
print("Profile Selected: " + profile)
time.sleep(0.5) # Give time to remove finger
return(profile) #
time.sleep(0.1)
# initialize onboard neopixel
# this will be used to indiciate what application is enabled
rgb_led = neopixel.NeoPixel(board.NEOPIXEL, 1)
rgb_led.brightness = 1.0
rgb_led[0] = (0, 0, 0)
kbd = Keyboard(usb_hid.devices)
# Digital input with pulldown on Neokey
button = DigitalInOut(board.SWITCH)
button.switch_to_input(pull=Pull.DOWN)
button_state = False # Starting with button not being pressed
change_button = False
mute = False # Starting value
if PROFILE == 'choose': # A known profile can be set
PROFILE = select_profile(profiles, rgb_led, button) # Get the desired chat app
while True:
# retreive the active controller button
controller_button = profiles[PROFILE]
if DEBUG:
print("Profile: " + controller_button['name'])
print("Mute: " + str(mute))
# set the color of the onboard neopixel to match the active controller button
if not mute:
rgb_led[0] = controller_button['color'] # Set the LED to the chat color
else:
rgb_led[0] = profiles['mute']['color'] # Turn the LED red
# detect if the button has been released
if button_pressed(button):
if DEBUG:
print("{} mute button press".format(controller_button['name']))
try:
kbd.send(controller_button['keycode']) # Send a one key mute command
except TypeError as e:
kbd.send(*controller_button['keycode']) # Send a multi key mute command
mute = not mute # Toggle the boolean
if not mute:
rgb_led[0] = controller_button['color'] # Set the LED to the chat color
else:
rgb_led[0] = profiles['mute']['color'] # Turn the LED red
time.sleep(BUTTON_LATCH_TIME) # Give time to remove finger
if DEBUG:
time.sleep(1)
print("Button Pressed: " + str(button_pressed(button)))
else:
time.sleep(0.1)