-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bluetooth.c
220 lines (133 loc) · 3.72 KB
/
Bluetooth.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
#include <avr/interrupt.h>
#include "include/Bluetooth.h"
#include "include/Time_x.h"
#include "include/Buzz.h"
#include "include/TM1637_x.h"
#include "include/nap.h"
volatile unsigned char USART_data[8];
volatile unsigned char _data[4];
void __init__bluetooth()
{
SREG |= (1<<7);
//Enable Transmition and Reception
UCSR0B |= (1<<RXEN0) | (1<<TXEN0);
// Recieve complete enable
UCSR0B |= (1<<RXCIE0);
//Select mode-Asynchronous USART0
UCSR0C &= (~(1<<UMSEL01)) & (~(1<<UMSEL00));
//Parity desabled
UCSR0C &= (~(1<<UPM01)) & (~(1<<UPM00));
//Stop bit select-1
UCSR0C &= ~(1<<USBS0);
//selct the charactor size of the frame-8 bit
UCSR0B &= ~(1<<UCSZ02);
UCSR0C |= (1<<UCSZ01) | (1<<UCSZ00);
//selct the boud rate 9600-103;
UBRR0 = 103;
//select the speed-2X
UCSR0A &= ~(1<<U2X0);
Transmitt("Hello");
}
ISR(USART_RX_vect)
{
PORTB ^= (1<<PORTB5);
for(uint8_t i=0; i<8; i++){
while(!(UCSR0A & (1<<RXC0)));
USART_data[i] = UDR0;
}
PORTB ^= (1<<PORTB5);
_data[0] = USART_data[4];
_data[1] = USART_data[5];
_data[2] = USART_data[6];
_data[3] = USART_data[7];
if( (USART_data[0] == 'S') && (USART_data[1] == 'T') && (USART_data[2] == '-') && (USART_data[3] == '-') ){
_setTime();
}else if( (USART_data[0] == 'E') && (USART_data[1] == 'F') && (USART_data[2] == '-') && (USART_data[3] == '-') ){
_selFormat();
}else if( (USART_data[0] == 'S') && (USART_data[1] == 'A') && (USART_data[2] == '-') && (USART_data[3] == '-') ){
_setAlarm();
}else if( (USART_data[0] == 'P') && (USART_data[1] == 'A') && (USART_data[2] == '-') && (USART_data[3] == '-') ){
_stopAlarm();
}else if( (USART_data[0] == 'A') && (USART_data[1] == 'R') && (USART_data[2] == '-') && (USART_data[3] == '-') ){
_setRing();
}else if( (USART_data[0] == 'B') && (USART_data[1] == 'V') && (USART_data[2] == '-') && (USART_data[3] == '-') ){
_setBuzzVol();
}else if( (USART_data[0] == 'D') && (USART_data[1] == 'B') && (USART_data[2] == '-') && (USART_data[3] == '-') ){
_setBri();
}else if( (USART_data[0] == 'S') && (USART_data[1] == 'L') && (USART_data[2] == '-') && (USART_data[3] == '-') ){
_sleepIt();
}
else{
Transmitt("Error");
}
}
void Transmitt(const unsigned char* dat)
{
for (uint8_t i = 0; i < 6; i++){
while (!(UCSR0A&(1<<UDRE0))){};
UDR0 = dat[i];
}
}
void _setTime()
{
uint8_t hrs;
uint8_t min;
hrs = (((uint8_t)(_data[0] - '0')) * 10) + ((uint8_t)(_data[1] - '0'));
min = ((uint8_t)(_data[2] - '0') * 10) + ((uint8_t)(_data[3] - '0'));
setTime(hrs, min);
Transmitt("Done.");
butClick();
}
void _selFormat()
{
if( (((((uint8_t)(_data[0] - '0')) * 10) + ((uint8_t)(_data[1] - '0'))) == 24) && (((uint8_t)(_data[2] - '0') * 10) + ((uint8_t)(_data[3] - '0')) == 0) ){
editFormat(1);
Transmitt("Done.");
butClick();
}
else if( (((((uint8_t)(_data[0] - '0')) * 10) + ((uint8_t)(_data[1] - '0'))) == 12) && (((uint8_t)(_data[2] - '0') * 10) + ((uint8_t)(_data[3] - '0')) == 0) ){
editFormat(2);
Transmitt("Done.");
butClick();
}else{
Transmitt("Error");
}
}
void _setAlarm()
{
uint8_t hrs;
uint8_t min;
hrs = (((uint8_t)(_data[0] - '0')) * 10) + ((uint8_t)(_data[1] - '0'));
min = ((uint8_t)(_data[2] - '0') * 10) + ((uint8_t)(_data[3] - '0'));
editAlarm(hrs, min);
Transmitt("Done.");
butClick();
}
void _stopAlarm()
{
stopAlarm();
}
void _setRing()
{
editRing( (uint8_t)(_data[3] - '0') );
Transmitt("Done.");
butClick();
}
void _setBuzzVol()
{
setBuzzVol( (((uint8_t)(_data[2] - '0') * 10) + ((uint8_t)(_data[3] - '0'))) );
Transmitt("Done.");
butClick();
}
void _setBri()
{
editBrightness( ((uint8_t)(_data[3] - '0')) );
Transmitt("Done.");
butClick();
}
void _sleepIt()
{
sleepIt();
Transmitt("Done.");
butClick();
}