Update main.ts, main.py

This commit is contained in:
H. Saw 2022-12-08 13:44:38 +08:00
parent ea70643c34
commit 638d65845f
2 changed files with 294 additions and 0 deletions

148
main.py
View file

@ -1 +1,149 @@
# the communicator data
communicator_data = {
'radio': {
'groupID': 1.0,
'receivedSignal': 0
},
'mode': {
'changeChannelMode': False
},
'received': {
'ID': 0
}
}
# sound effects
communicator_sounds = {
'beep': {
'tone': Note.C5,
'speed': music.beat(BeatFraction.EIGHTH)
},
'call': {
'tone': Note.G
}
}
def changeChannelMode(adjustIncrement = 0):
"""
changeChannelMode allows the changing of radio channels.
Parameters:
adjustIncrement: (integer) how much to adjust
Returns:
groupID: (integer) the current channel ID
"""
# Enable the change channel and ID mode.
communicator_data['mode']['changeChannelMode'] = True
# The groupID variable includes the channel number (integer) and the device's identifier (non-integer).
if (adjustIncrement):
if (adjustIncrement >= 1):
# Increase the selected ID number!
communicator_data['radio']['groupID'] += 0.1
elif (adjustIncrement <= -1) and (communicator_data['radio']['groupID'] > 1):
# Decrease the selected ID number!
communicator_data['radio']['groupID'] = communicator_data['radio']['groupID'] - 0.1
# Get device channel only.
device_groupID = str(communicator_data['radio']['groupID'])
device_channelNumber = int(device_groupID.split('.')[0])
# Set the group number properly.
radio.set_group(device_channelNumber)
# Stop the animation and show the number.
led.stop_animation()
basic.show_number(communicator_data['radio']['groupID'])
def message_send():
"""
Send the message.
Parameters: none
Returns: none
"""
# Convert
device_groupID = str(communicator_data['radio']['groupID'])
# Get the device number only.
device_number = int((device_groupID.split("."))[1])
# Broadcast the message.
radio.send_number(device_number)
def message_receive():
"""
Display the received message.
Parameters: none
Returns: none
"""
# Get the device number only.
device_receive_number = communicator_data['received']['ID']
# Display the device number.
basic.show_number(device_receive_number)
# Ring.
music.ring_tone(communicator_sounds['call']['tone'])
# Pause.
basic.pause(5000)
# Stop.
basic.clear_screen()
music.stop_all_sounds()
def on_button_pressed_ab():
# Play the button clicked sound.
music.play_tone(communicator_sounds['beep']['tone'], communicator_sounds['beep']['speed'])
# Stop the animation.
led.stop_animation()
# allow the toggling of channel changing
if not(communicator_data['mode']['changeChannelMode']):
changeChannelMode()
else:
communicator_data['mode']['changeChannelMode'] = False
basic.clear_screen()
def on_button_pressed_a():
# Play the button clicked sound.
music.play_tone(communicator_sounds['beep']['tone'], communicator_sounds['beep']['speed'])
# Stop the animation.
led.stop_animation()
if (communicator_data['mode']['changeChannelMode']):
changeChannelMode(-1)
else:
# A button is mapped to short sounds.
message_send()
def on_button_pressed_b():
# Play the button clicked sound.
music.play_tone(communicator_sounds['beep']['tone'], communicator_sounds['beep']['speed'])
# Stop the animation.
led.stop_animation()
if (communicator_data['mode']['changeChannelMode']):
changeChannelMode(1)
else:
# B button is mapped to the longer sound
message_send()
input.on_button_pressed(Button.AB, on_button_pressed_ab)
input.on_button_pressed(Button.A, on_button_pressed_a)
input.on_button_pressed(Button.B, on_button_pressed_b)
def on_received_number(receivedNumber):
communicator_data['received']['ID'] = receivedNumber
message_receive()
radio.on_received_number(on_received_number)

146
main.ts
View file

