From f362e178abc9928917d0d49469e33a95280802d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A1=A9=E5=85=A5=E5=AD=94=E7=AB=A0?= Date: Wed, 27 Feb 2019 11:57:45 +0800 Subject: [PATCH] update api en ja --- docs/en/api.md | 8 +- docs/en/api/button.md | 129 +++++++++++++++++++ docs/en/api/mpu9250.md | 259 +++++++++++++++++++++++++++++++++++++++ docs/en/api/system.md | 130 ++++++++++++++++++++ docs/en/api/tf.md | 62 ++++++++++ docs/ja/api.md | 8 +- docs/ja/api/button.md | 129 +++++++++++++++++++ docs/ja/api/lcd.md | 62 +++++----- docs/ja/api/mpu9250.md | 259 +++++++++++++++++++++++++++++++++++++++ docs/ja/api/speaker.md | 8 +- docs/ja/api/system.md | 130 ++++++++++++++++++++ docs/ja/api/tf.md | 62 ++++++++++ docs/ja/sidebar.md | 2 +- docs/sidebar.md | 2 +- docs/zh_CN/api/button.md | 94 ++++++-------- 15 files changed, 1245 insertions(+), 99 deletions(-) create mode 100644 docs/en/api/button.md create mode 100644 docs/en/api/mpu9250.md create mode 100644 docs/en/api/system.md create mode 100644 docs/en/api/tf.md create mode 100644 docs/ja/api/button.md create mode 100644 docs/ja/api/mpu9250.md create mode 100644 docs/ja/api/system.md create mode 100644 docs/ja/api/tf.md diff --git a/docs/en/api.md b/docs/en/api.md index 1fbeb78e..68a8f758 100644 --- a/docs/en/api.md +++ b/docs/en/api.md @@ -1,8 +1,12 @@ -# API Reference {docsify-ignore-all} +# Arduino API {docsify-ignore-all} + +## M5Core ||| |:---:|:---:| -|**[LCD](en/api/lcd)** | **[Speaker](en/api/speaker)** | +|**[System](en/api/system)** | **[Speaker](en/api/speaker)** | +|**[LCD](en/api/lcd)** | **[ボタン](en/api/button)** | +|**[IMU sensor(MPU9250)](en/api/mpu9250)** | **[TF Card](en/api/tf)** | + +**功能:初始化 MPU6500 芯片。** + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + IMU.initMPU9250(); + byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); + Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX); +} +``` + +## initAK8963() + +**函数原型:** + +void initAK8963(float * destination); + + + +**功能:初始化 AK8963 芯片。** + +| 参数 | 描述 | +| --- | --- | +| destination | 出厂预设于 AK8963 芯片内缺省值的数组指针 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + // "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() + +**函数原型:** + +void calibrateMPU9250(float * gyroBias, float * accelBias); + + + +**功能:初始化芯片后,执行该函数以读取静止状态下的陀螺仪和加速度值偏移,并保存到各自的偏移寄存器。** + +| 参数 | 描述 | +| --- | --- | +| gyroBias | 保存静止下陀螺仪值的数组的地址 | +| accelBias | 保存静止下加速度值的数组的地址 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + 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() + +**函数原型:** + +uint8_t readByte(uint8_t address, uint8_t subAddress); + + + +**功能:读取指定寄存器内数据。返回值:寄存器值,长度为8 位。** + +| 参数 | 描述 | +| --- | --- | +| address | 芯片 (MPU9250/AK8963)地址 | +| subAddress | 芯片内寄存器地址 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); + Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX); +} +``` + +## readGyroData() + +**函数原型:** + +void readGyroData(int16_t * destination); + + + +**功能:读取三个方向的陀螺仪值。** + +| 参数 | 描述 | +| --- | --- | +| destination | 保存陀螺仪值的数组的地址 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + 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() + +**函数原型:** + +void readAccelData(int16_t * destination); + + + +**功能:读取三个方向的加速度值。** + +| 参数 | 描述 | +| --- | --- | +| destination | 保存加速度值的数组的地址 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + 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 "); + } +} +``` \ No newline at end of file diff --git a/docs/en/api/system.md b/docs/en/api/system.md new file mode 100644 index 00000000..487c7967 --- /dev/null +++ b/docs/en/api/system.md @@ -0,0 +1,130 @@ +# 系统函数 + +## begin() + +**函数原型:** + +void begin(bool LCDEnable=true, bool SDEnable=true, bool SerialEnable=true); + + + +**功能:清串口缓冲区,设置串口波特率为 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 + +void setup(){ + M5.begin(); +} +``` + +## update() + +**函数原型:** + +void update(); + + + +**功能:读取按键 A, B, C 的状态。** + +**函数实现** +```arduino +void M5Stack::update() { + + //Button update + BtnA.read(); + BtnB.read(); + BtnC.read(); + + //Speaker update + Speaker.update(); +} +``` + +**例程** +```arduino +#include + +void setup(){ + M5.begin(); +} + +void loop(){ + M5.update(); +} +``` + +## powerOFF() + +**函数原型:** + +void powerOFF(); + + + +**功能:系统进入深度睡眠状态。** + +**函数实现** +```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 + +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(); +} +``` \ No newline at end of file diff --git a/docs/en/api/tf.md b/docs/en/api/tf.md new file mode 100644 index 00000000..37292231 --- /dev/null +++ b/docs/en/api/tf.md @@ -0,0 +1,62 @@ +# TF 卡 + +## begin() + +**函数原型:** + +begin(cspin); + +**功能:TF 卡初始化。** + +| 参数 | 描述 | +| --- | --- | +| cspin | TF 的片选引脚 (默认是 SPI 的 SS 引脚),可选 | + +**例程** +```arduino +#include + +M5.begin(); + +SD.begin(); +``` + +## open() + +**函数原型:** + +File open(const char *filepath, uint8_t mode; + +**功能:以指定模式打开指定文件。返回值:文件句柄。** + +| 参数 | 描述 | +| --- | --- | +| filepath | 文件路径 | +| mode | 打开模式 | + +**例程** +```arduino +/* + 提前通过 PC 将 datalog.txt 文件拷贝到 TF 卡内 +*/ +#include + +void setup(){ + M5.begin(); + Serial.begin(115200); + if (!SD.begin(chipSelect)) { + Serial.println("Card failed, or not present"); + while(1); + } + Serial.println("card initialized."); + File dataFile = SD.open("/datalog.txt", FILE_WRITE); + if (dataFile) + Serial.println("open datalog.txt successfully"); + else + Serial.println("error opening datalog.txt"); +} + +void loop(){ + +} +``` diff --git a/docs/ja/api.md b/docs/ja/api.md index 9572cd87..8b477940 100644 --- a/docs/ja/api.md +++ b/docs/ja/api.md @@ -1,5 +1,9 @@ -# API リファレンス {docsify-ignore-all} +# Arduino API {docsify-ignore-all} + +## M5Core ||| |:---:|:---:| -|**[LCD](ja/api/lcd)** | **[Speaker](ja/api/speaker)** | +|**[システム](ja/api/system)** | **[スピーカー](ja/api/speaker)** | +|**[LCD 画面表示](ja/api/lcd)** | **[ボタン](ja/api/button)** | +|**[IMUセンサー(MPU9250)](ja/api/mpu9250)** | **[TFカード(SDカード)](ja/api/tf)** | \ No newline at end of file diff --git a/docs/ja/api/button.md b/docs/ja/api/button.md new file mode 100644 index 00000000..8beb0b69 --- /dev/null +++ b/docs/ja/api/button.md @@ -0,0 +1,129 @@ +# ボタン + +## read() + +**説明:** + +この関数はボタンの状態を直接読み出します。 1: pressed, 0: released. + +**構文:** + +```arduino +uint8_t read(); +``` + +**使用例:** + +```arduino +#include + +void setup() { + M5.begin(); +} + +void loop() { + M5.Lcd.setCursor(0, 0); + M5.Lcd.print("Button A Status: "); + M5.Lcd.println(M5.BtnA.read()); +} +``` + +## isPressed() + +**説明:** + +この関数は最後にM5.Button.read()を呼び出した時のボタンの状態を読み出します。1: pressed, 0: released. + +**構文:** + +```arduino +uint8_t isPressed(); +``` + +**使用例:** + +```arduino +#include + +void setup() { + M5.begin(); +} + +void loop() { + M5.update(); // update()を呼び出す必要があります。 + M5.Lcd.setCursor(0, 0); + if (M5.BtnA.isPressed()) { + M5.Lcd.printf("A button is pressed."); + } else { + M5.Lcd.printf("A button is released."); + } +} +``` + +## wasPressed() + +**構文:** + +```arduino +uint8_t wasPressed(); +``` + +**説明:** + +この関数はボタンが押される度に一度だけ1を返します。 1: pressed, 0: released. + +**使用例:** + +```arduino +#include + +void setup() { + M5.begin(); +} + +void loop() { + M5.update(); + M5.Lcd.clear(); + M5.Lcd.setCursor(0, 0); + if (M5.BtnA.wasPressed()) { + M5.Lcd.printf("Button A was pressed."); + delay(1000); + } +} +``` + +## pressedFor() + +**構文:** + +```arduino +uint8_t pressedFor(uint32_t ms); +``` + +**説明:** + +この関数は引数で指定した時間以上ボタンが押し続けられたら1を返します。 1: pressed, 0: released. + +| argument | 説明 | type | +| --- | --- | -- | +| ms | pressing time (ms) | uint32_t | + +**使用例:** + +```arduino +#include + +void setup() { + M5.begin(); +} + +void loop() { + M5.update(); + M5.Lcd.clear(); + M5.Lcd.setCursor(0, 0); + if (M5.BtnA.pressedFor(2000)) { + M5.Lcd.printf("Button A was pressed for more than 2 seconds."); + delay(1000); + } +} +``` diff --git a/docs/ja/api/lcd.md b/docs/ja/api/lcd.md index 6c0c27e9..8d1297ad 100644 --- a/docs/ja/api/lcd.md +++ b/docs/ja/api/lcd.md @@ -8,7 +8,7 @@ 引数で指定した色で画面を塗りつぶします。 -**構文:** +**構文:** ```arduino fillScreen(uint16_t color); @@ -18,7 +18,7 @@ fillScreen(uint16_t color); | --- | --- | -- | | color | 塗りつぶしの色 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -33,7 +33,7 @@ M5.Lcd.fillScreen(RED); 文字の色や文字の背景色を引数で指定した色に変更します。 -**構文:** +**構文:** ```arduino setTextColor(uint16_t color, [uint16_t backgroundcolor]); @@ -44,7 +44,7 @@ setTextColor(uint16_t color, [uint16_t backgroundcolor]); | color | テキストの色 | uint16_t | | backgroundcolor| テキストの背景色。省略可能。 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -55,11 +55,11 @@ M5.Lcd.setTextColor(RED); ## setCursor() -**機能:** +**機能:** カーソルの位置を設定します。 -**構文:** +**構文:** ```arduino setCursor(uint16_t x, uint16_t y); @@ -70,7 +70,7 @@ setCursor(uint16_t x, uint16_t y); | x | x位置 | uint16_t | | y | y位置 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -82,11 +82,11 @@ M5.Lcd.print("Hello"); ## drawPixel() -**機能:** +**機能:** 指定した位置に指定色のピクセルを描画します。 -**構文:** +**構文:** ```arduino drawPixel(int16_t x, int16_t y, [uint16_t color]); @@ -98,7 +98,7 @@ drawPixel(int16_t x, int16_t y, [uint16_t color]); | y | y位置 | int16_t | | color | ピクセルの色。省略可能。 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -109,11 +109,11 @@ M5.Lcd.drawPixel(22, 22, RED); ## drawLine() -**機能:** +**機能:** 指定した始点から終点まで指定色の直線を描画します。 -**構文:** +**構文:** ```arduino drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, [uint16_t color]); @@ -127,7 +127,7 @@ drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, [uint16_t color]); | y1 | 終点のy位置 | int16_t | | color | 線の色。省略可能。 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -138,11 +138,11 @@ M5.Lcd.drawLine(0, 0, 12, 12, WHITE); ## drawTriangle() -**機能:** +**機能:** 指定した3点を結ぶ三角形を指定色で描画します。 -**構文:** +**構文:** ```arduino drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, [uint16_t color]); @@ -158,7 +158,7 @@ drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t | y2 | 点2のy位置 | int16_t | | color | 線の色。省略可能。 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -169,11 +169,11 @@ M5.Lcd.drawTriangle(22, 22, 69, 98, 51, 22, RED); ## fillTriangle() -**機能:** +**機能:** 指定した3点を結ぶ三角形を指定色で塗りつぶして描画します。 -**構文:** +**構文:** ```arduino fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, [uint16_t color]); @@ -189,7 +189,7 @@ fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t | y2 | 点2のy位置 | int16_t | | color | 塗りつぶしの色。省略可能。 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -200,11 +200,11 @@ M5.Lcd.fillTriangle(22, 22, 69, 98, 51, 22, RED); ## drawRect() -**機能:** +**機能:** 指定の点から指定の幅と高さの四角形を指定色で描画します。 -**構文:** +**構文:** ```arduino drawRect(int16_t x, int16_t y, int16_t w, int16_t h, [uint16_t color]); @@ -218,7 +218,7 @@ drawRect(int16_t x, int16_t y, int16_t w, int16_t h, [uint16_t color]); | h | 四角形の高さ | int16_t | | color | 線の色。省略可能。 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -229,11 +229,11 @@ M5.Lcd.drawRect(180, 12, 122, 10, BLUE); ## fillRect() -**機能:** +**機能:** 指定の左上の点(x,y)と幅と高さの四角形を指定色で塗りつぶして描画します。 -**構文:** +**構文:** ```arduino fillRect(int16_t x, int16_t y, int16_t w, int16_t h, [uint16_t color]); @@ -247,7 +247,7 @@ fillRect(int16_t x, int16_t y, int16_t w, int16_t h, [uint16_t color]); | h | 四角形の高さ | int16_t | | color | 塗りつぶしの色。省略可能。 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -258,11 +258,11 @@ M5.Lcd.fillRect(180, 12, 122, 10, BLUE); ## drawRoundRect() -**機能:** +**機能:** 左上の点(x,y)と幅と高さを指定して、かど丸の四角形を描画します。 -**構文:** +**構文:** ```arduino drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, [uint16_t color]); @@ -277,7 +277,7 @@ drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, [uint16_t c | r | コーナー半径 | int16_t | | color | 四角形の線の色。省略可能。 | uint16_t | -**使用例:** +**使用例:** ```arduino #include @@ -288,17 +288,17 @@ M5.Lcd.fillRoundRect(180, 70, 122, 10, 4, BLUE); ## print() -**機能:** +**機能:** 指定の文字列を描画します。 -**構文:** +**構文:** ```arduino print("表示する文字列"); ``` -**使用例:** +**使用例:** ```arduino #include diff --git a/docs/ja/api/mpu9250.md b/docs/ja/api/mpu9250.md new file mode 100644 index 00000000..e3590a54 --- /dev/null +++ b/docs/ja/api/mpu9250.md @@ -0,0 +1,259 @@ +# 姿态传感器 MPU9250 + +*姿态传感器 MPU9250 是一款多芯片模块,集成两个芯片,一个芯片是 MPU6500,由 3 轴陀螺仪和 3 轴加速度计组成,另外一个是 AK8963 是 3 轴磁力计。* + +## initMPU9250() + +**函数原型:** + +void initMPU9250(); + + + +**功能:初始化 MPU6500 芯片。** + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + IMU.initMPU9250(); + byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); + Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX); +} +``` + +## initAK8963() + +**函数原型:** + +void initAK8963(float * destination); + + + +**功能:初始化 AK8963 芯片。** + +| 参数 | 描述 | +| --- | --- | +| destination | 出厂预设于 AK8963 芯片内缺省值的数组指针 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + // "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() + +**函数原型:** + +void calibrateMPU9250(float * gyroBias, float * accelBias); + + + +**功能:初始化芯片后,执行该函数以读取静止状态下的陀螺仪和加速度值偏移,并保存到各自的偏移寄存器。** + +| 参数 | 描述 | +| --- | --- | +| gyroBias | 保存静止下陀螺仪值的数组的地址 | +| accelBias | 保存静止下加速度值的数组的地址 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + 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() + +**函数原型:** + +uint8_t readByte(uint8_t address, uint8_t subAddress); + + + +**功能:读取指定寄存器内数据。返回值:寄存器值,长度为8 位。** + +| 参数 | 描述 | +| --- | --- | +| address | 芯片 (MPU9250/AK8963)地址 | +| subAddress | 芯片内寄存器地址 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + byte c = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); + Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX); +} +``` + +## readGyroData() + +**函数原型:** + +void readGyroData(int16_t * destination); + + + +**功能:读取三个方向的陀螺仪值。** + +| 参数 | 描述 | +| --- | --- | +| destination | 保存陀螺仪值的数组的地址 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + 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() + +**函数原型:** + +void readAccelData(int16_t * destination); + + + +**功能:读取三个方向的加速度值。** + +| 参数 | 描述 | +| --- | --- | +| destination | 保存加速度值的数组的地址 | + +**例程:** +```arduino +#include +#include "utility/MPU9250.h" + +MPU9250 IMU; // new a MPU9250 object + +void setup(){ + M5.begin(); + Serial.begin(115200); + + 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 "); + } +} +``` \ No newline at end of file diff --git a/docs/ja/api/speaker.md b/docs/ja/api/speaker.md index b05d461f..cf698fd4 100644 --- a/docs/ja/api/speaker.md +++ b/docs/ja/api/speaker.md @@ -1,12 +1,12 @@ -# Speaker +# スピーカー ## tone() -**機能:** +**機能:** 指定した音と長さでスピーカーを鳴らします。 -**函数原型:** +**構文:** ```arduino tone(uint16_t freq, [uint32_t duration]); @@ -17,7 +17,7 @@ tone(uint16_t freq, [uint32_t duration]); | freq | 音の高さ(周波数Hz) | uint16_t | | duration | 鳴動時間 (ms)、 省略可能。 | uint16_t | -**使用例** +**使用例:** ```arduino #include diff --git a/docs/ja/api/system.md b/docs/ja/api/system.md new file mode 100644 index 00000000..487c7967 --- /dev/null +++ b/docs/ja/api/system.md @@ -0,0 +1,130 @@ +# 系统函数 + +## begin() + +**函数原型:** + +void begin(bool LCDEnable=true, bool SDEnable=true, bool SerialEnable=true); + + + +**功能:清串口缓冲区,设置串口波特率为 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 + +void setup(){ + M5.begin(); +} +``` + +## update() + +**函数原型:** + +void update(); + + + +**功能:读取按键 A, B, C 的状态。** + +**函数实现** +```arduino +void M5Stack::update() { + + //Button update + BtnA.read(); + BtnB.read(); + BtnC.read(); + + //Speaker update + Speaker.update(); +} +``` + +**例程** +```arduino +#include + +void setup(){ + M5.begin(); +} + +void loop(){ + M5.update(); +} +``` + +## powerOFF() + +**函数原型:** + +void powerOFF(); + + + +**功能:系统进入深度睡眠状态。** + +**函数实现** +```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 + +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(); +} +``` \ No newline at end of file diff --git a/docs/ja/api/tf.md b/docs/ja/api/tf.md new file mode 100644 index 00000000..37292231 --- /dev/null +++ b/docs/ja/api/tf.md @@ -0,0 +1,62 @@ +# TF 卡 + +## begin() + +**函数原型:** + +begin(cspin); + +**功能:TF 卡初始化。** + +| 参数 | 描述 | +| --- | --- | +| cspin | TF 的片选引脚 (默认是 SPI 的 SS 引脚),可选 | + +**例程** +```arduino +#include + +M5.begin(); + +SD.begin(); +``` + +## open() + +**函数原型:** + +File open(const char *filepath, uint8_t mode; + +**功能:以指定模式打开指定文件。返回值:文件句柄。** + +| 参数 | 描述 | +| --- | --- | +| filepath | 文件路径 | +| mode | 打开模式 | + +**例程** +```arduino +/* + 提前通过 PC 将 datalog.txt 文件拷贝到 TF 卡内 +*/ +#include + +void setup(){ + M5.begin(); + Serial.begin(115200); + if (!SD.begin(chipSelect)) { + Serial.println("Card failed, or not present"); + while(1); + } + Serial.println("card initialized."); + File dataFile = SD.open("/datalog.txt", FILE_WRITE); + if (dataFile) + Serial.println("open datalog.txt successfully"); + else + Serial.println("error opening datalog.txt"); +} + +void loop(){ + +} +``` diff --git a/docs/ja/sidebar.md b/docs/ja/sidebar.md index 757f8af1..0cb4e370 100644 --- a/docs/ja/sidebar.md +++ b/docs/ja/sidebar.md @@ -1,5 +1,5 @@ - [製品ドキュメント](ja/) -- [API リファレンス](ja/api) +- [Arduino API](ja/api) - [M5Stack アプリケーション](ja/case) - [その他のドキュメント](ja/related_documents) - [よくある質問](ja/faq) diff --git a/docs/sidebar.md b/docs/sidebar.md index 4c8729dc..4aadbc99 100644 --- a/docs/sidebar.md +++ b/docs/sidebar.md @@ -1,5 +1,5 @@ - [Product Documents](en/) -- [API For Arduino](en/api) +- [Arduino API](en/api) - [M5Stack Cases](en/case) - [Related Documents](en/related_documents) - [FAQ](en/faq) diff --git a/docs/zh_CN/api/button.md b/docs/zh_CN/api/button.md index 5477a934..0fd28a59 100644 --- a/docs/zh_CN/api/button.md +++ b/docs/zh_CN/api/button.md @@ -6,23 +6,20 @@ read(); // for arduino - - **功能:返回按键状态。1:按下;0:松开。** **例程** ```arduino #include -void setup(){ - M5.begin(); - Serial.begin(115200); +void setup() { + M5.begin(); } -void loop(){ - Serial.print("Button A Status: "); - Serial.println(M5.BtnA.read()); - delay(200); +void loop() { + M5.Lcd.setCursor(0, 0); + M5.Lcd.print("Button A Status: "); + M5.Lcd.println(M5.BtnA.read()); } ``` @@ -32,24 +29,24 @@ void loop(){ isPressed(); // for arduino - - **功能:返回键值。如果指定按键奇数次数按下后,一直返回 1,偶数次数按下,一直返回 0。** **例程** ```arduino #include -void setup(){ - M5.begin(); - Serial.begin(115200); +void setup() { + M5.begin(); } -void loop(){ - if(M5.BtnA.isPressed()) { // for button A - Serial.printf("A\r\n"); - } - M5.update(); +void loop() { + M5.update(); // update()を呼び出す必要があります。 + M5.Lcd.setCursor(0, 0); + if (M5.BtnA.isPressed()) { + M5.Lcd.printf("A button is pressed."); + } else { + M5.Lcd.printf("A button is released."); + } } ``` @@ -59,36 +56,26 @@ void loop(){ wasPressed(); // for arduino - - **功能:返回键值。如果指定按键按下,则返回 1 一次,然后置位为0,否则一直返回 0。** **例程** ```arduino #include -void setup(){ - M5.begin(); - Serial.begin(115200); +void setup() { + M5.begin(); } -void loop(){ - if(M5.BtnA.wasPressed()) { // for button A - M5.Lcd.printf("A"); - Serial.printf("A"); - } - M5.update(); +void loop() { + M5.update(); + M5.Lcd.clear(); + M5.Lcd.setCursor(0, 0); + if (M5.BtnA.wasPressed()) { + M5.Lcd.printf("Button A was pressed."); + delay(1000); + } } ``` - - -* * * ## pressedFor() @@ -96,8 +83,6 @@ lcd.fillScreen(lcd.RED) pressedFor(uint32_t ms); // for arduino - - **功能:返回键值。如果指定按键按下超过指定时间之后,则返回 1,否则返回 0。** | 参数 | 描述 | @@ -108,24 +93,17 @@ lcd.fillScreen(lcd.RED) ```arduino #include -void setup(){ - M5.begin(); - Serial.begin(115200); +void setup() { + M5.begin(); } -void loop(){ - if(M5.BtnA.pressedFor(2000)) { // for button A - Serial.printf("A"); - } - M5.update(); +void loop() { + M5.update(); + M5.Lcd.clear(); + M5.Lcd.setCursor(0, 0); + if (M5.BtnA.pressedFor(2000)) { + M5.Lcd.printf("Button A was pressed for more than 2 seconds."); + delay(1000); + } } ``` - - - \ No newline at end of file