// 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);
float magTmr = MAG_SNS_DEFAULT;
float touchTmr = TOUCH_SNS_DEFAULT;
-
bool lightEnable = true;
bool accEnable = true;
bool magEnable = true;
// 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){
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
}
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;
}
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;
}
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;
sendAccInfo();
accCnt = 0;
}
-
}
void sendLightInfo(){
// 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);
+}