PiicoDev. Download the PiicoDev Unified Library: PiicoDev_Unified.py (right-click, "save link as"). Download the device module: PiicoDev_Switch.py (right-click, "save link as"). This module is named "Switch" instead of "Button" because it could be used for other switching components too - not just buttons. Upload the files to your Pico. This process was covered in the Setup Thonny section. The PiicoDev Unified Library is responsible for communicating with PiicoDev hardware, and the device module contains functions for driving specific PiicoDev devices.{{widget type="Magento\Cms\Block\Widget\Block" template="widget/static_block/default.phtml" block_id="293" type_name="CMS Static Block"}}We will need these files to easily drive the PiicoDev Button: Save the following files to your preferred coding directory - In this tutorial, we save to My Documents > PiicoDev. Download the PiicoDev Unified Library: PiicoDev_Unified.py (right-click, "save link as"). Download the device module: PiicoDev_Switch.py (right-click, "save link as"). This module is named "Switch" instead of "Button" because it could be used for other switching components too - not just buttons. Upload the files to your Pico. This process was covered in the Setup Thonny section. The PiicoDev Unified Library is responsible for communicating with PiicoDev hardware, and the device module contains functions for driving specific PiicoDev devices.Check if the Button was / is pressedWe are ready to begin reading the state of the PiicoDev Button using two simple properties: .was_pressed and .is_pressed. Run the following example code to read the button using the .was_pressed property and observe the output. The program prints a zero to the shell unless a press is detected, then it prints a 1. Notice that holding the button down has no effect on the shell output?     
# Check if a PiicoDev Button was pressed

from PiicoDev_Switch import PiicoDev_Switch # Switch may be used for other types of PiicoDev Switch devices
from PiicoDev_Unified import sleep_ms
 
button = PiicoDev_Switch()   # Initialise the module

while True:
    if button.was_pressed: # was the button pressed since the last time we checked?
        print(1) # print a result that can be plotted
    else:
        print(0)
        
    sleep_ms(100)

.was_pressed only returns True if the button was pressed since the last time we checked. The program will print a 1 only on the first time we press the button - holding the button down has no effect. Remix Idea: now change .was_pressed to .is_pressed which returns True if the button is being pressed right now. You ought to observe a different result when the Button is held down. The following figure summarises the behaviour of .was_pressed and .is_pressed. Code RemixRaspberry Pi PicoRaspberry Pimicro:bitRemix: Flash Rate Selector Let's control something real now! This remix flashes the Pico's onboard LED. Pressing the button selects a new flash rate (from a list of delays). As we click the button the flashing becomes slower, until we hit the end of the list and wrap back to the start. This is a practical use for the .was_pressed property - even when a delay is quite long, the button press is still captured.
# Use a PiicoDev Button to control the flash-rate of RPi Pico's onboard LED
from machine import Pin
from PiicoDev_Switch import PiicoDev_Switch # Switch may be used for other types of PiicoDev Switch devices
from PiicoDev_Unified import sleep_ms
 
button = PiicoDev_Switch()   # Initialise the module
led = Pin('LED', Pin.OUT)    # Initialise the onboard LED

index = 0
delays = [100, 200, 500, 1000] # a pool of delay values

while True:
    if button.was_pressed:          # change the flash rate by
        index += 1                  # incrementing the index
        index = index % len(delays) # wrap the index back to zero when it goes out of bounds
        print("index: ",index)
        
    led.toggle()
    delay = delays[index] # select the current delay duration
    sleep_ms(delay)

Remix: Run a program at the touch of a button! Let's use the PiicoDev Button to trigger a system event. We can use the subprocess module to execute commands like we would in a terminal. This example will launch a new instance of the File Manager every time the button is pressed. The run() command accepts commands as a list, with the first item being the command (pcmanfm), and following items being the arguments (the directory to launch to manager at, /home/pi). Read more about subprocess in the subprocess docs.
# Use a PiicoDev Button to launch a program on the Raspberry Pi
# This can be used to launch any program or run a script.

from PiicoDev_Switch import PiicoDev_Switch
from PiicoDev_Unified import sleep_ms
import subprocess # subprocess is used for executing shell commands
 
button = PiicoDev_Switch()   # Initialise the module

while True:
    if button.was_pressed: # execute a command (open the File Manager at the user's home directory)
        p = subprocess.run(["pcmanfm", "/home/pi"])
    sleep_ms(100)
