tiz

Dependencies:   TextLCD X_NUCLEO_IKS01A1 func mbed-src mbed

Fork of mas by esproj

func/RawData.cpp

Committer:
herenvarno
Date:
2015-12-14
Revision:
4:3fecfc9eeadd
Parent:
3:4355890e55b4
Child:
5:100310ea8fba

File content as of revision 4:3fecfc9eeadd:

#include "RawData.h"

char buffer2[32];
uint32_t pressure[11]={1000, 950, 850, 700, 500, 400, 300, 250, 200, 150 , 100};
uint32_t altitude[11]={0, 700, 1500, 3000, 5500, 7000, 9000, 10000, 12000, 14000, 16000};

void RawData::str_date(char *buffer)
{
    struct tm *timeinfo = localtime (&(this->__time));
    strftime (buffer, 16, "%y-%m-%d", timeinfo);
}
void RawData::str_time(char *buffer)
{
    struct tm *timeinfo = localtime (&(this->__time));
    strftime (buffer, 16, "%T", timeinfo);
}
void RawData::str_temperature(char *buffer)
{
    if(this->__temp>=0)
        snprintf(buffer, 16, "T: %s", this->printDouble(buffer2, (this->__temp)));
    else
        snprintf(buffer, 16, "T:-%s", this->printDouble(buffer2, -1*(this->__temp)));
}
void RawData::str_humidity(char *buffer)
{
    if(this->__humi<100)
        snprintf(buffer, 16, "H: %s", this->printDouble(buffer2, (this->__humi)));
    else
        snprintf(buffer, 16, "H: 00.00");
}
void RawData::str_pressure(char *buffer)
{
    snprintf(buffer, 16, "P:%sK", this->printDouble(buffer2, (this->__pressure/10),1));
}
void RawData::str_altitude(char *buffer)
{
    uint32_t p=this->__pressure;
    int i=0;
    while(p<pressure[i])
    {
        i++;
        if(i>=11)
            break;
    }
    uint32_t a;
    if(i==0)
        a=altitude[0];
    else if(i>=11)
        a=altitude[10];
    else
        a=(altitude[i]-altitude[i-1])/float(pressure[i]-pressure[i-1])*(p-pressure[i-1])+altitude[i-1];
    snprintf(buffer, 16, "A:%6u", a);
}
void RawData::str_magnetic(char *buffer)
{
    float m=sqrt(float(this->__mag[0]*this->__mag[0]+this->__mag[1]*this->__mag[1]+this->__mag[2]*this->__mag[2]))/1000;
    snprintf(buffer, 16, "M: %s", this->printDouble(buffer2, m, 3));
}
void RawData::str_direction(char *buffer)
{
    snprintf(buffer, 16, "D:100");
}

char* RawData::printDouble(char* str, double v, int decimalDigits)
{
  int i = 1;
  int intPart, fractPart;
  int len;
  char *ptr;

  /* prepare decimal digits multiplicator */
  for (;decimalDigits!=0; i*=10, decimalDigits--);

  /* calculate integer & fractinal parts */
  intPart = (int)v;
  fractPart = (int)((v-(double)(int)v)*i);

  /* fill in integer part */
  sprintf(str, "%i.", intPart);

  /* prepare fill in of fractional part */
  len = strlen(str);
  ptr = &str[len];

  /* fill in leading fractional zeros */
  for (i/=10;i>1; i/=10, ptr++) {
    if(fractPart >= i) break;
    *ptr = '0';
  }

  /* fill in (rest of) fractional part */
  sprintf(ptr, "%i", fractPart);

  return str;
}

