Skip to content

Instantly share code, notes, and snippets.

@jonghwanhyeon
Created June 9, 2013 15:31
Show Gist options
  • Save jonghwanhyeon/5743946 to your computer and use it in GitHub Desktop.
Save jonghwanhyeon/5743946 to your computer and use it in GitHub Desktop.
import serial
import time
class SimpliciTI:
SIZE_OF_PACKET_HEADER = 3
HW_NO_ERROR = 0x06
def __init__(self):
self.device = serial.Serial()
self.device.port = '/dev/tty.usbmodem001'
self.device.baudrate = 115200
self.device.timeout = 30
def open(self):
self.device.open()
def close(self):
self.device.close()
def read(self, length = 0):
length += SimpliciTI.SIZE_OF_PACKET_HEADER
packet = self.device.read(length)
self.device.flush()
return packet
def write(self, command, data = []):
header = [0xFF, command, SimpliciTI.SIZE_OF_PACKET_HEADER + len(data)]
packet = header + data
self.device.flush()
self.device.write(bytes(packet))
time.sleep(1 / 1000)
def query(self, command, data = []):
self.write(command, data)
packet = self.read(len(data))
if packet[1] != SimpliciTI.HW_NO_ERROR: raise Exception('hardware error is occurred')
return list(packet[SimpliciTI.SIZE_OF_PACKET_HEADER:])
class AccessPoint:
BM_START_SIMPLICITI = 0x07
BM_GET_SIMPLICITIDATA = 0x08
BM_STOP_SIMPLICITI = 0x09
def __init__(self):
self.device = SimpliciTI()
self.device.open()
def __del__(self):
self.device.close()
def start(self):
self.device.query(AccessPoint.BM_START_SIMPLICITI)
def receive(self):
data = self.device.query(AccessPoint.BM_GET_SIMPLICITIDATA, [0x00, 0x00, 0x00, 0x00])
return data[0]
def stop(self):
self.device.query(AccessPoint.BM_STOP_SIMPLICITI)
class OperationNotifier:
TRANSITION_TABLE = {
0x12: 'star',
0x22: 'hash',
0x32: 'up',
}
UNKNOWN_TRANSITION = 0xFF
def __init__(self, handler):
self.handler = handler
self.last_transition = OperationNotifier.UNKNOWN_TRANSITION
def add(self, transition):
if self.isKnownTransition(transition):
if self.last_transition != transition:
self.handler(OperationNotifier.TRANSITION_TABLE[transition])
else:
transition = OperationNotifier.UNKNOWN_TRANSITION
self.last_transition = transition
def isKnownTransition(self, transition):
return transition in OperationNotifier.TRANSITION_TABLE
import Quartz
def pressKey(key):
event = Quartz.CGEventCreateKeyboardEvent(None, key, True)
Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)
def releaseKey(key):
event = Quartz.CGEventCreateKeyboardEvent(None, key, False)
Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)
def tapKey(key):
pressKey(key)
releaseKey(key)
def handler(operation):
if operation == 'star':
print('star is pressed')
tapKey(124)
elif operation == 'hash':
print('hash is pressed')
tapKey(123)
elif operation == 'up':
print('up is pressed')
tapKey(16)
try:
access_point = AccessPoint()
access_point.start()
print("started access point")
notifier = OperationNotifier(handler)
while True:
transition = access_point.receive()
notifier.add(transition)
time.sleep(0.1)
except KeyboardInterrupt:
access_point.stop()
print("stopped access point")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment