Author Topic: SimbleeForMobile displaying numbers for ios system  (Read 279 times)

Stanley

  • RFduino Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
SimbleeForMobile displaying numbers for ios system
« on: August 24, 2017, 11:35:35 PM »
Hi all,

Recently I have been working on creating two screens for my mobile app. I would like to display numbers on each screen. For the first screen, it will generate random number, which is done in void loop() using random(80,120) for example. The other screen, I just copy the codes from library to display temperature reading. But I found that there was no number displayed on the second screen and the numbers on the first screen were not expected. I wonder if it is correct to have "if....else if" statement or "switch case" statement inside void loop().

Can anyone help me with this? Thanks in advance.


The following is my codes.

Code: [Select]

#include <SimbleeForMobile.h>

 #include "thermometer_png.h"
 #include "heart6_png.h"
 #include "ss_jpg.h"
 #define  IMAGE1  2
 #define  IMAGE2  5
 #define  IMAGE3  6
 #define HRSPO2_SCREEN 1
 #define TEMP_SCREEN 2
 
int segment;
int fly;
int fly1=0;
int hr ;
int spo2 ;
char buf1[16];
char buf2[16];
char segmentText;
int slaveSegment;
int currentScreen;
int toTextTestingButtinID;
int toHRSPO2ScreenButtonID;
void setup()
{
  // this is the data we want to appear in the advertisement
  // (if the deviceName and advertisementData are too long to fix into the 31 byte
  // ble advertisement packet, then the advertisementData is truncated first down to
  // a single byte, then it will truncate the deviceName)
  SimbleeForMobile.advertisementData = "Data";

  // use a subdomain to create an application specific cache
  SimbleeForMobile.domain = "temp.Simblee.com";

  // establish a baseline to use the cache during development to bypass uploading
  // the image each time
  SimbleeForMobile.baseline = "Oct 23 2017";

  // start SimbleeForMobile
  SimbleeForMobile.begin();
   randomSeed(analogRead(0));
}

bool first_sample;
float first_temp;
float min_temp;
float max_temp;
float temp_range;

uint8_t text;
uint8_t mercury;
uint8_t text1;
uint8_t text2;
unsigned long lastUpdated = 0;
unsigned long updateRate = 500; // half a second
unsigned long loopTime = millis();
// include newlib printf float support (%f used in sprintf below)
asm(".global _printf_float");

void loop()

 
   if(fly1==0)
   RandomNumberLoop();
   else if (fly1==1)
   TempLoop();
 



  // process must be called in the loop for SimbleeForMobile
  SimbleeForMobile.process();
}

void SimbleeForMobile_onConnect()
{

   currentScreen = -1; 
   first_sample = true;
   
}
void SimbleeForMobile_onDisconnect()
{
}

void ui()
{
 if (SimbleeForMobile.screen == currentScreen) return;

  currentScreen = SimbleeForMobile.screen;
  switch (SimbleeForMobile.screen)
  {
    case HRSPO2_SCREEN:
      createHRSPO2Screen();
      break;

    case TEMP_SCREEN:
      createTempScreen();
      break;
   
    default:
      Serial.print("ui: Uknown screen requested: ");
      Serial.println(SimbleeForMobile.screen);
  }

}

