Skip to content

Instantly share code, notes, and snippets.

@eighthree
Created November 19, 2017 22:57
Show Gist options
  • Save eighthree/408075d4888b1bd3cc4652b2ed952e78 to your computer and use it in GitHub Desktop.
Save eighthree/408075d4888b1bd3cc4652b2ed952e78 to your computer and use it in GitHub Desktop.
USB Keyboard + Power Management for Raspberry Pi
import time
import busio
from adafruit_ina219 import INA219
from digitalio import DigitalInOut, Direction, Pull
import board
from board import *
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
buttonpins = [A0, A1, A2, A3, A4, A5]
buttons = []
buttonkeys = [Keycode.W, Keycode.Q, Keycode.ENTER, Keycode.A, Keycode.B, Keycode.RIGHT_SHIFT]
kbd = Keyboard()
layout = KeyboardLayoutUS(kbd)
for pin in buttonpins:
button = DigitalInOut(pin)
button.direction = Direction.INPUT
button.pull = Pull.UP
buttons.append(button)
print('Buttons initiated')
myI2C = busio.I2C(SCL, SDA)
ina219 = INA219(myI2C)
print("INA219 initiated")
time.sleep(5)
while True:
ctime = int(time.monotonic())
if ctime % 200 == 0:
busvoltage = 0
busvoltage = ina219.get_bus_voltage_V()
print(busvoltage)
for button in buttons:
s = button.value
if not button.value:
i = buttons.index(button)
k = buttonkeys[i]
if type(k) is str:
layout.write(k)
else:
if not s:
kbd.press(k)
time.sleep(0.07)
kbd.release_all()
time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment