Some function signature cleanup

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2023-11-06 17:22:18 +01:00
parent bf9a21b76d
commit 4e5e0f7345
8 changed files with 94 additions and 51 deletions

View File

@ -24,7 +24,6 @@ anchor_bottom = 1.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
script = ExtResource("1_28t32") script = ExtResource("1_28t32")
osc_dest_port = 9001
debug = true debug = true
[node name="MarginContainer" type="MarginContainer" parent="."] [node name="MarginContainer" type="MarginContainer" parent="."]

View File

@ -26,14 +26,14 @@ func _exit_tree():
stop_server() stop_server()
func register_callback(oscaddress, argtypes, callback): func register_callback(osc_address, arg_types, callback) -> void:
if not _observers.has([oscaddress, argtypes]): if not _observers.has([osc_address, arg_types]):
_observers[[oscaddress, argtypes]] = [] _observers[[osc_address, arg_types]] = []
_observers[[oscaddress, argtypes]].append(callback) _observers[[osc_address, arg_types]].append(callback)
func start_server(port:int = default_port, bind_address:String = default_address): func start_server(port:int = default_port, bind_address:String = default_address) -> void:
_server = UDPServer.new() _server = UDPServer.new()
if _server.listen(port, bind_address) != OK: if _server.listen(port, bind_address) != OK:
@ -44,7 +44,7 @@ func start_server(port:int = default_port, bind_address:String = default_address
_timer.start(poll_interval) _timer.start(poll_interval)
func stop_server(): func stop_server() -> void:
_timer.stop() _timer.stop()
remove_child(_timer) remove_child(_timer)
_timer.free() _timer.free()
@ -53,12 +53,12 @@ func stop_server():
_server.stop() _server.stop()
func _debug(msg): func _debug(msg) -> void:
if debug: if debug:
print(msg) print(msg)
func _poll(): func _poll() -> void:
if not _server.is_listening(): if not _server.is_listening():
return return
@ -81,11 +81,11 @@ func _poll():
_debug("OSC arg types: %s" % types) _debug("OSC arg types: %s" % types)
if _observers.has([address, types]): if _observers.has([address, types]):
var values = _parse_osc_values(packet.slice(offset), types) result = _parse_osc_values(packet.slice(offset), types)
if values == null: if result[0] != OK:
_debug("Invalid/Unsupported OSC message.") _debug("Invalid/Unsupported OSC message.")
elif values.size() != types.length(): elif result[1].size() != types.length():
_debug("Mismatch between expected / received number of OSC arguments.") _debug("Mismatch between expected / received number of OSC arguments.")
else: else:
var msg_info = { var msg_info = {
@ -96,9 +96,9 @@ func _poll():
} }
for callback in _observers[[address, types]]: for callback in _observers[[address, types]]:
callback.call(msg_info, values) callback.call(msg_info, result[1])
func _parse_osc_addr_and_types(packet: PackedByteArray): func _parse_osc_addr_and_types(packet: PackedByteArray) -> Array:
var asep = packet.find(0) var asep = packet.find(0)
var address = packet.slice(0, asep).get_string_from_ascii() var address = packet.slice(0, asep).get_string_from_ascii()
@ -110,7 +110,7 @@ func _parse_osc_addr_and_types(packet: PackedByteArray):
return [address, types, tsep + (4 - tsep % 4)] return [address, types, tsep + (4 - tsep % 4)]
func _parse_osc_values(packet: PackedByteArray, types: String): func _parse_osc_values(packet: PackedByteArray, types: String) -> Array:
var result var result
var values = [] var values = []
var stream = StreamPeerBuffer.new() var stream = StreamPeerBuffer.new()
@ -153,7 +153,7 @@ func _parse_osc_values(packet: PackedByteArray, types: String):
stream.seek(stream.get_position() + (4 - count % 4)) stream.seek(stream.get_position() + (4 - count % 4))
else: else:
_debug("Could not read OSC blob argument.") _debug("Could not read OSC blob argument.")
return return [ERR_PARSE_ERROR]
"t": "t":
result = stream.get_data(8) result = stream.get_data(8)
@ -161,7 +161,7 @@ func _parse_osc_values(packet: PackedByteArray, types: String):
values.append(result[1]) values.append(result[1])
else: else:
_debug("Could not read OSC timetag argument.") _debug("Could not read OSC timetag argument.")
return return [ERR_PARSE_ERROR]
"m", "r": "m", "r":
values.append([ values.append([
stream.get_u8(), stream.get_u8(),
@ -177,6 +177,6 @@ func _parse_osc_values(packet: PackedByteArray, types: String):
values.append(null) values.append(null)
_: _:
_debug("Argument type '%s' not supported." % type_id) _debug("Argument type '%s' not supported." % type_id)
return return [ERR_INVALID_DATA]
return values return [OK, values]

View File

@ -17,12 +17,12 @@ func _init():
_socket.set_dest_address(_address, _port) _socket.set_dest_address(_address, _port)
func _debug(msg): func _debug(msg) -> void:
if debug: if debug:
print(msg) print(msg)
func set_destination(dest): func set_destination(dest) -> void:
if dest is int: if dest is int:
_socket.set_dest_address(_address, dest) _socket.set_dest_address(_address, dest)
_port = dest _port = dest
@ -35,16 +35,18 @@ func set_destination(dest):
_port = dest[1] _port = dest[1]
func send_osc(oscaddress: String, argtypes: String, values: Array, dest = null): func create_message(osc_address: String, arg_types: String, values: Array) -> Array:
assert(oscaddress.begins_with("/")) if not osc_address.begins_with("/"):
return [ERR_INVALID_DATA]
var buf = StreamPeerBuffer.new() var buf = StreamPeerBuffer.new()
buf.set_big_endian(true) buf.set_big_endian(true)
_pack_string(buf, oscaddress) _pack_string(buf, osc_address)
_pack_string(buf, "," + argtypes) _pack_string(buf, "," + arg_types)
var vidx: int = 0 var vidx: int = 0
for i in argtypes.length(): for i in arg_types.length():
var typetag = argtypes[i] var typetag = arg_types[i]
var inc_vidx = true var inc_vidx = true
match typetag: match typetag:
@ -72,19 +74,28 @@ func send_osc(oscaddress: String, argtypes: String, values: Array, dest = null):
inc_vidx = false # no argument value sent inc_vidx = false # no argument value sent
_: _:
_debug("Argument type '%s' not supported." % typetag) _debug("Argument type '%s' not supported." % typetag)
return FAILED return [ERR_INVALID_PARAMETER]
if inc_vidx: if inc_vidx:
vidx += 1 vidx += 1
return [OK, buf.get_data_array()]
func send_osc(osc_address: String, arg_types: String, values: Array, dest = null) -> Error:
var res = create_message(osc_address, arg_types, values)
if res[0] != OK:
return res[0]
if dest != null: if dest != null:
set_destination(dest) set_destination(dest)
_socket.put_packet(buf.get_data_array()) _socket.put_packet(res[1])
return OK return OK
func _pack_string(buf: StreamPeerBuffer, s: String): func _pack_string(buf: StreamPeerBuffer, s: String) -> void:
## Pack a string into a binary OSC buffer ## Pack a string into a binary OSC buffer
buf.put_data(s.to_ascii_buffer()) buf.put_data(s.to_ascii_buffer())
@ -93,7 +104,7 @@ func _pack_string(buf: StreamPeerBuffer, s: String):
buf.put_u8(0) buf.put_u8(0)
func _pack_blob(buf, blob): func _pack_blob(buf, blob) -> void:
## Pack a PackedByteArray, Array or String into a binary OSC buffer ## Pack a PackedByteArray, Array or String into a binary OSC buffer
if blob is String: if blob is String:
blob = blob.to_utf8_buffer() blob = blob.to_utf8_buffer()

BIN
screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

34
screenshot.png.import Normal file
View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://bc0u3ivd6q1jg"
path="res://.godot/imported/screenshot.png-024a21af5d37bf0f0dd0e2bccdd149d0.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://screenshot.png"
dest_files=["res://.godot/imported/screenshot.png-024a21af5d37bf0f0dd0e2bccdd149d0.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

View File

@ -1,9 +1,5 @@
extends VSlider extends VSlider
func recv_osc(msg_info, values): func recv_osc(msg_info, values) -> void:
#print("Sender IP: %s" % msg_info["ip"])
#print("Sender Port: %d" % msg_info["port"])
#print("Address: %s" % msg_info["address"])
#print("Types: %s" % msg_info["types"])
set_value_no_signal(clampf(values[0], 0.0, 1.0)) set_value_no_signal(clampf(values[0], 0.0, 1.0))

View File

@ -1,9 +1,5 @@
extends Button extends Button
func recv_osc(msg_info, values): func recv_osc(msg_info, values) -> void:
#print("Sender IP: %s" % msg_info["ip"])
#print("Sender Port: %d" % msg_info["port"])
#print("Address: %s" % msg_info["address"])
#print("Types: %s" % msg_info["types"])
set_pressed_no_signal(bool(values[0])) set_pressed_no_signal(bool(values[0]))

27
ui.gd
View File

@ -27,33 +27,40 @@ func _ready():
# configure all sensors # configure all sensors
for i in range(4): for i in range(4):
var nodename = "VSlider%d" % (i + 1) var nodename = "VSlider%d" % (i + 1)
var osc_addr = "/slider/%d" % i var osc_address = "/slider/%d" % i
var slider_node = find_child(nodename) var slider_node = find_child(nodename)
osc_server.register_callback(osc_addr, "f", slider_node.recv_osc) osc_server.register_callback(osc_address, "f", slider_node.recv_osc)
slider_node.value_changed.connect(slider_callback.bind(i)) slider_node.value_changed.connect(slider_callback.bind(i))
if debug:
osc_server.register_callback(osc_address, "f", print_osc_message)
for i in range(4): for i in range(4):
var nodename = "Button%d" % (i + 1) var nodename = "Button%d" % (i + 1)
var osc_addr = "/button/%d" % i var osc_address = "/button/%d" % i
var button_node = find_child(nodename) var button_node = find_child(nodename)
osc_server.register_callback(osc_addr, "i", button_node.recv_osc)
button_node.pressed.connect(button_callback.bind(button_node, i)) button_node.pressed.connect(button_callback.bind(button_node, i))
osc_server.register_callback(osc_address, "i", button_node.recv_osc)
if debug:
osc_server.register_callback(osc_address, "i", print_osc_message)
# start listening for osc messages # start listening for osc messages
osc_server.start_server(osc_server_port, osc_server_address) osc_server.start_server(osc_server_port, osc_server_address)
func _button_pressed(btn: Button, idx: int): func _button_pressed(btn: Button, idx: int) -> void:
_debug("Button %s pressed: state = %s" % [idx, btn.button_pressed]) var value = 1 if btn.button_pressed else 0
osc_client.send_osc("/button/%d" % idx, "i", [1 if btn.button_pressed else 0]) _debug("Button %s pressed: value = %s" % [idx, value])
osc_client.send_osc("/button/%d" % idx, "i", [value])
func _slider_moved(value: float, idx: int): func _slider_moved(value: float, idx: int) -> void:
_debug("Slider %d moved: value = %f" % [idx, value]) _debug("Slider %d moved: value = %f" % [idx, value])
osc_client.send_osc("/slider/%d" % idx, "f", [value]) osc_client.send_osc("/slider/%d" % idx, "f", [value])
func recv_osc(msg_info, values): func print_osc_message(msg_info, values) -> void:
_debug("Sender IP: %s" % msg_info["ip"]) _debug("Sender IP: %s" % msg_info["ip"])
_debug("Sender Port: %d" % msg_info["port"]) _debug("Sender Port: %d" % msg_info["port"])
_debug("Address: %s" % msg_info["address"]) _debug("Address: %s" % msg_info["address"])
@ -61,6 +68,6 @@ func recv_osc(msg_info, values):
_debug("Values: %s" % values) _debug("Values: %s" % values)
func _debug(msg): func _debug(msg) -> void:
if debug: if debug:
print(msg) print(msg)