void createTempScreen()
{
  SimbleeForMobile.beginScreen(WHITE);

 text = SimbleeForMobile.drawText(145, 240, "", BLUE, 45);

  // usable area: 56, 136, 51, 262
  // mercury area: 65, 136, 33, 262 + 15
  mercury = SimbleeForMobile.drawRect(65, 156, 33, 262 + 15, rgb(160,0,0), rgb(204,0,0));

  // hide the mercury until the image is uploaded
  SimbleeForMobile.setVisible(mercury, false);
  SimbleeForMobile.imageSource(IMAGE1, PNG, thermometer_png, thermometer_png_len); 
  SimbleeForMobile.drawImage(IMAGE1, 30, 100);
  SimbleeForMobile.setVisible(mercury, true);


  toHRSPO2ScreenButtonID = SimbleeForMobile.drawButton(220, 80, 80, "Back");
  SimbleeForMobile.setEvents(toHRSPO2ScreenButtonID, EVENT_RELEASE);
 


if(SimbleeForMobile.remoteDeviceType == REMOTE_DEVICE_OS_IOS)
  {
      SimbleeForMobile.drawText(270, 300, "\xb0" "C", BLUE,30);
  }
  else if(SimbleeForMobile.remoteDeviceType == REMOTE_DEVICE_OS_ANDROID)
  {
      SimbleeForMobile.drawText(270, 300, "\xc2\xb0" "C", BLUE,30);
  }
  else if(SimbleeForMobile.remoteDeviceType == REMOTE_DEVICE_OS_UNKNOWN)
  {
      SimbleeForMobile.drawText(270, 300, "C", BLUE,30);
  }

  SimbleeForMobile.setVisible(text1,false);
 SimbleeForMobile.setVisible(text,true);
  SimbleeForMobile.endScreen();
  }

void createHRSPO2Screen()
{
 Serial.println("creating Hr and SpO2 reading screen");
 SimbleeForMobile.beginScreen(WHITE);

  int leftMargin = 5;
  int topMargin = 80;
  int segmentWidth = 310;
  int deltaY = 50;
  int textSize = 20;
  int leftMarginButton = 240;
   text1 = SimbleeForMobile.drawText(160, 250, "", BLUE, 45);
  //160,300
   SimbleeForMobile.setVisible(text,false);
  //30 270

    if(SimbleeForMobile.remoteDeviceType == REMOTE_DEVICE_OS_IOS) {
    SimbleeForMobile.drawText(220, 275,  "BPM", BLUE, 20);
    //230,325
  } else if(SimbleeForMobile.remoteDeviceType == REMOTE_DEVICE_OS_ANDROID) {
    SimbleeForMobile.drawText(270, 248, "\xc2\xb0" "C", BLUE);
  } else if(SimbleeForMobile.remoteDeviceType == REMOTE_DEVICE_OS_UNKNOWN) {
    SimbleeForMobile.drawText(270, 248, "C", BLUE);
  }
   
 
 
  text2 = SimbleeForMobile.drawText(180, 400, "", BLUE, 45);
  //160 470
 
  SimbleeForMobile.imageSource(IMAGE3, JPG,  ss_jpg, ss_jpg_len); 
  SimbleeForMobile.drawImage(IMAGE3,60,370);
  //30 460

 
  if(SimbleeForMobile.remoteDeviceType == REMOTE_DEVICE_OS_IOS) {
     SimbleeForMobile.drawText(230, 405,  "%", BLUE, 40);
    //230 475
  } else if(SimbleeForMobile.remoteDeviceType == REMOTE_DEVICE_OS_ANDROID) {
    SimbleeForMobile.drawText(230, 405, "%", BLUE);
  } else if(SimbleeForMobile.remoteDeviceType == REMOTE_DEVICE_OS_UNKNOWN) {
    SimbleeForMobile.drawText(230, 405, "%", BLUE);
  }
 
 SimbleeForMobile.setVisible(text1,true);
 
  toTextTestingButtinID = SimbleeForMobile.drawButton(100, 450, 120, "Temperature");

   SimbleeForMobile.imageSource(IMAGE2, PNG,  heart6_png, heart6_png_len); 
  SimbleeForMobile.drawImage(IMAGE2,60,200);


  char *titles[] = { "Simulate(N)", "Simulate(Abn)", "Sensor" };
  segment = SimbleeForMobile.drawSegment(leftMargin, topMargin, segmentWidth, titles, countof(titles));
  SimbleeForMobile.updateValue(segment, 0);

  topMargin += deltaY;
  segmentText = SimbleeForMobile.drawText(leftMargin, topMargin, "Displaying: ", BLACK, textSize);
  topMargin += 3 * deltaY / 4;

    SimbleeForMobile.setEvents(toTextTestingButtinID, EVENT_RELEASE);
    SimbleeForMobile.endScreen();
   
    Serial.println("Hr and SpO2 reading screen created");
  }






 
