]> Pileus Git - ~andy/csm213a-hw/blob - yue/main.cpp
Add sensors code 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 Serial serial(USBTX, USBRX);
18 Ticker clock1;
19 AnalogIn lightSensor(PTE22);
20
21
22 // Global Variables
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;
28
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
49     serial.printf("\r\n============= Start of the program ============\r\n");
50     while(1){
51         wait(3);
52     }
53 }
54
55
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");
64
65     // Receive the Serial Input
66     float interval;
67     char buffer[255];
68     char temp[255];
69     char ch = serial.getc();
70     int i;
71     for (i=0 ; ch!='\n' && ch!='\r'; i++){
72         serial.putc(ch);
73         buffer[i] = ch;
74         ch = serial.getc();
75     }
76     buffer[i] = '\0';
77     serial.printf("\r\n");
78     // TODO: buffer -> lower case
79
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){
86             interval  = 1;
87         }
88         if (strstr(buffer, "acc")){
89             accTmr = interval;
90         }else if (strstr(buffer, "mag")){
91             magTmr = interval;
92         }else if (strstr(buffer, "light")){
93             lightTmr = interval;
94         }else if (strstr(buffer, "touch")){
95             touchTmr = interval;
96         }
97     }
98
99     // Stop Command
100     else if (strstr(buffer, "stop")!= NULL){
101         if (strstr(buffer, "acc")){
102             accEnable = false;
103             accTmr = ACC_SNS_DEFAULT;
104         }else if (strstr(buffer, "mag")){
105             magEnable = false;
106             magTmr = MAG_SNS_DEFAULT;
107         }else if (strstr(buffer, "light")){
108             lightEnable = false;
109             lightTmr = LIGHT_SNS_DEFAULT;
110         }else if (strstr(buffer, "touch")){
111             touchEnable = false;
112             touchTmr = TOUCH_SNS_DEFAULT;
113         }
114     }
115
116     // Start Command
117     else if (strstr(buffer, "start")!=NULL){
118         if (strstr(buffer, "acc") && !accEnable){
119             accEnable = true;
120             accTmr = ACC_SNS_DEFAULT;
121         }else if (strstr(buffer, "mag") && !magEnable){
122             magEnable = true;
123             magTmr = MAG_SNS_DEFAULT;
124         }else if (strstr(buffer, "light") && !lightEnable){
125             lightEnable = true;
126             lightTmr = LIGHT_SNS_DEFAULT;
127         }else if (strstr(buffer, "touch") && !touchEnable){
128             touchEnable = true;
129             touchTmr = TOUCH_SNS_DEFAULT;
130         }
131     }
132     clock1.attach(&clock1_interrupt,0.1);
133 }
134
135
136 void clock1_interrupt(){
137     static int accCnt;
138     static int magCnt;
139     static int lightCnt;
140     static int touchCnt;
141
142     accCnt++;
143     magCnt++;
144     lightCnt++;
145     touchCnt++;
146
147     // TODO: send data through Serial
148     if (lightEnable && (lightCnt<0 || lightCnt>=lightTmr/TIME_ACCURACY)){
149         sendLightInfo();
150         lightCnt = 0;
151     }
152     /*if (magEnable){
153         sendMagInfo();
154     }*/
155     if (touchEnable && (touchCnt<0 || touchCnt>=touchTmr/TIME_ACCURACY)){
156         sendTouchInfo();
157         touchCnt = 0;
158     }
159     if (accEnable && (accCnt<0 || accCnt>=accTmr/TIME_ACCURACY)){
160         sendAccInfo();
161         accCnt = 0;
162     }
163
164 }
165
166 void sendLightInfo(){
167     serial.printf("[LGT] Light Intensity=%f\r\n", lightSensor.read());
168 }
169
170
171 void sendAccInfo(){
172     // get acc data
173     float accX = accSensor.getAccX();
174     float accY = accSensor.getAccY();
175     float accZ = accSensor.getAccZ();
176
177     // send acc data
178     serial.printf("[ACC] accX=%-2.4f accY=%-2.4f accZ=%-2.4f\r\n",accX,accY,accZ);
179 }
180
181 void sendTouchInfo(){
182     // get data
183     float touchForce = touchSensor.readPercentage();
184     float distance = touchSensor.readDistance();
185
186     // send data
187     serial.printf("[TCH] Force=%0.4f Distance=%2.2f\r\n", touchForce, distance);
188 }