@ -1 +1,147 @@
// the communicator data
let communicator_data = {
"radio" : {
"groupID" : 1.0,
"receivedSignal" : 0,
}
,
"mode" : {
"changeChannelMode" : false,
}
,
"received" : {
"ID" : 0,
}
,
}
// sound effects
let communicator_sounds = {
"beep" : {
"tone" : Note.C5,
"speed" : music.beat(BeatFraction.Eighth),
}
,
"call" : {
"tone" : Note.G,
}
,
}
function changeChannelMode(adjustIncrement: number = 0) {
let device_groupID: string;
let device_channelNumber: number;
/**
changeChannelMode allows the changing of radio channels.
Parameters:
adjustIncrement: (integer) how much to adjust
Returns:
groupID: (integer) the current channel ID
*/
// Enable the change channel and ID mode.
communicator_data["mode"]["changeChannelMode"] = true
// The groupID variable includes the channel number (integer) and the device's identifier (non-integer).
if (adjustIncrement) {
if (adjustIncrement >= 1) {
// Increase the selected ID number!
communicator_data["radio"]["groupID"] += 0.1
} else if (adjustIncrement <= -1 && communicator_data["radio"]["groupID"] > 1) {
// Decrease the selected ID number!
communicator_data["radio"]["groupID"] = communicator_data["radio"]["groupID"] - 0.1
}
// Get device channel only.
device_groupID = "" + communicator_data["radio"]["groupID"]
device_channelNumber = parseInt(_py.py_string_split(device_groupID, ".")[0])
// Set the group number properly.
radio.setGroup(device_channelNumber)
}
// Stop the animation and show the number.
led.stopAnimation()
basic.showNumber(communicator_data["radio"]["groupID"])
}
function message_send() {
/**
Send the message.
Parameters: none
Returns: none
*/
// Convert
let device_groupID = "" + communicator_data["radio"]["groupID"]
// Get the device number only.
let device_number = parseInt(_py.py_string_split(device_groupID, ".")[1])
// Broadcast the message.
radio.sendNumber(device_number)
}
function message_receive() {
/**
Display the received message.
Parameters: none
Returns: none
*/
// Get the device number only.
let device_receive_number = communicator_data["received"]["ID"]
// Display the device number.
basic.showNumber(device_receive_number)
// Ring.
music.ringTone(communicator_sounds["call"]["tone"])
// Pause.
basic.pause(5000)
// Stop.
basic.clearScreen()
music.stopAllSounds()
}
input.onButtonPressed(Button.AB, function on_button_pressed_ab() {
// Play the button clicked sound.
music.playTone(communicator_sounds["beep"]["tone"], communicator_sounds["beep"]["speed"])
// Stop the animation.
led.stopAnimation()
// allow the toggling of channel changing
if (!communicator_data["mode"]["changeChannelMode"]) {
changeChannelMode()
} else {
communicator_data["mode"]["changeChannelMode"] = false
basic.clearScreen()
}
})
input.onButtonPressed(Button.A, function on_button_pressed_a() {
// Play the button clicked sound.
music.playTone(communicator_sounds["beep"]["tone"], communicator_sounds["beep"]["speed"])
// Stop the animation.
led.stopAnimation()
if (communicator_data["mode"]["changeChannelMode"]) {
changeChannelMode(-1)
} else {
// A button is mapped to short sounds.
message_send()
}
})
input.onButtonPressed(Button.B, function on_button_pressed_b() {
// Play the button clicked sound.
music.playTone(communicator_sounds["beep"]["tone"], communicator_sounds["beep"]["speed"])
// Stop the animation.
led.stopAnimation()
if (communicator_data["mode"]["changeChannelMode"]) {
changeChannelMode(1)
} else {
// B button is mapped to the longer sound
message_send()
}
})
radio.onReceivedNumber(function on_received_number(receivedNumber: number) {
communicator_data["received"]["ID"] = receivedNumber
message_receive()
})