Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
vany5921
Published © MIT

Coronavirus apps with M5Atom Matrix

Try to count how many new coronavirus contact confirmation apps are around with M5 Atom Matrix

IntermediateFull instructions provided2 hours660
Coronavirus apps with M5Atom Matrix

Story

Read more

Code

Untitled file

C/C++
#include <M5Atom.h>
#include <BLEDevice.h>

// 各数字の配列データ 10以上は+で表示
extern const unsigned char image_0[77];
extern const unsigned char image_1[77];
extern const unsigned char image_2[77];
extern const unsigned char image_3[77];
extern const unsigned char image_4[77];
extern const unsigned char image_5[77];
extern const unsigned char image_6[77];
extern const unsigned char image_7[77];
extern const unsigned char image_8[77];
extern const unsigned char image_9[77];
extern const unsigned char image_plus[77];

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 4; //BLEタイムアウト
BLEScan* pBLEScan;

// 接触確認アプリのUUID
const char* uuid = "0000fd6f-0000-1000-8000-00805f9b34fb";

int deviceNum = 0;
bool dark = false;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
       if(advertisedDevice.haveServiceUUID()){
            if(strncmp(advertisedDevice.getServiceUUID().toString().c_str(),uuid, 36) == 0){
                int rssi = advertisedDevice.getRSSI();
                Serial.print("RSSI: ");
                Serial.println(rssi);
                Serial.print("ADDR: ");
                Serial.println(advertisedDevice.getAddress().toString().c_str());
                Serial.println("Found!");
                deviceNum++;
            }
        }

    }
};

void Task1(void *pvParameters) {
  // loop()内にdelay()を書くとM5.update()が実行されなくてボタンが取れないのでマルチスレッド化している
  while(1) {
    deviceNum = 0;
    BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
    switch (deviceNum) {
      case 0:
        M5.dis.displaybuff((uint8_t*)image_0,0,0);
        break;
      case 1:
        M5.dis.displaybuff((uint8_t*)image_1,0,0);
        break;
      case 2:
        M5.dis.displaybuff((uint8_t*)image_2,0,0);
        break;
      case 3:
        M5.dis.displaybuff((uint8_t*)image_3,0,0);
        break;
      case 4:
        M5.dis.displaybuff((uint8_t*)image_4,0,0);
        break;
      case 5:
        M5.dis.displaybuff((uint8_t*)image_5,0,0);
        break;
      case 6:
        M5.dis.displaybuff((uint8_t*)image_6,0,0);
        break;
      case 7:
        M5.dis.displaybuff((uint8_t*)image_7,0,0);
        break;
      case 8:
        M5.dis.displaybuff((uint8_t*)image_8,0,0);
        break;
      case 9:
        M5.dis.displaybuff((uint8_t*)image_9,0,0);
        break;
      default:
        M5.dis.displaybuff((uint8_t*)image_plus,0,0);
        break;
    }
    M5.dis.setBrightness(dark ? 3 : 10);
    Serial.print("Devices found: ");
    Serial.println(deviceNum);
    Serial.println("Scan done!");
    pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
    delay(2000); // LEDの点灯時間
    M5.dis.clear();

  }
}


void setup() {
  M5.begin(true, false, true);
  Serial.begin(115200);
  Serial.println("Scanning...");

  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(100);
  pBLEScan->setWindow(99);  // less or equal setInterval value
  xTaskCreatePinnedToCore(Task1,"Task1", 4096, NULL, 3, NULL, 1);
}

void loop() {
  M5.update();
    if ( M5.Btn.wasReleased() ) {
    // ボタンを押すと明るさチェンジ
    Serial.printf("brightness=%d",dark ? 3 : 10);
    dark = !dark;
  }

}

Credits

coppercele

Posted by vany5921

Comments

Please log in or sign up to comment.