当前位置: 首页 > news >正文

【CircuitPython】RaspberryPi Pico RP2040 自定义机械键盘实例

【CircuitPython】RaspberryPi Pico RP2040 Mechanical Keyboard实例


  • 本示例基于Thonny开发,所使用的固件是:CircuitPython,区别于MicroPython固件。

最近B站某君的瀚文键盘很火热,这里介绍利用RaspberryPi Pico RP2040实现键盘按键定制功能。由于RP2040引脚限制,最多支持20个自定义按键或组合键的输入。

  • 现有售卖模块有16按键的,如果自己打板可以设计最多20的按键,
  • 介绍链接:https://shop.pimoroni.com/products/pico-rgb-keypad-base?variant=32369517166675
    在这里插入图片描述

测试方法

  • 在线键盘按键测试
  • 找一个测试按键的专门网站:https://keyboard.bmcx.com/进行按键测试和验证。
    在这里插入图片描述
  • 测试方法:

调试时可以使用一根导线串联一个1K的电阻用于限流作用,一头接到GND,另一端去触碰引脚,处理GP16引脚外,其他数字引脚都可以去碰一碰,你可以在按键测试网站上看到,按键有被模拟按下。同时如果你打开了Thonny,在Shell调试窗口有对应的按键码值打印信息。
在这里插入图片描述

在这里插入图片描述

  • 按键映射可以自己在代码中修改成自己想要的指定按键或组合按键。
keymap = {
    (0): (KEY, (Keycode.GUI, Keycode.C)),# win + C
    (1): (KEY, (Keycode.GUI, Keycode.V)),# win +V
    (2): (KEY, [Keycode.THREE]),
    (3): (KEY, [Keycode.FOUR]),
    (4): (KEY, [Keycode.FIVE]),
    (5): (MEDIA, ConsumerControlCode.VOLUME_DECREMENT),
    (6): (MEDIA, ConsumerControlCode.VOLUME_INCREMENT),

    (7): (KEY, [Keycode.R]),
    (8): (KEY, [Keycode.G]),
    (9): (KEY, [Keycode.B]),
    (10): (KEY, [Keycode.UP_ARROW]),
    (11): (KEY, [Keycode.X]),  # plus key
    (12): (KEY, [Keycode.Y]),
    (13): (KEY, [Keycode.Z]),

    (14): (KEY, [Keycode.I]),
    (15): (KEY, [Keycode.O]),
    (16): (KEY, [Keycode.LEFT_ARROW]),
    (17): (KEY, [Keycode.DOWN_ARROW]),
    (18): (KEY, [Keycode.RIGHT_ARROW]),
    (19): (KEY, [Keycode.ALT]),
    (20): (KEY, [Keycode.U]),

}

环境搭建说明

  • 下载和烧录CircuitPython固件
  • 官方RP2040固件下载地址:https://circuitpython.org/downloads
    在这里插入图片描述
  • 如果使用的是YD2040的板子可以选择:pimoroni型号flash容量分:4MB8MB16MB
    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述

  • 固件选择说明
    在这里插入图片描述
  • 由于没有中文提示的,只有拼音
    在这里插入图片描述
  • 烧录方法
    在插入usb到电脑前先按住boot按键,在连接到电脑上,电脑上会显示一个虚拟出来的120MB容量的U盘,然后将下载下来的.uf2文件拷贝到虚拟的U盘当中即可实现固件的烧录。

库下载和导入

  • Thonny工具栏上当中-工具 -管理插件,搜索adafruit_hid
    在这里插入图片描述
    在这里插入图片描述
  • 找到下载路径:C:\Users\Administrator\AppData\Roaming\Thonny\plugins\Python310\site-packages

adafruit_hid文件夹拷贝到Pi Pico RP2040设备当中。

在这里插入图片描述

  • 先将此文件夹拷贝到桌面或其他自己能找到的地方,通过Thonny定位到文件夹,选中,右键上传到/(注意需要将设备退出U盘模式,不然上传会出错)
    在这里插入图片描述
    在这里插入图片描述

  • 如果没有将文件菜单勾选的:
    在这里插入图片描述

  • 直接拷贝方法:当RP2040插入到电脑上默认是有一个盘符的,不想Micropython ESP32
    在这里插入图片描述

  • 文件结构
    在这里插入图片描述

Pico_RP2040 Mech Keyboard 示例代码

  • 示例地址:https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/main/Pico_RP2040_Mech_Keyboard

在源代码上添加了按键调试串口输出,方便查阅。

# SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries
# SPDX-License-Identifier: MIT
# RaspberryPi Pico RP2040 Mechanical Keyboard

import time
import board
from digitalio import DigitalInOut, Direction, Pull
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

print("---Pico Pad Keyboard---")

led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT
led.value = True

kbd = Keyboard(usb_hid.devices)
cc = ConsumerControl(usb_hid.devices)

