pdf-icon

Arduino入門

2. デバイス&サンプル

Core2 Buttons 按键

类名: BtnA / BtnB / BtnC

read()

機能です:

0: 離れている; 1: 押下中

原型関数です:

uint8_t read()

使用例です:

cpp
1 2 3 4 5 6 7 8 9 10 11
#include <M5Core2.h>
void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}
void loop() {
M5.Lcd.setCursor(0, 0);
M5.Lcd.printf("Button A Status: %d ",M5.BtnA.read()); //打印按键A按下的状态
}

lastChange()

機能です:

最後に状態が変化した時刻を返却

原型関数です:

uint32_t lastChange()

注意:
1.返却される時刻はM5Core初期化の瞬間から計測され、単位はミリ秒

按键の押下状態を返却: 按键が押下中の場合はtrueを返却; そうでない場合はfalseを返却

使用例です:

cpp
1 2 3 4 5 6 7 8 9 10 11 12
#include <M5Core2.h>
void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}
void loop() {
M5.update();
M5.Lcd.setCursor(0, 0);
M5.Lcd.printf("The last change at %d ms /n",M5.BtnA.lastChange()); //打印按键A最后一次状态变化的时间
}

Press

isPressed()

機能です:

指定時間以上押下中の按键の状態を返却: 按键が指定時間以上押下中の場合はtrueを返却; そうでない場合はfalseを返却

原型関数です:

uint8_t isPressed()

使用例です:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <M5Core2.h>
void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}
void loop() {
M5.update(); //需添加M5.update()才能读取到按键的状态,细节请见System
M5.Lcd.setCursor(0, 0);
if (M5.BtnA.isPressed()) { //如果按键按下
M5.Lcd.println("Button is Pressed.");
}else{
M5.Lcd.println("Button is Released.");
}
delay(20);
}

pressedFor()

機能です:

按键の押下状態を返却: 按键が押下中の場合は一度trueを返却、そうでない場合はfalseを返却

原型関数です:

uint8_t pressedFor(uint32_t ms)

パラメータです タイプです 記述します
ms uint32_t 按键押下の時間 (ミリ秒)

使用例です:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <M5Core2.h>
void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}
void loop() {
M5.update();
if (M5.BtnA.pressedFor(2000)) { //如果按键按下超过2秒
M5.Lcd.println("Button A was pressed for more than 2 seconds.");
delay(1000);
}
}

wasPressed()

機能です:

按键の離脱状態を返却: 按键が離脱した場合はtrueを返却; そうでない場合はfalseを返却

原型関数です:

uint8_t wasPressed()

使用例です:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <M5Core2.h>
void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}
void loop() {
M5.update();
if (M5.BtnA.wasPressed()) { //如果按键按下
M5.Lcd.println("Button is pressed.");
}
delay(20);
}

Released

isReleased()

機能です:

按键の離脱状態を返却: 按键が離脱した場合はtrueを返却; そうでない場合はfalseを返却

原型関数です:

uint8_t isPressed()

使用例です:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <M5Core2.h>
void setup() {
M5.begin();
}
void loop() {
M5.update(); //需添加M5.update()才能读取到按键的状态,细节请见System
if (M5.BtnA.isReleased()) { //如果按键释放
M5.Lcd.println("Button is released.");
}else{
M5.Lcd.println("Button is Pressed .");
}
delay(20);
}

releasedFor()

機能です:

指定時間以上離脱中の按键の状態を返却: 按键が指定時間以上離脱中の場合はtrueを返却; そうでない場合はfalseを返却

原型関数です:

uint8_t releasedFor(uint32_t ms);
パラメータです タイプです 記述します
ms uint32_t ボタンの解放時間(ミリ秒)です

使用例です:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <M5Core2.h>
void setup() {
M5.begin();
}
void loop() {
M5.update();
M5.Lcd.setCursor(0, 0);
if (M5.BtnA.releasedFor(2000)) { //如果按键释放超过2秒
M5.Lcd.println("Button A was released for more than 2 seconds.");
delay(1000);
}else{
M5.Lcd.println("Button A is pressed ");
}
}

wasReleased()

機能です:

一度だけ離脱した按键の状態を返却: 按键が離脱した場合は一度trueを返却、そうでない場合はfalseを返却

原型関数です:

uint8_t wasReleased()

使用例です:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <M5Core2.h>
void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}
void loop() {
M5.update();
if(M5.BtnA.wasReleased()) { //如果按键释放
M5.Lcd.println("Button is Released.");
}
delay(20);
}

wasReleasefor()

機能です:

指定時間以上押下して離脱した按键の状態を返却: 按键が指定時間以上押下し、離脱した場合は一度trueを返却、そうでない場合はfalseを返却

原型関数です:

uint8_t wasReleasefor(uint32_t ms)

パラメータです タイプです 記述します
ms uint32_t 按键押下の時間 (ミリ秒

使用例です:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <M5Core2.h>
void setup() {
M5.begin();
M5.Lcd.println("Please pressed Button A.");
}
void loop() {
M5.update();
if (M5.BtnA.wasReleasefor(3000)) { //如果按键A按下3s之后释放
M5.Lcd.println("OK");
}
}
On This Page