diff --git a/docs/en/api.md b/docs/en/api.md index 1fbeb78e..fae0ae40 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)** | **[Button](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..d7881e83 --- /dev/null +++ b/docs/en/api/system.md @@ -0,0 +1,142 @@ +# System + +## begin() + +**Syntax:** + +```arudino +void begin(bool LCDEnable=true, bool SDEnable=true, bool SerialEnable=true); +``` + +**Description:** + +This function sets enable/disable LCD, TF Card and Serial port. + +**Definition:** + +```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); +} +``` + +**Sample:** + +```arduino +#include + +void setup() { + M5.begin(); +} +``` + +## update() + +**Syntax:** + +```arduino +void update(); +``` + +**Description:** + +This function reads The State of Button A and B and C. + +**Definition:** + +```arduino +void M5Stack::update() { + + //Button update + BtnA.read(); + BtnB.read(); + BtnC.read(); + + //Speaker update + Speaker.update(); +} +``` + +**Sample:** + +```arduino +#include + +void setup() { + M5.begin(); +} + +void loop() { + M5.update(); +} +``` + +## powerOFF() + +**Syntax:** + +```arduino +void powerOFF(); +``` + +**Description:** + +This function turns off the power of M5. + +**Definition:** + +```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(); +} +``` + +**Sample:** + +```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() { + M5.update(); + if (M5.BtnA.wasPressed()) { + M5.powerOFF(); + } +} +``` \ 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..0f9acb1d --- /dev/null +++ b/docs/en/api/tf.md @@ -0,0 +1,72 @@ +# TF card + +## begin() + +**Syntax:** + +```arduino +boolean begin(uint8_t cspin); +``` + +**Description:** + +This function initialize TF card. + +| Argument | Description | Type | +| --- | --- | -- | +| cspin | chip select line (defaults SS line of SPI bus) | uint8_t | + +**Sample:** + +```arduino +#include + +void setup() { + SD.begin(); +} +``` + +## open() + +**Syntax:** + +```arduino +File open(const char *filename, uint8_t mode = FILE_READ); +``` + +**Description:** + +This function open the file. + +| Argument | Description | Type | +| --- | --- | -- | +| filepath | path to file | const char * | +| mode | read / write / rw (optional) | uint8_t | + +**Sample:** + +```arduino +/* +display contents of hello.txt in TF card to M5 screen. +*/ +#include + +void setup() { + M5.begin(); + if (!SD.begin()) { + M5.Lcd.println("Card failed, or not present"); + while (1); + } + Serial.println("TF card initialized."); + File f = SD.open("/hello.txt", FILE_READ); + if (f) { + M5.Lcd.print(f.read()); + f.close(); + } else { + M5.Lcd.println("open error hello.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..8a7c4356 --- /dev/null +++ b/docs/ja/api/system.md @@ -0,0 +1,142 @@ +# システム + +## begin() + +**説明:** + +LCD、TFカード、シリアルポートの有効化/無効化を設定します。 + +**構文:** + +```arudino +void begin(bool LCDEnable=true, bool SDEnable=true, bool SerialEnable=true); +``` + +**定義:** + +```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() + +**説明:** + + ボタン A / B / C の読み取り状態を更新します。 + +**構文:** + +```arduino +void update(); +``` + +**定義:** + +```arduino +void M5Stack::update() { + + //ボタンアップデート + BtnA.read(); + BtnB.read(); + BtnC.read(); + + //スピーカーアップデート + Speaker.update(); +} +``` + +**使用例:** + +```arduino +#include + +void setup() { + M5.begin(); +} + +void loop() { + M5.update(); +} +``` + +## powerOFF() + +**構文:** + +**説明:** + +M5の電源をオフします。 + +```arduino +void powerOFF(); +``` + +**定義:** + +```arduino +void M5Stack::powerOFF() { + +#ifdef M5STACK_FIRE + // 電源ブーストオン + setPowerBoostKeepOn(true); +#endif + + // 画面オフ + Lcd.setBrightness(0); + Lcd.sleep(); + + // ESP32をディープスリープモードへ移行 + 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() { + M5.update(); + if (M5.BtnA.wasPressed()) { + M5.powerOFF(); + } +} +``` \ 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..ce8b55ba --- /dev/null +++ b/docs/ja/api/tf.md @@ -0,0 +1,72 @@ +# TF card + +## begin() + +**説明:** + +TFカード機能が使用可能になります。 + +**構文:** + +```arduino +boolean begin(uint8_t cspin); +``` + +| 引数 | 説明 | 型 | +| --- | --- | -- | +| cspin | chip select line (defaults SS line of SPI bus) | uint8_t | + +**使用例:** + +```arduino +#include + +void setup() { + SD.begin(); +} +``` + +## open() + +**説明:** + +ファイルを開きます。 + +**構文:** + +```arduino +File open(const char *filename, uint8_t mode = FILE_READ); +``` + +| 引数 | 説明 | 型 | +| --- | --- | -- | +| filepath | path to file | const char * | +| mode | read / write / rw (optional) | uint8_t | + +**使用例:** + +```arduino +/* +M5にTFカードのhello.txtの中身を表示する。 +*/ +#include + +void setup() { + M5.begin(); + if (!SD.begin()) { + M5.Lcd.println("Card failed, or not present"); + while (1); + } + Serial.println("TF card initialized."); + File f = SD.open("/hello.txt", FILE_READ); + if (f) { + M5.Lcd.print(f.read()); + f.close(); + } else { + M5.Lcd.println("open error hello.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 diff --git a/docs/zh_CN/api/system.md b/docs/zh_CN/api/system.md index 487c7967..d3f1547c 100644 --- a/docs/zh_CN/api/system.md +++ b/docs/zh_CN/api/system.md @@ -13,21 +13,21 @@ **函数实现** ```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); + 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); } ``` @@ -35,8 +35,8 @@ void M5Stack::begin(bool LCDEnable, bool SDEnable, bool SerialEnable) { ```arduino #include -void setup(){ - M5.begin(); +void setup() { + M5.begin(); } ``` @@ -54,13 +54,13 @@ void setup(){ ```arduino void M5Stack::update() { - //Button update - BtnA.read(); - BtnB.read(); - BtnC.read(); + //Button update + BtnA.read(); + BtnB.read(); + BtnC.read(); - //Speaker update - Speaker.update(); + //Speaker update + Speaker.update(); } ``` @@ -68,12 +68,12 @@ void M5Stack::update() { ```arduino #include -void setup(){ - M5.begin(); +void setup() { + M5.begin(); } -void loop(){ - M5.update(); +void loop() { + M5.update(); } ``` @@ -91,22 +91,22 @@ void loop(){ ```arduino void M5Stack::powerOFF() { - #ifdef M5STACK_FIRE - // Keep power keep boost on - setPowerBoostKeepOn(true); - #endif +#ifdef M5STACK_FIRE + // Keep power keep boost on + setPowerBoostKeepOn(true); +#endif - // power off the Lcd - Lcd.setBrightness(0); - Lcd.sleep(); + // power off the Lcd + Lcd.setBrightness(0); + Lcd.sleep(); - // ESP32 into deep sleep - esp_sleep_enable_ext0_wakeup((gpio_num_t)_wakeupPin , LOW); + // ESP32 into deep sleep + esp_sleep_enable_ext0_wakeup((gpio_num_t)_wakeupPin , LOW); - while(digitalRead(_wakeupPin) == LOW) { - delay(10); - } - esp_deep_sleep_start(); + while (digitalRead(_wakeupPin) == LOW) { + delay(10); + } + esp_deep_sleep_start(); } ``` @@ -114,7 +114,7 @@ void M5Stack::powerOFF() { ```arduino #include -void setup(){ +void setup() { M5.begin(); M5.Lcd.println("This is software power off demo"); M5.Lcd.println("Press the button A to power off."); @@ -122,9 +122,9 @@ void setup(){ } void loop() { - if(M5.BtnA.wasPressed()) { + M5.update(); + if (M5.BtnA.wasPressed()) { M5.powerOFF(); } - M5.update(); } ``` \ No newline at end of file diff --git a/docs/zh_CN/api/tf.md b/docs/zh_CN/api/tf.md index 37292231..afdf04ac 100644 --- a/docs/zh_CN/api/tf.md +++ b/docs/zh_CN/api/tf.md @@ -16,9 +16,9 @@ ```arduino #include -M5.begin(); - -SD.begin(); +void setup() { + SD.begin(); +} ``` ## open() @@ -41,22 +41,22 @@ SD.begin(); */ #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 setup() { + M5.begin(); + if (!SD.begin()) { + M5.Lcd.println("Card failed, or not present"); + while (1); + } + Serial.println("TF card initialized."); + File f = SD.open("/hello.txt", FILE_READ); + if (f) { + M5.Lcd.print(f.read()); + f.close(); + } else { + M5.Lcd.println("open error hello.txt"); + } } -void loop(){ - +void loop() { } ```