// #include <u8g2lib.h>
#include <U8g2lib.h>
#include <Wire.h>
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ -1); // All Boards without Reset of the Display
//**************************************************************************
//*********** REAL PROGRAM *************************************************
//**************************************************************************
int draw_state = 0;
void statusDisp(String strOne /*value*/, String strThree/*unit*/) {
u8g2.setFont( u8g2_font_fub25_tn);
u8g2.drawStr(45, 36, strOne.c_str());
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.drawStr( 45 , 60 , strThree.c_str() );
u8g2.setDrawColor(1);
}
void infoDisplay(uint8_t index, float hrate, float spo2, float tempc){
float hstatus = 1 - ((abs(hrate - 100) / hrate) + (abs(spo2 - 97.5) / spo2) + (abs(tempc - 36.85) / tempc));
String hrStr;
switch(index){
case 0:
hrStr = String((int)hrate);
statusDisp(hrStr, "Bpm") ;
// u8g2.setFont(u8g2_font_unifont_t_symbols);
u8g2.drawUTF8(114, 36, "h");
break;
case 1:
hrStr = String((int)spo2);
statusDisp(hrStr, " ") ;
// u8g2.setFont(u8g2_font_unifont_t_symbols);
u8g2.drawUTF8(114, 36, "%");
break;
case 2:
hrStr = String((int)tempc);
statusDisp(hrStr, "Celcius");
// u8g2.setFont(u8g2_font_unifont_t_symbols);
u8g2.drawUTF8(114, 36, "°");
break;
case 3:
float hstatus = 1 - ((abs(hrate - 100) / hrate) + (abs(spo2 - 97.5) / spo2) + (abs(tempc - 36.85) / tempc));
hrStr = String( (int)(hstatus* 100));
if(hstatus >0.8){
statusDisp(hrStr, "Excellent" );
u8g2.drawUTF8(114, 36, "%");
}
else if(hstatus > 0.6){
statusDisp(hrStr, "Good");
u8g2.drawUTF8(114, 36, "%");
}
else if(hstatus > 0.4){
statusDisp(hrStr, "Normal");
u8g2.drawUTF8(114, 36, "%");
}
else if(hstatus > 0.2){
statusDisp(hrStr, "Bad");
u8g2.drawUTF8(114, 36, "%");
}
else{
statusDisp(hrStr, "Need attention!");
u8g2.drawUTF8(114, 36, "%");
}
break;
default:
hrStr = String((int)hrate); break;
statusDisp(" ", " ");
}
}
class Menu { // The class
public: // Access specifier
uint8_t tabWidth = 40;
bool active = false;
String options[4] = { "Hrate", "Oxyg", "Temp", "Stat"};
uint8_t style = 0 ;// 0x00 => bottom, 0x01 =>leftVertical, 0x02 =>rightVertical, 0x03 =>top
};
Menu thisMenu;
void preset(void){
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.setFontRefHeightExtendedText();
u8g2.drawRFrame(0, 0, 40, 15, 3);
u8g2.drawStr( 4 , 12 , thisMenu.options[0].c_str());
u8g2.drawRFrame(0, 16, 40, 15, 3);
u8g2.drawStr( 4 , 1 * 16 + 12 , thisMenu.options[1].c_str());
u8g2.drawRFrame(0, 32, 40, 15, 3);
u8g2.drawStr( 4 , 2 * 16 + 12 , thisMenu.options[2].c_str());
u8g2.drawRFrame(0, 48, 40, 15, 3);
u8g2.drawStr( 4 , 3 * 16 + 12 , thisMenu.options[3].c_str());
u8g2.drawRFrame(41, 0, 87, 64, 5);
}
void menuinit(int selector){
// u8g2.setFont(u8g2_font_unifont_t_symbols);
// u8g2.setFont(u8g2_font_5x8_f);
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.setFontRefHeightExtendedText();
u8g2.drawRBox(0, selector * 16, 40, 16, 3);
u8g2.setDrawColor(0);
u8g2.drawStr( 4 , selector * 16 + 12 , thisMenu.options[selector].c_str());
u8g2.setDrawColor(1);
}
void menuShow(){
thisMenu.active = true;
}
void menuHide(){
thisMenu.active = false;
}
void toggleMenu(){
thisMenu.active = !(thisMenu.active);
}
void setup(void) {
u8g2.begin();
u8g2.setDrawColor(1);
}
void loop(void) {
// menu loop
u8g2.clearBuffer();
preset();
menuinit(draw_state);
float hrate = random( 70, 110) ;
float spo2 = random( 80, 99);
float tempc = random( 34, 40);
infoDisplay( draw_state, hrate, spo2, tempc);
u8g2.sendBuffer();
// increase the state
draw_state++;
if ( draw_state >= 4 )
draw_state = 0;
// deley between each page
delay(2000);
}