Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/serial_port.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
from ioLabs import USBBox
Expand All @@ -9,4 +10,4 @@
while True:
bytes=usbbox.serial.read()
if bytes:
print bytes
print(bytes)
36 changes: 20 additions & 16 deletions examples/simonsays.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from __future__ import print_function

from builtins import input
from builtins import zip
from builtins import range
from ioLabs import USBBox, REPORT
import time

Expand All @@ -12,7 +16,7 @@ def flash(usbbox,led_mask=0xFF,rate=1,count=1):

# ensure light off first
usbbox.commands.p2set(0x00)
for i in xrange(count):
for i in range(count):
usbbox.commands.p2set(led_mask)
time.sleep(rate/2.0)
usbbox.commands.p2set(0x00)
Expand All @@ -33,8 +37,8 @@ def simon_says(usbbox,num):

# create a random sequence (0-7)
import random
order=[random.choice(xrange(8)) for i in range(num)]
print "simon says:", " ".join([BUTTON_COLORS[i] for i in order])
order=[random.choice(range(8)) for i in range(num)]
print("simon says:", " ".join([BUTTON_COLORS[i] for i in order]))

# flash LEDs once
flash(usbbox)
Expand Down Expand Up @@ -104,25 +108,25 @@ def key_report(report):
# should tell us how long the user took to press all of the
# buttons
last_press=keyevents[-1].rtc
print "correct (%dms)" % last_press
print("correct (%dms)" % last_press)
flash(usbbox)
else:
print "user said:", " ".join([BUTTON_COLORS[event.key_code] for event in keyevents])
print "wrong"
print("user said:", " ".join([BUTTON_COLORS[event.key_code] for event in keyevents]))
print("wrong")
flash(usbbox,rate=0.25,count=3)

return correct

usbbox=USBBox()

print "Simon Says"
print "consists of several rounds of sequences being shown:"
print "1) LEDs will flash on box"
print "2) a sequence of LEDs will be shown"
print "3) after the sequence finishes the LEDs will flash again"
print "4) enter in the sequence previously shown (press the buttons that match the LEDs)"
print "5) if you get it right the LEDs will flash once and it'll repeat the cycle (with one more item to remember)"
print "6) if you get it wrong the LEDs will flash three times and you'll be asked if you want to play a new game"
print("Simon Says")
print("consists of several rounds of sequences being shown:")
print("1) LEDs will flash on box")
print("2) a sequence of LEDs will be shown")
print("3) after the sequence finishes the LEDs will flash again")
print("4) enter in the sequence previously shown (press the buttons that match the LEDs)")
print("5) if you get it right the LEDs will flash once and it'll repeat the cycle (with one more item to remember)")
print("6) if you get it wrong the LEDs will flash three times and you'll be asked if you want to play a new game")

# use logset so that 1 turns LED on and 0 turn LED off
usbbox.commands.logset(0xFF,0xFF)
Expand All @@ -132,15 +136,15 @@ def key_report(report):
usbbox.commands.dirset(0,0,0)

while True:
command=raw_input("play game y/n [y]: ").strip()
command=input("play game y/n [y]: ").strip()
if command == '':
command = 'y'
if command.lower() != 'y':
break

for i in range(1,50):
time.sleep(1)
print "%d to remember" % i
print("%d to remember" % i)
if not simon_says(usbbox,i):
break

35 changes: 19 additions & 16 deletions examples/simonsays2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import print_function

from builtins import input
from builtins import range
from ioLabs import USBBox, REPORT
import time

Expand All @@ -11,7 +14,7 @@ def flash(usbbox,led_mask=0xFF,rate=1,count=1):

# ensure light off first
usbbox.leds.state=0x00
for i in xrange(count):
for i in range(count):
usbbox.leds.state=led_mask
time.sleep(rate/2.0)
usbbox.leds.state=0x00
Expand All @@ -32,8 +35,8 @@ def simon_says(usbbox,num):

# create a random sequence (0-7)
import random
order=[random.choice(xrange(8)) for i in range(num)]
print "simon says:", " ".join([BUTTON_COLORS[i] for i in order])
order=[random.choice(range(8)) for i in range(num)]
print("simon says:", " ".join([BUTTON_COLORS[i] for i in order]))

# flash LEDs once
flash(usbbox)
Expand Down Expand Up @@ -87,25 +90,25 @@ def simon_says(usbbox,num):
# should tell us how long the user took to press all of the
# buttons
last_press=keyevents[-1].rtc
print "correct (%dms)" % last_press
print("correct (%dms)" % last_press)
flash(usbbox)
else:
print "user said:", " ".join([BUTTON_COLORS[event.key_code] for event in keyevents])
print "wrong"
print("user said:", " ".join([BUTTON_COLORS[event.key_code] for event in keyevents]))
print("wrong")
flash(usbbox,rate=0.25,count=3)

