APS Lab
/
COG_UART_Base
Uart with ADXL362
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2018 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 */ 00005 00006 #include "mbed.h" 00007 #include "stats_report.h" 00008 #include "ADXL362.h" 00009 #include "math.h" 00010 00011 #define G_2 1.0 00012 #define G_4 2.0 00013 #define G_8 4.0 00014 00015 00016 00017 #define MODE_NORMAL 0 00018 #define MODE_LOW_POWER 1 00019 #define MODE_ULTRA_LOW_POWER 2 00020 00021 DigitalOut led1(LED1); 00022 Serial PC(USBTX, USBRX); 00023 00024 char Buf[128]; 00025 char command[8]; 00026 char mode[8]; 00027 char val[8]; 00028 int f_val; 00029 00030 int set_mode(int m); 00031 int set_freq(int v); 00032 int set_grav(int v); 00033 void set_start(void); 00034 void set_stop(void); 00035 static void clearDisplay(void); 00036 static void drawCLI(ADXL362 *adxlCtrl, AccelTemp *getData); 00037 volatile int f_run = 0; 00038 volatile int f_wake = 0; 00039 SPI *spi1; 00040 ADXL362 *adxlCtrl; 00041 AccelTemp GetData; 00042 DigitalIn ADXL_WAKE(WAKE2); 00043 00044 00045 // main() runs in its own thread in the OS 00046 int main() 00047 { 00048 PC.baud(115200); 00049 PC.printf("EV-COG-AD4050LZ Demo\n"); 00050 00051 spi1 = new SPI(SPI1_MOSI, SPI1_MISO, SPI1_SCLK); 00052 adxlCtrl = new ADXL362(&PC, spi1); 00053 00054 clearDisplay(); 00055 00056 while (true) { 00057 // Blink LED and wait 0.5 seconds 00058 led1 = !led1; 00059 wait_ms(500); 00060 00061 PC.scanf("%s", command); 00062 00063 if(strncmp(command, "set", 3) == 0) 00064 { 00065 PC.scanf("%s",mode); 00066 00067 if(strncmp(mode, "freq", 4) == 0) 00068 { 00069 PC.scanf("%s", val); 00070 f_val = atoi(val); 00071 set_freq(f_val); 00072 } 00073 else if(strncmp(mode, "mode", 4) == 0) 00074 { 00075 PC.scanf("%s", val); 00076 f_val = atoi(val); 00077 set_mode(f_val); 00078 } 00079 else if(strncmp(mode, "grav", 4) == 0) 00080 { 00081 PC.scanf("%s", val); 00082 f_val = atoi(val); 00083 set_grav(f_val); 00084 } 00085 else if(strncmp(mode, "wakeup", 6) == 0) 00086 { 00087 PC.printf("ADXL362 wakeup mode\n"); 00088 adxlCtrl->set_wakeupmode(); 00089 f_wake = 1; 00090 PC.printf("ADXL362 wakeup waiting......\n"); 00091 while(f_wake) 00092 { 00093 if() 00094 { 00095 f_wake = 0; 00096 PC.printf("ADXL362 Wake-up\n"); 00097 } 00098 } 00099 00100 00101 }else 00102 { 00103 PC.printf("Command Invalid\n"); 00104 } 00105 } 00106 else if(strncmp(command, "start", 5) == 0) 00107 { 00108 set_start(); 00109 while(f_run) 00110 { 00111 adxlCtrl->SensorRead(&GetData); 00112 drawCLI(adxlCtrl, &GetData); 00113 wait(0.5); 00114 } 00115 } 00116 //else if(strncmp(command, "stop", 4) == 0) 00117 //{ 00118 // set_stop(); 00119 //} 00120 else 00121 { 00122 PC.printf("Command Usage\n"); 00123 PC.printf("Command : only 'set'\n"); 00124 PC.printf("Mode : 'freq' is ODR configuration\n"); 00125 PC.printf(" : ODR frequency from 12, 25, 50, 100, 200, 400Hz\n"); 00126 PC.printf("Mode : 'grav' is select gravity\n"); 00127 PC.printf(" : 0:2g 1:4g 2:8g\n"); 00128 PC.printf("Mode : 'mode' is power mode\n"); 00129 PC.printf(" : 0:Normal 1:Low Power 2:Ultra Low Power(freq is fixed at 100Hz)\n"); 00130 PC.printf("i.e. 'set mode 0'\n"); 00131 PC.printf("i.e. 'set freq 1000'\n"); 00132 PC.printf("i.e. 'start'\n"); 00133 } 00134 // Following the main thread wait, report on the current system status 00135 //sys_state.report_state(); 00136 } 00137 } 00138 00139 00140 int set_freq(int v) 00141 { 00142 if((v < 12) || (v > 400)) 00143 { 00144 PC.printf("ODR Freqency Range is over/under. Please configure for 12-400 Hz.\n"); 00145 return 1; 00146 } 00147 else 00148 { 00149 PC.printf("Set Frequency %d Hz\n", v); 00150 return 0; 00151 } 00152 } 00153 00154 int set_grav(int v) 00155 { 00156 switch(v) 00157 { 00158 case 0: 00159 PC.printf("Set gravity 2g\n"); 00160 adxlCtrl->set_gravity(GRAVITY_2G); 00161 break; 00162 case 1: 00163 PC.printf("Set gravity 4g\n"); 00164 adxlCtrl->set_gravity(GRAVITY_4G); 00165 break; 00166 case 2: 00167 PC.printf("Set gravity 8g\n"); 00168 adxlCtrl->set_gravity(GRAVITY_8G); 00169 break; 00170 default: 00171 PC.printf("Unknown Command\n"); 00172 break; 00173 }; 00174 00175 return 0; 00176 } 00177 int set_mode(int m) 00178 { 00179 switch(m) 00180 { 00181 case MODE_NORMAL: 00182 PC.printf("Set Normal Mode\n"); 00183 adxlCtrl->set_powermode(POWER_CTL_PARAM_LOWNOISE_NORM); 00184 break; 00185 case MODE_LOW_POWER: 00186 PC.printf("Set Low Power Mode\n"); 00187 adxlCtrl->set_powermode(POWER_CTL_PARAM_LOWNOISE_LOW); 00188 break; 00189 case MODE_ULTRA_LOW_POWER: 00190 PC.printf("Set Ultra Low Power Mode\n"); 00191 adxlCtrl->set_powermode(POWER_CTL_PARAM_LOWNOISE_ULTRA); 00192 break; 00193 default: 00194 PC.printf("Unknown Command\n"); 00195 break; 00196 }; 00197 00198 return 0; 00199 } 00200 00201 void set_start(void) 00202 { 00203 f_run = 1; 00204 adxlCtrl->start(); 00205 PC.printf("Start Accel\n"); 00206 } 00207 00208 void set_stop(void) 00209 { 00210 f_run = 0; 00211 adxlCtrl->stop(); 00212 PC.printf("Stop Accel\n"); 00213 } 00214 00215 static void clearDisplay(void) 00216 { 00217 PC.printf("\033[2J"); 00218 PC.printf("\033[0;0H"); 00219 PC.printf("\033[0m\033[37m"); 00220 } 00221 00222 static void drawCLI(ADXL362 *adxlCtrl, AccelTemp *getData) 00223 { 00224 AccelTemp *min = adxlCtrl->GetMinInfo(); 00225 AccelTemp *max = adxlCtrl->GetMaxInfo(); 00226 AccelTemp *p; 00227 float x, y, z, t; 00228 00229 PC.printf("\033[2J"); 00230 00231 /* BLANK LINE */ 00232 00233 PC.printf("\033[0m\033[33m\033[1m"); 00234 PC.printf("\033[2;1H"); 00235 PC.printf("\033[K"); 00236 PC.printf("ACCELEROMETERs"); 00237 00238 PC.printf("\033[0m\033[37m"); 00239 PC.printf("\033[3;3H"); 00240 PC.printf("\033[K"); 00241 PC.printf("\033[3;10H-X---\033[3;20H-Y---\033[3;30H-Z---"); 00242 00243 PC.printf("\033[0m\033[35m"); 00244 PC.printf("\033[4;3H"); 00245 PC.printf("\033[K"); 00246 p = min; 00247 x = sin(adxlCtrl->ConvAccel(p->ax)); 00248 y = adxlCtrl->ConvAccel(p->ay); 00249 z = adxlCtrl->ConvAccel(p->az); 00250 PC.printf("min\033[4;10H%04.2f\033[4;20H%04.2f\033[4;30H%04.2f", x, y, z); 00251 00252 PC.printf("\033[0m\033[37m\033[1m"); 00253 PC.printf("\033[5;3H"); 00254 PC.printf("\033[K"); 00255 p = getData; 00256 x = adxlCtrl->ConvAccel(p->ax); 00257 y = adxlCtrl->ConvAccel(p->ay); 00258 z = adxlCtrl->ConvAccel(p->az); 00259 PC.printf("-->\033[5;10H%04.2f\033[5;20H%04.2f\033[5;30H%04.2f", x, y, z); 00260 00261 PC.printf("\033[0m\033[36m"); 00262 PC.printf("\033[6;3H"); 00263 PC.printf("\033[K"); 00264 p = max; 00265 x = adxlCtrl->ConvAccel(p->ax); 00266 y = adxlCtrl->ConvAccel(p->ay); 00267 z = adxlCtrl->ConvAccel(p->az); 00268 PC.printf("max\033[6;10H%04.2f\033[6;20H%04.2f\033[6;30H%04.2f", x, y, z); 00269 00270 /* BLANK LINE */ 00271 00272 PC.printf("\033[0m\033[31m\033[1m"); 00273 PC.printf("\033[8;1H"); 00274 PC.printf("\033[K"); 00275 PC.printf("Temperature"); 00276 00277 /* BLANK LINE */ 00278 00279 PC.printf("\033[0m\033[35m"); 00280 PC.printf("\033[10;3H"); 00281 PC.printf("\033[K"); 00282 p = min; 00283 t = adxlCtrl->ConvThermal(p->tm); 00284 PC.printf("min\033[10;10H%04.2f", t); 00285 00286 PC.printf("\033[0m\033[37m\033[1m"); 00287 PC.printf("\033[11;3H"); 00288 PC.printf("\033[K"); 00289 p = getData; 00290 t = adxlCtrl->ConvThermal(p->tm); 00291 PC.printf("-->\033[11;10H%04.2f", t); 00292 00293 PC.printf("\033[0m\033[36m"); 00294 PC.printf("\033[12;3H"); 00295 PC.printf("\033[K"); 00296 p = max; 00297 t = adxlCtrl->ConvThermal(p->tm); 00298 PC.printf("max\033[12;10H%04.2f", t); 00299 } 00300 00301
Generated on Wed Jul 27 2022 11:33:44 by 1.7.2