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.286ConclusionThere'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®按钮——一个直观的输入设备,使您可以轻松地与您的项目。我们会穿过一些例子从按钮,读取混音示例代码做一些很酷的与我们的开发板。
下面开始——选择你dev.董事会。
跟随你需要:
你的Pico插入扩展板。确保它是安装在正确的方向-销0扩大董事会应毗邻Pico的USB连接器。
连接你的PiicoDev按钮的扩展板PiicoDev电缆。
跟随你需要:
Mount适配器到π的GPIO。确保插在正确的方向——箭适配器将指向π的以太网连接器(π3以太网连接器和USB接口交换)。
你的PiicoDev按钮连接到适配器PiicoDev电缆。
跟随你需要:
微:插入到适配器时,确保微观上的按键:一点面对。
你的PiicoDev按钮连接到适配器PiicoDev电缆。
的PiicoDev按钮特性四个ID开关——确保所有的开关都在继续之前。这将使用默认的设备地址。
与PiicoDev硬件工作,我们需要下载一些司机。驱动程序提供的所有函数轻松地与PiicoDev硬件连接和沟通。从上面的选项中选择你的开发板。
我们需要这些文件很容易驱动PiicoDev按钮:
的PiicoDev统一的图书馆负责沟通与PiicoDev硬件,设备模块包含驾驶特定PiicoDev设备的功能。
我们需要这些文件很容易驱动PiicoDev按钮:
的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。
混音:闪电速度选择器
现在让我们控制真实的东西!这混音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×或执行工厂复位过程:
现在,该设备将再次回复地址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按钮做一些很酷的东西我们很想看到它!请在下面留下你的评论,告诉我们你的项目或如果你有任何问题关于这个材料。
——制造快乐!