Difference between revisions of "KeyboardEmulator"
Jump to navigation
Jump to search
(One intermediate revision by one other user not shown) | |||
Line 3: | Line 3: | ||
== Connect D2 to GND to activate Keyboard == | == Connect D2 to GND to activate Keyboard == | ||
− | Acts | + | Acts as Keyboard: [[File:Cp10.png|200px]] |
No Keyboard output: [[File:Cp12.png|200px]] | No Keyboard output: [[File:Cp12.png|200px]] | ||
Line 81: | Line 81: | ||
print("nothing") | print("nothing") | ||
elif 20 < x <= 180: | elif 20 < x <= 180: | ||
− | print("nice, I like this") | + | print("nice, I like this\n") |
if write2keyboard: | if write2keyboard: | ||
keyboard_layout.write("nice, I like this\n") | keyboard_layout.write("nice, I like this\n") | ||
Line 87: | Line 87: | ||
print("are you careful?") | print("are you careful?") | ||
if write2keyboard: | if write2keyboard: | ||
− | keyboard_layout.write("are zou | + | keyboard_layout.write("are zou careful_\n") |
elif 300 < x <= 400: | elif 300 < x <= 400: | ||
print("be careful with me!") | print("be careful with me!") | ||
Line 106: | Line 106: | ||
else: | else: | ||
print("Value out of range") | print("Value out of range") | ||
+ | |||
+ | if write2keyboard: | ||
+ | time.sleep(1) | ||
+ | time.sleep(1) | ||
+ | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 09:16, 23 June 2024
Keyboard Emulator in CircuitPython[edit]
Connect D2 to GND to activate Keyboard[edit]
code for the keyboard emulator[edit]
1 #"""based on CircuitPython Essentials HID Keyboard example"""
2
3 # IF you connect D2 and GND it acts as keyboard
4
5 import time
6 import board
7 import digitalio
8 from digitalio import DigitalInOut, Direction, Pull
9 import usb_hid
10 from adafruit_hid.keyboard import Keyboard
11 from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
12 from adafruit_hid.keycode import Keycode
13 from adafruit_lsm6ds.lsm6dsox import LSM6DSOX
14
15 i2c = board.I2C() # uses board.SCL and board.SDA
16 # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
17 sensor = LSM6DSOX(i2c)
18
19 led = digitalio.DigitalInOut(board.LED)
20 led.direction = digitalio.Direction.OUTPUT
21
22 switch = DigitalInOut(board.D2)
23 switch.direction = Direction.INPUT
24 switch.pull = Pull.UP
25
26 def sensorRead():
27 a=(sensor.acceleration)
28 g=(sensor.gyro)
29 return (a+g)
30
31 # The keyboard object!
32 time.sleep(1) # Sleep for a bit to avoid a race condition on some systems
33 keyboard = Keyboard(usb_hid.devices)
34 keyboard_layout = KeyboardLayoutUS(keyboard) # We're in the US :)
35
36
37 while True:
38 readings = []
39
40 for _ in range(100):
41 reading = sensorRead()
42 readings.append(reading)
43 time.sleep(0.02) # Sleep for 100 milliseconds
44
45 sum_absolute_differences = [0] * 6
46
47 for i in range(1, len(readings)):
48 for j in range(6): # Assuming each reading has 6 components
49 sum_absolute_differences[j] += abs(readings[i][j] - readings[i-1][j])
50
51 x = sum(sum_absolute_differences)
52 print(x)
53
54 #for i, reading in enumerate(readings):
55 #print(f"Reading {i+1}: {reading}")
56
57 if switch.value:
58 led.value = False
59 write2keyboard = False
60 #print("Off")
61 else:
62 led.value = True
63 write2keyboard = True
64 #print("On")
65
66 #if write2keyboard:
67 # keyboard.press(Keycode.SHIFT, Keycode.A)
68 # keyboard.press(Keycode.ENTER)
69
70 if 0 <= x <= 20:
71 print("nothing")
72 elif 20 < x <= 180:
73 print("nice, I like this\n")
74 if write2keyboard:
75 keyboard_layout.write("nice, I like this\n")
76 elif 180 < x <= 300:
77 print("are you careful?")
78 if write2keyboard:
79 keyboard_layout.write("are zou careful_\n")
80 elif 300 < x <= 400:
81 print("be careful with me!")
82 if write2keyboard:
83 keyboard_layout.write("be careful with me!\n")
84 elif 400 < x <= 600:
85 print("no this is not nice!")
86 if write2keyboard:
87 keyboard_layout.write("no this is not nice!\n")
88 elif 600 < x <= 800:
89 print("this hurts!!!")
90 if write2keyboard:
91 keyboard_layout.write("this hurts!!!\n")
92 elif x > 800:
93 print("you are killing me")
94 if write2keyboard:
95 keyboard_layout.write("zou are killing me\n")
96 else:
97 print("Value out of range")
98
99 if write2keyboard:
100 time.sleep(1)
101 time.sleep(1)