| User | Revision | Line number | New contents of line |
| demo |
0:41f85a3f645d
|
1
|
// Nokia Presenter Bluetooth
|
| demo |
0:41f85a3f645d
|
2
|
// Copyright (c) 2009 sford
|
| demo |
0:41f85a3f645d
|
3
|
// Released under the MIT License: http://mbed.org/license/mit
|
| demo |
0:41f85a3f645d
|
4
|
|
| demo |
0:41f85a3f645d
|
5
|
#include "NokiaPresenter.h"
|
| demo |
0:41f85a3f645d
|
6
|
|
| demo |
0:41f85a3f645d
|
7
|
#include "mbed.h"
|
| demo |
0:41f85a3f645d
|
8
|
|
| demo |
0:41f85a3f645d
|
9
|
NokiaPresenter::NokiaPresenter(PinName tx, PinName rx, PinName rst) : _bt(tx, rx), _rst(rst) {
|
| demo |
0:41f85a3f645d
|
10
|
printf("Setup bt module\n");
|
| demo |
0:41f85a3f645d
|
11
|
_bt.baud(9600);
|
| demo |
0:41f85a3f645d
|
12
|
_rst = 0;
|
| demo |
0:41f85a3f645d
|
13
|
wait(0.5);
|
| demo |
0:41f85a3f645d
|
14
|
_rst = 1;
|
| demo |
0:41f85a3f645d
|
15
|
wait(0.5);
|
| demo |
0:41f85a3f645d
|
16
|
|
| demo |
0:41f85a3f645d
|
17
|
printf("Ensure we are in AT command mode...\n");
|
| demo |
0:41f85a3f645d
|
18
|
_bt.printf("+++");
|
| demo |
0:41f85a3f645d
|
19
|
wait(1);
|
| demo |
0:41f85a3f645d
|
20
|
while (_bt.readable()) {
|
| demo |
0:41f85a3f645d
|
21
|
_bt.getc(); // empty buffer
|
| demo |
0:41f85a3f645d
|
22
|
}
|
| demo |
0:41f85a3f645d
|
23
|
|
| demo |
0:41f85a3f645d
|
24
|
printf("Looking for BT Module...\n");
|
| demo |
0:41f85a3f645d
|
25
|
at("AT\r", "\r\nOK\r\n");
|
| demo |
0:41f85a3f645d
|
26
|
|
| demo |
0:41f85a3f645d
|
27
|
printf("Reset factory settings...\n");
|
| demo |
0:41f85a3f645d
|
28
|
at("AT&F\r", "\r\nOK\r\n");
|
| demo |
0:41f85a3f645d
|
29
|
|
| demo |
0:41f85a3f645d
|
30
|
printf("Setting name...\n");
|
| demo |
0:41f85a3f645d
|
31
|
at("AT+BTLNM=\"simon\"\r", "\r\nOK\r\n");
|
| demo |
0:41f85a3f645d
|
32
|
|
| demo |
0:41f85a3f645d
|
33
|
printf("Allow connections...\n");
|
| demo |
0:41f85a3f645d
|
34
|
at("AT+BTAUT=1, 0\r", "\r\nOK\r\n");
|
| demo |
0:41f85a3f645d
|
35
|
|
| demo |
0:41f85a3f645d
|
36
|
printf("Start BT server...\n");
|
| demo |
0:41f85a3f645d
|
37
|
at("AT+BTSRV=1\r", "\r\nOK\r\n");
|
| demo |
0:41f85a3f645d
|
38
|
|
| demo |
0:41f85a3f645d
|
39
|
printf("Waiting for presenter connection...\n");
|
| demo |
0:41f85a3f645d
|
40
|
char buffer[23];
|
| demo |
0:41f85a3f645d
|
41
|
for (int i=0; i<21; i++) {
|
| demo |
0:41f85a3f645d
|
42
|
buffer[i] = _bt.getc();
|
| demo |
0:41f85a3f645d
|
43
|
}
|
| demo |
0:41f85a3f645d
|
44
|
|
| demo |
0:41f85a3f645d
|
45
|
printf("Got request, starting handshake...\n");
|
| demo |
0:41f85a3f645d
|
46
|
for (int i=0; i<21; i++) {
|
| demo |
0:41f85a3f645d
|
47
|
_bt.putc(buffer[i]);
|
| demo |
0:41f85a3f645d
|
48
|
}
|
| demo |
0:41f85a3f645d
|
49
|
for (int i=0; i<3; i++) {
|
| demo |
0:41f85a3f645d
|
50
|
buffer[i] = _bt.getc();
|
| demo |
0:41f85a3f645d
|
51
|
}
|
| demo |
0:41f85a3f645d
|
52
|
const char connect[27] = {0x05,0x18,0x00,0x01,0x10,0x01,0x02,0x00,0xFF,0xFF,0x00,0x44,0x00,0x65,0x00,0x73,0x00,0x6B,0x00,0x74,0x00,0x6F,0x00,0x70,0x00,0x00,0x00};
|
| demo |
0:41f85a3f645d
|
53
|
for (int i=0; i<27; i++) {
|
| demo |
0:41f85a3f645d
|
54
|
_bt.putc(connect[i]);
|
| demo |
0:41f85a3f645d
|
55
|
}
|
| demo |
0:41f85a3f645d
|
56
|
|
| demo |
0:41f85a3f645d
|
57
|
printf("Handshake complete, waiting for desktop request...\n");
|
| demo |
0:41f85a3f645d
|
58
|
for (int i=0; i<23; i++) {
|
| demo |
0:41f85a3f645d
|
59
|
buffer[i] = _bt.getc();
|
| demo |
0:41f85a3f645d
|
60
|
}
|
| demo |
0:41f85a3f645d
|
61
|
for (int i=0; i<23; i++) {
|
| demo |
0:41f85a3f645d
|
62
|
_bt.putc(buffer[i]);
|
| demo |
0:41f85a3f645d
|
63
|
}
|
| demo |
0:41f85a3f645d
|
64
|
|
| demo |
0:41f85a3f645d
|
65
|
printf("Presenter connection all setup!\n");
|
| demo |
0:41f85a3f645d
|
66
|
}
|
| demo |
0:41f85a3f645d
|
67
|
|
| demo |
0:41f85a3f645d
|
68
|
char NokiaPresenter::key() {
|
| demo |
0:41f85a3f645d
|
69
|
char buffer[6];
|
| demo |
0:41f85a3f645d
|
70
|
for (int i=0; i<6; i++) {
|
| demo |
0:41f85a3f645d
|
71
|
buffer[i] = _bt.getc();
|
| demo |
0:41f85a3f645d
|
72
|
}
|
| demo |
0:41f85a3f645d
|
73
|
|
| demo |
0:41f85a3f645d
|
74
|
if (buffer[3] == 0x01) {
|
| demo |
0:41f85a3f645d
|
75
|
return 0x00; // keyup
|
| demo |
0:41f85a3f645d
|
76
|
}
|
| demo |
0:41f85a3f645d
|
77
|
if (buffer[5] == 0xF8) {
|
| demo |
0:41f85a3f645d
|
78
|
switch (buffer[4]) {
|
| demo |
0:41f85a3f645d
|
79
|
case 0x07:
|
| demo |
0:41f85a3f645d
|
80
|
return 'L';
|
| demo |
0:41f85a3f645d
|
81
|
case 0x08:
|
| demo |
0:41f85a3f645d
|
82
|
return 'R';
|
| demo |
0:41f85a3f645d
|
83
|
case 0x09:
|
| demo |
0:41f85a3f645d
|
84
|
return 'U';
|
| demo |
0:41f85a3f645d
|
85
|
case 0x0A:
|
| demo |
0:41f85a3f645d
|
86
|
return 'D';
|
| demo |
0:41f85a3f645d
|
87
|
case 0x45:
|
| demo |
0:41f85a3f645d
|
88
|
return 'C';
|
| demo |
0:41f85a3f645d
|
89
|
}
|
| demo |
0:41f85a3f645d
|
90
|
}
|
| demo |
0:41f85a3f645d
|
91
|
return buffer[4];
|
| demo |
0:41f85a3f645d
|
92
|
}
|
| demo |
0:41f85a3f645d
|
93
|
|
| demo |
0:41f85a3f645d
|
94
|
void NokiaPresenter::at(char *command, char *response) {
|
| demo |
0:41f85a3f645d
|
95
|
_bt.printf(command);
|
| demo |
0:41f85a3f645d
|
96
|
for (int i=0; i<strlen(response); i++) {
|
| demo |
0:41f85a3f645d
|
97
|
if (_bt.getc() != response[i]) {
|
| demo |
0:41f85a3f645d
|
98
|
error("AT Command [%s] Failed", command);
|
| demo |
0:41f85a3f645d
|
99
|
}
|
| demo |
0:41f85a3f645d
|
100
|
}
|
| demo |
0:41f85a3f645d
|
101
|
}
|