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
50 serial.printf("\r\n============= Start of the program ============\r\n");
57 /*---------------------------------------------------------------
58 ## Receive Interruption of the Serial ##
59 -> used to receive & process user command
60 -> and configure the board accordingly
61 ---------------------------------------------------------------*/
62 void serialRx_interrupt(){
63 clock1.detach(); // close the interrupt
64 serial.printf("\r\n");
66 // Receive the Serial Input
70 char ch = serial.getc();
72 for (i=0 ; ch!='\n' && ch!='\r'; i++){
75 if (ch==127){ // BackSpace
82 serial.printf("\r\nBUFFER: %s %d\r\n", buffer,i);
83 // TODO: buffer -> lower case
85 // Process the Serial Input
86 // Set-Interval Command
87 if (strstr(buffer, "set")!=NULL && strstr(buffer, "int")!= NULL){
88 sscanf(buffer, "%*[^0123456789.]%s", temp); // find the number in buffer
89 sscanf(temp, "%f", &interval); // translate into float
90 if (interval<0.1 || interval>5){
93 if (strstr(buffer, "acc")){
96 if (strstr(buffer, "mag")){
99 if (strstr(buffer, "light")){
102 if (strstr(buffer, "touch")){
108 else if (strstr(buffer, "stop")!= NULL){
109 serial.printf("STOP\r\n");
110 if (strstr(buffer, "acc")){
112 accTmr = ACC_SNS_DEFAULT;
114 if (strstr(buffer, "mag")){
116 magTmr = MAG_SNS_DEFAULT;
118 if (strstr(buffer, "light")){
120 lightTmr = LIGHT_SNS_DEFAULT;
122 if (strstr(buffer, "touch")){
124 touchTmr = TOUCH_SNS_DEFAULT;
129 else if (strstr(buffer, "start")!=NULL){
130 if (strstr(buffer, "acc") && !accEnable){
132 accTmr = ACC_SNS_DEFAULT;
134 if (strstr(buffer, "mag") && !magEnable){
136 magTmr = MAG_SNS_DEFAULT;
138 if (strstr(buffer, "light") && !lightEnable){
140 lightTmr = LIGHT_SNS_DEFAULT;
142 if (strstr(buffer, "touch") && !touchEnable){
144 touchTmr = TOUCH_SNS_DEFAULT;
147 clock1.attach(&clock1_interrupt,0.1);
151 void clock1_interrupt(){
162 // TODO: send data through Serial
163 if (lightEnable && (lightCnt<0 || lightCnt>=lightTmr/TIME_ACCURACY)){
167 if (magEnable && (magCnt<0 || magCnt>=magTmr/TIME_ACCURACY)){
171 if (touchEnable && (touchCnt<0 || touchCnt>=touchTmr/TIME_ACCURACY)){
175 if (accEnable && (accCnt<0 || accCnt>=accTmr/TIME_ACCURACY)){
181 void sendLightInfo(){
182 serial.printf("[LGT] Light Intensity=%f\r\n", lightSensor.read());
188 float accX = accSensor.getAccX();
189 float accY = accSensor.getAccY();
190 float accZ = accSensor.getAccZ();
193 serial.printf("[ACC] accX=%-2.4f accY=%-2.4f accZ=%-2.4f\r\n",accX,accY,accZ);
196 void sendTouchInfo(){
198 float touchForce = touchSensor.readPercentage();
199 float distance = touchSensor.readDistance();
202 serial.printf("[TCH] Force=%0.4f Distance=%2.2f\r\n", touchForce, distance);
207 int magX, magY, magZ;
208 magSensor.getValues(&magX, &magY, &magZ);
211 serial.printf("[MAG] magX=%d magY=%d magZ=%d\r\n",magX,magY,magZ);