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