diff --git a/.vscode/arduino.json b/.vscode/arduino.json index 1595543..23c394a 100644 --- a/.vscode/arduino.json +++ b/.vscode/arduino.json @@ -3,5 +3,5 @@ "board": "esp32:esp32:m5stick-c", "sketch": "M5StickCPlus_FactoryTest2022.ino", "output": "../ArduinoOutput", - "port": "/dev/tty.usbserial-6552E78DF6" + "port": "/dev/tty.usbserial-755AA16DAB" } \ No newline at end of file diff --git a/SampleSrc/TestBuild/TestBuild.ino b/SampleSrc/TestBuild/TestBuild.ino index f2f9147..9d99309 100644 --- a/SampleSrc/TestBuild/TestBuild.ino +++ b/SampleSrc/TestBuild/TestBuild.ino @@ -1,109 +1,69 @@ -#include "M5StickCPlus.h" -#include -#include - -esp_now_peer_info_t peerInfo; -int line = 0; - -void Init_ESPNOW() -{ // ESPNowの初期化 - // 引用: https://101010.fun/iot/esp32-m5stickc-plus-esp-now.html - WiFi.mode(WIFI_STA); - WiFi.disconnect(); - if (esp_now_init() == ESP_OK) - { - M5.Lcd.println("ESP-Now Init Success"); - } - else - { - M5.Lcd.println("ESP-Now Init failed"); - ESP.restart(); - } - // マルチキャスト用Slave登録 - memset(&peerInfo, 0, sizeof(peerInfo)); - for (int i = 0; i < 6; ++i) - { - peerInfo.peer_addr[i] = (uint8_t)0xff; - } - esp_err_t addStatus = esp_now_add_peer(&peerInfo); - if (addStatus == ESP_OK) - { - // Pair success - M5.Lcd.println("Pair success"); - esp_now_register_send_cb(onESPNOWSent); //送信後のコールバック関数を指定する - esp_now_register_recv_cb(onESPNOWReceive); ///受信時のコールバック関数を指定する - } -} -// データを送信した後に実行されるコールバック関数 -// 引用: https://101010.fun/iot/esp32-m5stickc-plus-esp-now.html -void onESPNOWSent(const uint8_t *mac_addr, esp_now_send_status_t status) -{ - // char macStr[18]; - // snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", - // mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); - line++; - if (line > 3) - { - M5.Lcd.fillScreen(YELLOW); - M5.Lcd.setCursor(0, 0, 1); - line = 0; - M5.Beep.tone(2000, 500); - } - else - { - M5.Beep.tone(1000, 500); - } - M5.Lcd.setTextColor(BLACK, GREEN); - - M5.Lcd.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); -} - -// データを受け取った時に実行されるコールバック関数 -void onESPNOWReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) -{ - char macStr[18]; - snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", - mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); - // Serial.println(); - // Serial.printf("Last Packet Recv from: %s\n", macStr); - // Serial.printf("Last Packet Recv Data(%d): ", data_len); - if (data[0] == 100 && data[1] == 254) // 100,254に深い意味はない。uint8_tは0〜255の数値 - { - line++; - if (line > 3) - { - M5.Beep.tone(2000, 1000); - M5.Lcd.fillScreen(YELLOW); - M5.Lcd.setCursor(0, 0, 1); - line = 0; - } - M5.Lcd.setTextColor(BLACK, CYAN); - - M5.Lcd.printf(" %s\n", macStr); - M5.Lcd.printf("Data: %d %d\n", data[0], data[1]); - } -} - -void setup() -{ - M5.begin(); - M5.Lcd.setRotation(3); - M5.Lcd.fillScreen(YELLOW); - - M5.Lcd.setTextColor(BLACK, YELLOW); - M5.Lcd.setCursor(0, 0, 1); - M5.Lcd.setTextSize(2); - - Init_ESPNOW(); -} -void loop() -{ - M5.update(); - M5.Beep.update(); - if (M5.BtnA.wasReleased()) - { - uint8_t data[2] = {100, 254}; // 送信データ  100, 254に深い意味はない。uint8_tは0〜255の数値 - esp_err_t result = esp_now_send(peerInfo.peer_addr, data, sizeof(data)); - } - delay(20); -} +#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 diff --git a/SampleSrc/line01.ino b/SampleSrc/line01.ino index b6cd9ab..613da6f 100644 --- a/SampleSrc/line01.ino +++ b/SampleSrc/line01.ino @@ -8,7 +8,7 @@ void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); - WiFiMulti.addAP("miura_asp_2g", "********"); + WiFiMulti.addAP("miura2g", "jikken2022"); // wait for WiFi connection Serial.print("Waiting for WiFi to connect..."); @@ -49,4 +49,4 @@ Serial.println(); Serial.println("Waiting 2 minutes..."); delay(60*2*1000); // 120秒まつ -} \ No newline at end of file +}