|
@@ -0,0 +1,80 @@
|
|
1
|
+// === ENTER NRF.getAdress() results of the couple of
|
|
2
|
+// === puck.js devices you want to identify each other
|
|
3
|
+const his = "12:34:56:78:9a:bc", hers = "fe:dc:ba:98:76:54";
|
|
4
|
+
|
|
5
|
+let me = NRF.getAddress(), other = me===hers ? his : hers;
|
|
6
|
+let other_id = `${other} random`;
|
|
7
|
+
|
|
8
|
+// heart-beat LED animation sequence
|
|
9
|
+let seq = [1,1,0,1,1,0,0,0,0], seq_idx = 0;
|
|
10
|
+let anim_interval_id = 0, main_interval_id = 0;
|
|
11
|
+let active = false, grace_period = 0;
|
|
12
|
+
|
|
13
|
+function anim_step() { // a single heart-beat animation step
|
|
14
|
+ digitalWrite(LED1, seq[seq_idx]);
|
|
15
|
+ seq_idx = (seq_idx+1)%seq.length;
|
|
16
|
+}
|
|
17
|
+
|
|
18
|
+// set interval (1/rate) of animation. 0 means stop animation
|
|
19
|
+function animate(interval) {
|
|
20
|
+ if (anim_interval_id) {
|
|
21
|
+ clearInterval(anim_interval_id);
|
|
22
|
+ digitalWrite(LED1, 0);
|
|
23
|
+ anim_interval_id = 0;
|
|
24
|
+ }
|
|
25
|
+ if (interval) {
|
|
26
|
+ anim_interval_id = setInterval(anim_step, interval);
|
|
27
|
+ }
|
|
28
|
+}
|
|
29
|
+
|
|
30
|
+let main_event;
|
|
31
|
+main_event = function() {
|
|
32
|
+ if (grace_period) {
|
|
33
|
+ grace_period--;
|
|
34
|
+ if (!grace_period) {
|
|
35
|
+ if (active) {
|
|
36
|
+ active = false;
|
|
37
|
+ if (main_interval_id) {
|
|
38
|
+ clearInterval(main_interval_id);
|
|
39
|
+ main_interval_id = 0;
|
|
40
|
+ }
|
|
41
|
+ console.log("Deactivated");
|
|
42
|
+ } else {
|
|
43
|
+ active = true;
|
|
44
|
+ console.log("Activated");
|
|
45
|
+ }
|
|
46
|
+ animate(0);
|
|
47
|
+ }
|
|
48
|
+ return;
|
|
49
|
+ }
|
|
50
|
+ NRF.requestDevice(
|
|
51
|
+ {filters: [{id: other_id}]}
|
|
52
|
+ ).then(
|
|
53
|
+ function(device) {
|
|
54
|
+ let rssi = device.rssi, interval = Math.max(30, 120-3*(rssi+80));
|
|
55
|
+ console.log(rssi, interval);
|
|
56
|
+ animate(interval);
|
|
57
|
+ },
|
|
58
|
+ function(e) {
|
|
59
|
+ console.log(e);
|
|
60
|
+ animate(0);
|
|
61
|
+ }
|
|
62
|
+ );
|
|
63
|
+};
|
|
64
|
+
|
|
65
|
+// Button activates/deactivates
|
|
66
|
+// Also animates for a grace period:
|
|
67
|
+// Slow for activate, fast for deactivate
|
|
68
|
+setWatch(function() {
|
|
69
|
+ grace_period = 2;
|
|
70
|
+ if (active) {
|
|
71
|
+ console.log("Deactivating");
|
|
72
|
+ animate(20);
|
|
73
|
+ } else {
|
|
74
|
+ console.log("Activating");
|
|
75
|
+ animate(100);
|
|
76
|
+ if (!main_interval_id) {
|
|
77
|
+ main_interval_id = setInterval(main_event, 3000);
|
|
78
|
+ }
|
|
79
|
+ }
|
|
80
|
+}, BTN, {edge:"rising", debounce:50, repeat:true});
|