tiz

Dependents:   mas

Files at this revision

API Documentation at this revision

Comitter:
youle1119
Date:
Wed Sep 14 08:55:41 2016 +0000
Commit message:
tiz

Changed in this revision

Config.cpp Show annotated file Show diff for this revision Revisions of this file
Config.h Show annotated file Show diff for this revision Revisions of this file
Display.cpp Show annotated file Show diff for this revision Revisions of this file
Display.h Show annotated file Show diff for this revision Revisions of this file
Error.cpp Show annotated file Show diff for this revision Revisions of this file
Error.h Show annotated file Show diff for this revision Revisions of this file
RawData.cpp Show annotated file Show diff for this revision Revisions of this file
RawData.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r d60c26d5fddc Config.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Config.cpp	Wed Sep 14 08:55:41 2016 +0000
@@ -0,0 +1,23 @@
+#include "Config.h"
+
+Config::Config(char *filename)
+{
+    FILE *fp = fopen(filename, "r+");
+    if(!fp)
+    {
+        raise_error(ERROR_CONFIG);
+    }
+    strcpy(this->__filename, filename);
+    fclose(fp);
+}
+
+void Config::write()
+{
+    FILE *fp = fopen(this->__filename, "w+");
+    if(!fp)
+    {
+        raise_error(ERROR_CONFIG);
+    }
+    fprintf(fp, "%d %d %d\n%d %d %d\n%d %d %d", this->__acc_offset[0], __acc_offset[1], __acc_offset[2], this->__gyo_offset[0], this->__gyo_offset[1], this->__gyo_offset[2], this->__mag_offset[0], this->__mag_offset[1], this->__mag_offset[2]);
+    fclose(fp);
+}
\ No newline at end of file
diff -r 000000000000 -r d60c26d5fddc Config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Config.h	Wed Sep 14 08:55:41 2016 +0000
@@ -0,0 +1,24 @@
+#include "mbed.h"
+#include "Error.h"
+
+#ifndef __CONFIG_H__
+#define __CONFIG_H__
+
+class Config
+{
+    private:
+    char __filename[50];
+    int32_t __acc_offset[3];
+    int32_t __gyo_offset[3];
+    int32_t __mag_offset[3];
+    
+    public:
+    Config(char *filename);
+    void write();
+    void set_acc_offset(int a[3]);
+    void set_ayo_offset(int a[3]);
+    void set_mag_offset(int a[3]);
+    ~Config();
+};
+
+#endif //__CONFIG_H__
\ No newline at end of file
diff -r 000000000000 -r d60c26d5fddc Display.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Display.cpp	Wed Sep 14 08:55:41 2016 +0000
@@ -0,0 +1,17 @@
+#include "Display.h"
+
+Display::Display()
+{
+    this->__lcd = new TextLCD(PA_6, PA_7, PB_6, PC_7, PA_9, PA_8, TextLCD::LCD16x2);
+}
+
+void Display::show(char *buffer0, char *buffer1)
+{
+    this->__lcd->cls();
+    if(buffer0)
+        this->__lcd->locate(0,0);
+        this->__lcd->printf("%s", buffer0);
+    if(buffer1)
+        this->__lcd->locate(0,1);
+        this->__lcd->printf("%s", buffer1);
+}
\ No newline at end of file
diff -r 000000000000 -r d60c26d5fddc Display.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Display.h	Wed Sep 14 08:55:41 2016 +0000
@@ -0,0 +1,16 @@
+#include "mbed.h"
+#include "TextLCD.h"
+
+#ifndef __DISPLAY_H__
+#define __DISPLAY_H__
+
+class Display
+{
+private:
+    TextLCD *__lcd;
+public:
+    Display();
+    void show(char *buffer0, char *buffer1=NULL);    // Display string in 2 rows.
+};
+
+#endif  // __DISPLAY_H__
\ No newline at end of file
diff -r 000000000000 -r d60c26d5fddc Error.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Error.cpp	Wed Sep 14 08:55:41 2016 +0000
@@ -0,0 +1,17 @@
+#include "Error.h"
+
+void raise_error(ErrorCode ec)
+{
+    DigitalOut led(LED1);
+    while(1)
+    {
+        for(int i=0; i<=ec; i++)
+        {
+            led=1;
+            wait(0.3);
+            led=0;
+            wait(0.3);
+        }
+        wait(2);
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r d60c26d5fddc Error.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Error.h	Wed Sep 14 08:55:41 2016 +0000
@@ -0,0 +1,13 @@
+#include "mbed.h"
+
+#ifndef __ERROR_H__
+#define __ERROR_H__
+
+enum ErrorCode{
+    ERROR_UNKNOW,
+    ERROR_CONFIG
+};
+
+void raise_error(ErrorCode ec);
+
+#endif //__ERROR_H__
\ No newline at end of file
diff -r 000000000000 -r d60c26d5fddc RawData.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RawData.cpp	Wed Sep 14 08:55:41 2016 +0000
@@ -0,0 +1,369 @@
+#include "RawData.h"
+
+char buffer2[32];
+const uint32_t __pressure_table[16]={1013, 995, 976, 959, 942, 925, 908, 892, 875, 859, 843, 812, 782, 752, 724, 696};
+const uint32_t __altitude_table[16]={0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 6000, 7000, 8000, 9000, 10000};
+const uint8_t __dir_table[5] = {'E', 'N', 'W', 'S', 'E'}; 
+
+RawData::RawData()
+{
+    this->__temp_offset=0;
+    this->__humi_offset=0;
+    this->__pressure_offset=0;
+    this->__altitude_offset=0;
+    this->__acc_offset[0]=0;
+    this->__acc_offset[1]=0;
+    this->__acc_offset[2]=0;
+    this->__gyr_offset[0]=0;
+    this->__gyr_offset[1]=0;
+    this->__gyr_offset[2]=0;
+    this->__mag_offset[0]=0;
+    this->__mag_offset[1]=0;
+    this->__mag_offset[2]=0;
+}
+
+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->calc_temperature()>=0)
+        snprintf(buffer, 16, "T: %sC", this->printDouble(buffer2, (this->calc_temperature())));
+    else
+        snprintf(buffer, 16, "T:-%sC", this->printDouble(buffer2, -1*(this->calc_temperature())));
+}
+void RawData::str_humidity(char *buffer)
+{
+    if(this->calc_humidity()<100)
+        snprintf(buffer, 16, "H: %sPer", this->printDouble(buffer2, (this->calc_humidity())));
+    else
+        snprintf(buffer, 16, "H: 00.0Per");
+}
+void RawData::str_altitude(char *buffer)
+{
+    snprintf(buffer, 16, "A: %4uM", this->calc_altitude());
+}
+void RawData::str_direction(char *buffer)
+{
+    uint32_t dir = this->calc_cpassdir();
+    snprintf(buffer, 16, "D: %c%3uDeg", __dir_table[(dir+45)/90], dir);
+}
+
+char* RawData::printDouble(char* str, double v, int integerDigits, 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 */
+  int powval=1;
+  for(int xx=0; xx<integerDigits;xx++)
+    powval = powval*10;
+    
+  if(integerDigits>0)
+    intPart = (int)(v) % powval;
+  else
+    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;
+}
+
+
+float RawData::calc_temperature()
+{
+    return this->__temp+this->__temp_offset;
+}
+float RawData::calc_humidity()
+{
+    return this->__humi+this->__humi_offset;
+}
+int32_t RawData::calc_altitude()
+{
+    uint32_t altitude=0;
+    uint32_t bound_h=0, bound_l=16;
+    float pressure = (this->__prss+this->__pressure_offset)/10;
+    for(uint32_t i=0; i<16; i++)
+    {
+        if(__pressure_table[i]<= pressure)
+        {
+            bound_h=i;
+            break;
+        }
+        bound_l=i;
+    }
+    if(bound_h==bound_l){
+        this->__pressure_offset = __pressure_table[bound_l]-this->__prss;
+        altitude = __altitude_table[bound_l];
+    }else{
+        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);
+    }
+    int32_t a=int32_t(altitude+__altitude_offset);
+    return a;
+}
+int32_t RawData::calc_cpassdir()
+{
+    int32_t dir =0;
+    float x = this->__mag[0]+this->__mag_offset[0];
+    float y = this->__mag[1]+this->__mag_offset[1];
+    if(y==0){
+        if(x>=0)
+            dir = 0;
+        else
+            dir = 180;
+    }else if(y>0){
+        dir = uint32_t(90-(atan(x/y))*180/3.14);
+    }else{
+        dir = uint32_t(270-(atan(x/y))*180/3.14);
+    }
+    return dir;
+}
+
+
+
+
+
+
+
+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);
+    set_time(this->__time);
+}
+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);
+    set_time(this->__time);
+}
+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);
+    set_time(this->__time);
+}
+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);
+    set_time(this->__time);
+}
+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);
+    set_time(this->__time);
+}
+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);
+    set_time(this->__time);
+}
+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);
+    set_time(this->__time);
+}
+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);
+    set_time(this->__time);
+}
+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);
+    set_time(this->__time);
+}
+void RawData::add_temp_sign()
+{
+    this->__temp_offset= -1*(this->calc_temperature())-this->__temp;
+}
+void RawData::add_temp_10()
+{
+    float val = this->__temp_offset;
+    if(this->__temp+val>=0)
+        val+=10;
+    else
+        val-=10;
+    if(this->__temp+val>=100)
+        val-=100;
+    else if(this->__temp+val<=-100)
+        val+=100;
+    this->__temp_offset=val;
+}
+void RawData::add_temp_1()
+{
+    float val= this->__temp_offset;
+    if(this->__temp+val>=0)
+        val+=1;
+    else
+        val-=1;
+    if((this->__temp+val)>=0 && (int)(this->__temp+val)%10==0)
+        val-=10;
+    else if((this->__temp+val)<0 && (int)(this->__temp+val)%10==0)
+        val+=10;
+    this->__temp_offset=val;
+}
+void RawData::add_temp_1_10()
+{
+    float val=(this->__temp_offset*10);
+    if(this->__temp*10+val>=0)
+        val+=1;
+    else
+        val-=1;
+    if(this->__temp*10+val>=0 && (int)(this->__temp*10+val)%10==0)
+        val-=10;
+    else if(this->__temp*10+val<0 && (int)(this->__temp*10+val)%10==0)
+        val+=10;
+    this->__temp_offset=val/10.0;
+}
+void RawData::add_humi_10()
+{
+    float val=this->__humi_offset;
+    val+=10;
+    if((int)(this->__humi+val)>=100)
+        val-=100;
+    this->__humi_offset=val;
+}
+void RawData::add_humi_1()
+{
+    float val=this->__humi_offset;
+    int delta = (int)(this->__humi_offset);
+    val = val-delta;
+    delta+=1;
+    if((int)(this->__humi+delta)%10==0)
+        delta-=10;
+    this->__humi_offset=val+delta;
+}
+void RawData::add_humi_1_10()
+{
+    int delta=(int)(this->__humi_offset*10);
+    delta+=1;
+    if((int)(this->__humi*10+delta)%10==0)
+        delta-=10;
+    this->__humi_offset=delta/10.0;
+}
+
+// ALTITUDE
+void RawData::add_altitude_1000()
+{
+    this->__altitude_offset+=1000;
+    if(this->calc_altitude()>=10000)
+        this->__altitude_offset-=10000;
+}
+void RawData::add_altitude_100()
+{
+    this->__altitude_offset+=100;
+    if((this->calc_altitude()/100)%10==0)
+        this->__altitude_offset-=1000;
+}
+void RawData::add_altitude_10()
+{
+    this->__altitude_offset+=10;
+    if((this->calc_altitude()/10)%10==0)
+       this->__altitude_offset-=100;
+}
+void RawData::add_altitude_1()
+{
+    this->__altitude_offset+=1;
+    if(this->calc_altitude()%10==0)
+       this->__altitude_offset-=10;
+}
\ No newline at end of file
diff -r 000000000000 -r d60c26d5fddc RawData.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RawData.h	Wed Sep 14 08:55:41 2016 +0000
@@ -0,0 +1,65 @@
+#include "mbed.h"
+
+#ifndef __MAS_RAW_DATA_H__
+#define __MAS_RAW_DATA_H__
+
+class RawData
+{
+public:
+    time_t __time;
+    float __temp;               // unit : Degree Celsius
+    float __humi;               // unit : %
+    float __prss;           // unit : kPa
+    int32_t __acc[3];           // unit : 
+    int32_t __gyr[3];           // unit :
+    int32_t __mag[3];           // unit :
+    float __temp_offset;        // unit : Degree Celsius
+    float __humi_offset;        // unit : %
+    float __pressure_offset;
+    float __altitude_offset;    // unit : m
+    int32_t __acc_offset[3];    // unit :
+    int32_t __gyr_offset[3];    // unit :
+    int32_t __mag_offset[3];    // unit :
+    
+public:
+    RawData();
+    void add_year_10();
+    void add_year_1();
+    void add_month();
+    void add_day();
+    void add_hour();
+    void add_min_10();
+    void add_min_1();
+    void add_sec_10();
+    void add_sec_1();
+    void add_temp_sign();
+    void add_temp_10();
+    void add_temp_1();
+    void add_temp_1_10();
+    void add_humi_10();
+    void add_humi_1();
+    void add_humi_1_10();
+    void add_altitude_1();
+    void add_altitude_10();
+    void add_altitude_100();
+    void add_altitude_1000();
+    time_t calc_datetime();
+    float calc_temperature();
+    float calc_humidity();
+    int32_t calc_altitude();
+    int32_t calc_cpassdir();
+    void str_date(char *buffer);
+    void str_time(char *buffer);
+    void str_temperature(char *buffer);
+    void str_humidity(char *buffer);
+    void str_pressure(char *buffer);
+    void str_altitude(char *buffer);
+    void str_magnetic(char *buffer);
+    void str_direction(char *buffer);
+    
+private:
+    char* printDouble(char* str, double v,int integerDigits=2, int decimalDigits=1);
+};
+
+
+#endif // __MAS_RAW_DATA_H__
\ No newline at end of file