Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
RawData.cpp
00001 #include "RawData.h" 00002 00003 char buffer2[32]; 00004 const uint32_t __pressure_table[16]={1013, 995, 976, 959, 942, 925, 908, 892, 875, 859, 843, 812, 782, 752, 724, 696}; 00005 const uint32_t __altitude_table[16]={0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 6000, 7000, 8000, 9000, 10000}; 00006 const uint8_t __dir_table[5] = {'E', 'N', 'W', 'S', 'E'}; 00007 00008 RawData::RawData() 00009 { 00010 this->__temp_offset=0; 00011 this->__humi_offset=0; 00012 this->__pressure_offset=0; 00013 this->__altitude_offset=0; 00014 this->__acc_offset[0]=0; 00015 this->__acc_offset[1]=0; 00016 this->__acc_offset[2]=0; 00017 this->__gyr_offset[0]=0; 00018 this->__gyr_offset[1]=0; 00019 this->__gyr_offset[2]=0; 00020 this->__mag_offset[0]=0; 00021 this->__mag_offset[1]=0; 00022 this->__mag_offset[2]=0; 00023 } 00024 00025 void RawData::str_date(char *buffer) 00026 { 00027 struct tm *timeinfo = localtime (&(this->__time)); 00028 strftime (buffer, 16, "%y-%m-%d", timeinfo); 00029 } 00030 void RawData::str_time(char *buffer) 00031 { 00032 struct tm *timeinfo = localtime (&(this->__time)); 00033 strftime (buffer, 16, "%T", timeinfo); 00034 } 00035 void RawData::str_temperature(char *buffer) 00036 { 00037 if(this->calc_temperature()>=0) 00038 snprintf(buffer, 16, "T: %sC", this->printDouble(buffer2, (this->calc_temperature()))); 00039 else 00040 snprintf(buffer, 16, "T:-%sC", this->printDouble(buffer2, -1*(this->calc_temperature()))); 00041 } 00042 void RawData::str_humidity(char *buffer) 00043 { 00044 if(this->calc_humidity()<100) 00045 snprintf(buffer, 16, "H: %sPer", this->printDouble(buffer2, (this->calc_humidity()))); 00046 else 00047 snprintf(buffer, 16, "H: 00.0Per"); 00048 } 00049 void RawData::str_altitude(char *buffer) 00050 { 00051 snprintf(buffer, 16, "A: %4uM", this->calc_altitude()); 00052 } 00053 void RawData::str_direction(char *buffer) 00054 { 00055 uint32_t dir = this->calc_cpassdir(); 00056 snprintf(buffer, 16, "D: %c%3uDeg", __dir_table[(dir+45)/90], dir); 00057 } 00058 00059 char* RawData::printDouble(char* str, double v, int integerDigits, int decimalDigits) 00060 { 00061 int i = 1; 00062 int intPart, fractPart; 00063 int len; 00064 char *ptr; 00065 00066 /* prepare decimal digits multiplicator */ 00067 for (;decimalDigits!=0; i*=10, decimalDigits--); 00068 00069 /* calculate integer & fractinal parts */ 00070 int powval=1; 00071 for(int xx=0; xx<integerDigits;xx++) 00072 powval = powval*10; 00073 00074 if(integerDigits>0) 00075 intPart = (int)(v) % powval; 00076 else 00077 intPart = (int)(v); 00078 fractPart = (int)((v-(double)(int)v)*i); 00079 00080 /* fill in integer part */ 00081 sprintf(str, "%i.", intPart); 00082 00083 /* prepare fill in of fractional part */ 00084 len = strlen(str); 00085 ptr = &str[len]; 00086 00087 /* fill in leading fractional zeros */ 00088 for (i/=10;i>1; i/=10, ptr++) { 00089 if(fractPart >= i) break; 00090 *ptr = '0'; 00091 } 00092 00093 /* fill in (rest of) fractional part */ 00094 sprintf(ptr, "%i", fractPart); 00095 00096 return str; 00097 } 00098 00099 00100 float RawData::calc_temperature() 00101 { 00102 return this->__temp+this->__temp_offset; 00103 } 00104 float RawData::calc_humidity() 00105 { 00106 return this->__humi+this->__humi_offset; 00107 } 00108 int32_t RawData::calc_altitude() 00109 { 00110 uint32_t altitude=0; 00111 uint32_t bound_h=0, bound_l=16; 00112 float pressure = (this->__prss+this->__pressure_offset)/10; 00113 for(uint32_t i=0; i<16; i++) 00114 { 00115 if(__pressure_table[i]<= pressure) 00116 { 00117 bound_h=i; 00118 break; 00119 } 00120 bound_l=i; 00121 } 00122 if(bound_h==bound_l){ 00123 this->__pressure_offset = __pressure_table[bound_l]-this->__prss; 00124 altitude = __altitude_table[bound_l]; 00125 }else{ 00126 altitude = __altitude_table[bound_l]+(__altitude_table[bound_h]-__altitude_table[bound_l])/double(__pressure_table[bound_l]-__pressure_table[bound_h])*(__pressure_table[bound_l]-pressure); 00127 } 00128 int32_t a=int32_t(altitude+__altitude_offset); 00129 return a; 00130 } 00131 int32_t RawData::calc_cpassdir() 00132 { 00133 int32_t dir =0; 00134 float x = this->__mag[0]+this->__mag_offset[0]; 00135 float y = this->__mag[1]+this->__mag_offset[1]; 00136 if(y==0){ 00137 if(x>=0) 00138 dir = 0; 00139 else 00140 dir = 180; 00141 }else if(y>0){ 00142 dir = uint32_t(90-(atan(x/y))*180/3.14); 00143 }else{ 00144 dir = uint32_t(270-(atan(x/y))*180/3.14); 00145 } 00146 return dir; 00147 } 00148 00149 00150 00151 00152 00153 00154 00155 void RawData::add_year_10() 00156 { 00157 struct tm *timeinfo = localtime (&(this->__time)); 00158 int year= timeinfo->tm_year; 00159 year+=10; 00160 if(year>=(2100-1900)) 00161 year=timeinfo->tm_year-90; 00162 timeinfo->tm_year=year; 00163 this->__time=mktime(timeinfo); 00164 set_time(this->__time); 00165 } 00166 void RawData::add_year_1() 00167 { 00168 struct tm *timeinfo = localtime (&(this->__time)); 00169 timeinfo->tm_year+=1; 00170 if(timeinfo->tm_year%10==0) 00171 timeinfo->tm_year-=10; 00172 this->__time=mktime(timeinfo); 00173 set_time(this->__time); 00174 } 00175 void RawData::add_month() 00176 { 00177 struct tm *timeinfo = localtime (&(this->__time)); 00178 int mon=timeinfo->tm_mon; 00179 mon+=1; 00180 if(mon>11) 00181 mon=0; 00182 timeinfo->tm_mon=mon; 00183 this->__time=mktime(timeinfo); 00184 set_time(this->__time); 00185 } 00186 void RawData::add_day() 00187 { 00188 struct tm *timeinfo = localtime (&(this->__time)); 00189 timeinfo->tm_yday=0; 00190 timeinfo->tm_wday=0; 00191 int day=timeinfo->tm_mday; 00192 day+=1; 00193 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) 00194 { 00195 if(day>31) 00196 day=1; 00197 } 00198 else if(timeinfo->tm_mon==1) 00199 { 00200 if((timeinfo->tm_year+1900)%400==0 || ((timeinfo->tm_year+1900)%100!=0 && (timeinfo->tm_year+1900)%4==0) ) 00201 { 00202 if(day>29) 00203 day=1; 00204 } 00205 else 00206 { 00207 if(day>28) 00208 day=1; 00209 } 00210 } 00211 else 00212 { 00213 if(day>30) 00214 day=1; 00215 } 00216 timeinfo->tm_mday=day; 00217 this->__time=mktime(timeinfo); 00218 set_time(this->__time); 00219 } 00220 void RawData::add_hour() 00221 { 00222 struct tm *timeinfo = localtime (&(this->__time)); 00223 int hour=timeinfo->tm_hour; 00224 hour+=1; 00225 if(hour>23) 00226 hour=0; 00227 timeinfo->tm_hour=hour; 00228 this->__time=mktime(timeinfo); 00229 set_time(this->__time); 00230 } 00231 void RawData::add_min_10() 00232 { 00233 struct tm *timeinfo = localtime (&(this->__time)); 00234 int min=timeinfo->tm_min; 00235 min+=10; 00236 if(min>=60) 00237 min-=60; 00238 timeinfo->tm_min=min; 00239 this->__time=mktime(timeinfo); 00240 set_time(this->__time); 00241 } 00242 void RawData::add_min_1() 00243 { 00244 struct tm *timeinfo = localtime (&(this->__time)); 00245 int min=timeinfo->tm_min; 00246 min+=1; 00247 if(min%10==0) 00248 min-=10; 00249 timeinfo->tm_min=min; 00250 this->__time=mktime(timeinfo); 00251 set_time(this->__time); 00252 } 00253 void RawData::add_sec_10() 00254 { 00255 struct tm *timeinfo = localtime (&(this->__time)); 00256 int sec=timeinfo->tm_sec; 00257 sec+=10; 00258 if(sec>=60) 00259 sec-=60; 00260 timeinfo->tm_sec=sec; 00261 this->__time=mktime(timeinfo); 00262 set_time(this->__time); 00263 } 00264 void RawData::add_sec_1() 00265 { 00266 struct tm *timeinfo = localtime (&(this->__time)); 00267 int sec=timeinfo->tm_sec; 00268 sec+=1; 00269 if(sec%10==0) 00270 sec-=10; 00271 timeinfo->tm_sec=sec; 00272 this->__time=mktime(timeinfo); 00273 set_time(this->__time); 00274 } 00275 void RawData::add_temp_sign() 00276 { 00277 this->__temp_offset= -1*(this->calc_temperature())-this->__temp; 00278 } 00279 void RawData::add_temp_10() 00280 { 00281 float val = this->__temp_offset; 00282 if(this->__temp+val>=0) 00283 val+=10; 00284 else 00285 val-=10; 00286 if(this->__temp+val>=100) 00287 val-=100; 00288 else if(this->__temp+val<=-100) 00289 val+=100; 00290 this->__temp_offset=val; 00291 } 00292 void RawData::add_temp_1() 00293 { 00294 float val= this->__temp_offset; 00295 if(this->__temp+val>=0) 00296 val+=1; 00297 else 00298 val-=1; 00299 if((this->__temp+val)>=0 && (int)(this->__temp+val)%10==0) 00300 val-=10; 00301 else if((this->__temp+val)<0 && (int)(this->__temp+val)%10==0) 00302 val+=10; 00303 this->__temp_offset=val; 00304 } 00305 void RawData::add_temp_1_10() 00306 { 00307 float val=(this->__temp_offset*10); 00308 if(this->__temp*10+val>=0) 00309 val+=1; 00310 else 00311 val-=1; 00312 if(this->__temp*10+val>=0 && (int)(this->__temp*10+val)%10==0) 00313 val-=10; 00314 else if(this->__temp*10+val<0 && (int)(this->__temp*10+val)%10==0) 00315 val+=10; 00316 this->__temp_offset=val/10.0; 00317 } 00318 void RawData::add_humi_10() 00319 { 00320 float val=this->__humi_offset; 00321 val+=10; 00322 if((int)(this->__humi+val)>=100) 00323 val-=100; 00324 this->__humi_offset=val; 00325 } 00326 void RawData::add_humi_1() 00327 { 00328 float val=this->__humi_offset; 00329 int delta = (int)(this->__humi_offset); 00330 val = val-delta; 00331 delta+=1; 00332 if((int)(this->__humi+delta)%10==0) 00333 delta-=10; 00334 this->__humi_offset=val+delta; 00335 } 00336 void RawData::add_humi_1_10() 00337 { 00338 int delta=(int)(this->__humi_offset*10); 00339 delta+=1; 00340 if((int)(this->__humi*10+delta)%10==0) 00341 delta-=10; 00342 this->__humi_offset=delta/10.0; 00343 } 00344 00345 // ALTITUDE 00346 void RawData::add_altitude_1000() 00347 { 00348 this->__altitude_offset+=1000; 00349 if(this->calc_altitude()>=10000) 00350 this->__altitude_offset-=10000; 00351 } 00352 void RawData::add_altitude_100() 00353 { 00354 this->__altitude_offset+=100; 00355 if((this->calc_altitude()/100)%10==0) 00356 this->__altitude_offset-=1000; 00357 } 00358 void RawData::add_altitude_10() 00359 { 00360 this->__altitude_offset+=10; 00361 if((this->calc_altitude()/10)%10==0) 00362 this->__altitude_offset-=100; 00363 } 00364 void RawData::add_altitude_1() 00365 { 00366 this->__altitude_offset+=1; 00367 if(this->calc_altitude()%10==0) 00368 this->__altitude_offset-=10; 00369 }
Generated on Sat Jul 16 2022 00:03:47 by
1.7.2