2443|2

1万

帖子

24

TA的资源

版主

楼主
 

使用 circuitpython 的 Python Keyboard [复制链接]

 

来自:

链接已隐藏,如需查看请登录或者注册


支持 python 的 USB 和蓝牙双模键盘,使用了  CircuitPython。

 

 

图片

 

主程序

import time
from board import *
import digitalio
import usb_hid

import adafruit_ble
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.hid import HIDService
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode as _

ROWS = (P27, P13, P30, P20, P3)
COLS = (P26, P31, P29, P28, P5, P4, P24, P25, P23, P22, P14, P15, P16, P17)

KEYMAP = (_.ESCAPE, _.ONE, _.TWO, _.THREE, _.FOUR, _.FIVE, _.SIX, _.SEVEN, _.EIGHT, _.NINE, _.ZERO, _.MINUS, _.EQUALS, _.BACKSPACE,
        _.TAB, _.Q, _.W, _.E, _.R, _.T, _.Y, _.U, _.I, _.O, _.P, _.LEFT_BRACKET, _.RIGHT_BRACKET, _.BACKSLASH,
        _.CAPS_LOCK, _.A, _.S, _.D, _.F, _.G, _.H, _.J, _.K, _.L, _.SEMICOLON, _.QUOTE, None, _.ENTER,
        _.LEFT_SHIFT, _.Z, _.X, _.C, _.V, _.B, _.N, _.M, _.COMMA, _.PERIOD, _.FORWARD_SLASH, None, _.RIGHT_SHIFT, None,
        _.LEFT_CONTROL, _.LEFT_ALT, _.LEFT_GUI, None, None, _.SPACE, None, None, _.RIGHT_ALT, _.RIGHT_GUI, _.APPLICATION, _.RIGHT_CONTROL, None, None)

class Matrix:
    def __init__(self, rows=ROWS, cols=COLS):
        self.rows = []
        for pin in rows:
            io = digitalio.DigitalInOut(pin)
            io.direction = digitalio.Direction.OUTPUT
            io.drive_mode = digitalio.DriveMode.PUSH_PULL
            io.value = 0
            self.rows.append(io)
        self.cols = []
        for pin in cols:
            io = digitalio.DigitalInOut(pin)
            io.direction = digitalio.Direction.INPUT
            io.pull = digitalio.Pull.DOWN
            self.cols.append(io)
        self.pressed_keys = []

    def scan(self):
        new_keys = []
        pressed_keys = []
        released_keys = self.pressed_keys
        for r in range(len(self.rows)):
            self.rows[r].value = 1
            for c in range(len(self.cols)):
                if self.cols[c].value:
                    key = r * len(self.cols) + c
                    pressed_keys.append(key)
                    if key in released_keys:
                        released_keys.remove(key)
                    else:
                        new_keys.append(key)
            self.rows[r].value = 0
        self.pressed_keys = pressed_keys
        return pressed_keys, released_keys, new_keys

def main():
    hid = HIDService()
    advertisement = ProvideServicesAdvertisement(hid)
    advertisement.appearance = 961
    ble = adafruit_ble.BLERadio()
    if ble.connected:
        for c in ble.connections:
            c.disconnect()
    ble.start_advertising(advertisement)
    advertising = True
    ble_keyboard = Keyboard(hid.devices)

    matrix = Matrix()
    usb_keyboard = Keyboard(usb_hid.devices)

    while True:
        pressed_keys, released_keys, new_keys = matrix.scan()
        if released_keys:
            released_keycodes = list(map(lambda i: KEYMAP, released_keys))
            print('released keys {}'.format(released_keycodes))

            usb_keyboard.release(*released_keycodes)
            if ble.connected:
                advertising = False
                ble_keyboard.release(*released_keycodes)
        if new_keys:
            new_keycodes = list(map(lambda i: KEYMAP, new_keys))
            print('new keys {}'.format(new_keycodes))
            usb_keyboard.press(*new_keycodes)
            if ble.connected:
                advertising = False
                ble_keyboard.press(*new_keycodes)

        if not ble.connected and not advertising:
            ble.start_advertising(advertisement)
            advertising = True

        time.sleep(0.001)

if __name__ == '__main__':
    main()

 

最新回复

CircuitPython好像是有商业公司赞助   详情 回复 发表于 2020-7-13 09:41
点赞 关注
 
 

回复
举报

310

帖子

5

TA的资源

一粒金砂(中级)

沙发
 

CircuitPython好像是有商业公司赞助

点评

它是adafruit公司的开源项目,adafruit使用它做了很多开发板  详情 回复 发表于 2020-7-13 10:29
 
 
 

回复

1万

帖子

24

TA的资源

版主

板凳
 
宋元浩 发表于 2020-7-13 09:41 CircuitPython好像是有商业公司赞助

它是adafruit公司的开源项目,adafruit使用它做了很多开发板

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表