diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..dbbc9f4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "array": "cpp", + "initializer_list": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/M5StickCPlus_FactoryTest2022.ino b/M5StickCPlus_FactoryTest2022.ino index 346ef6d..ddbbace 100644 --- a/M5StickCPlus_FactoryTest2022.ino +++ b/M5StickCPlus_FactoryTest2022.ino @@ -15,6 +15,11 @@ #include #endif +// #define ENABLE_OTA 1 +#ifdef ENABLE_OTA +#include +#endif + typedef struct { double x; @@ -58,6 +63,10 @@ bool TestMode = true; // テストモード 常時ONにした (A/Bボタンを押しながら起動すれば、通常のFactoryTestでもテストモードになる) bool startCoundDownShutdown = false; // 電源OFFのカウントダウンを開始するならtrue +#ifdef ENABLE_OTA +bool startOTA = false; +bool startOTAhandle = false; +#endif TFT_eSprite Disbuff = TFT_eSprite(&M5.Lcd); // 画面ちらつき防止スプライト(画面のバッファリング) @@ -88,6 +97,16 @@ } if (startCoundDownShutdown) countDownShutdown(); +#ifdef ENABLE_OTA + if (startOTA) + { + startOTA = false; + OTA_Setup(); + startOTAhandle = true; + } + if (startOTAhandle) + ArduinoOTA.handle(); +#endif } void Displaybuff() // Disbuffスプライトを表示する。(テストモードだったらTest Modeと表示する) @@ -95,8 +114,8 @@ if (TestMode) { Disbuff.setTextSize(1); - Disbuff.setTextColor(TFT_GREEN); - Disbuff.drawString("FactoryTest 2022", 0, 0, 1); + Disbuff.setTextColor(TFT_CYAN); + Disbuff.drawString("FactoryTest 2022", 10, 0, 1); Disbuff.setTextColor(TFT_WHITE); } Disbuff.pushSprite(0, 0); @@ -341,6 +360,8 @@ int8_t i2s_readraw_buff[2048]; uint8_t fft_dis_buff[241][128] = {0}; uint16_t posData = 160; +char fftmes[30]; +int fftmax, fftmaxidx; void MicRecordfft(void *arg) // フーリエ変換 { @@ -350,31 +371,40 @@ float adc_data; double data = 0; uint16_t ydata; + uint16_t count_offset = 1; while (1) { xSemaphoreTake(start_fft, portMAX_DELAY); xSemaphoreGive(start_fft); fft_config_t *real_fft_plan = fft_init(1024, FFT_REAL, FFT_FORWARD, NULL, NULL); - i2s_read(I2S_NUM_0, (char *)i2s_readraw_buff, 2048, &bytesread, (100 / portTICK_RATE_MS)); + i2s_read(I2S_NUM_0, (char *)i2s_readraw_buff, 2048, &bytesread, (100 / portTICK_RATE_MS)); // portTICK_RATE_MS は 1 buffptr = (int16_t *)i2s_readraw_buff; - for (count_n = 0; count_n < real_fft_plan->size; count_n++) + // fftmax, fftmaxidx = real_fft_plan->size; + + for (count_n = 0; count_n < real_fft_plan->size; count_n++) // eal_fft_plan->sizeは1024 { - adc_data = (float)map(buffptr[count_n], INT16_MIN, INT16_MAX, -2000, 2000); + adc_data = (float)map(buffptr[count_n], INT16_MIN, INT16_MAX, -2000, 2000); // long map(long x, long in_min, long in_max, long out_min, long out_max) real_fft_plan->input[count_n] = adc_data; } fft_execute(real_fft_plan); xSemaphoreTake(xSemaphore, 100 / portTICK_RATE_MS); - for (count_n = 1; count_n < real_fft_plan->size / 4; count_n++) + for (count_n = 1; count_n < real_fft_plan->size / 4; count_n++) // 1024/4=>256 { data = sqrt(real_fft_plan->output[2 * count_n] * real_fft_plan->output[2 * count_n] + real_fft_plan->output[2 * count_n + 1] * real_fft_plan->output[2 * count_n + 1]); - if ((count_n - 1) < 128) + if ((count_n - 1) < 254) { - data = (data > 2000) ? 2000 : data; - ydata = map(data, 0, 2000, 0, 255); - fft_dis_buff[posData][128 - count_n] = ydata; + data = (data > 3000) ? 3000 : data; + ydata = map(data, 0, 3000, 0, 255); + if (128 - count_n > 0) + fft_dis_buff[posData][128 - count_n] = ydata; + if (ydata > 40 && fftmax < ydata) + { + fftmax = ydata; + fftmaxidx = count_n; + } } } @@ -419,12 +449,19 @@ } } xSemaphoreGive(xSemaphore); - /* + Disbuff.setTextColor(WHITE); Disbuff.setTextSize(1); - Disbuff.fillRect(0,0,70,18,Disbuff.color565(20,20,20)); - Disbuff.drawString("MicroPhone",5,5,1); - */ + Disbuff.fillRect(0, 0, 235, 20, Disbuff.color565(20, 20, 20)); + if (fftmaxidx > 200) + { + Disbuff.fillRect(0, 0, 235, 20, Disbuff.color565(fftmax, 20, 20)); + } + sprintf(fftmes, "max %3d idx %3d %5.0f Hz", fftmax, fftmaxidx, fftmaxidx * 41.67); + Disbuff.drawString(fftmes, 5, 5, 2); + fftmax = 0; + fftmaxidx = 0; + Disbuff.pushSprite(0, 0); } } @@ -444,9 +481,9 @@ xSemaphoreGive(start_dis); xSemaphoreGive(start_fft); M5.update(); - // delay(100); xSemaphoreTake(start_dis, portMAX_DELAY); // 画面表示タスク 一時停止 xSemaphoreTake(start_fft, portMAX_DELAY); // フーリエ変換タスク 一時停止 + checkAXPPress(); } // xSemaphoreTake( start_dis , portMAX_DELAY ); // xSemaphoreTake( start_fft , portMAX_DELAY ); @@ -1175,6 +1212,12 @@ { startCoundDownShutdown = true; // シャットダウンタイマースタート予約(発動はcheckAXPPress()のなかで) } +#ifdef ENABLE_OTA + if (data[0] == 99 && data[1] == 254) // 99,254に深い意味はない。uint8_tは0〜255の数値 + { + startOTA = true; + } +#endif } void countDownShutdown() { @@ -1447,7 +1490,7 @@ Init_ESPNOW(); // ESPNOWの初期化 - ColorBar(); + ColorBar(); //ディスプレイ発色チェック checkI2CAddr(); checkAXP192(); // バッテリー電圧チェック。低下してたらエラーメッセージ表示 @@ -1484,23 +1527,112 @@ { // それぞれのテスト中は、関数のなかのループがまわる // AorBボタンを押したら、現在実行中の関数のループを抜け、次の関数を実行する - MPU6886Test(); - DisplayRTC(); - DisplayMicro(); - DisIRSend(); + MPU6886Test(); // 加速度・ジャイロ + DisplayRTC(); // リアルタイムクロック + DisplayMicro(); // マイク + DisIRSend(); // 赤外線通信 if (TestMode) { - DisplayTestMode(); + DisplayTestMode(); // バッテリー電圧 } #ifdef ENABLE_BLE DisPlayBLESend(); #else - ESP_NOW_SendShutdown(); + ESP_NOW_SendShutdown(); // シャットダウン信号の送信 #endif M5.update(); delay(50); Serial.println("end of loop"); } + +#ifdef ENABLE_OTA +const char *ssid = "miura2g"; +const char *password = "jikken2022"; +void OTA_Setup() +{ + WiFi.softAPdisconnect(true); + delay(1000); + WiFi.mode(WIFI_STA); + delay(1000); + + WiFi.begin(ssid, password); + int trycount = 0; + while ((WiFi.status() != WL_CONNECTED)) + { + M5.Beep.tone(2000); + delay(200); + M5.Beep.mute(); + delay(300); + M5.Lcd.print("."); + trycount++; + if (trycount == 100) + { + M5.Axp.Write1Byte(0x32, M5.Axp.Read8bit(0x32) | 0x80); //電源OFF + } + } + M5.Lcd.fillScreen(GREEN); + M5.Lcd.setCursor(10, 50); + M5.Lcd.setTextColor(BLACK, GREEN); + M5.Lcd.println(" CONNECTED! "); + M5.Beep.tone(4000); + delay(1000); + M5.Beep.mute(); + + // Port defaults to 3232 + ArduinoOTA.setPort(3232); + // Hostname defaults to esp3232-[MAC] + ArduinoOTA.setHostname("m5"); + // No authentication by default + // ArduinoOTA.setPassword(""); + // Password can be set with it's md5 value as well + // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 + // ArduinoOTA.setPasswordHash("2bf9b9f1272b09206f050251343dcfcc"); // + ArduinoOTA + .onStart([]() + { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) + type = "sketch"; + else // U_SPIFFS + type = "filesystem"; }) + .onEnd([]() + { + M5.Beep.tone(2000); + delay(150); + M5.Beep.tone(4000); + delay(150); + M5.Beep.tone(8000); + delay(300); + M5.Beep.mute(); }) + .onProgress([](unsigned int progress, unsigned int total) + { ota_progress(progress, total); }) + .onError([](ota_error_t error) {}); + // Serial.printf("Error[%u]: ", error); + // if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); + // else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); + // else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); + // else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); + // else if (error == OTA_END_ERROR) Serial.println("End Failed"); + + ArduinoOTA.begin(); +} + +int prev_progress = -1; +char buf[30]; +void ota_progress(unsigned int progress, unsigned int total) +{ + int cur_progress = (progress / (total / 100)); + sprintf(buf, "OTA %d%% done", cur_progress); + if (prev_progress < cur_progress) + { + M5.Lcd.setCursor(0, 30, 1); + M5.Lcd.fillScreen(BLACK); + M5.Lcd.setTextColor(WHITE, BLACK); + M5.Lcd.println(buf); + prev_progress = cur_progress; + } +} +#endif \ No newline at end of file