void RawData::add_year_10()
{
    struct tm *timeinfo = localtime (&(this->__time));
    int year= timeinfo->tm_year;
    year+=10;
    if(year>=(2100-1900))
        year=timeinfo->tm_year-90;
    timeinfo->tm_year=year;
    this->__time=mktime(timeinfo);
}
void RawData::add_year_1()
{
    struct tm *timeinfo = localtime (&(this->__time));
    timeinfo->tm_year+=1;
    if(timeinfo->tm_year%10==0)
        timeinfo->tm_year-=10;
    this->__time=mktime(timeinfo);
}
void RawData::add_month()
{
    struct tm *timeinfo = localtime (&(this->__time));
    int mon=timeinfo->tm_mon;
    mon+=1;
    if(mon>11)
        mon=0;
    timeinfo->tm_mon=mon;
    this->__time=mktime(timeinfo);
}
void RawData::add_day()
{
    struct tm *timeinfo = localtime (&(this->__time));
    timeinfo->tm_yday=0;
    timeinfo->tm_wday=0;
    int day=timeinfo->tm_mday;
    day+=1;
    if(timeinfo->tm_mon==0 || timeinfo->tm_mon==2 || timeinfo->tm_mon==4 || timeinfo->tm_mon==6 || timeinfo->tm_mon==7 || timeinfo->tm_mon==9 || timeinfo->tm_mon==11)
    {
        if(day>31)
            day=1;
    }
    else if(timeinfo->tm_mon==1)
    {
        if((timeinfo->tm_year+1900)%400==0 || ((timeinfo->tm_year+1900)%100!=0 && (timeinfo->tm_year+1900)%4==0) )
        {
            if(day>29)
                day=1;
        }
        else
        {
            if(day>28)
                day=1;
        }
    }
    else
    {
        if(day>30)
            day=1;
    }
    timeinfo->tm_mday=day;
    this->__time=mktime(timeinfo);
}
void RawData::add_hour()
{
    struct tm *timeinfo = localtime (&(this->__time));
    int hour=timeinfo->tm_hour;
    hour+=1;
    if(hour>23)
        hour=0;
    timeinfo->tm_hour=hour;
    this->__time=mktime(timeinfo);
}
void RawData::add_min_10()
{
    struct tm *timeinfo = localtime (&(this->__time));
    int min=timeinfo->tm_min;
    min+=10;
    if(min>=60)
        min-=60;
    timeinfo->tm_min=min;
    this->__time=mktime(timeinfo);
}
void RawData::add_min_1()
{
    struct tm *timeinfo = localtime (&(this->__time));
    int min=timeinfo->tm_min;
    min+=1;
    if(min%10==0)
        min-=10;
    timeinfo->tm_min=min;
    this->__time=mktime(timeinfo);
}
void RawData::add_sec_10()
{
    struct tm *timeinfo = localtime (&(this->__time));
    int sec=timeinfo->tm_sec;
    sec+=10;
    if(sec>=60)
        sec-=60;
    timeinfo->tm_sec=sec;
    this->__time=mktime(timeinfo);
}
void RawData::add_sec_1()
{
    struct tm *timeinfo = localtime (&(this->__time));
    int sec=timeinfo->tm_sec;
    sec+=1;
    if(sec%10==0)
       sec-=10;
    timeinfo->tm_sec=sec;
    this->__time=mktime(timeinfo);
}
void RawData::add_temp_sign()
{
    this->__temp*=-1;
}
void RawData::add_temp_10()
{
    int val=int(this->__temp);
    if(val>=0)
        val+=10;
    else
        val-=10;
    if(val>=100)
        val-=100;
    else if(val<=-100)
        val+=100;
    this->__temp=this->__temp+(val-int(this->__temp));
}
void RawData::add_temp_1()
{
    int val=int(this->__temp);
    if(val>=0)
        val+=1;
    else
        val-=1;
    if(val>=0 && val%10==0)
        val-=10;
    else if(val<0 && val%10==0)
        val+=10;
    this->__temp=this->__temp+(val-int(this->__temp));
}
void RawData::add_temp_1_10()
{
    int val=(this->__temp*10);
    if(val>=0)
        val+=1;
    else
        val-=1;
    if(val>=0 && val%10==0)
        val-=10;
    else if(val<0 && val%10==0)
        val+=10;
    this->__temp=this->__temp+(val-int(this->__temp*10))/10.0;
}
void RawData::add_temp_1_100()
{
    int val=(this->__temp*100);
    if(val>=0)
        val+=1;
    else
        val-=1;
    if(val>=0 && val%10==0)
        val-=10;
    else if(val<0 && val%10==0)
        val+=10;
    this->__temp=this->__temp+(val-int(this->__temp*100))/100.0;
}
void RawData::add_humi_10()
{
    int val=int(this->__humi);
    val+=10;
    if(val>=100)
        val-=100;
    this->__humi=this->__humi+(val-int(this->__humi));
}
void RawData::add_humi_1()
{
    int val=int(this->__humi);
    val+=1;
    if(val%10==0)
        val-=10;
    this->__humi=this->__humi+(val-int(this->__humi));
}
void RawData::add_humi_1_10()
{
    int val=(this->__humi*10);
    val+=1;
    if(val%10==0)
        val-=10;
    this->__humi=this->__humi+(val-int(this->__humi*10))/10.0;
}
void RawData::add_humi_1_100()
{
    int val=(this->__humi*100);
    val+=1;
    if(val%10==0)
        val-=10;
    this->__humi=this->__humi+(val-int(this->__humi*100))/100.0;
}