It should be noted this example assumes the current user is called pi. If you're using a different username, you'll have to change this directory as appropriate.Remix: Scroll through some images. Let's control something real now! This remix flashes the Pico's onboard LED. Pressing the button selects a new flash rate (from a list of delays). As we click the button the flashing becomes slower, until we hit the end of the list and wrap back to the start. This is a practical use for the .was_pressed property - even when a delay is quite long, the button press is still captured.
# Use a PiicoDev Button to scroll images on the micro:bit display
from microbit import display, Image
from PiicoDev_Switch import PiicoDev_Switch
from PiicoDev_Unified import sleep_ms
 
button = PiicoDev_Switch()   # Initialise the module

index = 0
images = [Image.HEART, Image.HAPPY, Image.SAD] # a pool of images to display

while True:
    if button.was_pressed:          # change the image by
        index += 1                  # incrementing the index
        if index >= len(images):    # wrap the index back to zero when it goes out of bounds
            index=0
        print("index: ",index)
        
    display.show( images[index] )   # Show the selected image
    
    sleep_ms(100)

Other Examples.press_count.ledMultiple Buttons.was_double_pressed.setI2Caddr()Tuning the button (debouncing)The .press_count property is a counter that returns the number of button presses since the switch was last read. This is useful when polling the switch slowly, where multiple press events may occur between polls. The following example will keep a running total of button-presses, even though the update rate is very slow (3 seconds). Try clicking the button rapidly and you ought to see the count remains accurate, even with a slow polling rate.
# Get an accurate press-count, even when the sample-rate is very slow (3 seconds)
# Try clicking the button multiple times between updates. You ought to see that
# every click is accounted for!

from PiicoDev_Switch import PiicoDev_Switch # Switch may be used for other types of PiicoDev Switch devices
from PiicoDev_Unified import sleep_ms
 
button = PiicoDev_Switch()   # Initialise the module

total_presses = 0

while True:
    total_presses += button.press_count # press_count resets to zero after being read
    print('Total Presses:', total_presses)
    sleep_ms(3000)

The power-LED on the PiicoDev Button turns on every time power is applied. However, you can still take control of this LED using the .led property. The following example toggles the LED every time the button is clicked. The .led property is both read/write capable: you can use it to read the current state of the LED, and set the desired state.
# Toggle the Button's "Power" LED on every click
from PiicoDev_Switch import PiicoDev_Switch
from PiicoDev_Unified import sleep_ms
 
button = PiicoDev_Switch()

while True:
    if button.was_pressed:
        button.led = not button.led
    sleep_ms(100)
It is possible to connect to more than one PiicoDev Button at the same time. The following example uses two PiicoDev Buttons to create a simple controller. One Button increments a counter while the other decrements. The incrementing-Button is left in the default state (ID switches OFF), and the decrementing-Button will have its ID #1 switch set to ON. Referring to the example code, we can see each Button is initialised with an id argument which encodes the positions of the ID switches (1=ON, 0=OFF). This example also makes use of .press_count to ensure that no clicks are ever missed.
# Use multiple PiicoDev Buttons to increment / decrement a counter

from PiicoDev_Switch import PiicoDev_Switch
from PiicoDev_Unified import sleep_ms
 
button_up   = PiicoDev_Switch(id=[0,0,0,0])   # Initialise the 1st module
button_down = PiicoDev_Switch(id=[1,0,0,0])   # Initialise the 2nd module

count = 0

while True:
    count +=   button_up.press_count
    count -= button_down.press_count
    
    print("Count",count)
    
    sleep_ms(1000)

The following table describes the actual I2C addresses that are selected: The PiicoDev Button can detect double presses too, though a single-press will still be detected for the first press. Detecting double presses is very similar to detecting single presses - use the .was_double_pressed attribute. The window for a double press (milliseconds) is tunable with the double_press_duration property.
from PiicoDev_Switch import PiicoDev_Switch
from PiicoDev_Unified import sleep_ms
button = PiicoDev_Switch(double_press_duration=400)
while True:
    print('Double Press:', button.was_double_pressed)
    sleep_ms(1000)
