]> Pileus Git - ~andy/csm213a-hw/blob - yue/main.cpp
Update from Yue
[~andy/csm213a-hw] / yue / main.cpp
1 #include "mbed.h"
2 #include "TSISensor.h"          // Touch Sensor
3 #include "MAG3110.h"            // Magnetic Sensor
4 #include "MMA8451Q.h"           // AcceleroMeter
5
6
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
13
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);
19 Ticker clock1;
20 AnalogIn lightSensor(PTE22);
21
22
23 // Global Variables
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;
29
30 bool lightEnable = true;
31 bool accEnable = true;
32 bool magEnable = true;
33 bool touchEnable = true;
34
35 void clock1_interrupt();
36 void serialRx_interrupt();
37 void sendLightInfo();
38 void sendAccInfo();
39 void sendMagInfo();
40 void sendTouchInfo();
41
42
43 int main() {
44     // Initialization
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
48     magSensor.begin();
49
50     serial.printf("\r\n============= Start of the program ============\r\n");
51     while(1){
52         wait(3);
53     }
54 }
55
56
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");
65
66     // Receive the Serial Input
67     float interval;
68     char buffer[255];
69     char temp[255];
70     char ch = serial.getc();
71     int i;
72     for (i=0 ; ch!='\n' && ch!='\r'; i++){
73         serial.putc(ch);
74         buffer[i] = ch;
75         if (ch==127){                // BackSpace
76             i--;
77             i--;
78         }
79         ch = serial.getc();
80     }
81     buffer[i] = '\0';
82     serial.printf("\r\nBUFFER: %s %d\r\n", buffer,i);
83     // TODO: buffer -> lower case
84
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){
91             interval  = 1;
92         }
93         if (strstr(buffer, "acc")){
94             accTmr = interval;
95         }
96         if (strstr(buffer, "mag")){
97             magTmr = interval;
98         }
99         if (strstr(buffer, "light")){
100             lightTmr = interval;
101         }
102         if (strstr(buffer, "touch")){
103             touchTmr = interval;
104         }
105     }
106
107     // Stop Command
108     else if (strstr(buffer, "stop")!= NULL){
109         serial.printf("STOP\r\n");
110         if (strstr(buffer, "acc")){
111             accEnable = false;
112             accTmr = ACC_SNS_DEFAULT;
113         }
114         if (strstr(buffer, "mag")){
115             magEnable = false;
116             magTmr = MAG_SNS_DEFAULT;
117         }
118         if (strstr(buffer, "light")){
119             lightEnable = false;
120             lightTmr = LIGHT_SNS_DEFAULT;
121         }
122         if (strstr(buffer, "touch")){
123             touchEnable = false;
124             touchTmr = TOUCH_SNS_DEFAULT;
125         }
126     }
127
128     // Start Command
129     else if (strstr(buffer, "start")!=NULL){
130         if (strstr(buffer, "acc") && !accEnable){
131             accEnable = true;
132             accTmr = ACC_SNS_DEFAULT;
133         }
134         if (strstr(buffer, "mag") && !magEnable){
135             magEnable = true;
136             magTmr = MAG_SNS_DEFAULT;
137         }
138         if (strstr(buffer, "light") && !lightEnable){
139             lightEnable = true;
140             lightTmr = LIGHT_SNS_DEFAULT;
141         }
142         if (strstr(buffer, "touch") && !touchEnable){
143             touchEnable = true;
144             touchTmr = TOUCH_SNS_DEFAULT;
145         }
146     }
147     clock1.attach(&clock1_interrupt,0.1);
148 }
149
150
151 void clock1_interrupt(){
152     static int accCnt;
153     static int magCnt;
154     static int lightCnt;
155     static int touchCnt;
156
157     accCnt++;
158     magCnt++;
159     lightCnt++;
160     touchCnt++;
161
162     // TODO: send data through Serial
163     if (lightEnable && (lightCnt<0 || lightCnt>=lightTmr/TIME_ACCURACY)){
164         sendLightInfo();
165         lightCnt = 0;
166     }
167     if (magEnable && (magCnt<0 || magCnt>=magTmr/TIME_ACCURACY)){
168         sendMagInfo();
169         magCnt = 0;
170     }
171     if (touchEnable && (touchCnt<0 || touchCnt>=touchTmr/TIME_ACCURACY)){
172         sendTouchInfo();
173         touchCnt = 0;
174     }
175     if (accEnable && (accCnt<0 || accCnt>=accTmr/TIME_ACCURACY)){
176         sendAccInfo();
177         accCnt = 0;
178     }
179 }
180
181 void sendLightInfo(){
182     serial.printf("[LGT] Light Intensity=%f\r\n", lightSensor.read());
183 }
184
185
186 void sendAccInfo(){
187     // get acc data
188     float accX = accSensor.getAccX();
189     float accY = accSensor.getAccY();
190     float accZ = accSensor.getAccZ();
191
192     // send acc data
193     serial.printf("[ACC] accX=%-2.4f accY=%-2.4f accZ=%-2.4f\r\n",accX,accY,accZ);
194 }
195
196 void sendTouchInfo(){
197     // get data
198     float touchForce = touchSensor.readPercentage();
199     float distance = touchSensor.readDistance();
200
201     // send data
202     serial.printf("[TCH] Force=%0.4f Distance=%2.2f\r\n", touchForce, distance);
203 }
204
205 void sendMagInfo(){
206     // get data
207     int magX, magY, magZ;
208     magSensor.getValues(&magX, &magY, &magZ);
209
210     // send data
211     serial.printf("[MAG] magX=%d magY=%d magZ=%d\r\n",magX,magY,magZ);
212 }