Author Topic: Simblee - Draw Text function  (Read 1354 times)

lsnyman

  • RFduino Sr. Member
  • ****
  • Posts: 141
  • Karma: +1/-0
    • View Profile
Simblee - Draw Text function
« on: May 09, 2016, 08:56:03 PM »
Using Simblee for Mobile, I am hitting a brick wall trying to figure how to use the drawText function to send  changing lines of text to the phone, meaning the text may change depending on program execution or sensor values etc.

For reference I am using the SimbleeForMobile Temperature example which prints a changing numerical (Temp) to the screen but the text C is a constant. I want to use the same structure but also vary the Text portion.

Code: [Select]
#include <SimbleeForMobile.h>

#include "thermometer_png.h"

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 = "temp";

  // 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 2014";

  // start SimbleeForMobile
  SimbleeForMobile.begin();
}

bool first_sample;

float first_temp;
float min_temp;
float max_temp;
float temp_range;

uint8_t text;
uint8_t mercury;

// include newlib printf float support (%f used in sprintf below)
asm(".global _printf_float");

void loop()
{
  // sample once per second
  // todo: Simblee_ULPDelay( SECONDS(1) );

  if (SimbleeForMobile.updatable)
  {
    // 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);
  }

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

void SimbleeForMobile_onConnect()
{
  first_sample = true;
}

void ui()
{
  #define  IMAGE1  1

  SimbleeForMobile.beginScreen(WHITE);

  // \xb0 is the symbol for "degrees"
  SimbleeForMobile.drawText(280, 248, "\xb0" "C", BLUE);
  text = SimbleeForMobile.drawText(125, 240, "", BLUE, 45);

  // usable area: 56, 136, 51, 262
  // mercury area: 65, 136, 33, 262 + 15
  mercury = SimbleeForMobile.drawRect(65, 136, 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);

  SimbleeForMobile.endScreen();
}

void ui_event(event_t &event)
{
}

How can I make the "C" in the line
SimbleeForMobile.drawText(280, 248, "\xb0" "C", BLUE);
a string variable that can change. Meaning it may read "Cold", "Warm" or "Hot" depending on temperature.

The reference document says the function structure is
uint8_t   drawText(uint16_t   x,   uint16_t   y,   const   char   *title,   color_t   color);

Ive been staring at it too long, Any help appreciated.

tolson

  • Global Moderator
  • *****
  • Posts: 812
  • Karma: +19/-0
    • View Profile
    • Thomas Olson Consulting
Re: Simblee - Draw Text function
« Reply #1 on: May 09, 2016, 10:00:37 PM »

How can I make the "C" in the line
SimbleeForMobile.drawText(280, 248, "\xb0" "C", BLUE);
a string variable that can change. Meaning it may read "Cold", "Warm" or "Hot" depending on temperature.

The reference document says the function structure is
uint8_t   drawText(uint16_t   x,   uint16_t   y,   const   char   *title,   color_t   color);

Ive been staring at it too long, Any help appreciated.

HINT:
Assign it's ID to a const that you can reference.

uint8_t heatID;

//Move the string to left enough to handle the longest string you want to display.
heatID = SimbleeForMobile.drawText(240, 248, "\xb0" "C", BLUE);

In your loop() you can decide what to change the text to based on your desired boundary conditions...
For example...
Code: [Select]
// update heat value
char *heat;
  heat = "Warm";
  if (temp < 31){
    heat = "Cold";
  }else if (temp > 32){
    heat = "Hot";
  }
SimbleeForMobile.updateText(heatID, heat);
« Last Edit: May 09, 2016, 10:20:32 PM by tolson »

bsiever

  • RFduino Full Member
  • ***
  • Posts: 89
  • Karma: +4/-0
    • View Profile
Re: Simblee - Draw Text function
« Reply #2 on: May 10, 2016, 05:40:29 AM »
You can also use sprintf() to create a formatted string with mixed data.  For example, here's a snippet that includes an integer in a character string:
Code: [Select]
void ui_event(event_t &event)
{
  if(event.id == ....) {
    char buffer[25];  // Enough space for entire formatted string
    // The only event possible should be the button
    unsigned newValue = random(0,30);
    sprintf(buffer, "Temp %d Deg C", newValue);
    SimbleeForMobile.updateText(IDofTextBox, buffer);
(Assume IDofTextBox is a global variable that contains the ID of the textbox that needs to be updated).

This can be done with character strings as well.  Building on tolson's example:
Code: [Select]
  char *heat;
  heat = "Warm";
  if (temp < 31){
    heat = "Cold";
  }else if (temp > 32){
    heat = "Hot";
  }
  sprintf(buffer, "Temp %s !!!", heat);
  SimbleeForMobile.updateText(IDofTextBox, buffer);

  Bill

lsnyman

  • RFduino Sr. Member
  • ****
  • Posts: 141
  • Karma: +1/-0
    • View Profile
Re: Simblee - Draw Text function
« Reply #3 on: May 10, 2016, 05:56:01 AM »
Thank you guys. I guess I was overlooking the uint8_t ID concept, and pointers trip me up every time.
I appreciate your help very much. ;D