Skip to content

Instantly share code, notes, and snippets.

@t-oot
Created November 7, 2021 06:58
Show Gist options
  • Save t-oot/311f055a736d3107ed23fbaf67aa1e14 to your computer and use it in GitHub Desktop.
Save t-oot/311f055a736d3107ed23fbaf67aa1e14 to your computer and use it in GitHub Desktop.
obごっこ
//obnizのリスト
let obniz_list = [];
obniz_list[0] = "****-****"; //鬼のobniz
obniz_list[1] = "****-****";
obniz_list[2] = "****-****";
let obniz_wifi_list = {}; //BLEで送る名前
obniz_wifi_list[obniz_list[0]] = "1101";
obniz_wifi_list[obniz_list[1]] = "1102";
obniz_wifi_list[obniz_list[2]] = "1103";
let obniz_dist_list = {}
let bpress = false; //ボタン押下状態
//rssiから距離[m]を求める
function rssi2dist(RSSI) {
let WIFI_POWER_19_5dBm = -64;
let Distance = Math.pow(10, ((WIFI_POWER_19_5dBm - RSSI) / (10 * 2)));
// let Distance = 10;
return Distance;
}
//valueからkey取得
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
let pwmc = 0;
async function sol(distance, obniz, pwm) {
console.log("set sol");
//obniz間距離によって振動を変化
if (distance > 1250) {
pwm.start({
io: 5
});
pwm.freq(60); // 60 hz
pwm.duty(50) // 50%
} else if (distance > 30) {
pwm.start({
io: 5
});
pwm.freq(45); // 45 hz
pwm.duty(50) // 50%
} else if (distance > 0) {
pwm.start({
io: 5
});
pwm.freq(26); // 26 hz
pwm.duty(30) // 30%
} else if (distance > 10) {
pwm.start({
io: 5
});
pwm.freq(23); // 23 hz
pwm.duty(30) // 30%
} else {
pwm.start({
io: 5
});
pwm.freq(20); // 20 hz
pwm.duty(30) // 30%
}
}
//鬼側のプログラム
var obniz = new Obniz(obniz_list[0]);
obniz.onconnect = async function() {
// Button
var button = obniz.wired("Keyestudio_Button", {
signal: 0,
vcc: 1,
gnd: 2
});
button.onchange = function(pressed) {
bpress = !pressed;
console.log("pressed:" + pressed)
};
obniz.setVccGnd(11, 10, "5v")
let dist__ = 0;
var pwm = obniz.getFreePwm();
let dist_count = 0;
let key_back = "";
obniz.onloop = async function() {
// console.log(Object.keys(obniz_dist_list).length);
if (Object.keys(obniz_dist_list).length >= 2) {
var array = Object.keys(obniz_dist_list).map((k) => ({
key: k,
value: obniz_dist_list[k]
}));
array.sort((a, b) => a.value - b.value);
let key = array[0].key;
if (key_back != key) dist_count = 0;
key_back = key;
console.log("near key:" + array[0].key);
let dist_ = Math.round(array[0].value * 100 * 10) / 10;
console.log("near dist:" + dist_ + "cm");
pwmc++;
if (dist__ != dist_) {
sol(dist_, obniz, pwm);
pwmc = 0;
dist__ = dist_;
}
if (pwmc >= 5) {
pwm.end();
pwmc = 0;
console.log("reset");
await obniz.wait(10);
sol(dist_, obniz, pwm);
}
if (dist_ <= 60.0) {
if (dist_count <= 30) dist_count++;
} else {
dist_count = 0;
}
if (bpress && (dist_ <= 8.0 || dist_count >= 30)) {
let send_target = getKeyByValue(obniz_wifi_list, array[0].key);
console.log("send to:" + send_target);
obniz.message(send_target, "pressed");
}
obniz.display.clear();
if (dist_ <= 8.0 || dist_count >= 30) {
obniz.display.print("[!!]You can catch");
} else {
if (dist_count >= 1) {
obniz.display.print("[==]Wait " + (30 - dist_count));
} else {
obniz.display.print("[--]Keep looking");
}
}
}
}
obniz.display.clear();
obniz.display.print("BLE ID:" + obniz_wifi_list[obniz_list[0]] + "(oni)");
//obniz.display.clear();
//obniz.display.print("Hello World");
await obniz.ble.initWait();
obniz.ble.advertisement.setAdvData({
serviceUuids: [obniz_wifi_list[obniz_list[0]]]
});
obniz.ble.advertisement.setScanRespData({
localName: obniz_wifi_list[obniz_list[0]],
});
obniz.ble.advertisement.start();
console.log("Started adv");
await obniz.ble.initWait();
var target = {
uuids: [obniz_wifi_list[obniz_list[1]], , obniz_wifi_list[obniz_list[2]]]
};
var setting = {
duration: 60, // 60 sec
duplicate: true, // allow duplicate,
filterOnDevice: false
}
let f_count = 0;
obniz.ble.scan.onfind = async function(peripheral) {
console.log("onfind")
console.log(peripheral)
let dist = rssi2dist(peripheral.rssi);
if (peripheral.localName == null) {
console.error("skip");
return;
}
obniz_dist_list[peripheral.localName] = dist;
console.log(peripheral.localName + ":" + peripheral.rssi + ":" + dist + "m")
f_count++;
if (f_count == 2) {
f_count = 0;
obniz.ble.scan.startWait(target, setting);
}
};
obniz.ble.scan.onfinish = function() {
console.log("scan timeout!")
obniz.ble.scan.startWait(target, setting)
};
await obniz.ble.scan.startWait(target, setting);
};
//逃げる側のプログラム
let obniz_ = []
for (let i = 1; i < obniz_list.length; i++) {
obniz_[i] = new Obniz(obniz_list[i]);
console.log(obniz_list[i]);
obniz_[i].onconnect = async function() {
var speaker = obniz_[i].wired("Keyestudio_Buzzer", {
signal: 0,
vcc: 1,
gnd: 2
});
console.log(obniz_wifi_list[obniz_list[i]])
obniz_[i].display.clear();
obniz_[i].display.print("BLE ID:" + obniz_wifi_list[obniz_list[i]]);
//start sound
for (let j = 0; j < 3; j++) {
speaker.play(500 * (i + 1)) // 1000 Hz
await obniz_[i].wait(100);
speaker.play(0) // 0 Hz
await obniz_[i].wait(100);
}
await obniz_[i].ble.initWait();
obniz_[i].ble.advertisement.setAdvData({
serviceUuids: [obniz_wifi_list[obniz_list[i]]]
});
obniz_[i].ble.advertisement.setScanRespData({
localName: obniz_wifi_list[obniz_list[i]],
});
obniz_[i].onmessage = async function(message, from) {
if (message === "pressed") {
obniz_[i].display.print("You've been caught!!");
speaker.play(500 * (i + 1)) // 1000 Hz
await obniz_[i].wait(500);
speaker.play(0) // 0 Hz
}
};
obniz_[i].ble.advertisement.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment