http://www.multiwingspan.co.uk/micro.php?page=snes
代码: 全选
from microbit import *
class SNES:
# set up which micro:bit pins to use
def __init__(self, latch, clock, data):
self.latch = latch
self.clock = clock
self.data = data
# take a reading and return a 16bit integer
def getReading(self):
self.latch.write_digital(1)
self.latch.write_digital(0)
word = 0
for i in range(16):
self.clock.write_digital(0)
word = word + (self.data.read_digital()<<i)
self.clock.write_digital(1)
return word
# create and return dictionary of button names and the readings
def info(self):
reading = self.getReading()
keys = ["0","0","0","0","RSH","LSH","X","A","R","L","D","U","Start","Select","Y","B"]
bits = [reading >> i & 1 for i in range(15,-1,-1)]
return dict(zip(keys,bits))
# create and return a list of buttons that have been pressed
def pressed(self):
d = self.info()
output = []
for k in d:
if d[k]!=1:
output.append(k)
return output