#include <Wire.h>
#include <LiquidCrystal_I2C.h>

int virPin = 12;
boolean mutex = false;

byte smiley[8] = {
  B00000,
  B10001,
  B00000,
  B00000,
  B10001,
  B01110,
  B00000,
};

byte fullheart[8] = {0b00000, 0b01010, 0b11111, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000};
//byte halfheart[8] ={0b00000,0b01010,0b10101, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000};
//byte halfheart[8] ={0b0000,0b01010,0b10101,0b10001,0b11111,0b01110,0b00100,0b00000};
byte lessheart[8] ={0b0000,0b01010,0b10101,0b10001,0b10001,0b01010,0b00100,0b00000};

// just some reference flags
int stringStart = 0;
int stringStop = 16;
int scrollCursor = 0;
int prevHr = 0;
String displayString="";

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
   Serial.begin(115200);  // 用於手動輸入文字
  lcd.begin(16, 2);
  lcd.createChar(0, smiley);
  lcd.createChar(1, fullheart);
  //lcd.createChar(2, halfheart);
   lcd.createChar(3, lessheart);
  
  pinMode(virPin, OUTPUT);
}


void displayMon(int pulse,boolean &mutex,String hint,int life)
{
  if(prevHr!=pulse)
  {
   lcd.clear();
   }

  if(pulse >100)
  {
    if(mutex == false)
  {
    lcd.backlight();
  }
  else if(mutex ==true)
  {
   lcd.noBacklight();
  }
  }
  else
  {
    lcd.backlight();
   }

  mutex = !mutex;
  
  lcd.setCursor(0, 0); // 設定游標位置在第一行行首
  lcd.print("HR: ");
  lcd.print(pulse);
  lcd.setCursor(8, 0);
  lcd.print("Life:");
if(life == 3)
{lcd.write(byte(1));
  lcd.write(byte(1));
  lcd.write(byte(1));
  }
  else if(life == 2)
  {
    lcd.write(byte(1));
  lcd.write(byte(1));
  lcd.write(byte(3));
 }
 else if(life == 1)
 {
  lcd.write(byte(1));
  lcd.write(byte(3));
  lcd.write(byte(3));
 }
  else if(life == 0)
 {
  lcd.write(byte(3));
  lcd.write(byte(3));
  lcd.write(byte(3));
 }
 if(hint.length()>16)
 {
  lcd.setCursor(scrollCursor, 1);
  displayString = hint.substring(stringStart,stringStop);
  if(stringStop == hint.length())
  {
   displayString += " ";
   int k = map(stringStop-stringStart,16,0,0,16);
   displayString += hint.substring(0,k);
   
  }
  lcd.print(displayString);
  if(stringStart == 0 && scrollCursor > 0){
    scrollCursor--;
    stringStop++;
  } else if (stringStart==stringStop){
    stringStart = 0;
    stringStop = 16;
    scrollCursor = 0;
    displayString = "";
  } else if (stringStop == hint.length() && scrollCursor == 0) {
    stringStart++;
  } 
  else {
    stringStart++;
    stringStop++;
  }
 }
 else
 {
  lcd.setCursor(0,1);
  lcd.print(hint);
  
  }
  prevHr = pulse;
 }


void heartbeat(int pulse)
{
  if(pulse <80)
     pulse =80;
  else if(pulse>140)
     pulse =140;
  int speed = map(pulse,80,140,1000,400);
  
  digitalWrite(virPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(80);                       // wait for a second
  digitalWrite(virPin, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
   digitalWrite(virPin, HIGH);
    delay(80);  
   digitalWrite(virPin, LOW);
   delay(speed);

}




// the loop function runs over and over again forever
void loop() {
  int testpulse = 80;
  String testword ="Don't open the door,You will regret ";
  int testlife = 3;
 for(int i=testpulse;i<=140;i=i+5)
 { 
  if(i==100)
  {
    testlife =2;
    }
      if(i==120)
  {
    testlife =1;
    }
          if(i==135)
  {
    testlife =1;
    }
heartbeat(i);
displayMon(i,mutex,testword,testlife);
 }
}