void ui_event(event_t &event)
{
 
     switch (currentScreen)
  {
    case HRSPO2_SCREEN:
      handleHRSPO2ScreenEvents(event);
      break;
   
    case TEMP_SCREEN:
      handleTempScreenEvents(event);
      break;

   
  }


}



void handleHRSPO2ScreenEvents(event_t&event)
{
      if (event.id == segment)
      {
          char buffer[200];
          char *msg1[]={"Normal Range"};
          char *msg2[]={"Abnormal Range"};
          char *msg3[]={"Data from sensor"};

          switch(event.value)
         {
            case 0: sprintf(buffer, "Displaying: %s", *msg1);
                    fly=0;
                    break;
            case 1: sprintf(buffer, "Displaying: %s", *msg2);
                    fly=1;         
                    break;
            case 2: sprintf(buffer, "Displaying: %s", *msg3);
                    break;
          }
     
           
          SimbleeForMobile.updateText(segmentText, buffer);
          SimbleeForMobile.updateValue(slaveSegment, event.value);
     
      }
     
      if (event.id == toTextTestingButtinID && event.type == EVENT_RELEASE)
      {
          SimbleeForMobile.showScreen(TEMP_SCREEN);
          fly1=0;
      }
}

void handleTempScreenEvents(event_t&event)
{
   if (event.id == toHRSPO2ScreenButtonID && event.type == EVENT_RELEASE)
   {
    SimbleeForMobile.showScreen(HRSPO2_SCREEN);
    fly1=1;
   } 
}


void TempLoop()
{
 
  if (SimbleeForMobile.updatable && updateRate < (loopTime - lastUpdated))
  {
    lastUpdated = loopTime;
   
    // get a cpu temperature sample
    // degrees c (-128.00 to +127.00)
    // degrees f (-198.00 to +260.00)
    // CELSIUS / FAHRENHEIT
    float temp = Simblee_temperature(CELSIUS);
 
    // requires newlib printf float support
    char buf[16];
    sprintf(buf, "%.02f", temp);
 
    // base everything on the first sample / ambient temperature
    if (first_sample)
    {
      first_temp = temp;
     
      // putting your finger on the Simblee shield raises the temp approx 2 degrees
      min_temp = first_temp - 0.25;
      max_temp = first_temp + 1.0;
      temp_range = max_temp - min_temp;
     
      first_sample = false;
    }

    // update the text first with the actual temp
    SimbleeForMobile.updateText(text, buf);

    // restrict temp to range
    if (temp < min_temp)
      temp = min_temp;
    if (temp > max_temp)
      temp = max_temp;

    int mercury_range = 262;
    int mercury_position = ((temp - min_temp) / temp_range) * mercury_range;

    // invert so the smallest value at the bottom
    mercury_position = mercury_range - (mercury_position);

    // update the mercury
    SimbleeForMobile.updateRect(mercury, 65, 136 + mercury_position, 33, 262 + 15 - mercury_position);
  }

}

void RandomNumberLoop()
{
    if (SimbleeForMobile.updatable && updateRate < (loopTime - lastUpdated))
  {
    lastUpdated = loopTime;
    if(fly==0){
      hr=random(60,80);
      spo2=random(95,100);
    }
    else if(fly==1){
      hr=random(80,120);
      spo2=random(55,95);
    }
    sprintf(buf1, "%d", hr);
    sprintf(buf2, "%d", spo2);
    SimbleeForMobile.updateText(text1, buf1);
    SimbleeForMobile.updateText(text2, buf2);

  } 
 
}

« Last Edit: August 24, 2017, 11:38:49 PM by Stanley »

 

anything