The PiicoDev Button can detect double presses too, though a single-press will still be detected for the first press. Detecting double presses is very similar to detecting single presses - use the .was_double_pressed attribute.Up to 16 PiicoDev Buttons may share an I2C bus by selecting unique addresses with the ID switch (see the Multiple Buttons example). For advanced users, the device I2C address may be set explicitly using the .setI2Caddr() method which accepts the new desired address. This allows setting any address in the valid I2C address space (0x08-0x77). The address update is immediate and the device will only respond to the new address for subsequent commands. To use a user-defined software address, the Button must have all ID switches OFF.
# Set a new (software) address
from PiicoDev_Switch import PiicoDev_Switch
from PiicoDev_Unified import sleep_ms
 
button = PiicoDev_Switch()   # Initialise the module

new_address = 0x70

print("Setting new address to {}".format(new_address))
button.setI2Caddr(new_address)
To reset the device to defaults, either use the .setI2Caddr() method to set the address back to 0x42 or perform the factory reset procedure: Remove power Select a hardware address by setting any of the ID switches ON Apply power Remove power Now, the device will respond to address 0x42 again when all ID switches are OFF.The PiicoDev Button features internal debouncing using an EMA filter which is pretuned by default to provide good results. The state of the button is polled rapidly, which updates the value of the moving average - the average increases when the button is closed, and decays when the button is open. A press is valid when the average is greater than some threshold. The EMA tuning parameter can be read/write with the .ema_parameter property which accepts 0-255 and maps to 0->1.0. The polling period (milliseconds) can be read/write with the .ema_period property.
from PiicoDev_Switch import PiicoDev_Switch
from PiicoDev_Unified import sleep_ms
 
button = PiicoDev_Switch(ema_parameter=73, ema_period=30)   # Initialise the module with 30ms poll period and EMA parameter = 73.0/255.0 => 0.286
ConclusionThere's more than meets the eye to the PiicoDev Button - a simple input device, yes - but capable and feature-rich. We can read the button status in a couple of ways, measure counts and connect many buttons together for more complex projects. The button really is the perfect, intuitive input device. If you make something cool using the PiicoDev Button we'd love to see it! Leave a comment below to show us your projects or if you have any questions about this material. - Happy Making!Resources PiicoDev Switch MicroPython code and Button Firmware Hardware Repository Schematic  "> 澳大利亚PiicoDev按钮|入门指南——指南 - 188金宝搏网

PiicoDev按钮|入门指南

更新2022年11月13日

按钮是一个无处不在的用户界面——当然,你看过他们无处不在!卑微的按钮通常是最快的方法为你的项目创建一个控制接口。本指南将帮助您开始使用一个PiicoDev®按钮——一个直观的输入设备,使您可以轻松地与您的项目。我们会穿过一些例子从按钮,读取混音示例代码做一些很酷的与我们的开发板。

下面开始——选择你dev.董事会。


硬件连接

跟随你需要:

你的Pico插入扩展板。确保它是安装在正确的方向-销0扩大董事会应毗邻Pico的USB连接器。

连接你的PiicoDev按钮的扩展板PiicoDev电缆

跟随你需要:

Mount适配器到π的GPIO。确保插在正确的方向——箭适配器将指向π的以太网连接器(π3以太网连接器和USB接口交换)。

你的PiicoDev按钮连接到适配器PiicoDev电缆。

跟随你需要:

微:插入到适配器时,确保微观上的按键:一点面对。

你的PiicoDev按钮连接到适配器PiicoDev电缆。

PiicoDev按钮特性四个ID开关——确保所有的开关都在继续之前。这将使用默认的设备地址。


设置Thonny

从上面的标签来选择你的开发板准备与首次Thonny编程。

如果你已经与PiicoDev编程模块,可能不需要遵循以下步骤。

让我们设置了脚本在Thonny覆盆子π皮科。

我们将安装Thonny, Pico配置和编写第一个脚本。跟随你需要:

内容

安装Thonny

如果你工作在一个覆盆子π4,你很幸运,Thonny预装。对于那些在另一个操作系统,在这里下载Thonny并运行安装程序。

一旦安装完成,Thonny运行。

设置Thonny

按住BOOTSEL按钮在你的“微小”和你的“微小”连接到你的电脑通过USB接口。

去Run >选择翻译,选择MicroPython(覆盆子π皮科)

这也是一个好主意来安装或更新固件。这将更新你的Pico MicroPython的最新版本,或安装MicroPython如果没有了。

select-interpreter

REPL接口(壳)

我们可以立即开始执行代码的REPL - shell选项卡中输入这个代码:打印(“Hello, World !”)

命令将被发送到你的“微小”,将执行命令和显示的消息

我们也可以控制的车载由执行下面的代码:

