-
Notifications
You must be signed in to change notification settings - Fork 0
/
micro-networking.js
67 lines (67 loc) · 2 KB
/
micro-networking.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
enum RadioMessage {
message1 = 49434
}
/**
* Created by Artucuno#1898
*
* https://github.com/Artucuno
*/
radio.onReceivedValue(function (name, value) {
if (!(isConnected)) {
// Returned from the new host when the connectionNum has been guessed
if (value == 69) {
isConnected = 1
}
// Sends the number 69 to other microbit to notify it that the code has been guessed.
if (value == connectionNum) {
isConnected = 1
isServer = 1
radio.sendValue("connectionCode", 69)
}
}
})
// createServer Function
// LED (True/False) - Enable LED light once connected
// group (int) - Set radio group
// min (int) - Set minimum random number
// max (int) - Set maximum random number
function createServer (LED: boolean, showNum: boolean, group: number, min: number, max: number) {
radio.setGroup(group)
radio.setTransmitPower(7)
connectionNum = randint(min, max)
// Turns on an LED light once connected. Can be used for anything.
// (Set the write pin)
if (showNum) {
basic.showNumber(connectionNum)
basic.pause(200)
}
// Guesses the other microbit connectionNum
while (!(isConnected)) {
radio.sendValue("connectionCode", randint(min, max))
basic.pause(randint(50, 100))
}
// Turns on an LED light once connected. Can be used for anything.
// (Set the write pin)
if (LED) {
pins.digitalWritePin(DigitalPin.P0, 1)
}
basic.showIcon(IconNames.Yes)
}
let isServer = 0
let connectionNum = 0
let isConnected = 0
radio.sendString("")
basic.showNumber(0)
// createServer Function
// LED (True/False) - Enable LED light once connected
// showNum (True/False) - Show connection number
// group (int) - Set radio group
// min (int) - Set minimum random number
// max (int) - Set maximum random number
createServer(false, false, 87, 0, 10)
// Displays which microbit is the host.
// 0 = Client
// 1 = Host
basic.forever(function () {
basic.showNumber(isServer)
})