# list of pins to use (skipping GP15 on Pico because it's funky)
pins = (
    board.GP0,
    board.GP1,
    board.GP2,
    board.GP3,
    board.GP4,
    board.GP5,
    board.GP6,
    board.GP7,
    board.GP8,
    board.GP9,
    board.GP10,
    board.GP11,
    board.GP12,
    board.GP13,
    board.GP14,
    board.GP16,
    board.GP17,
    board.GP18,
    board.GP19,
    board.GP20,
    board.GP21,
)

MEDIA = 1
KEY = 2

keymap = {
    (0): (KEY, (Keycode.GUI, Keycode.C)),# win + C
    (1): (KEY, (Keycode.GUI, Keycode.V)),# win +V
    (2): (KEY, [Keycode.THREE]),
    (3): (KEY, [Keycode.FOUR]),
    (4): (KEY, [Keycode.FIVE]),
    (5): (MEDIA, ConsumerControlCode.VOLUME_DECREMENT),
    (6): (MEDIA, ConsumerControlCode.VOLUME_INCREMENT),

    (7): (KEY, [Keycode.R]),
    (8): (KEY, [Keycode.G]),
    (9): (KEY, [Keycode.B]),
    (10): (KEY, [Keycode.UP_ARROW]),
    (11): (KEY, [Keycode.X]),  # plus key
    (12): (KEY, [Keycode.Y]),
    (13): (KEY, [Keycode.Z]),

    (14): (KEY, [Keycode.I]),
    (15): (KEY, [Keycode.O]),
    (16): (KEY, [Keycode.LEFT_ARROW]),
    (17): (KEY, [Keycode.DOWN_ARROW]),
    (18): (KEY, [Keycode.RIGHT_ARROW]),
    (19): (KEY, [Keycode.ALT]),
    (20): (KEY, [Keycode.U]),

}

switches = []
for i in range(len(pins)):
    switch = DigitalInOut(pins[i])
    switch.direction = Direction.INPUT
    switch.pull = Pull.UP
    switches.append(switch)


switch_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

while True:
    for button in range(21):
        if switch_state[button] == 0:
            if not switches[button].value:
                try:
                    if keymap[button][0] == KEY:
                        kbd.press(*keymap[button][1])
                        print(keymap[button][1])
                    else:
                        cc.send(keymap[button][1])
                        print(keymap[button][1])
                except ValueError:  # deals w six key limit
                    pass
                switch_state[button] = 1

        if switch_state[button] == 1:
            if switches[button].value:
                try:
                    if keymap[button][0] == KEY:
                        kbd.release(*keymap[button][1])

                except ValueError:
                    pass
                switch_state[button] = 0

    time.sleep(0.01)  # debounce


相关文章:

  • CentOS7.6安装Rabbitmq
  • 谁说文艺青年开花店必亏,我用3年时间挣了20万
  • 11种增加访问者在网站上平均停留时间的技巧
  • centos7.6 yum安装 elasticsearch
  • Android字母、数字版本、API级别对照表2022
  • 专利的分类
  • 删除链表的节点
  • Docker安装和基本配置
  • A40I工控主板(SBC-X40I)LED,铵键测试
  • 中国将从9月起实施严格的新跨境数据传输规则
  • 新唐NUC980使用记录:使用wpa_supplicant访问无线网络
  • 教师教室预约管理系统(SpringBoot,H-UI,Mysql)
  • Centos7五步安装Docker并解决docker官方镜像无法访问问题
  • flink技术总结待续
  • Mac - 安装Nginx
  • 深入了解以太坊
  • [nginx文档翻译系列] 控制nginx
  • 「面试题」如何实现一个圣杯布局?
  • angular2 简述
  • CentOS6 编译安装 redis-3.2.3
  • create-react-app做的留言板
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • Docker: 容器互访的三种方式
  • ES6之路之模块详解
  • Fabric架构演变之路
  • Java到底能干嘛?
  • js对象的深浅拷贝
  • Laravel Mix运行时关于es2015报错解决方案
  • mac修复ab及siege安装
  • Odoo domain写法及运用
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • rabbitmq延迟消息示例
  • Vue--数据传输
  • 简析gRPC client 连接管理
  • 经典排序算法及其 Java 实现
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 微信开放平台全网发布【失败】的几点排查方法
  • 为什么要用IPython/Jupyter?
  • 学习使用ExpressJS 4.0中的新Router
  • 一文看透浏览器架构
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • ​【数据结构与算法】冒泡排序:简单易懂的排序算法解析
  • ​比特币大跌的 2 个原因
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • #我与虚拟机的故事#连载20:周志明虚拟机第 3 版:到底值不值得买?
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (LeetCode) T14. Longest Common Prefix
  • (补充):java各种进制、原码、反码、补码和文本、图像、音频在计算机中的存储方式
  • (二刷)代码随想录第16天|104.二叉树的最大深度 559.n叉树的最大深度● 111.二叉树的最小深度● 222.完全二叉树的节点个数
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (区间dp) (经典例题) 石子合并
  • (实战)静默dbca安装创建数据库 --参数说明+举例
  • (转)项目管理杂谈-我所期望的新人