从机进口销领导=销(25日Pin.OUT) led.toggle ()

这段代码将切换了。如果你继续执行led.toggle () LED将不断改变的状态。

repl接口

上了

写一个脚本

创建一个新的脚本文件>新粘贴以下代码:

从机进口销进口睡眠时间了=销(25 Pin.OUT) n = 0而如此:led.toggle()打印(“13 x {} = {}”。格式(13 * n, n)) # 13乘以表打印n = n + 1睡眠(0.5)

保存脚本,系统将提示您保存到你的电脑或皮科。选择保存到文件main.py Pico和名称

回到REPL和按Ctrl + D(或使用停止/重启按钮)重启你的皮科。领导应该flash在以稳定的速度外壳应该开始印刷13的倍数。

脚本输出

结论

我们安装Thonny和上传脚本覆盆子πPico -如果你有任何问题随时开始下面的谈话,或打开一个话题在我们论坛——我们全职制造商和乐意帮助!

好消息!Thonny与覆盆子π预装操作系统。然而,与PiicoDev我们需要启用I2C通讯如下:

  • 权力在你的树莓π。
  • 打开首选项>覆盆子π配置,选择接口选项卡
  • 确保I2C启用

你只需要做这一步对你的第一个PiicoDev项目——从这里你可能不需要使用PiicoDev硬件时重复这一步骤。

让我们与脚本设置Thonny微:。我们会安装Thonny,为微观配置:,写我们的第一个脚本。

你只需要跟随微观:v2去装备

内容

安装Thonny

下载Thonny在这里并运行安装程序。

一旦安装完成,Thonny运行。

设置Thonny

连接你的微:V2你电脑的USB电缆。

connect-microbit-to-computer-with-usb-cable

打开Thonny,菜单栏中找到运行>选择翻译并选择MicroPython (BBC微:位)

这也是一个好主意安装或更新固件。这将更新你的微:MicroPython的最新版本,或安装MicroPython如果没有了。

select-microbit-interpreter-in-thonny

确保文件面板和绘图机通过选择他们是可见的查看>文件,视图>绘图机

REPL接口(壳)

单击红色的停止按钮重新启动对微MicroPython:如果有必要。的壳牌标签应该显示一块文本表明MicroPython运行:

thonny-shell-restarted

我们可以立即开始执行代码的REPL - shell选项卡中输入这个代码:打印(“Hello, World !”)

命令将被送到你的微:,将执行命令和显示的消息——好!

接下来,我们还可以控制车载扬声器通过执行以下代码:

从微钻头导入*导入音频audio.play (Sound.HAPPY)

这段代码将扮演一个快乐的语气从微观:钻头的演讲者!如果你的领导。如果你继续执行audio.play (Sound.HAPPY),会重复的语气。

microbit-thonny-repl-play-audio

写一个脚本

REPL非常适合测试一些新的命令和执行——短实验真正的权力是在脚本。

创建一个新的脚本文件>粘贴在下面的代码:

从微钻头导入*导入音频打印(“你好!”)多个= 1 #预置计数器,而真正的:如果button_a.was_pressed(): #得到下一个13的倍数结果=多个* 13 #计算结果打印(“13倍”+ str(多个)+ " " + str(结果))#打印乘法多=多+ 1 #增加多个如果button_b.was_pressed (): # display.show问好(Image.HAPPY) audio.play (Sound.HAPPY) display.clear睡眠()(10)# 10毫秒的延迟

保存脚本,系统将提示您保存到您的计算机或微:。选择保存到微:和名称的文件main.py

回到REPL,并按Ctrl + D重启你的微:。如果有错误,使用停止/重启按钮,然后按Ctrl + D。

现在,当我们按下一个按钮,Shell将打印下一个13的倍数,如果我们按B-button,微:给我们一个微笑,你好。

thonny-example-script-for-microbit

注意情节也显示出一些线,他们是按照数字打印声明!在我的例子中:

  • 蓝线是常数13日
  • 橙色的线是多个变量增加缓慢,
  • 红线是结果变量,从而增加很快。

有用的技巧

  • 你可以停止正在运行的脚本通过选择和按Ctrl + C Shell窗口。这是有用的,如果我们想更改文件(s)存储在微观上:。
  • 重启你的微:钻头(或其他MicroPython设备)通过选择Shell窗口,按Ctrl + D
  • 如果出现错误,你可以单击菜单栏中的红色停止按钮

上传和下载文件

