Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Light Switch example #337

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
*** xref:lua/api/ReflectionSystemStructModule.adoc[ReflectionSystemStructModule]
// FINLuaDocumentationEnd //
** xref:lua/examples/index.adoc[Examples]
*** xref:lua/examples/lightSwitch.adoc[Light Switch]
*** xref:lua/examples/multiThreading.adoc[Multi Threading]
*** xref:lua/examples/paint.adoc[Paint]
*** xref:lua/examples/randomPlot.adoc[Random Plot]
Expand Down
45 changes: 45 additions & 0 deletions docs/modules/ROOT/pages/lua/examples/lightSwitch.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
= Light Switch Example

This small program uses a panel, a switch on the panel, which will control a light that is connected on the network.

More info on the Modular Panel and Events that this example uses xref:lua/guide/ModularPanelAndEvents.adoc[can be found here].

== Setup

You will need: +

1. A computer with a Lua CPU, a little bit of RAM, and an EEPROM
2. A modular panel with a 2 position switch in the middle of the panel
3. One of the lights available in the base game

Connect the computer to a network pole. Connect the light and the control panel to the network pole using network cables.
Plug in the Text EEPROM and paste the code below in the code editor.
Run the computer and switch the button on the control panel on/off.

== Code

[source,Lua]
----
local panels = component.proxy(component.findComponent(classes.LargeControlPanel))
local lights = component.proxy(component.findComponent(classes.LightSource))

local panel = panels[1]
local light = lights[1]

-- The switch is expected to be in the middle of the panel
local switch = panel:getModule(5, 5, 0)

event.ignoreAll()
event.clear()

event.listen(switch)

while true do
local e, s = event.pull()
if s == switch and e == "ChangeState" then
light.isLightEnabled = s.state
end
end
----

include::partial$lua_examples_footer.adoc[]
2 changes: 2 additions & 0 deletions docs/modules/ROOT/partials/lua_examples.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Lua Examples::
+
====
xref:lua/examples/lightSwitch.adoc[Light Switch]
-
xref:lua/examples/multiThreading.adoc[Multi Threading]
-
xref:lua/examples/PCIDevices.adoc[PCI-Devices]
Expand Down