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.
Dependencies: X_NUCLEO_IKS01A1-f255a2c75ecb mbed-rtos mbed
main.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file main.cpp 00004 * @author ANG Group (Nelson Santos; Irina Baptista; Pierluigi Urru) 00005 * @version V0.0.3 00006 * @brief Simple Example application for using the X_NUCLEO_IKS01A1 00007 * MEMS Inertial & Environmental Sensor Nucleo expansion board. 00008 ****************************************************************************** 00009 */ 00010 00011 #include "mbed.h" 00012 #include "x_nucleo_iks01a1.h" 00013 #include "rtos.h" 00014 #include <cstring> 00015 #include <stdexcept> 00016 #include <vector> 00017 #include "sensor.h" 00018 #include "userMethods.h" 00019 #include "log.h" 00020 00021 #define QUEUESIZE 120 00022 00023 struct tm t = {t.tm_hour = 0}; 00024 00025 int n = 0; 00026 bool flag = false; 00027 00028 Mail<log_data, QUEUESIZE> mail_box; 00029 vector<log_data> v; 00030 00031 ExpansionBoard e(n, flag); 00032 UserMethods u(v, n, e, flag, mail_box); 00033 00034 int i = 0; 00035 00036 void sampleData() 00037 { 00038 //while(true){ 00039 if(flag) printf("\nAbout to alloc\n"); 00040 log_data *log_d = mail_box.alloc(); 00041 00042 if (log_d == NULL) { 00043 if(flag) printf("Out of memory, last sample deleted\n"); 00044 osEvent evt = mail_box.get(); 00045 log_data *log = (log_data*)evt.value.p; 00046 printf("\nI = %d\n", i); 00047 printf("Pressure: %f", log->pressure); 00048 printf("Counter: %d", log->counter); 00049 log_data* log_d = (log_data*)mail_box.alloc(); 00050 if(flag) printf("Allocated after out of memory\n"); 00051 } 00052 00053 if(flag) printf("Not null, reading values\n"); 00054 //Store read data in a sample 00055 float value; 00056 //log_d->date = time(NULL); 00057 temp_sensor->GetTemperature(&value); 00058 log_d->tempCelsius = value; 00059 humidity_sensor->GetHumidity(&value); 00060 log_d->humidity = value; 00061 pressure_sensor->GetPressure(&value); 00062 log_d->pressure = value; 00063 log_d->counter = i; 00064 if(flag) printf("All values STORED\n"); 00065 //Send pointer to sample to the queue 00066 //osStatus stat = 00067 mail_box.put(log_d); 00068 Thread::wait(4000); 00069 00070 i++; 00071 00072 //v.push_back(*log_d); 00073 00074 if(flag) printf("MAIL_BOX.PUT run\n"); 00075 //printf("%d", v.size()); 00076 // printf("Pressure: %f, Humidity: %f", v[0].pressure, v[0].humidity); 00077 // Check for resource error 00078 /* if (stat == osErrorResource) { 00079 if(flag) printf("mail_box->put() Error %4Xh\n", stat); 00080 //Error, free up memory block taken 00081 mail_box.free(log_d); 00082 00083 }*/ 00084 // Thread::wait(1000); 00085 //} 00086 } 00087 00088 int main() 00089 { 00090 printf("Sampling started...\n"); 00091 00092 printf("\n\rWelcome! Type one of the following commands and press space or enter"); 00093 printf("\n\r Don't forget to use only capitals when typing the commands!"); 00094 printf("\n\r READ ALL\n\r READ <n>\n\r DELETE ALL\n\r DELETE <n>"); 00095 printf("\n\r SETDATE dd mm yyyy\n\r SETTIME hh mm ss"); 00096 printf("\n\r SETT <T>\n\r STATE <x>\n\r LOGGING <x>\n\r EXIT\n\n\r"); 00097 00098 //Initialise time 00099 set_time(0); 00100 00101 //ISR to sample data 00102 Ticker ticker; 00103 ticker.attach(&sampleData, e.T); 00104 00105 char command[20]; 00106 char arg[10]; 00107 00108 //User commands 00109 00110 while(1) { 00111 scanf("%s", command); 00112 00113 if (strcmp("READ", command)==0) { 00114 scanf("%s", arg); 00115 printf("\n"); 00116 if(strcmp("ALL", arg)==0) 00117 Thread readAllThread(u.readAllData, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); 00118 00119 else if (atoi(arg)!= 0) { 00120 n = atoi(arg); //A bit hacky, but it works 00121 Thread readThread(u.readNData, NULL,osPriorityBelowNormal, DEFAULT_STACK_SIZE); 00122 } 00123 00124 else perror("The argument is invalid\n\r"); 00125 } 00126 00127 else if (strcmp("DELETE", command)==0) { 00128 scanf("%s", arg); 00129 printf("\n"); 00130 if (strcmp("ALL", arg)==0) { 00131 //int n = v.size(); 00132 //v.clear(); 00133 printf("DELETED %d RECORDS", n); 00134 } else if (atoi(arg)!= 0) { 00135 /*int n = v.size(); 00136 v.erase(v.begin(), v.end()); 00137 if(n < atoi(arg)) 00138 */ printf("DELETED %d RECORDS", n); 00139 //else printf("DELETED %d RECORDS", atoi(arg)); 00140 } 00141 00142 else perror("The argument is invalid\n\r"); 00143 } 00144 00145 else if (strcmp("SETDATE", command)==0) { 00146 char day[2], month[2], year[4]; 00147 //Read day 00148 scanf("%s", day); 00149 if (atoi(day)!=0) 00150 atoi(day)<31 ? t.tm_mday=atoi(day) : printf("Invalid day\n"); 00151 // Read month 00152 scanf("%s", month); 00153 if (atoi(month)!=0) 00154 atoi(month)<=12 ? t.tm_mon=atoi(month)-1 : printf("Invalid month\n"); 00155 //Read year 00156 scanf("%s", year); 00157 printf("\n"); 00158 if (atoi(year)!=0) 00159 atoi(year)>=70 ? t.tm_year=atoi(year)-1900 : printf("Invalid year\n"); 00160 00161 // Check whether date is correct 00162 if (mktime(&t)>0) { 00163 printf("DATE UPDATED TO %d %d %d", t.tm_mday, 00164 t.tm_mon+1, t.tm_year+1900); 00165 set_time(mktime(&t)); 00166 } else perror("Date inserted is invalid\n\r"); 00167 } 00168 00169 else if (strcmp("SETTIME", command)==0) { 00170 char hour[2], min[2], sec[2]; 00171 00172 scanf("%s", hour); 00173 if (atoi(hour)!=0) 00174 t.tm_hour = atoi(hour); 00175 scanf("%s", min); 00176 if (atoi(min)!=0) 00177 t.tm_min = atoi(min); 00178 scanf("%s", sec); 00179 if (atoi(sec)!=0) 00180 t.tm_sec = atoi(sec); 00181 printf("\n"); 00182 00183 //If the user sets this before without DATE, it's gonna be garbage 00184 00185 // Check whether time is correct 00186 if (mktime(&t)>0) { 00187 set_time(mktime(&t)); 00188 printf("TIME UPDATED TO %d %d %d", t.tm_hour, t.tm_min, t.tm_sec); 00189 } else perror("Time inserted is invalid\n\r"); 00190 } 00191 00192 // Set sampling period 00193 else if (strcmp("SETT", command)==0) { 00194 scanf("%s", arg); 00195 printf("\n"); 00196 if (atof(arg) >= 0.1 && atof(arg) <= 60.0 ) { 00197 e.T = atof(arg); 00198 ticker.attach(sampleData,e.T); 00199 printf("T UPDATED TO %.1f", e.T); 00200 } else perror ("Value must be between 1 and 60"); 00201 00202 //Toggle sampling on and off 00203 00204 } else if (strcmp("STATE", command)==0) { 00205 scanf("%s", arg); 00206 printf("\n"); 00207 if (strcmp("ON", arg)==0) { 00208 ticker.attach(sampleData, e.T); 00209 printf("SAMPLING ON"); 00210 } else if (strcmp("OFF", arg)==0) { 00211 ticker.detach(); 00212 printf("SAMPLIG OFF"); 00213 } 00214 00215 else perror("The argument is invalid\n\r"); 00216 } 00217 00218 //Toggle debug messages on and off 00219 else if(strcmp("LOGGING", command)==0) { 00220 scanf("%s", arg); 00221 printf("\n"); 00222 if (strcmp("ON", arg)==0){ 00223 printf("Diagnostics ON"); 00224 flag= true; 00225 00226 } 00227 else if (strcmp("OFF", arg)==0){ 00228 00229 printf("Diagnostics OFF"); 00230 flag= false; 00231 00232 } 00233 else perror ("The argument is invalid\n\r"); 00234 } 00235 00236 00237 else if(strcmp("EXIT", command) == 0) { 00238 break; 00239 } 00240 00241 else perror("\nThere is no command matching. Try again"); 00242 00243 // Clear the input to avoid it to being reused in the next cycle 00244 command[0] = arg[0] = 0; 00245 printf("\n\r"); 00246 } 00247 00248 00249 printf ("Thank you for using our system \n"); 00250 printf ("\n Press the reset button to start again \n"); 00251 00252 return 0; 00253 }
Generated on Fri Jul 15 2022 17:35:06 by
1.7.2