diff --git a/SampleSrc/accgyro.ino b/SampleSrc/accgyro.ino index f0955b1..4963b47 100644 --- a/SampleSrc/accgyro.ino +++ b/SampleSrc/accgyro.ino @@ -1,27 +1,28 @@ -// 参考にしたサイト: https://qiita.com/kitazaki/items/1ce671532270cd44fabf - -#include - -float gyroX, gyroY, gyroZ; // ジャイロデータ  -float ax, ay, az; // 加速度データ - -void setup() { - M5.begin(); - M5.Lcd.setRotation(3); - M5.IMU.Init(); - Serial.begin(115200); - Serial.println(""); -} -void loop() { - M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ); - M5.IMU.getAccelData(&ax, &ay, &az); - M5.Lcd.setCursor(0, 10, 2); - - M5.Lcd.printf("aX :%7.2f \naY :%7.2f \naZ :%7.2f mg \n\n", ax , ay , az ); - Serial.printf("%7.2f , %7.2f , %7.2f \n", ax , ay , az); //シリアルプロッタ用の出力 - - M5.Lcd.printf("gX :%7.2f \ngY :%7.2f \ngZ :%7.2f mg ", gyroX * M5.IMU.gRes, gyroY * M5.IMU.gRes, gyroZ * M5.IMU.gRes); - // Serial.printf("%7.2f,%7.2f,%7.2f,", gyroX * M5.IMU.gRes, gyroY * M5.IMU.gRes, gyroZ * M5.IMU.gRes); - - delay(50); -} \ No newline at end of file +// 参考にしたサイト: https://qiita.com/kitazaki/items/1ce671532270cd44fabf + +#include + +float gyroX, gyroY, gyroZ; // ジャイロデータ  +float ax, ay, az; // 加速度データ + +void setup() { + M5.begin(); + M5.Lcd.setRotation(3); + M5.IMU.Init(); + Serial.begin(115200); + Serial.println(""); +} +void loop() { + M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ); + M5.IMU.getAccelData(&ax, &ay, &az); + M5.Lcd.setCursor(0, 10, 2); + + M5.Lcd.printf("aX :%7.2f \naY :%7.2f \naZ :%7.2f mg \n\n", ax , ay , az ); + Serial.printf("%7.2f , %7.2f , %7.2f \n", ax , ay , az); //シリアルプロッタ用の出力 + + M5.Lcd.printf("gX :%7.2f \ngY :%7.2f \ngZ :%7.2f mg ", gyroX * M5.IMU.gRes, gyroY * M5.IMU.gRes, gyroZ * M5.IMU.gRes); + // Serial.printf("%7.2f,%7.2f,%7.2f,", gyroX * M5.IMU.gRes, gyroY * M5.IMU.gRes, gyroZ * M5.IMU.gRes); + + delay(50); +} + diff --git a/SampleSrc/analogread.ino b/SampleSrc/analogread.ino index 402677c..d7b029e 100644 --- a/SampleSrc/analogread.ino +++ b/SampleSrc/analogread.ino @@ -1,24 +1,25 @@ -#include - -int PIN = 26; - -// +--- 15kΩ抵抗 --+-- CdSセル --+ -// | | | -// GND G26 3V3 -// https://www.storange.jp/2012/03/arduinocds.html - -// 注意点: https://lang-ship.com/reference/unofficial/M5StickC/Peripherals/ADC/ - -void setup() { - M5.begin(); - - pinMode(PIN, ANALOG); // PINのモード設定 - - // https://lang-ship.com/blog/work/m5stickc-io/ -} - -void loop() { - Serial.printf("%04d\n", analogRead(PIN) ); // 暗 0〜4095 明 - - delay(1000); -} \ No newline at end of file +#include + +int PIN = 26; + +// +--- 15kΩ抵抗 --+-- CdSセル --+ +// | | | +// GND G26 3V3 +// https://www.storange.jp/2012/03/arduinocds.html + +// 注意点: https://lang-ship.com/reference/unofficial/M5StickC/Peripherals/ADC/ + +void setup() { + M5.begin(); + + pinMode(PIN, ANALOG); // PINのモード設定 + + // https://lang-ship.com/blog/work/m5stickc-io/ +} + +void loop() { + Serial.printf("%04d\n", analogRead(PIN) ); // 暗 0〜4095 明 + + delay(1000); +} + diff --git a/SampleSrc/button01.ino b/SampleSrc/button01.ino index c125448..5d1c552 100644 --- a/SampleSrc/button01.ino +++ b/SampleSrc/button01.ino @@ -1,23 +1,24 @@ -#include - -void setup() { - M5.begin(); - M5.Lcd.setRotation(3); -} - -void loop() { - M5.update(); // 各ボタンの状態を(読み取って)更新する:ボタンを判定するときは必須。 - - if (M5.BtnA.wasReleasefor(1000) ) { - Serial.println("[A] was Pressed longer than 1s"); - } else if (M5.BtnA.wasReleased()) { - Serial.println("[A] was Pressed"); - } else if (M5.BtnB.wasReleasefor(1000) ) { - Serial.println("[B] was Pressed longer than 1s"); - } else if (M5.BtnB.wasReleased()) { - Serial.println("[B] was Pressed"); - } else if (M5.Axp.GetBtnPress() == 2) { - Serial.println("[Power] was Pressed"); - } - delay(10); -} \ No newline at end of file +#include + +void setup() { + M5.begin(); + M5.Lcd.setRotation(3); +} + +void loop() { + M5.update(); // 各ボタンの状態を(読み取って)更新する:ボタンを判定するときは必須。 + + if (M5.BtnA.wasReleasefor(1000) ) { + Serial.println("[A] was Pressed longer than 1s"); + } else if (M5.BtnA.wasReleased()) { + Serial.println("[A] was Pressed"); + } else if (M5.BtnB.wasReleasefor(1000) ) { + Serial.println("[B] was Pressed longer than 1s"); + } else if (M5.BtnB.wasReleased()) { + Serial.println("[B] was Pressed"); + } else if (M5.Axp.GetBtnPress() == 2) { + Serial.println("[Power] was Pressed"); + } + delay(10); +} + diff --git a/SampleSrc/espnow01.ino b/SampleSrc/espnow01.ino index 5d9ac81..850c749 100644 --- a/SampleSrc/espnow01.ino +++ b/SampleSrc/espnow01.ino @@ -123,3 +123,4 @@ } delay(1); } + diff --git a/SampleSrc/serial01.ino b/SampleSrc/serial01.ino index bc94cc1..007db49 100644 --- a/SampleSrc/serial01.ino +++ b/SampleSrc/serial01.ino @@ -8,11 +8,9 @@ num++; if (num%5==1){ digitalWrite(10, LOW ); // LED ON (LOW=0) - delay(1500); // 1500ミリ秒待つ + delay(1500); // 1500ミリ秒待つ } else { digitalWrite(10, HIGH ); // LED OFF (HIGH=1) - delay(500); // 500ミリ秒待つ + delay(500); // 500ミリ秒待つ } } - - diff --git a/SampleSrc/str01.ino b/SampleSrc/str01.ino index 9d99309..9e3d177 100644 --- a/SampleSrc/str01.ino +++ b/SampleSrc/str01.ino @@ -1,69 +1,74 @@ -#include - -void setup() { - Serial.begin(115200); - M5.begin(); - M5.Lcd.setRotation(3); - Serial.println("整数 または 6文字の16進数(RRGGBB)を入力してください。"); -} - -String str = ""; // String: 可変長の文字列クラス -void loop() { - char buf[100] = {0}; - int pos = 0; - while (Serial.available()) { //PCから送信された文字があるあいだ、くりかえす - char c = Serial.read(); // 1バイト読み取る - buf[pos] = c; // 配列buf に格納 - pos++; // 格納位置をひとつ右へ - } - if (pos > 0) { - buf[pos] = 0; - String sbuf = buf; - sbuf.trim(); //文字列の前と後の空白と改行を取り除く(破壊的メソッド) - str.concat(sbuf); // 文字列の連結 (意味としては、str = str + sbuf) - Serial.println(str); - if (isDigit(sbuf)) { - int num = sbuf.toInt(); - Serial.println( num * num ); - } - if (sbuf.length() == 6) { // RRGGBBとして、LCD画面の背景を塗りつぶす - sbuf.toLowerCase(); - uint8_t r = hexToDec( sbuf.substring(0, 2) ); - uint8_t g = hexToDec( sbuf.substring(2, 4) ); - uint8_t b = hexToDec( sbuf.substring(4, 6) ); - M5.Lcd.fillScreen( getColorUInt16( r , g , b ) ); - M5.Lcd.setCursor(0, 70, 4); - M5.Lcd.printf(" %02x %02x %02x \n", r, g, b); - M5.Lcd.printf(" %d %d %d ", r, g, b); - } - } - delay(50); -} -//全ての文字が0〜9なら、1(true)を返す -bool isDigit(String s) { - bool isAllDigit = true; - const char *p = s.c_str(); // String.c_str() は、NULLで終端されたchar配列の先頭アドレスを返す - while ( *p != 0 ) { - if (*p < '0' || '9' < *p) isAllDigit = false; - p++; - } - return isAllDigit; -} -// M5 用の、16ビットカラー値に変換 引用:https://qiita.com/nnn112358/items/ea6b5e81623ba690343c -uint16_t getColorUInt16(uint8_t red, uint8_t green, uint8_t blue) { - return ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3); -} -// 引数hex が16進表記の文字列だと仮定して、10進の値を返す -uint8_t hexToDec(String hex) { - int dec = 0; - int tmp = 0; - for (int p = 0; p < hex.length(); p++) { - tmp = int(hex.charAt(p)); - if ( '0' <= tmp && tmp <= '9' ) tmp = tmp - '0'; - if ( 'a' <= tmp && tmp <= 'f' ) tmp = tmp - 'a' + 10; - if ( 'A' <= tmp && tmp <= 'F' ) tmp = tmp - 'A' + 10; - tmp = constrain(tmp, 0, 15); //例外処理 - dec = (dec * 16) + tmp; - } - return dec; -} \ No newline at end of file +#include + +void setup() { + Serial.begin(115200); + M5.begin(); + M5.Lcd.setRotation(3); + Serial.println("整数 または 6文字の16進数(RRGGBB)を入力してください。"); + M5.Lcd.fillScreen( getColorUInt16( 255 , 255 , 200 ) ); + M5.Lcd.setCursor(0, 70, 4); + M5.Lcd.printf(" input RRGGBB\n"); +} + +String str = ""; // String: 可変長の文字列クラス +void loop() { + char buf[100] = {0}; + int pos = 0; + while (Serial.available()) { //PCから送信された文字があるあいだ、くりかえす + char c = Serial.read(); // 1バイト読み取る + buf[pos] = c; // 配列buf に格納 + pos++; // 格納位置をひとつ右へ + } + if (pos > 0) { + buf[pos] = 0; + String sbuf = buf; + sbuf.trim(); //文字列の前と後の空白と改行を取り除く(破壊的メソッド) + str.concat(sbuf); // 文字列の連結 (意味としては、str = str + sbuf) + Serial.println(str); + if (isDigit(sbuf)) { + int num = sbuf.toInt(); + Serial.println( num * num ); + } + if (sbuf.length() == 6) { // RRGGBBとして、LCD画面の背景を塗りつぶす + sbuf.toLowerCase(); + uint8_t r = hexToDec( sbuf.substring(0, 2) ); + uint8_t g = hexToDec( sbuf.substring(2, 4) ); + uint8_t b = hexToDec( sbuf.substring(4, 6) ); + M5.Lcd.fillScreen( getColorUInt16( r , g , b ) ); + M5.Lcd.setCursor(0, 70, 4); + M5.Lcd.printf(" %02x %02x %02x \n", r, g, b); + M5.Lcd.printf(" %d %d %d ", r, g, b); + } + } + delay(50); +} +//全ての文字が0〜9なら、1(true)を返す +bool isDigit(String s) { + bool isAllDigit = true; + const char *p = s.c_str(); // String.c_str() は、NULLで終端されたchar配列の先頭アドレスを返す + while ( *p != 0 ) { + if (*p < '0' || '9' < *p) isAllDigit = false; + p++; + } + return isAllDigit; +} +// M5 用の、16ビットカラー値に変換 引用:https://qiita.com/nnn112358/items/ea6b5e81623ba690343c +uint16_t getColorUInt16(uint8_t red, uint8_t green, uint8_t blue) { + return ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3); +} +// 引数hex が16進表記の文字列だと仮定して、10進の値を返す +uint8_t hexToDec(String hex) { + int dec = 0; + int tmp = 0; + for (int p = 0; p < hex.length(); p++) { + tmp = int(hex.charAt(p)); + if ( '0' <= tmp && tmp <= '9' ) tmp = tmp - '0'; + if ( 'a' <= tmp && tmp <= 'f' ) tmp = tmp - 'a' + 10; + if ( 'A' <= tmp && tmp <= 'F' ) tmp = tmp - 'A' + 10; + tmp = constrain(tmp, 0, 15); //例外処理 + dec = (dec * 16) + tmp; + } + return dec; +} + +