我们一直使用文件存储直接在微观上:一些——如果你想保持一个复制在您的计算机上,右键单击main.py文件,选择“下载”选项。同样,你可以上传你的微代码:有些文件在您的计算机上单击右键并选择上传按钮。

download-script-from-microbit-to-computer

结论

我们安装我们微Thonny和上传我们的第一个脚本:。该脚本包括分支机构根据哪个按钮被按下,可以生成音频音调和执行基本的算术。现在我们可以编写脚本,将他们之间的微观:和计算机,或使用REPL测试代码交互。

如果你有任何问题随时开始下面的谈话或在我们打开一个话题论坛——我们全职制造商和乐意帮助!


下载/安装PiicoDev模块

与PiicoDev硬件工作,我们需要下载一些司机。驱动程序提供的所有函数轻松地与PiicoDev硬件连接和沟通。从上面的选项中选择你的开发板。

我们需要这些文件很容易驱动PiicoDev按钮:

  • 以下文件保存到您的首选编码目录——在本教程中,我们保存我的文档>PiicoDev
    • 下载PiicoDev统一的图书馆:PiicoDev_Unified.py(右键单击“链接另存为”)。
    • 下载设备模块:PiicoDev_Switch.py(右键单击“链接另存为”)。这个模块被命名为“开关”而不是“按钮”,因为它可以用于其他开关组件——不仅仅是按钮。
  • 上传文件到你的“微小”。这个过程是在设置Thonny部分。

PiicoDev统一的图书馆负责沟通与PiicoDev硬件,设备模块包含驾驶特定PiicoDev设备的功能。

现在我们将安装/升级Thonny PiicoDev python模块。这个模块包含所有的司机和所有当前PiicoDev硬件。即使你已经安装了PiicoDev模块之前,仍然值得升级到最新版本,如果一个是可用的。

首先,运行Thonny点击:

  1. π的开始菜单
  2. 编程
  3. Thonny IDE

接下来,打开包管理器。从内部Thonny点击工具>管理包

PyPI上输入“piicodev”,然后单击“搜索”

最后,安装PiicoDev。如果你已经安装了PiicoDev,相反,可能会有升级按钮升级到一个新版本。

安装了PiicoDev模块我们现在准备开始编程与PiicoDev硬件。

我们需要这些文件很容易驱动PiicoDev按钮:

  • 以下文件保存到您的首选编码目录——在本教程中,我们保存我的文档>PiicoDev
    • 下载PiicoDev统一的图书馆:PiicoDev_Unified.py(右键单击“链接另存为”)。
    • 下载设备模块:PiicoDev_Switch.py(右键单击“链接另存为”)。这个模块被命名为“开关”而不是“按钮”,因为它可以用于其他开关组件——不仅仅是按钮。
  • 上传文件到你的“微小”。这个过程是在设置Thonny部分。

PiicoDev统一的图书馆负责沟通与PiicoDev硬件,设备模块包含驾驶特定PiicoDev设备的功能。

检查如果按钮/

我们已经准备好开始阅读的状态PiicoDev按钮使用两个简单的属性:.was_pressed.is_pressed。运行下面的示例代码阅读使用的按钮.was_pressed属性,观察输出。程序输出一个0到shell除非检测到一个新闻,然后输出一个1。注意,压低了按钮对壳牌的输出没有影响吗?

#检查是否从PiicoDev_Switch进口PiicoDev_Switch # PiicoDev按钮被按下开关可以用于其他类型的PiicoDev开关设备从PiicoDev_Unified进口sleep_ms按钮= PiicoDev_Switch() #初始化模块,而事实:如果按钮。was_pressed: #以来按钮按我们最后一次检查?打印(1)#打印结果,可以绘制:打印(0)sleep_ms (100)

.was_pressed只返回True,如果按钮被按下自从我们最后一次检查。程序将打印1只在我们第一次按下按钮,按住这个按钮没有任何影响。

混音的想法:现在改变.was_pressed.is_pressed如果按钮被按下时,返回True现在。你应该观察不同的结果时,按钮按下举行。下图总结了行为的.was_pressed.is_pressed

不同read-types PiicoDev按钮。was_pressed将拒绝button-held事件。


代码混音

混音:闪电速度选择器

现在让我们控制真实的东西!这混音Pico的船上。按下按钮选择一个新的flash率(从延迟的列表)。随着我们单击按钮闪烁变得缓慢,直到我们点击列表和包装结束回到开始。这是一个实际使用的.was_pressed属性——即使延迟很长,按钮按下仍然捕获。

