add API (button mpu9250 speaker system tf)

This commit is contained in:
LiHuashen
2019-02-26 17:56:32 +08:00
parent 70109e2b13
commit 2b5e7685e2
11 changed files with 473 additions and 31 deletions
+2
View File
@@ -71,3 +71,5 @@
* [m5stack-onscreen-keyboard](https://github.com/yellowelise/m5stack-onscreen-keyboard) - Full size qwerty keyboard for M5Stack
* [gui for m5stack by micropython](https://github.com/lifedebug/m5stack) - gui for m5stack by micropython
* [M5Stack-Tetris-using-RCWController](https://github.com/PartsandCircuits/M5Stack-Tetris-using-RCWController) - Play Tetris on an M5Stack using the RCWController iPhone app to control the action.
+1 -1
View File
@@ -1,5 +1,5 @@
- [Product Documents](en/)
- [API Reference](en/api)
- [API For Arduino](en/api)
- [M5Stack Cases](en/case)
- [Related Documents](en/related_documents)
- [FAQ](en/faq)
+14 -2
View File
@@ -1,8 +1,20 @@
# API参考 {docsify-ignore-all}
# Arduino API {docsify-ignore-all}
## M5Core
|||
|:---:|:---:|
|**[LCD](zh_CN/api/lcd)** | **[Speaker](zh_CN/api/speaker)** |
|**[System](zh_CN/api/system)** | **[喇叭](zh_CN/api/speaker)** |
|**[LCD 屏幕](zh_CN/api/lcd)** | **[按键](zh_CN/api/button)** |
|**[姿态传感器 (MPU9250)](zh_CN/api/mpu9250)** | **[TF 卡](zh_CN/api/tf)** |
<!-- ## M5stick
|||
|:---:|:---:|
|**[system](zh_CN/api/system)** | **[Speaker](zh_CN/api/speaker)** |
|**[OLED](zh_CN/api/oled)** | **[button](zh_CN/api/button)** |
|**[MPU9250](zh_CN/api/mpu9250)** | **[Microphone](zh_CN/api/mic)** | -->
<!-- |**[Button](zh_CN/api/button)** | **[MPU9250](zh_CN/api/mpu9250)** | -->
+28 -3
View File
@@ -1,6 +1,31 @@
# 按键
### isPressed()
## read()
**函数原型:**
<mark>read();</mark> // for arduino
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:返回按键状态。1:按下;0:松开。**
**例程**
```arduino
#include <M5Stack.h>
void setup(){
M5.begin();
}
void loop(){
Serial.print("Button A Status: ");
Serial.println(M5.BtnA.read());
delay(200);
}
```
## isPressed()
**函数原型:**
@@ -26,7 +51,7 @@ void loop(){
}
```
### wasPressed()
## wasPressed()
**函数原型:**
@@ -62,7 +87,7 @@ lcd.fillScreen(lcd.RED)
* * *
### pressedFor()
## pressedFor()
**函数原型:**
+15 -15
View File
@@ -2,7 +2,7 @@
*屏幕像素为 320x240,以屏幕左上角为原点 (0,0)*
### fillScreen()
## fillScreen()
**函数原型:**
@@ -34,7 +34,7 @@ lcd.fillScreen(lcd.RED)
* * *
<!-- ### <mark>lcd.setRotation(degree)</mark>
<!-- ## <mark>lcd.setRotation(degree)</mark>
**例程:**
```arduino
@@ -54,8 +54,8 @@ lcd.setRotation(90)
* * * -->
<!-- ### <mark>lcd.setColor(color [, background_color])</mark> -->
### setTextColor()
<!-- ## <mark>lcd.setColor(color [, background_color])</mark> -->
## setTextColor()
**函数原型:**
@@ -91,7 +91,7 @@ lcd.setTextColor(lcd.ORANGE, lcd.DARKCYAN)
* * *
### setCursor()
## setCursor()
**函数原型:**
@@ -120,7 +120,7 @@ lcd.drawPixel(22,22,lcd.RED)
* * *
### drawPixel()
## drawPixel()
**函数原型:**
@@ -152,7 +152,7 @@ lcd.drawPixel(22,22,lcd.RED)
* * *
### drawLine()
## drawLine()
**函数原型:**
@@ -184,7 +184,7 @@ lcd.drawLine(0,0,12,12,lcd.WHITE)
* * *
### drawTriangle()
## drawTriangle()
**函数原型:**
@@ -216,7 +216,7 @@ lcd.drawLine(0,0,12,12,lcd.WHITE)
* * *
### fillTriangle()
## fillTriangle()
**函数原型:**
@@ -248,7 +248,7 @@ lcd.drawLine(0,0,12,12,lcd.WHITE)
* * *
### drawRect()
## drawRect()
**函数原型:**
@@ -282,7 +282,7 @@ lcd.drawLine(0,0,12,12,lcd.WHITE)
* * *
### fillRect()
## fillRect()
**函数原型:**
@@ -317,7 +317,7 @@ lcd.drawLine(0,0,12,12,lcd.WHITE)
* * *
### drawRoundRect()
## drawRoundRect()
**函数原型:**
@@ -352,7 +352,7 @@ lcd.drawLine(0,0,12,12,lcd.WHITE)
* * *
### print()
## print()
**函数原型:**
@@ -380,7 +380,7 @@ lcd.drawLine(0,0,12,12,lcd.WHITE)
* * *
<!-- ### <mark>lcd.clear([color])</mark>
<!-- ## <mark>lcd.clear([color])</mark>
**例程:**
```python
@@ -392,7 +392,7 @@ lcd.clear()
<!-- * * * -->
### Usage {docsify-ignore}
## Usage {docsify-ignore}
```arduino
#include <M5Stack.h>
+253
View File
@@ -0,0 +1,253 @@
# 姿态传感器 MPU9250
*姿态传感器 MPU9250 是一款多芯片模块,集成两个芯片,一个芯片是 MPU6500,由 3 轴陀螺仪和 3 轴加速度计组成,另外一个是 AK8963 是 3 轴磁力计。*
## initMPU9250()
**函数原型:**
<mark>void initMPU9250();</mark>
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:初始化 MPU6500 芯片。**
**例程:**
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup(){
M5.begin();
IMU.initMPU9250();
byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
}
```
## initAK8963()
**函数原型:**
<mark>void initAK8963(float * destination);</mark>
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:初始化 AK8963 芯片。**
| 参数 | 描述 |
| --- | --- |
| destination | 出厂预设于 AK8963 芯片内缺省值的数组指针 |
**例程:**
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup(){
M5.begin();
// "magCalibration[3] = {0};" was defined at file "MPU9250.h"
IMU.initAK8963(IMU.magCalibration);
Serial.println("AK8963 initialized for active data mode....");
if (Serial)
{
// Serial.println("Calibration values: ");
Serial.print("X-Axis sensitivity adjustment value ");
Serial.println(IMU.magCalibration[0], 2);
Serial.print("Y-Axis sensitivity adjustment value ");
Serial.println(IMU.magCalibration[1], 2);
Serial.print("Z-Axis sensitivity adjustment value ");
Serial.println(IMU.magCalibration[2], 2);
}
}
```
## calibrateMPU9250()
**函数原型:**
<mark>void calibrateMPU9250(float * gyroBias, float * accelBias);</mark>
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:初始化芯片后,执行该函数以读取静止状态下的陀螺仪和加速度值偏移,并保存到各自的偏移寄存器。**
| 参数 | 描述 |
| --- | --- |
| gyroBias | 保存静止下陀螺仪值的数组的地址 |
| accelBias | 保存静止下加速度值的数组的地址 |
**例程:**
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup(){
M5.begin();
IMU.initMPU9250();
byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
// "gyroBias[3] = {0};" was defined at file "MPU9250.h"
// "accelBias[3] = {0};" was defined at file "MPU9250.h"
IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(0, 0); M5.Lcd.print("MPU9250 bias");
M5.Lcd.setCursor(0, 16); M5.Lcd.print(" x y z ");
M5.Lcd.setCursor(0, 32); M5.Lcd.print((int)(1000*IMU.accelBias[0]));
M5.Lcd.setCursor(32, 32); M5.Lcd.print((int)(1000*IMU.accelBias[1]));
M5.Lcd.setCursor(64, 32); M5.Lcd.print((int)(1000*IMU.accelBias[2]));
M5.Lcd.setCursor(96, 32); M5.Lcd.print("mg");
M5.Lcd.setCursor(0, 48); M5.Lcd.print(IMU.gyroBias[0], 1);
M5.Lcd.setCursor(32, 48); M5.Lcd.print(IMU.gyroBias[1], 1);
M5.Lcd.setCursor(64, 48); M5.Lcd.print(IMU.gyroBias[2], 1);
M5.Lcd.setCursor(96, 48); M5.Lcd.print("o/s");
}
void loop(){
}
```
## readByte()
**函数原型:**
<mark>uint8_t readByte(uint8_t address, uint8_t subAddress);</mark>
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:读取指定寄存器内数据。返回值:寄存器值,长度为8 位。**
| 参数 | 描述 |
| --- | --- |
| address | 芯片 (MPU9250/AK8963)地址 |
| subAddress | 芯片内寄存器地址 |
**例程:**
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup(){
M5.begin();
byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
}
```
## readGyroData()
**函数原型:**
<mark>void readGyroData(int16_t * destination);</mark>
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:读取三个方向的陀螺仪值。**
| 参数 | 描述 |
| --- | --- |
| destination | 保存陀螺仪值的数组的地址 |
**例程:**
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup(){
M5.begin();
IMU.initMPU9250();
byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
}
void loop(){
// If intPin goes high, all data registers have new data
// On interrupt, check if data ready interrupt
if (IMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
{
// "gyroCount[3] = {0};" was defined at file "MPU9250.h"
IMU.readGyroData(IMU.gyroCount); // Read the x/y/z adc values
IMU.getGres(); // get Gyro scales saved to "gRes"
IMU.gx = (float)IMU.gyroCount[0]*IMU.gRes;
IMU.gy = (float)IMU.gyroCount[1]*IMU.gRes;
IMU.gz = (float)IMU.gyroCount[2]*IMU.gRes;
Serial.print("X-gyro rate: "); Serial.print(IMU.gx, 3);
Serial.print(" degrees/sec ");
Serial.print("Y-gyro rate: "); Serial.print(IMU.gy, 3);
Serial.print(" degrees/sec ");
Serial.print("Z-gyro rate: "); Serial.print(IMU.gz, 3);
Serial.println(" degrees/sec");
}
}
```
## readAccelData()
**函数原型:**
<mark>void readAccelData(int16_t * destination);</mark>
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:读取三个方向的加速度值。**
| 参数 | 描述 |
| --- | --- |
| destination | 保存加速度值的数组的地址 |
**例程:**
```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"
MPU9250 IMU; // new a MPU9250 object
void setup(){
M5.begin();
IMU.initMPU9250();
byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
}
void loop(){
// If intPin goes high, all data registers have new data
// On interrupt, check if data ready interrupt
if (IMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
{
// "accelCount[3] = {0};" was defined at file "MPU9250.h"
IMU.readAccelData(IMU.accelCount);
IMU.getAres(); // get accelerometer scales saved to "aRes"
IMU.ax = (float)IMU.accelCount[0]*IMU.aRes; // - accelBias[0];
IMU.ay = (float)IMU.accelCount[1]*IMU.aRes; // - accelBias[1];
IMU.az = (float)IMU.accelCount[2]*IMU.aRes; // - accelBias[2];
Serial.print("X-acceleration: "); Serial.print(1000*IMU.ax);
Serial.print(" mg ");
Serial.print("Y-acceleration: "); Serial.print(1000*IMU.ay);
Serial.print(" mg ");
Serial.print("Z-acceleration: "); Serial.print(1000*IMU.az);
Serial.println(" mg ");
}
}
```
+1 -8
View File
@@ -14,7 +14,7 @@
M5.Speaker.tone(100);
``` -->
### tone()
## tone()
**函数原型:**
@@ -40,10 +40,3 @@ M5.begin();
M5.Speaker.tone(900, 1000);
```
<!-- ```python
# MicroPython
from m5stack import *
from m5ui import *
lcd.fillScreen(lcd.RED)
``` -->
+130
View File
@@ -0,0 +1,130 @@
# 系统函数
## begin()
**函数原型:**
<mark>void begin(bool LCDEnable=true, bool SDEnable=true, bool SerialEnable=true);</mark>
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:清串口缓冲区,设置串口波特率为 115200;初始化 LCD;初始化 SD 卡;设置按键 A 是睡眠唤醒按键。**
**函数实现**
```arduino
void M5Stack::begin(bool LCDEnable, bool SDEnable, bool SerialEnable) {
if (isInited) return;
else isInited = true;
if (SerialEnable) {
Serial.begin(115200);
Serial.flush();
delay(50);
Serial.print("M5Stack initializing...");
}
if (LCDEnable) {
Lcd.begin();
}
if (SDEnable) {
SD.begin(TFCARD_CS_PIN, SPI, 40000000);
}
setWakeupButton(BUTTON_A_PIN);
}
```
**例程**
```arduino
#include <M5Stack.h>
void setup(){
M5.begin();
}
```
## update()
**函数原型:**
<mark>void update();</mark>
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:读取按键 A, B, C 的状态。**
**函数实现**
```arduino
void M5Stack::update() {
//Button update
BtnA.read();
BtnB.read();
BtnC.read();
//Speaker update
Speaker.update();
}
```
**例程**
```arduino
#include <M5Stack.h>
void setup(){
M5.begin();
}
void loop(){
M5.update();
}
```
## powerOFF()
**函数原型:**
<mark>void powerOFF();</mark>
<!-- <mark>fillScreen(color)</mark> # for micropython -->
**功能:系统进入深度睡眠状态。**
**函数实现**
```arduino
void M5Stack::powerOFF() {
#ifdef M5STACK_FIRE
// Keep power keep boost on
setPowerBoostKeepOn(true);
#endif
// power off the Lcd
Lcd.setBrightness(0);
Lcd.sleep();
// ESP32 into deep sleep
esp_sleep_enable_ext0_wakeup((gpio_num_t)_wakeupPin , LOW);
while(digitalRead(_wakeupPin) == LOW) {
delay(10);
}
esp_deep_sleep_start();
}
```
**例程**
```arduino
#include <M5Stack.h>
void setup(){
M5.begin();
M5.Lcd.println("This is software power off demo");
M5.Lcd.println("Press the button A to power off.");
M5.setWakeupButton(BUTTON_A_PIN);
}
void loop() {
if(M5.BtnA.wasPressed()) {
M5.powerOFF();
}
M5.update();
}
```
+25
View File
@@ -0,0 +1,25 @@
# TF 卡
## begin()
**函数原型:**
<mark>begin();</mark>
**功能:喇叭响指定频率的声音持续指定时间。**
| 参数 | 描述 |
| --- | --- |
| freq | 声音频率 |
| duration | 持续时间 (单位:毫秒) |
<!-- *如果函数的 duration 值没给出,则使用当前的背景颜色。* -->
**例程**
```arduino
#include <M5Stack.h>
M5.begin();
M5.Speaker.tone(900, 1000);
```
+3 -1
View File
@@ -68,4 +68,6 @@
* [m5stack-onscreen-keyboard](https://github.com/yellowelise/m5stack-onscreen-keyboard) - Full size qwerty keyboard for M5Stack
* [gui for m5stack by micropython](https://github.com/lifedebug/m5stack) - gui for m5stack by micropython
* [gui for m5stack by micropython](https://github.com/lifedebug/m5stack) - gui for m5stack by micropython
* [M5Stack-Tetris-using-RCWController](https://github.com/PartsandCircuits/M5Stack-Tetris-using-RCWController) - Play Tetris on an M5Stack using the RCWController iPhone app to control the action.
+1 -1
View File
@@ -1,5 +1,5 @@
- [产品介绍](zh_CN/)
- [API 参考](zh_CN/api)
- [Arduino API](zh_CN/api)
- [M5Stack 案例](zh_CN/case)
- [相关文档](zh_CN/related_documents)
- [常见问题解答](zh_CN/faq)