M5StickC로 접촉 확인 앱 감지
4694 단어 M5StickC
#include <Arduino.h>
#include <M5StickC.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 4;
BLEScan* pBLEScan;
// 接触確認アプリのUUID
const char* uuid = "0000fd6f-0000-1000-8000-00805f9b34fb";
int deviceNum = 0;
#define LISTDEVMAX 7
typedef struct DEVICE {
int rssi;
String addr;
};
DEVICE devices[LISTDEVMAX];
int pivot(int i, int j) {
int k = i + 1;
while (k <= j && devices[i].rssi == devices[k].rssi) k++;
if (k > j) return -1;
if (devices[i].rssi <= devices[k].rssi) return i;
return k;
}
int partition(int i, int j, int x) {
int l = i, r = j;
while (l <= r) {
while (l <= j && devices[l].rssi > x) l++;
while (r >= i && devices[r].rssi <= x) r--;
if (l > r) break;
String addr = devices[l].addr;
int rssi = devices[l].rssi;
devices[l].addr = devices[r].addr;
devices[l].rssi = devices[r].rssi;
devices[r].addr = addr;
devices[r].rssi = rssi;
l++; r--;
}
return l;
}
void quickSort(int i, int j) {
if (i == j) return;
int p = pivot(i, j);
if (p != -1) {
int k = partition(i, j, devices[p].rssi);
quickSort(i, k - 1);
quickSort(k, j);
}
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if(advertisedDevice.haveServiceUUID()){
if(strncmp(advertisedDevice.getServiceUUID().toString().c_str(),uuid, 36) == 0){
if (deviceNum<LISTDEVMAX){
devices[deviceNum].rssi = advertisedDevice.getRSSI();
devices[deviceNum].addr = advertisedDevice.getAddress().toString().c_str();
}
deviceNum++;
Serial.print("RSSI: ");
Serial.println(devices[deviceNum].rssi);
Serial.print("ADDR: ");
Serial.println(devices[deviceNum].addr);
Serial.println("Found!");
}
}
}
};
void drawScreen() {
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.printf("COCOAs: %2d \n", deviceNum);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(RED);
M5.Lcd.printf(" Bat:%5.1fV Chg:%5.1f\n", M5.Axp.GetBatVoltage(), M5.Axp.GetBatCurrent());
M5.Lcd.setTextColor(BLUE);
quickSort(0, deviceNum - 1);
for(int i=0; i<deviceNum; i++){
M5.Lcd.printf(" %s %03d \n", devices[i].addr.c_str(), devices[i].rssi);
}
for(int i=deviceNum; i<LISTDEVMAX; i++){
M5.Lcd.print(" \n");
}
}
void Task1(void *pvParameters) {
// loop()書くとBLEスキャン中M5.update()が実行されなくてボタンが取れないのでマルチスレッド化している
while(1) {
deviceNum = 0;
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(deviceNum);
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
drawScreen();
}
}
void setup() {
M5.begin();
Serial.begin(115200);
Serial.println("Scanning...");
M5.Lcd.setRotation(1);
M5.Axp.ScreenBreath(8);
M5.Lcd.fillScreen(BLACK);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(5000); // スキャン間隔5秒
pBLEScan->setWindow(4999); // less or equal setInterval value
xTaskCreatePinnedToCore(Task1,"Task1", 4096, NULL, 3, NULL, 1);
}
void loop() {
M5.update();
if ( M5.BtnA.wasReleased() ) {
M5.Axp.PowerOff();
}
}
Reference
이 문제에 관하여(M5StickC로 접촉 확인 앱 감지), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/uniqode/items/dbbaaded2913e824fede텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)