UNPKG

3.59 kBJavaScriptView Raw
1var tessel = require('tessel');
2
3var master = tessel.port('a');
4var slave = tessel.port('b');
5
6var passedLED = tessel.led(1);
7var errLED = tessel.led(2);
8passedLED.output().low();
9errLED.output().low();
10
11var failTimeout;
12var failed;
13
14var ble = require('../index');
15
16var _masterController;
17var _slaveController;
18
19
20var connectToMaster = function(callback) {
21 // Connect master port to ble module
22 _masterController = ble.use(master, function(err) {
23
24 // If there wasn't a failure
25 if (!modulesFailed(err, "Error Connecting to Master")) {
26
27 // Report
28 console.log("Successfully connected to master.")
29
30 // Call callback
31 setImmediate(function() {
32 callback && callback();
33 });
34 }
35 });
36}
37
38var connectToSlave = function(callback) {
39
40 console.log("Connecting to slave...");
41 // Connect slave port to module
42 _slaveController = ble.use(slave, function(err) {
43 if (!modulesFailed(err, "Error connecting to slave!")) {
44
45 console.log("Successfully connected to slave.")
46
47 // Add the slave address to the master's whitelist
48 addSlaveToWhitelist(callback);
49 }
50 });
51}
52
53function addSlaveToWhitelist(callback) {
54 // Get this slave's bluetooth address
55 _slaveController.getAddress(function(err, address) {
56
57 if (!modulesFailed(err)) {
58 // Logging
59 console.log("Here is my address!", address);
60 // Add the slave controller's address to the master's whitelist
61 _masterController.whitelistAppend(address, function(err, response) {
62
63 // if there was no problem adding the slave's address to the whitelist
64 if (!modulesFailed(err, "Error adding slave address to whitelist")) {
65
66 console.log("Adding slave address to whitelist successful.")
67 // Call the callback
68 setImmediate(function() {
69 callback && callback();
70 });
71 }
72 });
73 }
74 });
75}
76
77var connectModules = function(callback) {
78 connectToMaster(function() {
79 connectToSlave(function() {
80 callback && callback();
81 })
82 })
83}
84
85var connectOverBLE = function(callback) {
86 console.log("Beginning slave advertisement...");
87
88 // Have the slave start advertising
89 _slaveController.startAdvertising( function(err, response) {
90 if (!modulesFailed(err, "Error making slave advertise...")) {
91
92 console.log("Slave started advertising successfully...")
93 // Then the master should connect to any peripheral on its whitelist (the slave)
94 _masterController.connectSelective(function(err, response) {
95 if (!modulesFailed(err, "Error initiating connect selective mode")) {
96 console.log("Connect request successful");
97 // call that callback
98 setImmediate(function() {
99 callback && callback();
100 })
101 }
102 })
103 }
104 });
105}
106
107function modulesFailed(err, reason) {
108 if (err) {
109 errLED.high();
110 passedLED.low();
111 console.log("Modules failed.", reason, ":", err);
112 failed = true;
113 return 1;
114 }
115 return 0;
116}
117
118function modulesPassed() {
119 console.log("Tests passed!");
120 passedLED.high();
121}
122
123connectModules( function() {
124 console.log("Set timeout");
125 failTimeout = setTimeout(modulesFailed.bind(null, new Error("Did not connect in time."), "Timeout called"), 25000);
126 connectOverBLE();
127});
128
129// Woohoo! Master connected to the slave!
130_masterController.once('connected', function() {
131 console.log("cleared timeout");
132 clearTimeout(failTimeout);
133 console.log("Finished connecting!");
134
135// clear the whitelist
136_masterController.clearWhitelist(function(err, response) {
137 if (!modulesFailed(err, "Well shit. Failed to clear whitelist")) {
138 // Woot the modules passed!
139
140 // If the timeout didn't occur in the meantime....
141 if (!failed) {
142 modulesPassed();
143 }
144
145 }
146 })
147});
148
149
150
151
152