diff --git a/Icon.c b/Icon.c index f2df695..8ba2206 100644 --- a/Icon.c +++ b/Icon.c @@ -621,6 +621,7 @@ // 图像储存模式 : 高位在先 // /************************************************************************************/ +#ifdef ENABLE_BLE const unsigned char icon_ble[4608] = { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , @@ -1215,6 +1216,10 @@ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , }; + #else + const unsigned char icon_ble[1] = {}; + const unsigned char icon_ble_disconnect[1] = {}; + #endif /*************************************************************************************/ // // 图像文件 diff --git a/M5StickCPlus_FactoryTest2022.ino b/M5StickCPlus_FactoryTest2022.ino index 3872649..20e4611 100644 --- a/M5StickCPlus_FactoryTest2022.ino +++ b/M5StickCPlus_FactoryTest2022.ino @@ -1,15 +1,19 @@ #include "M5StickCPlus.h" #include "fft.h" #include "esp_pm.h" +#include +#include #include #include #include +// #define ENABLE_BLE 1 +#ifdef ENABLE_BLE #include #include #include #include - +#endif typedef struct { @@ -43,75 +47,79 @@ extern const unsigned char ImageData[768]; extern const unsigned char error_48[4608]; extern const unsigned char icon_ir[4608]; -extern const unsigned char icon_ble[4608]; extern const unsigned char icon_wifi[4608]; +#ifdef ENABLE_BLE +extern const unsigned char icon_ble[4608]; extern const unsigned char icon_ble_disconnect[4608]; +#else +extern const unsigned char icon_ble[1]; +extern const unsigned char icon_ble_disconnect[1]; +#endif -bool TestMode = true; +bool TestMode = true; // テストモード 常時ONにした (A/Bボタンを押しながら起動すれば、通常のFactoryTestでもテストモードになる) -TFT_eSprite Disbuff = TFT_eSprite(&M5.Lcd); +TFT_eSprite Disbuff = TFT_eSprite(&M5.Lcd); // 画面ちらつき防止スプライト(画面のバッファリング) hw_timer_t *timer = NULL; volatile SemaphoreHandle_t timerSemaphore; portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; volatile uint8_t TimerCount = 0; - -void IRAM_ATTR onTimer() +void IRAM_ATTR onTimer() // タイマー処理(LEDをチカチカさせている) { portENTER_CRITICAL_ISR(&timerMux); - digitalWrite(10, TimerCount % 100 ); + digitalWrite(10, TimerCount % 100); // LEDの明るさ TimerCount++; portEXIT_CRITICAL_ISR(&timerMux); } -void checkAXPPress() +void checkAXPPress() // 電源ボタン押下チェック { - if( M5.Axp.GetBtnPress()) // 電源ボタン押下したら - { - do{ + if (M5.Axp.GetBtnPress()) // 電源ボタン押下したら + { + do + { delay(20); - } - while( M5.Axp.GetBtnPress()); + } while (M5.Axp.GetBtnPress()); M5.Beep.mute(); - // ESP.restart(); + // ESP.restart(); M5.Axp.Write1Byte(0x32, M5.Axp.Read8bit(0x32) | 0x80); - } + } } -void Displaybuff() +void Displaybuff() // Disbuffスプライトを表示する。(テストモードだったらTest Modeと表示する) { if (TestMode) { Disbuff.setTextSize(1); - Disbuff.setTextColor(TFT_RED); - Disbuff.drawString("Test Mode", 0, 0, 1); + Disbuff.setTextColor(TFT_GREEN); + Disbuff.drawString("FactoryTest 2022", 0, 0, 1); Disbuff.setTextColor(TFT_WHITE); } Disbuff.pushSprite(0, 0); } -void ErrorDialog( uint8_t code, const char *str ) +void ErrorDialog(uint8_t code, const char *str) // エラー表示(主にバッテリー切れ警告) { - Disbuff.fillRect(28,20,184,95,Disbuff.color565(45,45,45)); - Disbuff.fillRect(30,22,180,91,TFT_BLACK); - //Disbuff.drawRect(30,22,180,91,Disbuff.color565(45,45,45)); + Disbuff.fillRect(28, 20, 184, 95, Disbuff.color565(45, 45, 45)); + Disbuff.fillRect(30, 22, 180, 91, TFT_BLACK); + // Disbuff.drawRect(30,22,180,91,Disbuff.color565(45,45,45)); Disbuff.setSwapBytes(true); Disbuff.pushImage(40, 43, 48, 48, (uint16_t *)error_48); Disbuff.setCursor(145, 37); Disbuff.setTextFont(2); Disbuff.printf("%02X", code); - Disbuff.drawString("ERROR", 55 + 45, 10 + 27 , 2); - Disbuff.drawString("-----------------", 55 + 45, 30 + 27 , 1); - Disbuff.drawString(str, 55 + 45, 45 + 27 , 1); - Disbuff.drawString("check Hardware ", 55 + 45 , 60 + 27, 1); + Disbuff.drawString("ERROR", 55 + 45, 10 + 27, 2); + Disbuff.drawString("-----------------", 55 + 45, 30 + 27, 1); + Disbuff.drawString(str, 55 + 45, 45 + 27, 1); + Disbuff.drawString("check Hardware ", 55 + 45, 60 + 27, 1); Disbuff.pushSprite(0, 0); while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) { M5.update(); - checkAXPPress(); + checkAXPPress(); delay(100); } while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) @@ -224,7 +232,7 @@ {.start_point = {-1, 1, -1}, .end_point = {-1, -1, -1}}, }; -void MPU6886Test() +void MPU6886Test() // 加速度テスト { float accX = 0; float accY = 0; @@ -259,8 +267,8 @@ while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) { - M5.Imu.getAccelData(&accX, &accY, &accZ); - //M5.MPU6886.getAccelData(&accX, &accY, &accZ); + M5.Imu.getAccelData(&accX, &accY, &accZ); + // M5.MPU6886.getAccelData(&accX, &accY, &accZ); if ((accX < 1) && (accX > -1)) { theta = asin(-accX) * 57.295; @@ -279,7 +287,7 @@ Disbuff.printf("%.2f", theta); Disbuff.setCursor(10, 125); Disbuff.printf("%.2f", phi); - //Displaybuff(); + // Displaybuff(); delay(20); z.end_point.x = 0; @@ -295,7 +303,7 @@ RotatePoint(&rect_source[n].end_point, &rect_dis.end_point, theta, phi, (double)0); printLine3D(&Disbuff, &rect_dis, TFT_WHITE); } - //Disbuff.fillRect(0,0,160,80,BLACK); + // Disbuff.fillRect(0,0,160,80,BLACK); printLine3D(&Disbuff, &x, TFT_RED); printLine3D(&Disbuff, &y, TFT_GREEN); printLine3D(&Disbuff, &z, TFT_BLUE); @@ -310,7 +318,7 @@ last_phi = phi; M5.update(); - checkAXPPress(); + checkAXPPress(); } while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) { @@ -331,7 +339,7 @@ uint8_t fft_dis_buff[241][128] = {0}; uint16_t posData = 160; -void MicRecordfft(void *arg) +void MicRecordfft(void *arg) // フーリエ変換 { int16_t *buffptr; size_t bytesread; @@ -361,7 +369,7 @@ 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) { - data = ( data > 2000 ) ? 2000 : data; + data = (data > 2000) ? 2000 : data; ydata = map(data, 0, 2000, 0, 255); fft_dis_buff[posData][128 - count_n] = ydata; } @@ -377,7 +385,7 @@ } } -void Drawdisplay(void *arg) +void Drawdisplay(void *arg) // フーリエ変換のときの画面表示 { uint16_t count_x = 0, count_y = 0; uint16_t colorPos; @@ -401,10 +409,10 @@ Disbuff.drawPixel(count_x, count_y, Disbuff.color565(ImageData[colorPos * 3 + 0], ImageData[colorPos * 3 + 1], ImageData[colorPos * 3 + 2])); /* - disbuff[ count_y * 160 + count_x ].r = ImageData[ colorPos * 3 + 0 ]; - disbuff[ count_y * 160 + count_x ].g = ImageData[ colorPos * 3 + 1 ]; - disbuff[ count_y * 160 + count_x ].b = ImageData[ colorPos * 3 + 2 ]; - */ + disbuff[ count_y * 160 + count_x ].r = ImageData[ colorPos * 3 + 0 ]; + disbuff[ count_y * 160 + count_x ].g = ImageData[ colorPos * 3 + 1 ]; + disbuff[ count_y * 160 + count_x ].b = ImageData[ colorPos * 3 + 2 ]; + */ } } xSemaphoreGive(xSemaphore); @@ -415,7 +423,6 @@ Disbuff.drawString("MicroPhone",5,5,1); */ Disbuff.pushSprite(0, 0); - } } @@ -427,21 +434,21 @@ Disbuff.fillRect(0, 0, 160, 80, Disbuff.color565(0, 0, 0)); //黒(R=0,G=0,B=0)で塗りつぶす Disbuff.pushSprite(0, 0); - xSemaphoreGive(start_dis); - xSemaphoreGive(start_fft); - while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) + xSemaphoreGive(start_dis); // 画面表示タスク スタート + xSemaphoreGive(start_fft); // フーリエ変換タスク スタート + while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) // ABボタン押していない間、くりかえす { xSemaphoreGive(start_dis); xSemaphoreGive(start_fft); M5.update(); - //delay(100); - xSemaphoreTake(start_dis, portMAX_DELAY); - xSemaphoreTake(start_fft, portMAX_DELAY); + // delay(100); + xSemaphoreTake(start_dis, portMAX_DELAY); // 画面表示タスク 一時停止 + xSemaphoreTake(start_fft, portMAX_DELAY); // フーリエ変換タスク 一時停止 } - //xSemaphoreTake( start_dis , portMAX_DELAY ); - //xSemaphoreTake( start_fft , portMAX_DELAY ); + // xSemaphoreTake( start_dis , portMAX_DELAY ); + // xSemaphoreTake( start_fft , portMAX_DELAY ); - while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) + while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) // ABボタン押している間、くりかえす { M5.update(); checkAXPPress(); @@ -455,7 +462,7 @@ #define PIN_CLK 0 #define PIN_DATA 34 -bool InitI2SMicroPhone() +bool InitI2SMicroPhone() // マイクの初期化 { esp_err_t err = ESP_OK; i2s_config_t i2s_config = { @@ -474,12 +481,11 @@ pin_config.ws_io_num = PIN_CLK; pin_config.data_out_num = I2S_PIN_NO_CHANGE; pin_config.data_in_num = PIN_DATA; - err += i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); err += i2s_set_pin(I2S_NUM_0, &pin_config); err += i2s_set_clk(I2S_NUM_0, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO); - //i2s_set_clk(0) + // i2s_set_clk(0) if (err != ESP_OK) { @@ -491,10 +497,10 @@ } } -void DisplayRTC() +void DisplayRTC() // リアルタイムクロック(内蔵時計) { Disbuff.fillRect(0, 0, 240, 135, Disbuff.color565(0, 0, 0)); - //Displaybuff(); + // Displaybuff(); M5.Rtc.GetBm8563Time(); RTC_TimeTypeDef time; M5.Rtc.GetTime(&time); @@ -511,12 +517,12 @@ Disbuff.setTextColor(TFT_WHITE); Disbuff.setCursor(25, 50); Disbuff.printf("%02d:%02d:%02d", time.Hours, time.Minutes, time.Seconds); - Disbuff.fillRect(0,0,240,25,Disbuff.color565(20,20,20)); + Disbuff.fillRect(0, 0, 240, 25, Disbuff.color565(20, 20, 20)); Disbuff.setTextSize(2); - Disbuff.drawString("BMP8563 RTC Time",26,5,1); + Disbuff.drawString("BMP8563 RTC Time", 26, 5, 1); Displaybuff(); M5.update(); - checkAXPPress(); + checkAXPPress(); delay(100); } while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) @@ -540,17 +546,17 @@ void ir_tx_callback(rmt_channel_t channel, void *arg) { - //static BaseType_t xHigherPriorityTaskWoken = false; + // static BaseType_t xHigherPriorityTaskWoken = false; if (channel == RMT_TX_CHANNEL) { esp_pm_lock_release(rmt_freq_lock); - //xHigherPriorityTaskWoken = pdFALSE; - //xSemaphoreGiveFromISR( irTxSem, &xHigherPriorityTaskWoken ); + // xHigherPriorityTaskWoken = pdFALSE; + // xSemaphoreGiveFromISR( irTxSem, &xHigherPriorityTaskWoken ); free(tx_buffer); } } -bool InitIRTx() +bool InitIRTx() // 赤外線通信 初期化 { rmt_config_t rmt_tx; rmt_tx.rmt_mode = RMT_MODE_TX; @@ -573,7 +579,6 @@ return true; } - bool ir_uart_tx(const uint8_t *src, uint8_t len, bool wait_tx_done) { /* @@ -587,15 +592,15 @@ } #if BAUD == 4800 - const rmt_item32_t bit0 = {{{8421, 1, 8421, 1}}}; //Logical 0 - const rmt_item32_t bit1 = {{{8421, 0, 8421, 0}}}; //Logical 1 + const rmt_item32_t bit0 = {{{8421, 1, 8421, 1}}}; // Logical 0 + const rmt_item32_t bit1 = {{{8421, 0, 8421, 0}}}; // Logical 1 #else - const rmt_item32_t bit0 = {{{16842, 1, 16842, 1}}}; //Logical 0 - const rmt_item32_t bit1 = {{{16842, 0, 16842, 0}}}; //Logical 1 + const rmt_item32_t bit0 = {{{16842, 1, 16842, 1}}}; // Logical 0 + const rmt_item32_t bit1 = {{{16842, 0, 16842, 0}}}; // Logical 1 #endif uint8_t *psrc = (uint8_t *)src; - //xSemaphoreTake(irTxSem, portMAX_DELAY); + // xSemaphoreTake(irTxSem, portMAX_DELAY); tx_buffer = (rmt_item32_t *)malloc(sizeof(rmt_item32_t) * 10 * len); if (tx_buffer == NULL) @@ -632,76 +637,81 @@ return true; } - -#define SERVICE_UUID "1bc68b2a-f3e3-11e9-81b4-2a2ae2dbcce4" -#define CHARACTERISTIC_RX_UUID "1bc68da0-f3e3-11e9-81b4-2a2ae2dbcce4" -#define CHARACTERISTIC_TX_UUID "1bc68efe-f3e3-11e9-81b4-2a2ae2dbcce4" +#ifdef ENABLE_BLE +#define SERVICE_UUID "1bc68b2a-f3e3-11e9-81b4-2a2ae2dbcce4" +#define CHARACTERISTIC_RX_UUID "1bc68da0-f3e3-11e9-81b4-2a2ae2dbcce4" +#define CHARACTERISTIC_TX_UUID "1bc68efe-f3e3-11e9-81b4-2a2ae2dbcce4" BLEServer *pServer = NULL; BLEService *pService = NULL; -BLECharacteristic * pTxCharacteristic; +BLECharacteristic *pTxCharacteristic; bool deviceConnected = false; bool oldDeviceConnected = false; -class MyServerCallbacks: public BLEServerCallbacks { - void onConnect(BLEServer* pServer) { - deviceConnected = true; +class MyServerCallbacks : public BLEServerCallbacks +{ + void onConnect(BLEServer *pServer) + { + deviceConnected = true; }; - void onDisconnect(BLEServer* pServer) { - deviceConnected = false; + void onDisconnect(BLEServer *pServer) + { + deviceConnected = false; } }; -uint8_t* data = new uint8_t[128]; +uint8_t *data = new uint8_t[128]; -class MyCallbacks: public BLECharacteristicCallbacks { - - void onWrite(BLECharacteristic *pCharacteristic) { - data = pCharacteristic->getData(); - } -}; - -bool InitBLEServer() +class MyCallbacks : public BLECharacteristicCallbacks { - uint64_t chipid = ESP.getEfuseMac(); + + void onWrite(BLECharacteristic *pCharacteristic) + { + data = pCharacteristic->getData(); + } +}; + +bool InitBLEServer() // Bluetooth Low Energyサーバ初期化 +{ + uint64_t chipid = ESP.getEfuseMac(); String blename = "M5-" + String((uint32_t)(chipid >> 32), HEX); BLEDevice::init(blename.c_str()); - //BLEDevice::setPower(ESP_PWR_LVL_N12); - pServer = BLEDevice::createServer(); + // BLEDevice::setPower(ESP_PWR_LVL_N12); + pServer = BLEDevice::createServer(); - pServer->setCallbacks(new MyServerCallbacks()); - pService = pServer->createService(SERVICE_UUID); - pTxCharacteristic = pService->createCharacteristic( - CHARACTERISTIC_RX_UUID, - BLECharacteristic::PROPERTY_NOTIFY - ); + pServer->setCallbacks(new MyServerCallbacks()); + pService = pServer->createService(SERVICE_UUID); + pTxCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_RX_UUID, + BLECharacteristic::PROPERTY_NOTIFY); - pTxCharacteristic->addDescriptor(new BLE2902()); - BLECharacteristic * pRxCharacteristic = pService->createCharacteristic( - CHARACTERISTIC_TX_UUID, - BLECharacteristic::PROPERTY_WRITE - ); - pRxCharacteristic->setCallbacks(new MyCallbacks()); + pTxCharacteristic->addDescriptor(new BLE2902()); + BLECharacteristic *pRxCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_TX_UUID, + BLECharacteristic::PROPERTY_WRITE); + pRxCharacteristic->setCallbacks(new MyCallbacks()); return true; } -bool checkAXP192() +#endif + +bool checkAXP192() // 電源・バッテリー管理モジュール { float VBat = M5.Axp.GetBatVoltage(); - while( VBat < 3.2 ) + while (VBat < 3.2) { VBat = M5.Axp.GetBatVoltage(); - ErrorDialog(0x22,"Bat Vol error"); + ErrorDialog(0x22, "Bat Vol error"); } return true; } -uint8_t crc8(uint8_t data, uint8_t *buff, uint32_t length) +uint8_t crc8(uint8_t data, uint8_t *buff, uint32_t length) //巡回冗長検査 { uint8_t bit; // bit mask uint8_t crc = 0xFF; // calculated checksum @@ -725,41 +735,41 @@ } // ENV. II SENSORをつないだときに、温度と湿度がとれる -int getTempAndHum(float *temp,float *hum ) +int getTempAndHum(float *temp, float *hum) { - static float _tempSave,_HUmSave; + static float _tempSave, _HUmSave; uint8_t databuff[6]; Wire.beginTransmission(0x44); Wire.write(0x2c); Wire.write(0x06); Wire.endTransmission(); - Wire.requestFrom(0x44,6); - - for( int i = 0; i < 6; i++ ) + Wire.requestFrom(0x44, 6); + + for (int i = 0; i < 6; i++) { databuff[i] = Wire.read(); - Serial.printf("%02X ",databuff[i]); + Serial.printf("%02X ", databuff[i]); } Serial.println("--"); - if( crc8(0x31,&databuff[0],2) == databuff[2] ) + if (crc8(0x31, &databuff[0], 2) == databuff[2]) { uint16_t tempraw = databuff[0] << 8 | databuff[1]; - *temp = -45 + 175 * ( float(tempraw) / 65535.0 ); + *temp = -45 + 175 * (float(tempraw) / 65535.0); _tempSave = *temp; - Serial.printf("Temp:%.2f ",*temp); + Serial.printf("Temp:%.2f ", *temp); } else { *temp = _tempSave; } - if( crc8(0x31,&databuff[3],2) == databuff[5] ) + if (crc8(0x31, &databuff[3], 2) == databuff[5]) { uint16_t Humraw = databuff[3] << 8 | databuff[4]; - *hum= 100 * ( float(Humraw) / 65535.0 ); + *hum = 100 * (float(Humraw) / 65535.0); _HUmSave = *hum; - Serial.printf("Hum:%.2f \n",*hum); + Serial.printf("Hum:%.2f \n", *hum); } else { @@ -768,50 +778,49 @@ return 0; } - -void DisIRSend() +void DisIRSend() // 赤外線通信 画面表示 { - uint8_t senddata[20]={0}; - memset(senddata,0x00,sizeof(uint8_t)*20); + uint8_t senddata[20] = {0}; + memset(senddata, 0x00, sizeof(uint8_t) * 20); while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) { Disbuff.fillRect(0, 0, 240, 135, TFT_BLACK); Disbuff.setSwapBytes(true); Disbuff.pushImage(180, 16, 48, 48, (uint16_t *)icon_ir); - Disbuff.setTextColor(Disbuff.color565(180,180,180)); + Disbuff.setTextColor(Disbuff.color565(180, 180, 180)); Disbuff.setTextSize(3); Disbuff.setCursor(12, 20); Disbuff.printf("IR Send "); Disbuff.setTextSize(5); Disbuff.setCursor(12, 75); - if( senddata[0] % 4 == 0 ) + if (senddata[0] % 4 == 0) { - Disbuff.printf("0x%02X> ",senddata[0]); + Disbuff.printf("0x%02X> ", senddata[0]); } - else if( senddata[0] % 4 == 1 ) + else if (senddata[0] % 4 == 1) { - Disbuff.printf("0x%02X>>",senddata[0]); + Disbuff.printf("0x%02X>>", senddata[0]); } - else if( senddata[0] % 4 == 2 ) + else if (senddata[0] % 4 == 2) { - Disbuff.printf("0x%02X >>",senddata[0]); + Disbuff.printf("0x%02X >>", senddata[0]); } - else if( senddata[0] % 4 == 3 ) + else if (senddata[0] % 4 == 3) { - Disbuff.printf("0x%02X >",senddata[0]); + Disbuff.printf("0x%02X >", senddata[0]); } - + Displaybuff(); senddata[1]++; - if( senddata[1] > 3 ) + if (senddata[1] > 3) { senddata[1] = 0; senddata[0]++; - ir_uart_tx( senddata ,20, true); + ir_uart_tx(senddata, 20, true); } - checkAXPPress(); + checkAXPPress(); M5.update(); delay(100); } @@ -827,52 +836,53 @@ Disbuff.setTextColor(TFT_WHITE); } -void DisPlayBLESend() +#ifdef ENABLE_BLE +void DisPlayBLESend() // Bluetooth Low Energy 画面表示 { - uint8_t senddata[2]={0}; + uint8_t senddata[2] = {0}; pService->start(); - pServer->getAdvertising()->start(); + pServer->getAdvertising()->start(); - uint64_t chipid = ESP.getEfuseMac(); + uint64_t chipid = ESP.getEfuseMac(); String blename = "M5-" + String((uint32_t)(chipid >> 32), HEX); while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) { Disbuff.fillRect(0, 0, 240, 135, TFT_BLACK); - if( deviceConnected ) + if (deviceConnected) { - Disbuff.pushImage(180, 16, 48, 48, (uint16_t *)icon_ble); - Disbuff.setTextColor(Disbuff.color565(180,180,180)); + Disbuff.pushImage(180, 16, 48, 48, (uint16_t *)icon_ble); + Disbuff.setTextColor(Disbuff.color565(180, 180, 180)); Disbuff.setTextSize(3); Disbuff.setCursor(12, 20); - //Disbuff.printf("BLE connect!\n"); + // Disbuff.printf("BLE connect!\n"); Disbuff.printf("BLE Send\n"); Disbuff.setTextSize(5); Disbuff.setCursor(12, 75); - if( senddata[0] % 4 == 0 ) + if (senddata[0] % 4 == 0) { - Disbuff.printf("0x%02X> ",senddata[0]); + Disbuff.printf("0x%02X> ", senddata[0]); } - else if( senddata[0] % 4 == 1 ) + else if (senddata[0] % 4 == 1) { - Disbuff.printf("0x%02X>>",senddata[0]); + Disbuff.printf("0x%02X>>", senddata[0]); } - else if( senddata[0] % 4 == 2 ) + else if (senddata[0] % 4 == 2) { - Disbuff.printf("0x%02X >>",senddata[0]); + Disbuff.printf("0x%02X >>", senddata[0]); } - else if( senddata[0] % 4 == 3 ) + else if (senddata[0] % 4 == 3) { - Disbuff.printf("0x%02X >",senddata[0]); + Disbuff.printf("0x%02X >", senddata[0]); } senddata[1]++; - if( senddata[1] > 3 ) + if (senddata[1] > 3) { senddata[1] = 0; senddata[0]++; - pTxCharacteristic->setValue( senddata, 1 ); + pTxCharacteristic->setValue(senddata, 1); pTxCharacteristic->notify(); } } @@ -880,21 +890,21 @@ { Disbuff.setTextSize(2); Disbuff.setCursor(12, 20); - Disbuff.setTextColor( TFT_RED ); + Disbuff.setTextColor(TFT_RED); Disbuff.printf("BLE disconnect\n"); Disbuff.setCursor(12, 45); - Disbuff.setTextColor(Disbuff.color565(18,150,219)); - - Disbuff.printf(String("Name:"+blename+"\n").c_str()); + Disbuff.setTextColor(Disbuff.color565(18, 150, 219)); + + Disbuff.printf(String("Name:" + blename + "\n").c_str()); Disbuff.setCursor(12, 70); Disbuff.printf("UUID:1bc68b2a\n"); Disbuff.pushImage(180, 16, 48, 48, (uint16_t *)icon_ble_disconnect); } Displaybuff(); - + M5.update(); delay(100); - checkAXPPress(); + checkAXPPress(); } while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) { @@ -909,39 +919,40 @@ pService->stop(); pServer->getAdvertising()->stop(); } +#endif -void DisplayGroveSHT30() +void DisplayGroveSHT30() // SHT30=ENV. II SENSOR 温度と湿度 { float tempdata, humdata; uint8_t count = 0; - Wire.begin(32,33); + Wire.begin(32, 33); while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) { Disbuff.fillRect(0, 0, 240, 135, TFT_BLACK); Disbuff.setSwapBytes(true); Disbuff.pushImage(180, 16, 48, 48, (uint16_t *)icon_ir); - Disbuff.setTextColor(Disbuff.color565(180,180,180)); + Disbuff.setTextColor(Disbuff.color565(180, 180, 180)); Disbuff.setTextSize(3); Disbuff.setCursor(12, 20); Disbuff.printf("IR Send "); - if( count >= 10 ) + if (count >= 10) { count = 0; - getTempAndHum(&tempdata, &humdata);// ENV. II SENSORをつないだときに、温度と湿度がとれる - + getTempAndHum(&tempdata, &humdata); // ENV. II SENSORをつないだときに、温度と湿度がとれる + Disbuff.setCursor(12, 50); - Disbuff.printf("%.2f",tempdata); + Disbuff.printf("%.2f", tempdata); Disbuff.setCursor(12, 80); - Disbuff.printf("%.2f",humdata); - Disbuff.pushSprite(0,0); + Disbuff.printf("%.2f", humdata); + Disbuff.pushSprite(0, 0); } - checkAXPPress(); + checkAXPPress(); M5.update(); delay(100); - count ++; + count++; } while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) { @@ -953,9 +964,8 @@ delay(50); M5.Beep.mute(); Disbuff.setTextColor(TFT_WHITE); - } -void DisplayTestMode() +void DisplayTestMode() // テストモード:ピンの電圧とバッテリー電圧 { float tempdata, humdata; uint8_t count = 10, count_u = 0, count_t = 0; @@ -965,31 +975,30 @@ pin_config.ws_io_num = 33; pin_config.data_out_num = I2S_PIN_NO_CHANGE; pin_config.data_in_num = PIN_DATA; - i2s_set_pin(I2S_NUM_0,&pin_config); - + i2s_set_pin(I2S_NUM_0, &pin_config); + i2s_driver_uninstall(I2S_NUM_0); - + gpio_reset_pin(GPIO_NUM_0); gpio_reset_pin(GPIO_NUM_26); - pinMode(26,OUTPUT); - pinMode(25,INPUT_PULLDOWN); - pinMode(36,INPUT_PULLDOWN); - pinMode(0,OUTPUT); + pinMode(26, OUTPUT); + pinMode(25, INPUT_PULLDOWN); + pinMode(36, INPUT_PULLDOWN); + pinMode(0, OUTPUT); - digitalWrite(0,0); - digitalWrite(26,0); - + digitalWrite(0, 0); + digitalWrite(26, 0); while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) { Disbuff.fillRect(0, 0, 240, 135, TFT_BLACK); Disbuff.setSwapBytes(true); - Disbuff.setTextColor(Disbuff.color565(180,180,180)); + Disbuff.setTextColor(Disbuff.color565(180, 180, 180)); Disbuff.setTextSize(3); Disbuff.setCursor(12, 7); - if( M5.Axp.GetBatVoltage() > 3.2 ) //バッテリーの電圧(残量によって変化する) + if (M5.Axp.GetBatVoltage() > 3.2) //バッテリーの電圧(残量によって変化する) { Disbuff.setTextColor(TFT_GREEN); } @@ -997,9 +1006,9 @@ { Disbuff.setTextColor(TFT_RED); } - Disbuff.printf("b%.2f",M5.Axp.GetBatVoltage());// バッテリー電圧を表示する + Disbuff.printf("b%.2f", M5.Axp.GetBatVoltage()); // バッテリー電圧を表示する Disbuff.setCursor(12, 37); - if( M5.Axp.GetVinVoltage() > 4.6 ) + if (M5.Axp.GetVinVoltage() > 4.6) { Disbuff.setTextColor(TFT_GREEN); } @@ -1007,30 +1016,30 @@ { Disbuff.setTextColor(TFT_RED); } - Disbuff.printf("v%.2f",M5.Axp.GetVinVoltage());//外部電源電圧(5V←の電圧) + Disbuff.printf("v%.2f", M5.Axp.GetVinVoltage()); //外部電源電圧(5V←の電圧) - digitalWrite(0,0); - digitalWrite(26,0); + digitalWrite(0, 0); + digitalWrite(26, 0); count_u = 0; count_t = 0; - for( int i = 0 ; i< 10; i++ ) + for (int i = 0; i < 10; i++) { - digitalWrite( 0, i % 2 ); + digitalWrite(0, i % 2); delay(10); - //pin36_adc = analogRead(36); - if(( digitalRead(36) == HIGH )&&( i%2 == 1 )) + // pin36_adc = analogRead(36); + if ((digitalRead(36) == HIGH) && (i % 2 == 1)) { - count_u ++; + count_u++; } - if(( digitalRead(25) == HIGH )&&( i%2 == 1 )) + if ((digitalRead(25) == HIGH) && (i % 2 == 1)) { count_t++; } } Disbuff.setCursor(110, 7); - if( count_u >= 5 ) + if (count_u >= 5) { Disbuff.setTextColor(TFT_GREEN); Disbuff.printf(" %d G0", count_u); @@ -1043,7 +1052,7 @@ Disbuff.setTextColor(TFT_WHITE); Disbuff.setCursor(110, 37); - if( count_t >= 5 ) + if (count_t >= 5) { Disbuff.setTextColor(TFT_GREEN); Disbuff.printf(" %d G25", count_t); @@ -1055,23 +1064,23 @@ } Disbuff.setTextColor(TFT_WHITE); - digitalWrite(0,0); - digitalWrite(26,0); + digitalWrite(0, 0); + digitalWrite(26, 0); count_u = 0; - for( int i = 0 ; i< 10; i++ ) + for (int i = 0; i < 10; i++) { - digitalWrite( 26, i % 2 ); + digitalWrite(26, i % 2); delay(10); - //pin36_adc = analogRead(36); - if(( digitalRead(36) == HIGH )&&( i%2 == 1 )) + // pin36_adc = analogRead(36); + if ((digitalRead(36) == HIGH) && (i % 2 == 1)) { - count_u ++; + count_u++; } } Disbuff.setCursor(110, 67); - if( count_u >= 5 ) + if (count_u >= 5) { Disbuff.setTextColor(TFT_GREEN); Disbuff.printf(" %d G26", count_u); @@ -1083,28 +1092,28 @@ } Disbuff.setTextColor(TFT_WHITE); - digitalWrite(0,0); - digitalWrite(26,0); - //Serial.printf("G36 Vol:%d\n",analogRead(36)); - - if( count >= 10 ) + digitalWrite(0, 0); + digitalWrite(26, 0); + // Serial.printf("G36 Vol:%d\n",analogRead(36)); + + if (count >= 10) { count = 0; - getTempAndHum(&tempdata, &humdata);// ENV. II SENSORをつないだときに、温度と湿度がとれる + getTempAndHum(&tempdata, &humdata); // ENV. II SENSORをつないだときに、温度と湿度がとれる } - Disbuff.setTextColor(TFT_WHITE);//白色で + Disbuff.setTextColor(TFT_WHITE); //白色で Disbuff.setCursor(12, 67); - Disbuff.printf(" %.1f",tempdata);//温度? + Disbuff.printf(" %.1f", tempdata); //温度? Disbuff.setCursor(12, 97); - Disbuff.printf(" %.1f",humdata);//湿度? - - Disbuff.pushSprite(0,0); + Disbuff.printf(" %.1f", humdata); //湿度? - checkAXPPress(); + Disbuff.pushSprite(0, 0); + + checkAXPPress(); M5.update(); delay(10); - count ++; + count++; } while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) { @@ -1120,7 +1129,98 @@ InitI2SMicroPhone(); } -void ColorBar() +esp_now_peer_info_t peerInfo; +void ESP_NOW_SendShutdown() // Bボタンを押したら、ESPNOWで周辺デバイスの電源OFF +{ + while ((!M5.BtnA.isPressed()) && (!M5.BtnB.isPressed())) + { + Disbuff.fillRect(0, 0, 240, 135, TFT_BLUE); + Disbuff.setTextColor(TFT_YELLOW); + Disbuff.setTextSize(3); + Disbuff.setCursor(12, 20); + Disbuff.printf("Press B to shutdown all devices "); + + Disbuff.pushSprite(0, 0); + checkAXPPress(); + M5.update(); + delay(100); + } + while ((M5.BtnA.isPressed()) || (M5.BtnB.isPressed())) + { + if (M5.BtnB.isPressed()) + { + uint8_t data[2] = {111, 222}; // 送信データ + esp_err_t result = esp_now_send(peerInfo.peer_addr, data, sizeof(data)); + Serial.print("Send Status: "); + if (result == ESP_OK) + { + Serial.println("Success"); + } + else if (result == ESP_ERR_ESPNOW_NOT_INIT) + { + Serial.println("ESPNOW not Init."); + } + else if (result == ESP_ERR_ESPNOW_ARG) + { + Serial.println("Invalid Argument"); + } + else if (result == ESP_ERR_ESPNOW_INTERNAL) + { + Serial.println("Internal Error"); + } + else if (result == ESP_ERR_ESPNOW_NO_MEM) + { + Serial.println("ESP_ERR_ESPNOW_NO_MEM"); + } + else if (result == ESP_ERR_ESPNOW_NOT_FOUND) + { + Serial.println("Peer not found."); + } + else + { + Serial.println("Not sure what happened"); + } + } + M5.update(); + checkAXPPress(); + M5.Beep.tone(4000); + delay(10); + } + delay(50); + M5.Beep.mute(); + Disbuff.setTextColor(TFT_WHITE); +} +// 引用: https://101010.fun/iot/esp32-m5stickc-plus-esp-now.html +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); + for (int i = 0; i < data_len; i++) + { + Serial.print(data[i]); + Serial.print(" "); + if (data[i] == 222) + { + M5.Axp.Write1Byte(0x32, M5.Axp.Read8bit(0x32) | 0x80); + } + } +} +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]); + Serial.print("Last Packet Sent to: "); + Serial.println(macStr); + Serial.print("Last Packet Send Status: "); + Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); +} + +void ColorBar() // 起動直後のカラーバー表示 { float color_r, color_g, color_b; @@ -1128,7 +1228,7 @@ color_g = 0; color_b = 255; - for (int i = 0; i < 384; i=i+4) + for (int i = 0; i < 384; i = i + 4) { if (i < 128) { @@ -1227,102 +1327,129 @@ delay(500); } - uint8_t addrcheckbuff[3] = { 0x34, // 0x51, // 0x68 // }; -int checkI2CAddr() +int checkI2CAddr() // I2C 通信のテスト { uint8_t faild_count = 0; - do{ + do + { faild_count = 0; for (int i = 0; i < sizeof(addrcheckbuff); i++) { Wire1.beginTransmission(addrcheckbuff[i]); - if( Wire1.endTransmission() == ESP_OK ) + if (Wire1.endTransmission() == ESP_OK) { - Serial.printf("find %02X addr successful\r\n",addrcheckbuff[i]); + Serial.printf("find %02X addr successful\r\n", addrcheckbuff[i]); } else { - Serial.printf("find %02X addr faild\r\n",addrcheckbuff[i]); + Serial.printf("find %02X addr faild\r\n", addrcheckbuff[i]); char strbuff[128]; - sprintf(strbuff,"i2c %02X fail",addrcheckbuff[i]); - ErrorDialog(addrcheckbuff[i],(const char *)strbuff); - faild_count ++; + sprintf(strbuff, "i2c %02X fail", addrcheckbuff[i]); + ErrorDialog(addrcheckbuff[i], (const char *)strbuff); + faild_count++; } } - }while( faild_count != 0 ); + } while (faild_count != 0); return 0; } -void setup() +void setup() { M5.begin(); - Wire.begin(32,33); + Wire.begin(32, 33); // GROOVE端子のI2C通信 - M5.Lcd.setRotation(3); - + M5.Lcd.setRotation(3); // 画面向きは横 + Disbuff.createSprite(240, 135); - Disbuff.fillRect(0,0,240,135,Disbuff.color565(10,10,10)); - Disbuff.pushSprite(0,0); + Disbuff.fillRect(0, 0, 240, 135, Disbuff.color565(10, 10, 10)); + Disbuff.pushSprite(0, 0); delay(500); M5.update(); - if (M5.BtnB.isPressed()) + if (M5.BtnB.isPressed()) // Bボタンを押して起動したら、テストモード { M5.Beep.tone(4000); delay(100); M5.Beep.mute(); TestMode = true; - - while(M5.BtnB.isPressed()) + + while (M5.BtnB.isPressed()) { M5.update(); delay(10); } } - M5.Axp.ScreenBreath(12); + M5.Axp.ScreenBreath(12); // 画面明るさ調整  8〜15 + + // 引用: https://101010.fun/iot/esp32-m5stickc-plus-esp-now.html + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + if (esp_now_init() == ESP_OK) + { + Serial.println("ESP-Now Init Success"); + } + else + { + Serial.println("ESP-Now Init failed"); + } + for (int i = 0; i < 6; ++i) + { + peerInfo.peer_addr[i] = (uint8_t)0xff; + } + peerInfo.channel = 0; + peerInfo.ifidx = WIFI_IF_AP; + peerInfo.encrypt = false; + if (esp_now_add_peer(&peerInfo) != ESP_OK) + { + Serial.println("Failed to add peer"); + return; + } + esp_now_register_send_cb(onESPNOWSent); + esp_now_register_recv_cb(onESPNOWReceive); ColorBar(); checkI2CAddr(); - checkAXP192(); + checkAXP192(); // バッテリー電圧チェック。低下してたらエラーメッセージ表示 - M5.Imu.Init(); - InitI2SMicroPhone(); - InitIRTx(); - InitBLEServer(); + M5.Imu.Init(); // 加速度・角速度(ジャイロ)初期化 + InitI2SMicroPhone(); // マイク初期化 + InitIRTx(); // 赤外線通信 初期化 +#ifdef ENABLE_BLE + InitBLEServer(); // Bluetooth Low Energyサーバ初期化 +#endif - pinMode(10, OUTPUT); - timerSemaphore = xSemaphoreCreateBinary(); - timer = timerBegin(0, 80, true); - timerAttachInterrupt(timer, &onTimer, true); - timerAlarmWrite(timer, 50000, true); + pinMode(10, OUTPUT); // LEDのポートを出力に設定 + timerSemaphore = xSemaphoreCreateBinary(); // バイナリセマフォ作成 + timer = timerBegin(0, 80, true); // タイマーID=0, 80クロックで1カウントする, カウントアップならtrue + timerAttachInterrupt(timer, &onTimer, true); // 割り込み関数onTimer()を登録 + timerAlarmWrite(timer, 30000, true); // トリガー条件。50000カウントで発動。trueは繰り返し実行(falseにすると1回のみ) timerAlarmEnable(timer); - - xSemaphore = xSemaphoreCreateMutex(); + + xSemaphore = xSemaphoreCreateMutex(); // ミューテックス排他制御 start_dis = xSemaphoreCreateMutex(); start_fft = xSemaphoreCreateMutex(); - xSemaphoreTake(start_dis, portMAX_DELAY); - xSemaphoreTake(start_fft, portMAX_DELAY); + xSemaphoreTake(start_dis, portMAX_DELAY); // フーリエ変換のときの画面表示タスクを「待ち」状態にする + xSemaphoreTake(start_fft, portMAX_DELAY); // FFTタスクを「待ち」状態にする xTaskCreate(Drawdisplay, "Drawdisplay", 1024 * 2, (void *)0, 4, &xhandle_display); xTaskCreate(MicRecordfft, "MicRecordfft", 1024 * 2, (void *)0, 5, &xhandle_fft); Disbuff.pushSprite(0, 0); - } bool beepstate = false; -void loop() +void loop() { // それぞれのテスト中は、関数のなかのループがまわる // AorBボタンを押したら、現在実行中の関数のループを抜け、次の関数を実行する @@ -1331,12 +1458,17 @@ DisplayMicro(); DisIRSend(); - if( TestMode ) + if (TestMode) { DisplayTestMode(); } +#ifdef ENABLE_BLE DisPlayBLESend(); +#else + ESP_NOW_SendShutdown(); +#endif + M5.update(); delay(50); Serial.println("end of loop");