Bladeren bron

Initial commit

master
The Dod 2 jaren geleden
commit
424db21514
3 gewijzigde bestanden met toevoegingen van 105 en 0 verwijderingen
  1. 1
    0
      .gitignore
  2. 24
    0
      README.md
  3. 80
    0
      proximibeat.js

+ 1
- 0
.gitignore Bestand weergeven

@@ -0,0 +1 @@
1
+our-proximibeat.js

+ 24
- 0
README.md Bestand weergeven

@@ -0,0 +1,24 @@
1
+### Proximibeat
2
+
3
+Code that makes a couple of [puck.js](https://www.puck-js.com/) devices blink
4
+the red LED in a heart-beat sequence if they sense each other on a BLE scan.
5
+The blink rate increases the stronger the signal is (perfect as his/hers blings).
6
+
7
+![Neora and Nimrod demonstrate](https://zzzen.com/kraftz/2022-02-22%20-%20proximibeat.gif)
8
+
9
+Necklaces were designed by [Ortal Ben Dayan](https://www.ortalbendayan.com/).
10
+
11
+Button activates/deactivates scan and blink functionality (in order to save battery).
12
+Even when deactivated it doesn't stop advertising BLE (and other device, if active,
13
+can sense it). Note, however, that if you connect to a device (for example via the web IDE),
14
+the other device won't sense it (this is a very common face-palm situation).
15
+
16
+As feedback, the device performs a few heart beat animations (regardless of whether or not
17
+it senses the other device):
18
+
19
+* When activating, animation rate is slow.
20
+* When deactivating, animation rate is fast.
21
+
22
+Initial state of the device (after uploading code) is inactive.
23
+
24
+You can do a `save()` from IDE in that state (no need for `onInit()` or such, because button would activate whatever needs activating).

+ 80
- 0
proximibeat.js Bestand weergeven

@@ -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});

Laden…
Annuleren
Opslaan