LE YOU / Mbed 2 deprecated mas

Dependencies:   TextLCD X_NUCLEO_IKS01A1 func mbed-src mbed

Fork of mas by esproj

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    14-August-2015
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A1 
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037 */ 
00038 
00039 /* Includes */
00040 #include "main.h"
00041 
00042 /* Instantiate the expansion board */
00043 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
00044 
00045 /* Retrieve the composing elements of the expansion board */
00046 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
00047 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
00048 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
00049 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
00050 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
00051 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
00052 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
00053 
00054 /* Helper function for printing floats & doubles */
00055 static char *printDouble(char* str, double v, int decimalDigits=2)
00056 {
00057   int i = 1;
00058   int intPart, fractPart;
00059   int len;
00060   char *ptr;
00061 
00062   /* prepare decimal digits multiplicator */
00063   for (;decimalDigits!=0; i*=10, decimalDigits--);
00064 
00065   /* calculate integer & fractinal parts */
00066   intPart = (int)v;
00067   fractPart = (int)((v-(double)(int)v)*i);
00068 
00069   /* fill in integer part */
00070   sprintf(str, "%i.", intPart);
00071 
00072   /* prepare fill in of fractional part */
00073   len = strlen(str);
00074   ptr = &str[len];
00075 
00076   /* fill in leading fractional zeros */
00077   for (i/=10;i>1; i/=10, ptr++) {
00078     if(fractPart >= i) break;
00079     *ptr = '0';
00080   }
00081 
00082   /* fill in (rest of) fractional part */
00083   sprintf(ptr, "%i", fractPart);
00084 
00085   return str;
00086 }
00087 
00088 DigitalOut myled(LED1);
00089 DigitalIn mybutton(USER_BUTTON);
00090 Display mydisplay;
00091 RawData myrawdata;
00092 
00093 uint8_t fbtn=BTN_RELEASED;
00094 uint8_t fbtn_counter=0;
00095 uint8_t mode=MODE_NORMAL;
00096 uint8_t func=FUNC_DATETIME;
00097 uint8_t config_index=0;
00098 uint8_t flag_flash=0;
00099 
00100 
00101 /**
00102  * FUNC: wait_action
00103  * DESC: wait for pressing button action, update the global flag for next polling cycle
00104  */
00105 void wait_action()
00106 {
00107     fbtn=BTN_RELEASED;
00108     for(int i=0; i<5; i++)
00109     {
00110         if(fbtn==BTN_RELEASED)
00111         {
00112             if(mybutton==1 and fbtn==BTN_RELEASED)
00113             {
00114                 if(fbtn_counter>0 and fbtn_counter<=20)
00115                 {
00116                     fbtn=BTN_PRESSED;
00117                 }
00118                 else if(fbtn_counter>20 and fbtn_counter<=50)
00119                 {
00120                     fbtn=BTN_PRESSED_LONG;
00121                 }
00122                 else if(fbtn_counter>50)
00123                 {
00124                     fbtn=BTN_PRESSED_VERY_LONG;
00125                 }
00126                 fbtn_counter=0;
00127             }
00128             else
00129             {
00130                 fbtn_counter+=1;
00131             }
00132         }
00133         wait(0.1);
00134     }
00135 }
00136 
00137 
00138 /* Simple main function */
00139 int main() {
00140   myled=0;
00141   uint8_t func=0;
00142   uint8_t button_mem=0;
00143   
00144 
00145   
00146   uint8_t id;
00147   float value1, value2;
00148   char buffer0[16];
00149   char buffer1[32], buffer2[32];
00150   int32_t axes[3];
00151   int16_t axxx[3];
00152   
00153  /* 
00154   
00155   PersistorStorage ps=PersistorStorage();
00156   strcpy(ps.storage, "hello");
00157   ps.writeStorageToFlash();
00158   
00159   PersistorStorage ps1=PersistorStorage();
00160   ps1.ReadStorageFromFlash();
00161 
00162   printf("%s", ps1.storage);
00163 */
00164 /*HAL_Init();
00165 __HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_EOP | FLASH_FLAG_OPERR |FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
00166 printf("hello\n");
00167 uint64_t a=124;
00168 uint32_t pageerr = 100;
00169 HAL_FLASH_Unlock();
00170 FLASH_EraseInitTypeDef es;
00171 es.TypeErase=FLASH_TYPEERASE_MASSERASE;
00172 es.Banks=FLASH_BANK_2;
00173 es.Page=0;
00174 es.NbPages=1;
00175 
00176 if( HAL_OK != (HAL_FLASHEx_Erase_IT( &es))) {
00177         printf("ERROR %d\n", pageerr);
00178         HAL_FLASH_Lock();
00179 }
00180 __HAL_FLASH_CLEAR_FLAG( FLASH_FLAG_EOP | FLASH_FLAG_OPERR |FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
00181 HAL_FLASH_Unlock();
00182 printf("%d\n", HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, 0x08080000, a));
00183 printf("hello\n");
00184 printf("%d\n", HAL_FLASH_GetError());
00185 HAL_FLASH_Lock();
00186 
00187 static const int StorageSize = 64;
00188 static unsigned int storage[ StorageSize ] __attribute__((aligned(32)));
00189 HAL_FLASH_Unlock();
00190 memcpy( (uint32_t*)storage, (uint32_t*)(0x08080000), StorageSize);
00191 HAL_FLASH_Lock();
00192 
00193 for(int i=0; i<StorageSize; i++)
00194 {
00195     printf("%4x ",storage[i]);
00196 }
00197 printf("\n");
00198 */
00199   
00200 /*  
00201   printf("\r\n--- Starting new run ---\r\n");
00202 
00203   humidity_sensor->ReadID(&id);
00204   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00205   pressure_sensor->ReadID(&id);
00206   printf("LPS25H  pressure & temperature    = 0x%X\r\n", id);
00207   magnetometer->ReadID(&id);
00208   printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
00209   gyroscope->ReadID(&id);
00210   printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
00211 */
00212 float pressure=0;
00213 while(1)
00214 {
00215     // MEASURE AND DISPLAY
00216     if(mode==MODE_NORMAL)
00217     {
00218         myled=0;
00219         if(func==FUNC_DATETIME)
00220         {
00221             myrawdata.__time=time(NULL);
00222             myrawdata.str_date(buffer0);
00223             myrawdata.str_time(buffer1);
00224         }
00225         else if(func==FUNC_TEMPHUMI)
00226         {
00227             temp_sensor1->GetTemperature(&myrawdata.__temp);
00228             humidity_sensor->GetHumidity(&myrawdata.__humi);
00229             myrawdata.str_temperature(buffer0);
00230             myrawdata.str_humidity(buffer1);
00231         }
00232         else if(func==FUNC_ALTICDIR)
00233         {
00234             pressure_sensor->GetPressure(&pressure);
00235             myrawdata.__prss = pressure * 10;
00236             magnetometer->Get_M_Axes(myrawdata.__mag);
00237             accelerometer->Get_X_Axes(myrawdata.__acc);
00238             gyroscope->Get_G_Axes(myrawdata.__gyr);
00239             myrawdata.str_altitude(buffer0);
00240             myrawdata.str_direction(buffer1);
00241         }
00242         mydisplay.show(buffer0, buffer1);
00243     }
00244     else if(mode==MODE_CONFIG)
00245     {
00246         myled=1;
00247         if(func==FUNC_DATETIME)
00248         {
00249             myrawdata.str_date(buffer0);
00250             myrawdata.str_time(buffer1);
00251             if(flag_flash)
00252             {
00253                 if(config_index==0)
00254                 {
00255                     buffer0[0]=0xff;
00256                 }
00257                 else if(config_index==1)
00258                 {
00259                     buffer0[1]=0xff;
00260                 }
00261                 else if(config_index==2)
00262                 {
00263                     buffer0[3]=0xff;
00264                     buffer0[4]=0xff;
00265                 }
00266                 else if(config_index==3)
00267                 {
00268                     buffer0[6]=0xff;
00269                     buffer0[7]=0xff;
00270                 }
00271                 else if(config_index==4)
00272                 {
00273                     buffer1[0]=0xff;
00274                     buffer1[1]=0xff;
00275                 }
00276                 else if(config_index==5)
00277                 {
00278                     buffer1[3]=0xff;
00279                 }
00280                 else if(config_index==6)
00281                 {
00282                     buffer1[4]=0xff;
00283                 }
00284                 else if(config_index==7)
00285                 {
00286                     buffer1[6]=0xff;
00287                 }
00288                 else if(config_index==8)
00289                 {
00290                     buffer1[7]=0xff;
00291                 }
00292             }
00293         }
00294         if(func==FUNC_TEMPHUMI)
00295         {
00296             myrawdata.str_temperature(buffer0);
00297             myrawdata.str_humidity(buffer1);
00298             if(flag_flash)
00299             {
00300                 if(config_index==0)
00301                 {
00302                     buffer0[2]=0xff;
00303                 }
00304                 else if(config_index==1)
00305                 {
00306                     buffer0[3]=0xff;
00307                 }
00308                 else if(config_index==2)
00309                 {
00310                     buffer0[4]=0xff;
00311                 }
00312                 else if(config_index==3)
00313                 {
00314                     buffer0[6]=0xff;
00315                 }
00316                 else if(config_index==4)
00317                 {
00318                     buffer1[3]=0xff;
00319                 }
00320                 else if(config_index==5)
00321                 {
00322                     buffer1[4]=0xff;
00323                 }
00324                 else if(config_index==6)
00325                 {
00326                     buffer1[6]=0xff;
00327                 }
00328             }
00329         }
00330         else if(func==FUNC_ALTICDIR)
00331         {
00332             myrawdata.str_altitude(buffer0);
00333             myrawdata.str_direction(buffer1);
00334             if(flag_flash)
00335             {
00336                 if(config_index==0)
00337                 {
00338                     buffer0[3]=0xff;
00339                 }
00340                 else if(config_index==1)
00341                 {
00342                     buffer0[4]=0xff;
00343                 }
00344                 else if(config_index==2)
00345                 {
00346                     buffer0[5]=0xff;
00347                 }
00348                 else if(config_index==3)
00349                 {
00350                     buffer0[6]=0xff;
00351                 }
00352             }
00353         }
00354         
00355         if(flag_flash)
00356         {
00357             flag_flash=0;
00358         }
00359         else
00360         {
00361             flag_flash=1;
00362         }
00363         mydisplay.show(buffer0, buffer1);
00364     }
00365     else
00366     {
00367         mydisplay.show("CALIBRATION", "START IN 3s ...");
00368         wait(1);
00369         mydisplay.show("CALIBRATION", "START IN 2s ...");
00370         wait(1);
00371         mydisplay.show("CALIBRATION", "START IN 1s ...");
00372         wait(1);
00373         mydisplay.show("CALIBRATION", "PHASE 1 ...");
00374         int32_t acc[3]={0}, gyr[3]={0};
00375         for(int i=0;i<100;i++)
00376         {
00377             int32_t acc_tmp[3]={0}, gyr_tmp[3]={0};
00378             accelerometer->Get_X_Axes(acc_tmp);
00379             gyroscope->Get_G_Axes(gyr_tmp);
00380             acc[0] += acc_tmp[0];
00381             acc[1] += acc_tmp[1];
00382             acc[2] += acc_tmp[2];
00383             gyr[0] += gyr_tmp[0];
00384             gyr[1] += gyr_tmp[1];
00385             gyr[2] += gyr_tmp[2];
00386             wait(0.02);
00387         }
00388         myrawdata.__gyr_offset[0]=-gyr[0]/100;
00389         myrawdata.__gyr_offset[1]=-gyr[1]/100;
00390         myrawdata.__gyr_offset[2]=-gyr[2]/100;
00391         myrawdata.__acc_offset[0]=-acc[0]/100;
00392         myrawdata.__acc_offset[1]=-acc[1]/100;
00393         myrawdata.__acc_offset[2]=1-acc[2]/100;  // gravaty
00394         
00395         mydisplay.show("CALIBRATION", "PHASE 2 ...");
00396         int32_t mag_max[3]={-10000}, mag_min[3]={10000};
00397         for(int i=0;i<500;i++)
00398         {
00399             int32_t mag_tmp[3]={0};
00400             magnetometer->Get_M_Axes(mag_tmp);
00401             mag_max[0]=mag_tmp[0]>mag_max[0]?mag_tmp[0]:mag_max[0];
00402             mag_max[1]=mag_tmp[1]>mag_max[1]?mag_tmp[1]:mag_max[1];
00403             mag_max[2]=mag_tmp[2]>mag_max[2]?mag_tmp[2]:mag_max[2];
00404             mag_min[0]=mag_tmp[0]<mag_min[0]?mag_tmp[0]:mag_min[0];
00405             mag_min[1]=mag_tmp[1]<mag_min[1]?mag_tmp[1]:mag_min[1];
00406             mag_min[2]=mag_tmp[2]<mag_min[2]?mag_tmp[2]:mag_min[2];
00407             wait(0.02);
00408         }
00409         myrawdata.__mag_offset[0]=-((mag_max[0]+mag_min[0])/2);
00410         myrawdata.__mag_offset[1]=-((mag_max[1]+mag_min[1])/2);
00411         myrawdata.__mag_offset[2]=-((mag_max[2]+mag_min[2])/2);
00412         
00413         mode=MODE_NORMAL;
00414     }
00415     
00416     // NEXT STATE
00417     if(mode==MODE_NORMAL)
00418     {
00419         if(fbtn==BTN_PRESSED_LONG)
00420         {
00421             mode=MODE_CONFIG;
00422         }
00423         else if(fbtn==BTN_PRESSED)
00424         {
00425             func=func+1;
00426             if(func>FUNC_ALTICDIR)
00427                 func=FUNC_DATETIME;
00428         }
00429         else if(fbtn==BTN_PRESSED_VERY_LONG)
00430         {
00431             mode=MODE_CONFIG_AUTO;
00432         }
00433     }
00434     else if(mode==MODE_CONFIG)
00435     {
00436         if(fbtn==BTN_PRESSED_VERY_LONG)
00437         {
00438             config_index=0;
00439             mode=MODE_NORMAL;
00440         }
00441         else if(fbtn==BTN_PRESSED_LONG)
00442         {
00443             config_index+=1;
00444             if(func==FUNC_DATETIME){
00445                 if(config_index>8)
00446                 {
00447                     config_index=0;
00448                 }
00449             } else if(func== FUNC_TEMPHUMI){
00450                 if(config_index>6)
00451                 {
00452                     config_index=0;
00453                 }
00454             } else if(func==FUNC_ALTICDIR){
00455                 if(config_index>3)
00456                 {
00457                     config_index=0;
00458                 }
00459             }
00460         }
00461         else if(fbtn==BTN_PRESSED)
00462         {
00463             if(func==FUNC_DATETIME)
00464             {
00465                 if(config_index==0)
00466                 {
00467                     myrawdata.add_year_10();
00468                 }
00469                 else if(config_index==1)
00470                 {
00471                     myrawdata.add_year_1();
00472                 }
00473                 else if(config_index==2)
00474                 {
00475                     myrawdata.add_month();
00476                 }
00477                 else if(config_index==3)
00478                 {
00479                     myrawdata.add_day();
00480                 }
00481                 else if(config_index==4)
00482                 {
00483                     myrawdata.add_hour();
00484                 }
00485                 else if(config_index==5)
00486                 {
00487                     myrawdata.add_min_10();
00488                 }
00489                 else if(config_index==6)
00490                 {
00491                     myrawdata.add_min_1();
00492                 }
00493                 else if(config_index==7)
00494                 {
00495                     myrawdata.add_sec_10();
00496                 }
00497                 else if(config_index==8)
00498                 {
00499                     myrawdata.add_sec_1();
00500                 }
00501             }
00502             else if(func==FUNC_TEMPHUMI)
00503             {
00504                 if(config_index==0)
00505                 {
00506                     myrawdata.add_temp_sign();
00507                 }
00508                 else if(config_index==1)
00509                 {
00510                     myrawdata.add_temp_10();
00511                 }
00512                 else if(config_index==2)
00513                 {
00514                     myrawdata.add_temp_1();
00515                 }
00516                 else if(config_index==3)
00517                 {
00518                     myrawdata.add_temp_1_10();
00519                 }
00520                 else if(config_index==4)
00521                 {
00522                     myrawdata.add_humi_10();
00523                 }
00524                 else if(config_index==5)
00525                 {
00526                     myrawdata.add_humi_1();
00527                 }
00528                 else if(config_index==6)
00529                 {
00530                     myrawdata.add_humi_1_10();
00531                 }
00532             }
00533             else if(func==FUNC_ALTICDIR)
00534             {
00535                 if(config_index==0)
00536                 {
00537                     myrawdata.add_altitude_1000();
00538                 }
00539                 else if(config_index==1)
00540                 {
00541                     myrawdata.add_altitude_100();
00542                 }
00543                 else if(config_index==2)
00544                 {
00545                     myrawdata.add_altitude_10();
00546                 }
00547                 else if(config_index==3)
00548                 {
00549                     myrawdata.add_altitude_1();
00550                 }
00551             }
00552         }
00553     }
00554     else
00555     {
00556         ;
00557     }
00558     
00559     wait_action();
00560 }
00561 
00562 /*  
00563 raise_error(ERROR_CONFIG);
00564 
00565   printf("START CALIBRATION\n");
00566   int32_t m_max[3]={0};
00567   int32_t m_min[3]={0};
00568   for (int i=0; i<1000; i++)
00569   {
00570        
00571         magnetometer->Get_M_Axes(axes);
00572         if(i==0)
00573         {
00574             for(int j=0; j<3; j++)
00575             {
00576                 m_max[j]=axes[j];
00577                 m_min[j]=axes[j];
00578             }
00579         }
00580         else
00581         {
00582             for(int j=0; j<3; j++)
00583             {
00584                 if(axes[j]>m_max[j])  m_max[j]=axes[j];
00585                 if(axes[j]<m_min[j])  m_min[j]=axes[j];
00586             }
00587         }
00588         wait(0.05);
00589   }
00590   
00591   printf("MAX : %d, %d, %d\n", m_max[0], m_max[1], m_max[2]);
00592   printf("MIN : %d, %d, %d\n", m_min[0], m_min[1], m_min[2]);
00593   
00594  
00595   while(1) {
00596     if((mybutton!=0) && (button_mem==0))
00597     {
00598         func++;
00599         if(func>=6)
00600             func=0;
00601     }
00602     button_mem=mybutton;
00603     printf("func: %d\r\n", func);
00604     printf("\r\n");
00605     
00606     if(func==0)
00607     {
00608         time_t seconds = time(NULL);
00609         printf("Time as a basic string = %s\r\n", ctime(&seconds));
00610     }
00611     else if(func==1)
00612     {
00613     temp_sensor1->GetTemperature(&value1);
00614     humidity_sensor->GetHumidity(&value2);
00615     
00616     if(value2 > 70)
00617         myled=1;
00618     else
00619         myled=0;
00620     printf("HTS221: [temp] %7s C,   [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00621     }
00622     else if(func==2)
00623     {
00624     temp_sensor2->GetFahrenheit(&value1);
00625     pressure_sensor->GetPressure(&value2);
00626     printf("LPS25H: [temp] %7s F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00627     printf("Altitude: %s m\r\n", printDouble(buffer1, 44300-44300*pow((value2/1013.25), 0.00019)));
00628     }
00629     else if(func==3)
00630     {
00631     magnetometer->Get_M_Axes(axes);
00632     axxx[0]=axes[0]-((m_max[0]+m_min[0])/2);
00633     axxx[1]=axes[1]-((m_max[1]+m_min[1])/2);
00634     axxx[2]=axes[2]-((m_max[2]+m_min[2])/2);
00635     printf("LIS3MDL [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axxx[0], axxx[1], axxx[2]);
00636     printf("DIR: %s deg\r\n", printDouble(buffer1, 90-atan(float(axxx[0])/float(axxx[1])/3.1416)));
00637     }
00638     else if(func==4)
00639     {
00640     accelerometer->Get_X_Axes(axes);
00641     printf("LSM6DS0 [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00642     }
00643     else if(func==5)
00644     {
00645     gyroscope ->Get_G_Axes(axes);
00646     printf("LSM6DS0 [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00647     }
00648     wait(1.5);
00649   }*/
00650   
00651 }