Godot + Arduino series: companion project code
Self-contained project code for the "Godot Isn't Just for Games" series: Arduino sketches, the Python serial<->UDP bridge, and the Godot 4 projects, one folder per article.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
extends Control
|
||||
|
||||
# Part 1 — the minimal round trip: a Godot button that toggles an LED on the
|
||||
# Arduino. Only the OUTBOUND (command) half of the link is used here; Part 2
|
||||
# adds inbound telemetry from the joystick.
|
||||
|
||||
const COMMAND_ADDR := "127.0.0.1"
|
||||
const COMMAND_PORT := 4243
|
||||
|
||||
@onready var led_button: Button = $Panel/LedButton
|
||||
|
||||
var _udp_out := PacketPeerUDP.new()
|
||||
var _led_on := false
|
||||
|
||||
func _ready() -> void:
|
||||
# To SEND with PacketPeerUDP in Godot 4, use set_dest_address — NOT
|
||||
# connect_to_host, which is for receiving and silently drops outbound packets.
|
||||
_udp_out.set_dest_address(COMMAND_ADDR, COMMAND_PORT)
|
||||
led_button.pressed.connect(_on_led_pressed)
|
||||
|
||||
func _on_led_pressed() -> void:
|
||||
_led_on = not _led_on
|
||||
_udp_out.put_packet(("L1" if _led_on else "L0").to_utf8_buffer())
|
||||
led_button.text = "LED: ON" if _led_on else "LED: OFF"
|
||||
@@ -0,0 +1 @@
|
||||
uid://ctgyyk0it7dpd
|
||||
@@ -0,0 +1,91 @@
|
||||
[gd_scene load_steps=6 format=3]
|
||||
|
||||
[ext_resource type="Script" path="res://LedBasics.gd" id="1_led"]
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="bg"]
|
||||
bg_color = Color(0.043, 0.055, 0.078, 1)
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="btn_normal"]
|
||||
bg_color = Color(0.11, 0.145, 0.204, 1)
|
||||
border_width_left = 1
|
||||
border_width_top = 1
|
||||
border_width_right = 1
|
||||
border_width_bottom = 1
|
||||
border_color = Color(0.2, 0.6, 1, 0.5)
|
||||
corner_radius_top_left = 8
|
||||
corner_radius_top_right = 8
|
||||
corner_radius_bottom_right = 8
|
||||
corner_radius_bottom_left = 8
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="btn_hover"]
|
||||
bg_color = Color(0.145, 0.196, 0.278, 1)
|
||||
border_width_left = 1
|
||||
border_width_top = 1
|
||||
border_width_right = 1
|
||||
border_width_bottom = 1
|
||||
border_color = Color(0.3, 0.7, 1, 1)
|
||||
corner_radius_top_left = 8
|
||||
corner_radius_top_right = 8
|
||||
corner_radius_bottom_right = 8
|
||||
corner_radius_bottom_left = 8
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="btn_pressed"]
|
||||
bg_color = Color(0.2, 0.6, 1, 1)
|
||||
corner_radius_top_left = 8
|
||||
corner_radius_top_right = 8
|
||||
corner_radius_bottom_right = 8
|
||||
corner_radius_bottom_left = 8
|
||||
|
||||
[node name="Main" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
script = ExtResource("1_led")
|
||||
|
||||
[node name="Panel" type="Panel" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
theme_override_styles/panel = SubResource("bg")
|
||||
|
||||
[node name="Title" type="Label" parent="Panel"]
|
||||
layout_mode = 0
|
||||
offset_left = 40.0
|
||||
offset_top = 96.0
|
||||
offset_right = 480.0
|
||||
offset_bottom = 130.0
|
||||
theme_override_colors/font_color = Color(0.85, 0.93, 1, 1)
|
||||
theme_override_font_sizes/font_size = 24
|
||||
text = "GODOT + ARDUINO: LED"
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
|
||||
[node name="Subtitle" type="Label" parent="Panel"]
|
||||
layout_mode = 0
|
||||
offset_left = 40.0
|
||||
offset_top = 132.0
|
||||
offset_right = 480.0
|
||||
offset_bottom = 152.0
|
||||
theme_override_colors/font_color = Color(0.4, 0.55, 0.7, 1)
|
||||
theme_override_font_sizes/font_size = 12
|
||||
text = "· click to reach down the wire ·"
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
|
||||
[node name="LedButton" type="Button" parent="Panel"]
|
||||
layout_mode = 0
|
||||
offset_left = 150.0
|
||||
offset_top = 220.0
|
||||
offset_right = 370.0
|
||||
offset_bottom = 284.0
|
||||
theme_override_colors/font_color = Color(0.85, 0.93, 1, 1)
|
||||
theme_override_colors/font_hover_color = Color(1, 1, 1, 1)
|
||||
theme_override_colors/font_pressed_color = Color(0.043, 0.055, 0.078, 1)
|
||||
theme_override_font_sizes/font_size = 18
|
||||
theme_override_styles/normal = SubResource("btn_normal")
|
||||
theme_override_styles/hover = SubResource("btn_hover")
|
||||
theme_override_styles/pressed = SubResource("btn_pressed")
|
||||
theme_override_styles/focus = SubResource("btn_normal")
|
||||
text = "LED: OFF"
|
||||
@@ -0,0 +1,24 @@
|
||||
; Engine configuration file.
|
||||
; It's best edited using the editor UI and not directly,
|
||||
; since the parameters that go here are not all obvious.
|
||||
;
|
||||
; Format:
|
||||
; [section] ; section goes between []
|
||||
; param=value ; assign values to parameters
|
||||
|
||||
config_version=5
|
||||
|
||||
[application]
|
||||
|
||||
config/name="ArduinoGodot"
|
||||
run/main_scene="res://LedBasics.tscn"
|
||||
config/features=PackedStringArray("4.7")
|
||||
|
||||
[display]
|
||||
|
||||
window/size/viewport_width=520
|
||||
window/size/viewport_height=610
|
||||
|
||||
[dotnet]
|
||||
|
||||
project/assembly_name="ArduinoGodot"
|
||||
Reference in New Issue
Block a user