#用PiicoDev按钮控制零售物价指数的闪光速度Pico的车载机进口针从PiicoDev_Switch进口PiicoDev_Switch #开关可以用于其他类型的PiicoDev开关设备从PiicoDev_Unified进口sleep_ms按钮= PiicoDev_Switch() #初始化模块领导=销(‘领导’,Pin.OUT) #预置上领导指数= 0延迟=(100、200、500、1000)#池延迟值而真实的:如果按钮。was_pressed: #改变flash利率指数+ = 1 #递增指数指数=指数% len(延迟)#包装索引为0时的打印(“指数:指数)led.toggle()延迟=延迟(指数)#选择当前sleep_ms延迟时间(延迟)

混音:运行一个程序在触摸一个按钮!

让我们使用PiicoDev按钮触发系统事件。我们可以使用子流程模块执行命令,就像我们在一个终端。这个例子将推出的一个新实例文件管理器每次按钮被按下。run()命令接受命令列表,第一项是命令(pcmanfm)和项目后的参数(目录启动经理/home/pi)。

阅读更多关于子流程子流程文档

#使用PiicoDev按钮启动一个程序覆盆子π#这可以用来启动任何程序或运行一个脚本。从PiicoDev_Switch进口PiicoDev_Switch PiicoDev_Unified导入sleep_ms导入子流程#子流程用于执行shell命令按钮= PiicoDev_Switch() #初始化模块,而事实:如果按钮。was_pressed: #执行一个命令(打开文件管理器在用户的主目录)p = subprocess.run ([“pcmanfm”、“/ home /π”])sleep_ms (100)

应该注意这个例子假设当前用户称为π。如果您正在使用一个不同的用户名,你将不得不改变这个目录。

混音:滚动图像。

现在让我们控制真实的东西!这混音Pico的船上。按下按钮选择一个新的flash率(从延迟的列表)。随着我们单击按钮闪烁变得缓慢,直到我们点击列表和包装结束回到开始。这是一个实际使用的.was_pressed属性——即使延迟很长,按钮按下仍然捕获。

#使用微PiicoDev按钮滚动图片:显示从微钻头进口显示,图像从PiicoDev_Switch进口PiicoDev_Switch PiicoDev_Unified进口sleep_ms按钮= PiicoDev_Switch() #初始化模块指数= 0 =图像(图像。心,形象。快乐,形象。悲伤)#一个池的图片来显示尽管如此:如果按钮。was_pressed: #改变图像通过索引+ = 1 #递增指数如果指数> = len(图像):#包指数回零在超出范围指数= 0打印“指数:”指数显示。展示(图片(指数))#显示所选图像sleep_ms (100)

其他的例子

.press_count属性是一个计数器,它返回的数量自开关按钮按下最后一次阅读。当轮询切换慢这是有用的,在多个新闻事件之间可能发生的民意调查。

下面的例子将运行的按键,即使更新速度非常缓慢(3秒)。迅速尝试单击按钮,您应该看到数仍然准确,即使投票速度缓慢。

#得到一个准确的press-count,即使采样率非常缓慢(3秒)#之间的多次尝试单击此按钮更新。你应该看到#每次点击占!从PiicoDev_Switch进口PiicoDev_Switch #开关可以用于其他类型的PiicoDev开关设备从PiicoDev_Unified进口sleep_ms按钮= PiicoDev_Switch total_presses = 0() #初始化模块,而事实:total_presses + =按钮。press_count # press_count重置为零后读打印(“总按:”,total_presses) sleep_ms (3000)

上的功率led PiicoDev按钮打开每一次权力。然而,你仍然可以控制这个领导使用.led财产。下面的例子将切换导致每次点击的按钮。的.led房地产既是读/写能力:你可以用它来阅读的当前状态,并设置所需的状态。

#切换按钮的“权力”在每次点击从PiicoDev_Switch从PiicoDev_Unified进口进口PiicoDev_Switch sleep_ms按钮= PiicoDev_Switch(),而事实:如果按钮。was_pressed:按钮。领导=没有按钮。领导sleep_ms (100)