return correct

usbbox=USBBox()

print "Simon Says"
print "consists of several rounds of sequences being shown:"
print "1) LEDs will flash on box"
print "2) a sequence of LEDs will be shown"
print "3) after the sequence finishes the LEDs will flash again"
print "4) enter in the sequence previously shown (press the buttons that match the LEDs)"
print "5) if you get it right the LEDs will flash once and it'll repeat the cycle (with one more item to remember)"
print "6) if you get it wrong the LEDs will flash three times and you'll be asked if you want to play a new game"
print("Simon Says")
print("consists of several rounds of sequences being shown:")
print("1) LEDs will flash on box")
print("2) a sequence of LEDs will be shown")
print("3) after the sequence finishes the LEDs will flash again")
print("4) enter in the sequence previously shown (press the buttons that match the LEDs)")
print("5) if you get it right the LEDs will flash once and it'll repeat the cycle (with one more item to remember)")
print("6) if you get it wrong the LEDs will flash three times and you'll be asked if you want to play a new game")

# use logset so that 1 turns LED on and 0 turn LED off
usbbox.leds.logic=0xFF
Expand All @@ -115,15 +118,15 @@ def simon_says(usbbox,num):
usbbox.disable_loopback()

while True:
command=raw_input("play game y/n [y]: ").strip()
command=input("play game y/n [y]: ").strip()
if command == '':
command = 'y'
if command.lower() != 'y':
break

for i in range(1,50):
time.sleep(1)
print "%d to remember" % i
print("%d to remember" % i)
if not simon_says(usbbox,i):
break

1 change: 1 addition & 0 deletions hid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
and interacting with those devices.
'''

from builtins import object
import logging

import os
Expand Down
33 changes: 18 additions & 15 deletions hid/cparser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
from builtins import next
from past.builtins import basestring
from builtins import object
from ctypes import *
import re

Expand Down Expand Up @@ -42,7 +45,7 @@ def define(name, t):

def _parse_type(type_str):
# see if the type is there
if _types.has_key(type_str):
if type_str in _types:
return _types[type_str]
if type_str.endswith('*'):
type_str=type_str[:-1]
Expand Down Expand Up @@ -71,7 +74,7 @@ def __init__(self,s):

self.i=-1

def next(self):
def __next__(self):
if self.empty():
raise ValueError('no more tokens found parsing - %s' % self.s)
self.i+=1
Expand Down Expand Up @@ -140,28 +143,28 @@ def __repr__(self):

def parse_type(t):
# grab variable name (including * for pointers)
base_type=t.next()
base_type=next(t)
while not t.empty():
if t.next() == '*':
if next(t) == '*':
base_type += '*'
else:
t.push_back()
break
return c_type(base_type)

def parse_fn_name(t):
if t.next() == '(':
if next(t) == '(':
# fn pointer name
assert t.next() == '*'
name=t.next()
assert t.next() == ')'
assert next(t) == '*'
name=next(t)
assert next(t) == ')'
else:
name=t.current()
return name

def parse_param(t):
param_type=parse_type(t)
name=t.next()
name=next(t)
if WORD.match(name):
param_type.name=name
else:
Expand All @@ -171,12 +174,12 @@ def parse_param(t):


def parse_param_list(t):
assert t.next() == '('
assert next(t) == '('
params=[]
while t.next() != ')':
while next(t) != ')':
t.push_back()
params.append(parse_param(t))
if t.next() != ',':
if next(t) != ',':
break
assert t.current() == ')'
return params
Expand All @@ -191,7 +194,7 @@ def parse(s):
t=tokenizer(s)
def_type = parse_type(t)
if not t.empty():
if t.next() == '(':
if next(t) == '(':
# looks like we're parsing a function pointer definition
# e.g. void (*my_fn)(void)
t.push_back()
Expand All @@ -202,9 +205,9 @@ def parse(s):
return c_function(def_type, fn_name, param_list)
else:
t.push_back()
name = t.next()
name = next(t)
if not t.empty():
if t.next() == '(':
if next(t) == '(':
# parsing function def again (regular def not pointer)
# e.g. void my_fn(void)
t.push_back()
Expand Down
6 changes: 4 additions & 2 deletions hid/osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Refer to the hid module for available functions
'''

from builtins import range
from builtins import object
from ctypes import *
from ctypes.util import find_library

Expand Down Expand Up @@ -186,7 +188,7 @@ class IOHIDDeviceInterface122(Structure):

########################################################
# class to handle COM object references
class COMObjectRef:
class COMObjectRef(object):
def __init__(self,ref):
self.ref=ref
logging.info("created: %s",self)
Expand All @@ -195,7 +197,7 @@ def __del__(self):
logging.info("releasing: %s",self)
self.Release()

def __nonzero__(self):
def __bool__(self):
return self.ref is not None

def __str__(self):
Expand Down
Loading