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 MAG3110 magSensor(PTE25, PTE24);
18 Serial serial(USBTX, USBRX);
20 AnalogIn lightSensor(PTE22);
24 // Initial interval: in seconds
25 float lightTmr = LIGHT_SNS_DEFAULT;
26 float accTmr = ACC_SNS_DEFAULT;
27 float magTmr = MAG_SNS_DEFAULT;
28 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
51 serial.printf("\r\n============= Start of the program ============\r\n");
58 /*---------------------------------------------------------------
59 ## Receive Interruption of the Serial ##
60 -> used to receive & process user command
61 -> and configure the board accordingly
62 ---------------------------------------------------------------*/
63 void serialRx_interrupt(){
64 clock1.detach(); // close the interrupt
65 serial.printf("\r\n");
67 // Receive the Serial Input
71 char ch = serial.getc();
73 for (i=0 ; ch!='\n' && ch!='\r'; i++){
76 if (ch==127){ // BackSpace
83 serial.printf("\r\nBUFFER: %s %d\r\n", buffer,i);
84 // TODO: buffer -> lower case
86 // Process the Serial Input
87 // Set-Interval Command
88 if (strstr(buffer, "set")!=NULL && strstr(buffer, "int")!= NULL){
89 sscanf(buffer, "%*[^0123456789.]%s", temp); // find the number in buffer
90 sscanf(temp, "%f", &interval); // translate into float
91 if (interval<0.1 || interval>5){
94 if (strstr(buffer, "acc")){
97 if (strstr(buffer, "mag")){
100 if (strstr(buffer, "light")){
103 if (strstr(buffer, "touch")){
109 else if (strstr(buffer, "stop")!= NULL){
110 serial.printf("STOP\r\n");
111 if (strstr(buffer, "acc")){
113 accTmr = ACC_SNS_DEFAULT;
115 if (strstr(buffer, "mag")){
117 magTmr = MAG_SNS_DEFAULT;
119 if (strstr(buffer, "light")){
121 lightTmr = LIGHT_SNS_DEFAULT;
123 if (strstr(buffer, "touch")){
125 touchTmr = TOUCH_SNS_DEFAULT;
130 else if (strstr(buffer, "start")!=NULL){
131 if (strstr(buffer, "acc") && !accEnable){
133 accTmr = ACC_SNS_DEFAULT;
135 if (strstr(buffer, "mag") && !magEnable){
137 magTmr = MAG_SNS_DEFAULT;
139 if (strstr(buffer, "light") && !lightEnable){
141 lightTmr = LIGHT_SNS_DEFAULT;
143 if (strstr(buffer, "touch") && !touchEnable){
145 touchTmr = TOUCH_SNS_DEFAULT;
148 clock1.attach(&clock1_interrupt,0.1);
152 void clock1_interrupt(){
163 // TODO: send data through Serial
164 if (lightEnable && (lightCnt<0 || lightCnt>=lightTmr/TIME_ACCURACY)){
168 if (magEnable && (magCnt<0 || magCnt>=magTmr/TIME_ACCURACY)){
172 if (touchEnable && (touchCnt<0 || touchCnt>=touchTmr/TIME_ACCURACY)){
176 if (accEnable && (accCnt<0 || accCnt>=accTmr/TIME_ACCURACY)){
182 void sendLightInfo(){
183 serial.printf("[LGT] Light Intensity=%f\r\n", lightSensor.read());
189 float accX = accSensor.getAccX();
190 float accY = accSensor.getAccY();
191 float accZ = accSensor.getAccZ();
194 serial.printf("[ACC] accX=%-2.4f accY=%-2.4f accZ=%-2.4f\r\n",accX,accY,accZ);
197 void sendTouchInfo(){
199 float touchForce = touchSensor.readPercentage();
200 float distance = touchSensor.readDistance();
203 serial.printf("[TCH] Force=%0.4f Distance=%2.2f\r\n", touchForce, distance);
208 int magX, magY, magZ;
209 magSensor.getValues(&magX, &magY, &magZ);
212 serial.printf("[MAG] magX=%d magY=%d magZ=%d\r\n",magX,magY,magZ);