Skip to content

Commit

Permalink
Restore files referenced from Vector OS docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MS3FGX authored Nov 4, 2023
1 parent 38491a0 commit 5ee86a8
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
55 changes: 55 additions & 0 deletions source/keyboardrepeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from keyboardcb import KeyboardCB

class KeyboardRepeat(KeyboardCB):
"""
A class that inherits from KeyboardCB and adds functionality to handle key repeat events.
Attributes:
repeat_count (int): The number of times a key needs to be pressed before it is considered a repeat.
key_count (dict): A dictionary that keeps track of the number of times each key has been pressed.
"""

def __init__(self, repeat_count=3, callback={}, filter=[], attach=True):
"""
The constructor for KeyboardRepeat class.
Args:
repeat_count (int): The number of times a key needs to be pressed before it is considered a repeat. Default is 3.
callback (dict): A dictionary of callback functions. Default is an empty dictionary.
filter (list): A list of keys to filter. Default is an empty list.
attach (bool): A flag to indicate if the keyboard should be attached during initialization. Default is True.
"""

super().__init__(callback, filter, False, attach)
self.key_count = {}
self.repeat_count = repeat_count

async def key(self, b):
"""
Method to handle key press events.
This method overrides the key method in the parent class. It updates the count of key presses for each key
and calls the parent class's key method with the keys that are considered repeats.
Args:
b (list): The list of pressed keys.
"""

for item in b:
if item in self.key_count:
self.key_count[item] += 1
else:
self.key_count[item] = 0 # mark first time

remove = [k for k in self.key_count if k not in b]

for k in remove:
del self.key_count[k]

superkey = [k for k, v in self.key_count.items() if v == self.repeat_count or v == 0]
resetkey = [k for k, v in self.key_count.items() if v == self.repeat_count]

for item in resetkey:
self.key_count[item] = 1 # reset repeat count

await super().key(superkey)
116 changes: 116 additions & 0 deletions source/led.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import keyboardio
import keyleds
import asyncio

class LED:
"""
A class to represent an LED.
...
Attributes
----------
mask : int
a bitmask representing the LED
Methods
-------
set():
Sets the LED.
reset():
Resets the LED.
toggle():
Toggles the LED.
"""

def __init__(self, mask):
"""
Constructs all the necessary attributes for the LED object.
Parameters
----------
mask : int
a bitmask representing the LED
"""
self.mask = mask

def set(self):
"""Sets the LED by applying the mask to the keyboard LEDs."""
keyboardio.KeyboardIO.leds |= self.mask

def reset(self):
"""Resets the LED by applying the inverted mask to the keyboard LEDs."""
keyboardio.KeyboardIO.leds &= ~self.mask

def toggle(self):
"""Toggles the LED by XORing the mask with the keyboard LEDs."""
keyboardio.KeyboardIO.leds ^= self.mask

def __call__(self,value):
self.value=value

@property
def value(self):
"""
Gets the value of the LED.
Returns
-------
bool
True if the LED is set, False otherwise.
"""
return (keyboardio.KeyboardIO.leds & self.mask) != 0

@value.setter
def value(self, val):
"""
Sets or resets the LED based on the provided value.
Parameters
----------
val : bool
The value to set. If True, the LED is set. If False, it is reset.
"""
if val:
self.set()
else:
self.reset()

# Create instances of LEDs with different masks.
X = LED(keyleds.LED_X)
Y = LED(keyleds.LED_Y)
Triangle = LED(keyleds.LED_TRI)
Square = LED(keyleds.LED_SQ)
Sine = LED(keyleds.LED_SINE)
Sig = LED(keyleds.LED_SIG)
Scope = LED(keyleds.LED_SCOPE)
Saw = LED(keyleds.LED_SAW)


if __name__=="__main__":
from vectoros import sleep_forever
from timer import Timer

keyboardio.KeyboardIO.run(50)
Timer.run()

flip=False

def tick():
global flip
if flip:
Y.set()
X.reset()
flip=False
Scope.value=1
Sig(False)
else:
Y.reset()
X.set()
flip=True
Scope.value=0
Sig(True)
Sine.toggle()

Timer.add_timer(10,tick)
sleep_forever()

0 comments on commit 5ee86a8

Please sign in to comment.