2 #include "TSISensor.h" // Touch Sensor
3 #include "MAG3110.h" // Magnetic Sensor
4 #include "MMA8451Q.h" // AcceleroMeter
7 #define MMA8451_I2C_ADDRESS (0x1d<<1) // acc sensor address
8 #define TIME_ACCURACY 0.1
9 #define LIGHT_SNS_DEFAULT 1 // default collecting interval in seconds
10 #define ACC_SNS_DEFAULT 1
11 #define MAG_SNS_DEFAULT 1
12 #define TOUCH_SNS_DEFAULT 1
14 // Define Devices & Pins
15 MMA8451Q accSensor(PTE25, PTE24, MMA8451_I2C_ADDRESS);
16 TSISensor touchSensor;
17 Serial serial(USBTX, USBRX);
19 AnalogIn lightSensor(PTE22);
23 // Initial interval: in seconds
24 float lightTmr = LIGHT_SNS_DEFAULT;
25 float accTmr = ACC_SNS_DEFAULT;
26 float magTmr = MAG_SNS_DEFAULT;
27 float touchTmr = TOUCH_SNS_DEFAULT;
30 bool lightEnable = true;
31 bool accEnable = true;
32 bool magEnable = true;
33 bool touchEnable = true;
35 void clock1_interrupt();
36 void serialRx_interrupt();
45 // Interruption Declarations
46 clock1.attach(&clock1_interrupt, TIME_ACCURACY); // maximun accuracy be 0.1s
47 serial.attach(&serialRx_interrupt, Serial::RxIrq); // receive interrupt for serialS
49 serial.printf("\r\n============= Start of the program ============\r\n");
56 /*---------------------------------------------------------------
57 ## Receive Interruption of the Serial ##
58 -> used to receive & process user command
59 -> and configure the board accordingly
60 ---------------------------------------------------------------*/
61 void serialRx_interrupt(){
62 clock1.detach(); // close the interrupt
63 serial.printf("\r\n");
65 // Receive the Serial Input
69 char ch = serial.getc();
71 for (i=0 ; ch!='\n' && ch!='\r'; i++){
77 serial.printf("\r\n");
78 // TODO: buffer -> lower case
80 // Process the Serial Input
81 // Set-Interval Command
82 if (strstr(buffer, "set")!=NULL && strstr(buffer, "int")!= NULL){
83 sscanf(buffer, "%*[^0123456789.]%s", temp); // find the number in buffer
84 sscanf(temp, "%f", &interval); // translate into float
85 if (interval<0.1 || interval>5){
88 if (strstr(buffer, "acc")){
90 }else if (strstr(buffer, "mag")){
92 }else if (strstr(buffer, "light")){
94 }else if (strstr(buffer, "touch")){
100 else if (strstr(buffer, "stop")!= NULL){
101 if (strstr(buffer, "acc")){
103 accTmr = ACC_SNS_DEFAULT;
104 }else if (strstr(buffer, "mag")){
106 magTmr = MAG_SNS_DEFAULT;
107 }else if (strstr(buffer, "light")){
109 lightTmr = LIGHT_SNS_DEFAULT;
110 }else if (strstr(buffer, "touch")){
112 touchTmr = TOUCH_SNS_DEFAULT;
117 else if (strstr(buffer, "start")!=NULL){
118 if (strstr(buffer, "acc") && !accEnable){
120 accTmr = ACC_SNS_DEFAULT;
121 }else if (strstr(buffer, "mag") && !magEnable){
123 magTmr = MAG_SNS_DEFAULT;
124 }else if (strstr(buffer, "light") && !lightEnable){
126 lightTmr = LIGHT_SNS_DEFAULT;
127 }else if (strstr(buffer, "touch") && !touchEnable){
129 touchTmr = TOUCH_SNS_DEFAULT;
132 clock1.attach(&clock1_interrupt,0.1);
136 void clock1_interrupt(){
147 // TODO: send data through Serial
148 if (lightEnable && (lightCnt<0 || lightCnt>=lightTmr/TIME_ACCURACY)){
155 if (touchEnable && (touchCnt<0 || touchCnt>=touchTmr/TIME_ACCURACY)){
159 if (accEnable && (accCnt<0 || accCnt>=accTmr/TIME_ACCURACY)){
166 void sendLightInfo(){
167 serial.printf("[LGT] Light Intensity=%f\r\n", lightSensor.read());
173 float accX = accSensor.getAccX();
174 float accY = accSensor.getAccY();
175 float accZ = accSensor.getAccZ();
178 serial.printf("[ACC] accX=%-2.4f accY=%-2.4f accZ=%-2.4f\r\n",accX,accY,accZ);
181 void sendTouchInfo(){
183 float touchForce = touchSensor.readPercentage();
184 float distance = touchSensor.readDistance();
187 serial.printf("[TCH] Force=%0.4f Distance=%2.2f\r\n", touchForce, distance);