forked from bkzshabbaz/EADAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
236 lines (198 loc) · 4.71 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* EADAS
* Peripheral Usage
* Pulse Sensor
* - P8.4
* - Timer1_A1
* - ADC Channel A7
* - Heart Segment on LCD
* Gyro Connections:
*
* LSM9DS0 ------------- MSP430FR6989
* CSG ------------- P1.4
* CSXM ------------- P3.2
* SDOG ------------- P2.1
* SDOXM ------------- P2.1
* SCL ------------- P1.5
* SDA ------------- P2.0
* VDD ------------- 3v3
* GND ------------- GND
* LCD
*
* GSM Module
* UART - P3.4 P3.5
*
*/
#include <msp430.h>
#include "LSM9DS0.h"
#include <stdio.h>
#include <math.h>
#include "usci.h"
#include "system.h"
#include "lcd.h"
#include "fona808.h"
/*
* Some code used from Sparkfun's LSM9DS0 library.
*/
#define LED0 BIT0 //LED 0
#define LED1 BIT7 //LED 1
int alarm_fall = 0; //Alarm because a fall was detected
int alarm_heartrate = 0; //Alarm because a change in heartrate was detected
int distress_sent = 0; //Flag to indicate whether we've sent a distress signal
#ifdef DEBUG
extern int16_t gx, gy, gz; // x, y, and z axis readings of the gyroscope
extern int16_t ax, ay, az; // x, y, and z axis readings of the accelerometer
extern int16_t mx, my, mz; // x, y, and z axis readings of the magnetometer
#endif
extern volatile int i,beatinterval,bpm,flagup,beat_detect;
extern volatile unsigned int result,highp,lowp,avp,time;
int volatile ADC_request=0;
char bpm_str[5];
#define AVG_NUM 15
static int bpms[AVG_NUM],bpmav=0;
unsigned int bpm_threshold = 0,nobeatcounter=0;
extern unsigned int adc_flag;
int begin()
{
enum gyro_scale gScl = G_SCALE_245DPS;
enum accel_scale aScl = A_SCALE_2G;
enum mag_scale mScl = M_SCALE_2GS;
enum gyro_odr gODR = G_ODR_95_BW_125;
enum accel_odr aODR = A_ODR_50;
enum mag_odr mODR = M_ODR_50;
volatile uint16_t whoami = lsm9ds0_begin(gScl, aScl, mScl, gODR, aODR, mODR);
return (whoami == 0x49D4);
}
void send_distress(char *message)
{
send_sms(message);
distress_sent = 1;
}
/*
* main.c
*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
system_init();
__bis_SR_register(GIE);
__no_operation();
P1OUT &= ~LED0;
P1DIR |= LED0;
P9OUT &= ~LED1;
P9DIR |= LED1;
if (!begin())
{
//printf("Unable to initialize LSM9DS0!\n");
} else {
//printf("LSM9DS0 initialized!\n");
}
initialize_fona();
entrPhone();
int i=-1,j;
for(;;) {
readGyro();
if (adc_flag) {
if(!(ADC12CTL1 & ADC12BUSY) && ADC_request==1)
{
result = ADC12MEM0&0x0FFF;
ADC_request=0;
if(result>highp && result<3500)
highp = result;
if (result<lowp && result>1500)
lowp=result;
avp = (highp+lowp)/2;
if(result>=avp && flagup==1)
{
flagup=0;
beatinterval = time;
time=0;
lcdBlinkSym(HRT,1);
beat_detect=1;
}
if(result<avp && flagup == 0)
{
flagup=1;
lcdBlinkSym(HRT,0);
}
bpm = 60000/(2*beatinterval);
if(bpm<200){
i=(i+1)%AVG_NUM;
bpms[i]=bpm;
}
bpmav=0;
if(beat_detect==1 || nobeatcounter<1000)
{
if(beat_detect==1)
nobeatcounter=0;
for(j=0;j<15;j++)
bpmav=bpmav+bpms[j];
bpmav=bpmav/15;
}
if(beat_detect==0)
nobeatcounter++;
}
sprintf(bpm_str,"%d",bpmav);
if(bpmav<10)
{
lcdPrint(" ", 4, 5);
lcdPrint(bpm_str,6,6);
}
else if(bpmav<100)
{
lcdPrint(" ", 4, 4);
lcdPrint(bpm_str,5,6);
}
else
lcdPrint(bpm_str,4,6);
if(bpmav>130 || bpmav==0)
{
bpm_threshold++;
} else if (bpm_threshold != 0){
bpm_threshold--;
}
}
if (bpm_threshold > 1000) {
P1OUT |= BIT0;
if (!distress_sent) {
send_distress("Heart attack!");
}
lcdPrint("HRT", 1, 3);
lcdBlinkSym(EXCL,1);
} else if (alarm_fall) {
if (!distress_sent) {
send_distress("I've fallen!");
}
lcdPrint("FAL", 1, 3);
lcdBlinkSym(EXCL,1);
P9OUT |= LED1;
} else {
lcdPrint("GUD", 1, 3);
lcdBlinkSym(EXCL,0);
P1OUT &= ~BIT0;
P9OUT &= ~LED1;
}
#ifdef DEBUG
/*******************************************************************
* WARNING!!!
* TURNING ON PRINTFs WILL CAUSE TIMING ISSUES.
* E.G., UART and TIMER WON'T WORK PROPERLY.
*******************************************************************/
/*printf("G: %.2f", fabs(calcGyro(gx)));
printf(", ");
printf("%.2f",fabs(calcGyro(gy)));
printf(", ");
printf("%.2f\n",fabs(calcGyro(gz)));*/
/*
* The accelerometer will read 1g when for an axis that's
* vertical.
*/
//readAccel();
// printf("A: %.2f", fabs(calcAccel(ax)));
// printf(", ");
// printf("%.2f",fabs(calcAccel(ay)));
// printf(", ");
// printf("%.2f\n",fabs(calcAccel(az)));
//TODO: Update the LCD with status information
#endif
}
return 0;
}