Skip to content

Commit d292297

Browse files
tinicclaude
andcommitted
refactor: extract HAL pin setup to hal_pins.py
Pulls the ~35 halc.newpin() calls out of lathe_halcomp.py. The new module side-effects the HAL component creation on import, which has to happen before Flask starts routing — same ordering as before. Route handlers continue to read/write the same pin names, re-exported from lathe_halcomp. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b45b057 commit d292297

2 files changed

Lines changed: 80 additions & 36 deletions

File tree

elle-app/elle-hal/hal_pins.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""HAL pin registration for the lathe component.
2+
3+
The pins defined here are the contract with the real-time HAL layer (see
4+
lathe.hal). They need to exist before the Flask app starts serving
5+
requests, hence being module-level. Importing this module has the side
6+
effect of creating the HAL components and pins.
7+
"""
8+
import hal
9+
10+
11+
halc = hal.component("lathe")
12+
haluic = hal.component("halui")
13+
14+
hal_pin_machine_is_on = haluic.newpin("machine.is-on", hal.HAL_BIT, hal.HAL_OUT)
15+
16+
hal_pin_control_source = halc.newpin("control_source", hal.HAL_BIT, hal.HAL_OUT)
17+
18+
hal_pin_position_z = halc.newpin("position_z", hal.HAL_FLOAT, hal.HAL_IN)
19+
hal_pin_position_x = halc.newpin("position_x", hal.HAL_FLOAT, hal.HAL_IN)
20+
hal_pin_position_a = halc.newpin("position_a", hal.HAL_FLOAT, hal.HAL_IN)
21+
hal_pin_speed_rps = halc.newpin("speed_rps", hal.HAL_FLOAT, hal.HAL_IN)
22+
23+
hal_pin_forward_z = halc.newpin("forward_z", hal.HAL_FLOAT, hal.HAL_OUT)
24+
hal_pin_forward_x = halc.newpin("forward_x", hal.HAL_FLOAT, hal.HAL_OUT)
25+
hal_pin_enable_z = halc.newpin("enable_z", hal.HAL_BIT, hal.HAL_OUT)
26+
hal_pin_enable_x = halc.newpin("enable_x", hal.HAL_BIT, hal.HAL_OUT)
27+
hal_pin_enable_stepper_z = halc.newpin("enable_stepper_z", hal.HAL_BIT, hal.HAL_OUT)
28+
hal_pin_enable_stepper_x = halc.newpin("enable_stepper_x", hal.HAL_BIT, hal.HAL_OUT)
29+
30+
hal_pin_position_z_encoder = halc.newpin("position_z_encoder", hal.HAL_FLOAT, hal.HAL_IN)
31+
hal_pin_position_x_encoder = halc.newpin("position_x_encoder", hal.HAL_FLOAT, hal.HAL_IN)
32+
hal_pin_offset_z_encoder = halc.newpin("offset_z_encoder", hal.HAL_FLOAT, hal.HAL_OUT)
33+
hal_pin_offset_z_stepper = halc.newpin("offset_z_stepper", hal.HAL_FLOAT, hal.HAL_OUT)
34+
hal_pin_offset_x_encoder = halc.newpin("offset_x_encoder", hal.HAL_FLOAT, hal.HAL_OUT)
35+
hal_pin_offset_x_stepper = halc.newpin("offset_x_stepper", hal.HAL_FLOAT, hal.HAL_OUT)
36+
37+
hal_pin_control_z_type = halc.newpin("control_z_type", hal.HAL_BIT, hal.HAL_OUT)
38+
hal_pin_control_x_type = halc.newpin("control_x_type", hal.HAL_BIT, hal.HAL_OUT)
39+
hal_pin_velocity_z_cmd = halc.newpin("velocity_z_cmd", hal.HAL_FLOAT, hal.HAL_OUT)
40+
hal_pin_velocity_x_cmd = halc.newpin("velocity_x_cmd", hal.HAL_FLOAT, hal.HAL_OUT)
41+
42+
hal_pin_reset_z = halc.newpin("reset_z", hal.HAL_U32, hal.HAL_OUT)
43+
hal_pin_reset_x = halc.newpin("reset_x", hal.HAL_U32, hal.HAL_OUT)
44+
45+
hal_pin_scale_encoder_z = halc.newpin("scale_encoder_z", hal.HAL_FLOAT, hal.HAL_OUT)
46+
hal_pin_scale_encoder_x = halc.newpin("scale_encoder_x", hal.HAL_FLOAT, hal.HAL_OUT)

elle-app/elle-hal/lathe_halcomp.py

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,44 @@
1212
from gcode.threading import generate_threading_gcode_core
1313
from gcode.turning import generate_turning_gcode_core
1414

15-
halc = hal.component("lathe")
16-
haluic = hal.component("halui")
15+
# Side-effect import: creating HAL pins has to happen before the Flask app
16+
# serves its first request. Re-exported below so existing route handlers can
17+
# read/write pins without a name change.
18+
from hal_pins import (
19+
halc,
20+
haluic,
21+
hal_pin_machine_is_on,
22+
hal_pin_control_source,
23+
hal_pin_position_z,
24+
hal_pin_position_x,
25+
hal_pin_position_a,
26+
hal_pin_speed_rps,
27+
hal_pin_forward_z,
28+
hal_pin_forward_x,
29+
hal_pin_enable_z,
30+
hal_pin_enable_x,
31+
hal_pin_enable_stepper_z,
32+
hal_pin_enable_stepper_x,
33+
hal_pin_position_z_encoder,
34+
hal_pin_position_x_encoder,
35+
hal_pin_offset_z_encoder,
36+
hal_pin_offset_z_stepper,
37+
hal_pin_offset_x_encoder,
38+
hal_pin_offset_x_stepper,
39+
hal_pin_control_z_type,
40+
hal_pin_control_x_type,
41+
hal_pin_velocity_z_cmd,
42+
hal_pin_velocity_x_cmd,
43+
hal_pin_reset_z,
44+
hal_pin_reset_x,
45+
hal_pin_scale_encoder_z,
46+
hal_pin_scale_encoder_x,
47+
)
48+
1749
c = linuxcnc.command()
1850
reset_z = 0
1951
reset_x = 0
2052

21-
hal_pin_machine_is_on = haluic.newpin("machine.is-on", hal.HAL_BIT, hal.HAL_OUT)
22-
23-
hal_pin_control_source = halc.newpin("control_source", hal.HAL_BIT, hal.HAL_OUT)
24-
25-
hal_pin_position_z = halc.newpin("position_z", hal.HAL_FLOAT, hal.HAL_IN)
26-
hal_pin_position_x = halc.newpin("position_x", hal.HAL_FLOAT, hal.HAL_IN)
27-
hal_pin_position_a = halc.newpin("position_a", hal.HAL_FLOAT, hal.HAL_IN)
28-
hal_pin_speed_rps = halc.newpin("speed_rps", hal.HAL_FLOAT, hal.HAL_IN)
29-
30-
hal_pin_forward_z = halc.newpin("forward_z", hal.HAL_FLOAT, hal.HAL_OUT)
31-
hal_pin_forward_x = halc.newpin("forward_x", hal.HAL_FLOAT, hal.HAL_OUT)
32-
hal_pin_enable_z = halc.newpin("enable_z", hal.HAL_BIT, hal.HAL_OUT)
33-
hal_pin_enable_x = halc.newpin("enable_x", hal.HAL_BIT, hal.HAL_OUT)
34-
hal_pin_enable_stepper_z = halc.newpin("enable_stepper_z", hal.HAL_BIT, hal.HAL_OUT)
35-
hal_pin_enable_stepper_x = halc.newpin("enable_stepper_x", hal.HAL_BIT, hal.HAL_OUT)
36-
37-
hal_pin_position_z_encoder = halc.newpin("position_z_encoder", hal.HAL_FLOAT, hal.HAL_IN)
38-
hal_pin_position_x_encoder = halc.newpin("position_x_encoder", hal.HAL_FLOAT, hal.HAL_IN)
39-
hal_pin_offset_z_encoder = halc.newpin("offset_z_encoder", hal.HAL_FLOAT, hal.HAL_OUT)
40-
hal_pin_offset_z_stepper = halc.newpin("offset_z_stepper", hal.HAL_FLOAT, hal.HAL_OUT)
41-
hal_pin_offset_x_encoder = halc.newpin("offset_x_encoder", hal.HAL_FLOAT, hal.HAL_OUT)
42-
hal_pin_offset_x_stepper = halc.newpin("offset_x_stepper", hal.HAL_FLOAT, hal.HAL_OUT)
43-
44-
hal_pin_control_z_type = halc.newpin("control_z_type", hal.HAL_BIT, hal.HAL_OUT)
45-
hal_pin_control_x_type = halc.newpin("control_x_type", hal.HAL_BIT, hal.HAL_OUT)
46-
hal_pin_velocity_z_cmd = halc.newpin("velocity_z_cmd", hal.HAL_FLOAT, hal.HAL_OUT)
47-
hal_pin_velocity_x_cmd = halc.newpin("velocity_x_cmd", hal.HAL_FLOAT, hal.HAL_OUT)
48-
49-
hal_pin_reset_z = halc.newpin("reset_z", hal.HAL_U32, hal.HAL_OUT)
50-
hal_pin_reset_x = halc.newpin("reset_x", hal.HAL_U32, hal.HAL_OUT)
51-
52-
hal_pin_scale_encoder_z = halc.newpin("scale_encoder_z", hal.HAL_FLOAT, hal.HAL_OUT)
53-
hal_pin_scale_encoder_x = halc.newpin("scale_encoder_x", hal.HAL_FLOAT, hal.HAL_OUT)
54-
5553
app = Flask(__name__)
5654
CORS(app, resources={r"/*": {"origins": "*"}})
5755

0 commit comments

Comments
 (0)