#include <M5StickCPlus.h>
#include <BluetoothSerial.h>

BluetoothSerial SerialBT;

bool willConnect = true;
bool connected;

const char* dname = "BT_Serial_00"; // Slave device name

void setup() {
  M5.begin();
  M5.Lcd.setRotation(3);
  M5.Lcd.fillScreen(BLUE);

  M5.Lcd.setTextColor(BLACK, CYAN);
  M5.Lcd.setCursor(0, 0, 1);
  M5.Lcd.setTextSize(2);

  Serial.begin(115200);
  SerialBT.begin("BT_Serial_01", true);
  Serial.println("The device started in master mode, make sure remote BT device is on!");

  // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
  // to resolve name to address first, but it allows to connect to different devices with the same name.
  // Set CoreDebugLevel to Info to view devices bluetooth address and device names
  if (false) {
    connected = SerialBT.connect(dname);
  } else {
    // uint8_t address[6]  = {0x24, 0xA1, 0x60, 0x47, 0x84, 0x4E}; // MACアドレスが 24:A1:60:47:84:4E のとき
    uint8_t address[6]  = {0x94, 0xB9, 0x7e, 0xad, 0x53, 0x92}; 
    connected = SerialBT.connect(address);
  }
  if (connected) {
    Serial.println("Connected Succesfully!");
    M5.Beep.tone(2000, 1000);
    M5.Lcd.fillScreen(CYAN);
  }
  // disconnect() may take upto 10 secs max
  //  if (SerialBT.disconnect()) {
  //    connected = false;
  //    Serial.println("Disconnected Succesfully!");
  //  }
  // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
  //SerialBT.connect();
}

void connectOrDisconnect(bool willCon) {
  if (willCon && connected) return;
  if (willCon) {
    if (connected) {
      Serial.println("Connected Succesfully!");
      M5.Beep.tone(2000, 1000);
      M5.Lcd.fillScreen(CYAN);
    } else {
      while (!SerialBT.connected(10000)) {
        Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
        M5.Lcd.fillScreen(PURPLE);
      }
    }
  } else {
    if ( SerialBT.disconnect() ) {
      connected = false;
      Serial.println("Disconnected Succesfully!");
      M5.Lcd.fillScreen(BLUE);
    }
  }
}
int line = 0;
void loop() {
  connectOrDisconnect( willConnect );

  char c;
  if (Serial.available()) {
    SerialBT.write(c = Serial.read());
    Serial.printf("read:%c\n", c);
  }
  if (SerialBT.available()) {
    Serial.write(c = SerialBT.read());
    M5.Lcd.print(c);
    if (c == 0xa) line++;
    if (line > 7) {
      M5.Lcd.fillScreen(CYAN);  M5.Lcd.setCursor(0, 0, 1);
      line = 0;
    }
  }

  M5.update();  M5.Beep.update();
  if (M5.BtnA.wasReleasefor(1000) ) {
    SerialBT.println("[A] was Pressed longer than 1s");
  } else if (M5.BtnA.wasReleased()) {
    SerialBT.println("[A] was Pressed");
  } else if (M5.BtnB.wasReleasefor(1000) ) {
    SerialBT.println("[B] was Pressed longer than 1s");
    if (SerialBT.disconnect()) {
      connected = false;
      M5.Lcd.fillScreen(PURPLE);
    }
  } else if (M5.BtnB.wasReleased()) {
    SerialBT.println("[B] was Pressed");
  } else if (M5.Axp.GetBtnPress() == 2) {
    SerialBT.println("[Power] was Pressed");
  }
  delay(20);
}
