使用 SNES 控制器

micro:bit编程、教学、展示
STEM
回复
头像
shaoziyang
帖子: 3954
注册时间: 2019年 10月 21日 13:48

使用 SNES 控制器

#1

帖子 shaoziyang »

SNES(任天堂)是1992年发布的游戏机,早期的手柄使用了2个 4021 移位寄存器连接到12个按键。我们很容易就可以将它连接到micro:bit上:

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
 

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章