可以连接到多个PiicoDev按钮在同一时间。下面的例子使用了两个PiicoDev按钮来创建一个简单的控制器。一个按钮增加一个计数器,而另一个衰减。incrementing-Button留在默认状态(ID关闭),和decrementing-Button将其ID # 1开关设置为上。指的是示例代码,我们可以看到每个按钮是启动id参数的编码标识的位置开关(1 = 0 =)。这个示例还使用了.press_count确保没有点击都错过了。

#使用多个PiicoDev按钮从PiicoDev_Switch递增/递减计数器从PiicoDev_Unified进口进口PiicoDev_Switch sleep_ms button_up = PiicoDev_Switch (id =(0, 0, 0, 0)) #初始化第一模块button_down = PiicoDev_Switch (id =[1, 0, 0, 0) #初始化模块数= 0而真正2:+ = button_up计数。- = button_down press_count计数。press_count打印(“计数”,计数)sleep_ms (1000)

下表描述了实际I2C选定的地址:

PiicoDev按钮也可以检测按两倍,尽管single-press仍将检测到第一媒体。检测双按非常类似于检测单按——使用.was_double_pressed属性。

双机的窗口(毫秒)是可调的double_press_duration财产。

从进口PiicoDev_Switch PiicoDev_Switch PiicoDev_Unified进口sleep_ms按钮= PiicoDev_Switch (double_press_duration = 400),而真正的:打印(“双重新闻:”,button.was_double_pressed) sleep_ms (1000)

PiicoDev按钮也可以检测按两倍,尽管single-press仍将检测到第一媒体。检测双按非常类似于检测单按——使用.was_double_pressed属性。

16 PiicoDev按钮可以共享一个I2C总线通过选择独特的地址和ID开关(见多个按钮例子)。对于高级用户,设备I2C地址可能设置显式地使用.setI2Caddr ()方法接受所需的新地址。这允许设置有效的I2C地址中的任何地址空间(0 x08-0x77)。地址更新是直接和设备只会回复后续的新地址命令。使用一个用户定义的软件地址,关闭的按钮必须有所有ID。

#设置一个新的(软件)地址PiicoDev_Switch从PiicoDev_Unified进口进口PiicoDev_Switch sleep_ms按钮= PiicoDev_Switch() #初始化模块new_address = 0 x70打印(“新地址设置为{}”.format (new_address)) button.setI2Caddr (new_address)

重置设备的缺省值,要么使用.setI2Caddr()方法来设置地址回到0×或执行工厂复位过程:

  • 关断电源
  • 选择一个硬件地址通过设置开关上的ID
  • 应用能力
  • 关断电源

现在,该设备将再次回复地址0×当所有ID开关。

PiicoDev按钮功能使用一个内部消除抖动EMA过滤器这是预调默认情况下提供良好的结果。按钮的状态迅速调查,更新移动平均线的值,平均增加关闭按钮时,按钮时和衰变开放。新闻时有效平均大于某个阈值。

EMA调优参数可读/写的.ema_parameter财产接受0 - 255和映射到0 - > 1.0。轮询时间(毫秒)可读/写的.ema_period财产。

从PiicoDev_Switch进口PiicoDev_Switch PiicoDev_Unified进口sleep_ms按钮= PiicoDev_Switch (ema_parameter = 73, ema_period = 30) #初始化模块与30 ms调查时期和EMA参数= 73.0/255.0 = > 0.286

结论

有超越视觉PiicoDev按钮——一个简单的输入设备,是的——但是能干,功能丰富。我们可以阅读按钮状态在几个方面,测量计算和连接许多按钮在一起对于更复杂的项目。这个按钮是完美的,直观的输入设备。

如果您使用PiicoDev按钮做一些很酷的东西我们很想看到它!请在下面留下你的评论,告诉我们你的项目或如果你有任何问题关于这个材料。

——制造快乐!

有问题吗?问作者今天该指南!

请输入至少20个字符

你的评论将在我们的支持论坛上发表(自动)公开访问。188金宝搏bet安卓不输入私人信息,比如你的电话号码。

期待快速回复在营业时间,我们中的许多人周末入住。

评论


加载……
反馈

请继续,如果你想要离开反馈这些主题:

  • 网站功能/问题
  • 内容错误/改进
  • 丢失的产品/类别
  • 产品作业类别
  • 搜索结果的相关性

对于所有其他调查(订单状态、库存等),请联系我们的支持团队188金宝搏bet安卓快速的帮助。

注:单击continue和草稿邮件将打开编辑。如果你没有一个电子邮件客户端设备,然后通过聊天发送消息图标左下角的我们的网站。

Baidu
map