-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicroprocessorsDesignProject.c
More file actions
270 lines (212 loc) · 7.15 KB
/
MicroprocessorsDesignProject.c
File metadata and controls
270 lines (212 loc) · 7.15 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Rohan Shanbhag - 250955627
Jodhveer Gill - 250949498
Osama Masud - 250957075
Eshaan Tyagi - 250956760
*/
#include <stdio.h>
#include <stdlib.h>
#include "address_map_arm.h"
#include "GSInterface.h"
#include <unistd.h>
#include <curl/curl.h>
/* Default - 10K max memory for our param strings */
#define MAX_TWILIO_MESSAGE_SIZE 10000
// defines boolean operator in C as it does not exist in library
typedef enum { false, true } bool;
/** Prototypes */
void sendSMS(void);
int twilio_send_message(char *account_sid,
char *auth_token,
char *message,
char *from_number,
char *to_number,
char *picture_url,
bool verbose);
volatile int lookupTable[] = {0x3F, 0x37, 0x71, 0x77, 0x38, 0x79, 0x50, 0x78, 0x00}; //hex disp O, N, F, A, L, E, R, T, off
volatile unsigned char *(HEX3_HEX0_BASE_ptr) = (unsigned char *)HEX3_HEX0_BASE; //first 4
volatile unsigned char *(HEX5_HEX4_BASE_ptr) = (unsigned char *)HEX5_HEX4_BASE; //next 2
unsigned char x[2]; //two bytes (char is one byte, size 2)
volatile int *switches = (int*)SW_BASE;
volatile int *buttons = (int*)KEY_BASE;
int armed = 0;
bool alert = false;
// password is read from switches
int password = 7;
int ReadSwitches(void) {
volatile int a;
a = *(SW_ptr) &= 0xF;
return a;
}
void GSInit(void) {
//for +/- 2G range set D0 and D1 to 0
WriteGSRegister(0x31, 8);
// Configure for 200Hz sampling rate. Sampling rate of 10 == 100Hz. Each incremement after that is a doubling.
WriteGSRegister(0x2c, 11);
// Configure to begin measurement
WriteGSRegister(0x2d, 8);
}
int main (void) {
I2C0Init();
// initial state is OFF
*(HEX3_HEX0_BASE_ptr) = lookupTable[2];
*(HEX3_HEX0_BASE_ptr + 1) = lookupTable[2];
*(HEX3_HEX0_BASE_ptr + 2) = lookupTable[0];
// ensures reading of correct register
if (ReadGSRegister(GS_DEVID) == 0xE5)
{
// initialization of GS sensor
GSInit();
while (1){
// password must be inputed before arming system
if (*buttons == 0b0001 && ReadSwitches() == password) {
armed =1;
// display "ARMED/ON"
*(HEX3_HEX0_BASE_ptr) = lookupTable[1];
*(HEX3_HEX0_BASE_ptr + 1) = lookupTable[0];
*(HEX3_HEX0_BASE_ptr + 2) = lookupTable[8];
}
while (armed){
MultiReadGS(GS_DATAY0, x, 2);
signed short xInt = ((x[1] << 8) | x[0]);
int threshold = ((xInt + 260) / 52); // change to find threshold value
if (threshold < 2) {
alert = true;
//display "Intrusion Alert" and send text
*(HEX3_HEX0_BASE_ptr) = lookupTable[7];
*(HEX3_HEX0_BASE_ptr+ 1) = lookupTable[6];
*(HEX3_HEX0_BASE_ptr + 2) = lookupTable[5];
*(HEX3_HEX0_BASE_ptr + 3) = lookupTable[4];
*(HEX5_HEX4_BASE_ptr) = lookupTable[3];
*(HEX5_HEX4_BASE_ptr + 1) = lookupTable[8];
// Send twilio message
sendSMS();
}
if (*buttons == 0b0010 && alert) {
// change display to Armed/ON
*(HEX3_HEX0_BASE_ptr) = lookupTable[1];
*(HEX3_HEX0_BASE_ptr + 1) = lookupTable[0];
*(HEX3_HEX0_BASE_ptr + 2) = lookupTable[8];
*(HEX3_HEX0_BASE_ptr + 2) = lookupTable[8];
*(HEX5_HEX4_BASE_ptr) = lookupTable[8];
*(HEX5_HEX4_BASE_ptr + 1) = lookupTable[8];
alert = false;
}
if (*buttons == 0b0100) {
armed = 0;
// display "DISARMED/OFF"
*(HEX3_HEX0_BASE_ptr) = lookupTable[2];
*(HEX3_HEX0_BASE_ptr + 1) = lookupTable[2];
*(HEX3_HEX0_BASE_ptr + 2) = lookupTable[0];
*(HEX5_HEX4_BASE_ptr) = lookupTable[8];
*(HEX5_HEX4_BASE_ptr + 1) = lookupTable[8];
}
}
}
}
}
/** PRAGMA MARK: Twilio SMS */
/** Twilio Helper Function */
void sendSMS(void) {
char *account_sid = "insert account sid here";
char *auth_token = "enter auth token here";
char *message = "ALERT! Someone has intruded your home. This is an automated message. Seek help or acknowledge immediately.";
char *from_number = "enter from number";
char *to_number = "enter to number";
twilio_send_message(account_sid,
auth_token,
message,
from_number,
to_number,
NULL,
false);
}
size_t _twilio_null_write(char *ptr, size_t size, size_t nmemb, void *userdata)
{
return size * nmemb;
}
int twilio_send_message(char *account_sid,
char *auth_token,
char *message,
char *from_number,
char *to_number,
char *picture_url,
bool verbose)
{
if (strlen(message) > 1600) {
fprintf(stderr, "SMS send failed.\n"
"Message body must be less than 1601 characters.\n"
"The message had %zu characters.\n", strlen(message));
return -1;
}
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
char url[MAX_TWILIO_MESSAGE_SIZE];
snprintf(url,
sizeof(url),
"%s%s%s",
"https://api.twilio.com/2010-04-01/Accounts/",
account_sid,
"/Messages");
char parameters[MAX_TWILIO_MESSAGE_SIZE];
if (!picture_url) {
snprintf(parameters,
sizeof(parameters),
"%s%s%s%s%s%s",
"To=",
to_number,
"&From=",
from_number,
"&Body=",
message);
} else {
snprintf(parameters,
sizeof(parameters),
"%s%s%s%s%s%s%s%s",
"To=",
to_number,
"&From=",
from_number,
"&Body=",
message,
"&MediaUrl=",
picture_url);
}
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, parameters);
curl_easy_setopt(curl, CURLOPT_USERNAME, account_sid);
curl_easy_setopt(curl, CURLOPT_PASSWORD, auth_token);
if (!verbose) {
curl_easy_setopt(curl,
CURLOPT_WRITEFUNCTION,
_twilio_null_write);
}
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
if (res != CURLE_OK) {
if (verbose) {
fprintf(stderr,
"SMS send failed: %s.\n",
curl_easy_strerror(res));
}
return -1;
} else if (http_code != 200 && http_code != 201) {
if (verbose) {
fprintf(stderr,
"SMS send failed, HTTP Status Code: %ld.\n",
http_code);
}
return -1;
} else {
if (verbose) {
fprintf(stderr,
"SMS sent successfully!\n");
}
return 0;
}
}