Skip to content

Instantly share code, notes, and snippets.

@spinningD20
Created September 20, 2016 19:38
Show Gist options
  • Save spinningD20/3960f074127d58cc7e28c322ba561634 to your computer and use it in GitHub Desktop.
Save spinningD20/3960f074127d58cc7e28c322ba561634 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from pyzmo import *
from evdev.ecodes import *
from evdev import InputDevice
import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 8000
import serial
ser = serial.Serial('/dev/ttyS2', 9600, timeout=1)
# kinda messy implementation, but make it match the Teensy's expected inputs
serial_dict = {
KEY_F5: b'0',
KEY_PAGEUP: b'1',
KEY_PAGEDOWN: b'2',
KEY_DOT: b'3'
}
def send_tcp(message):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(message)
s.close()
def send_serial(message):
print('SERIAL: {}'.format(message))
ser.write(message)
# triggered when KEY_PLAYPAUSE is pressed
@key(KEY_PAGEUP)
def pageup(events):
print('LOCAL PAGEUP')
send_tcp('TCP: LEFT ARROW PRESSED...')
send_serial(serial_dict[KEY_PAGEUP])
@key(KEY_PAGEDOWN)
def pagedown(events):
print('LOCAL PAGEDOWN')
send_tcp('TCP: RIGHT ARROW BUTTON PRESSED...')
send_serial(serial_dict[KEY_PAGEDOWN])
@key(KEY_F5, KEY_ESC)
def f5esc(events):
print('LOCAL F5')
send_tcp('TCP: PLAY BUTTON PRESSED...')
send_serial(serial_dict[KEY_F5])
@key(KEY_DOT)
def dot(events):
print('LOCAL DOT')
send_tcp('TCP: SCREEN BUTTON PRESSED...')
send_serial(serial_dict[KEY_DOT])
dev = InputDevice('/dev/input/event4')
dev.grab()
poll(dev)
void setup() {
Serial1.begin(9600);
Keyboard.begin();
Serial1.begin(9600);
}
void loop() {
if (Serial1.available() > 0) {
char inChar = Serial1.read();
if (inChar == '0') {
Keyboard.press(KEY_F5);
}
if (inChar == '1') {
Keyboard.press(KEY_PAGE_UP);
}
if (inChar == '2') {
Keyboard.press(KEY_PAGE_DOWN);
}
if (inChar == '3') {
Keyboard.press('.');
}
Keyboard.releaseAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment