From 9d420b97abb440f7ab2ac92754d1019131758873 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Mon, 3 Feb 2014 21:55:49 +0000 Subject: [PATCH] Update from Yue --- yue/main.cpp | 52 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/yue/main.cpp b/yue/main.cpp index fc45431..20186e1 100644 --- a/yue/main.cpp +++ b/yue/main.cpp @@ -14,6 +14,7 @@ // Define Devices & Pins MMA8451Q accSensor(PTE25, PTE24, MMA8451_I2C_ADDRESS); TSISensor touchSensor; +MAG3110 magSensor(PTE25, PTE24); Serial serial(USBTX, USBRX); Ticker clock1; AnalogIn lightSensor(PTE22); @@ -26,7 +27,6 @@ float accTmr = ACC_SNS_DEFAULT; float magTmr = MAG_SNS_DEFAULT; float touchTmr = TOUCH_SNS_DEFAULT; - bool lightEnable = true; bool accEnable = true; bool magEnable = true; @@ -45,6 +45,7 @@ int main() { // Interruption Declarations clock1.attach(&clock1_interrupt, TIME_ACCURACY); // maximun accuracy be 0.1s serial.attach(&serialRx_interrupt, Serial::RxIrq); // receive interrupt for serialS + magSensor.begin(); serial.printf("\r\n============= Start of the program ============\r\n"); while(1){ @@ -71,10 +72,14 @@ void serialRx_interrupt(){ for (i=0 ; ch!='\n' && ch!='\r'; i++){ serial.putc(ch); buffer[i] = ch; + if (ch==127){ // BackSpace + i--; + i--; + } ch = serial.getc(); } buffer[i] = '\0'; - serial.printf("\r\n"); + serial.printf("\r\nBUFFER: %s %d\r\n", buffer,i); // TODO: buffer -> lower case // Process the Serial Input @@ -87,27 +92,34 @@ void serialRx_interrupt(){ } if (strstr(buffer, "acc")){ accTmr = interval; - }else if (strstr(buffer, "mag")){ + } + if (strstr(buffer, "mag")){ magTmr = interval; - }else if (strstr(buffer, "light")){ + } + if (strstr(buffer, "light")){ lightTmr = interval; - }else if (strstr(buffer, "touch")){ + } + if (strstr(buffer, "touch")){ touchTmr = interval; } } // Stop Command else if (strstr(buffer, "stop")!= NULL){ + serial.printf("STOP\r\n"); if (strstr(buffer, "acc")){ accEnable = false; accTmr = ACC_SNS_DEFAULT; - }else if (strstr(buffer, "mag")){ + } + if (strstr(buffer, "mag")){ magEnable = false; magTmr = MAG_SNS_DEFAULT; - }else if (strstr(buffer, "light")){ + } + if (strstr(buffer, "light")){ lightEnable = false; lightTmr = LIGHT_SNS_DEFAULT; - }else if (strstr(buffer, "touch")){ + } + if (strstr(buffer, "touch")){ touchEnable = false; touchTmr = TOUCH_SNS_DEFAULT; } @@ -118,13 +130,16 @@ void serialRx_interrupt(){ if (strstr(buffer, "acc") && !accEnable){ accEnable = true; accTmr = ACC_SNS_DEFAULT; - }else if (strstr(buffer, "mag") && !magEnable){ + } + if (strstr(buffer, "mag") && !magEnable){ magEnable = true; magTmr = MAG_SNS_DEFAULT; - }else if (strstr(buffer, "light") && !lightEnable){ + } + if (strstr(buffer, "light") && !lightEnable){ lightEnable = true; lightTmr = LIGHT_SNS_DEFAULT; - }else if (strstr(buffer, "touch") && !touchEnable){ + } + if (strstr(buffer, "touch") && !touchEnable){ touchEnable = true; touchTmr = TOUCH_SNS_DEFAULT; } @@ -149,9 +164,10 @@ void clock1_interrupt(){ sendLightInfo(); lightCnt = 0; } - /*if (magEnable){ + if (magEnable && (magCnt<0 || magCnt>=magTmr/TIME_ACCURACY)){ sendMagInfo(); - }*/ + magCnt = 0; + } if (touchEnable && (touchCnt<0 || touchCnt>=touchTmr/TIME_ACCURACY)){ sendTouchInfo(); touchCnt = 0; @@ -160,7 +176,6 @@ void clock1_interrupt(){ sendAccInfo(); accCnt = 0; } - } void sendLightInfo(){ @@ -186,3 +201,12 @@ void sendTouchInfo(){ // send data serial.printf("[TCH] Force=%0.4f Distance=%2.2f\r\n", touchForce, distance); } + +void sendMagInfo(){ + // get data + int magX, magY, magZ; + magSensor.getValues(&magX, &magY, &magZ); + + // send data + serial.printf("[MAG] magX=%d magY=%d magZ=%d\r\n",magX,magY,magZ); +} -- 2.43.2