-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathled_control.py
More file actions
146 lines (108 loc) · 4.27 KB
/
Copy pathled_control.py
File metadata and controls
146 lines (108 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
LED Control Examples
Demonstrates different approaches to controlling printer LEDs:
1. Standalone TCP LED control (for aftermarket LEDs or direct TCP connections)
2. Dual-mode scenario (HTTP client using internal TCP as fallback)
Use Cases:
- Printers with aftermarket LED installations not detected by HTTP API
- Cases where led_control capability flag incorrectly reports False
- Direct TCP-only connections without HTTP API access
"""
import asyncio
import os
from flashforge import FlashForgeClient
from flashforge.tcp.ff_client import FlashForgeClient as TcpClient
async def standalone_tcp_led_control():
"""
Example 1: Standalone TCP LED Control
Use this approach when connecting directly via TCP without the HTTP API.
This is useful for aftermarket LED installations or when you need direct
G-code control.
"""
print("=== Standalone TCP LED Control ===\n")
ip_address = os.getenv("FLASHFORGE_IP", "192.168.1.100")
client = TcpClient(ip_address)
try:
if not await client.init_control():
print("Failed to initialize control session")
return
print("Connected via TCP")
print("Turning LEDs on...")
if await client.led_on():
print("LEDs turned on successfully")
else:
print("Failed to turn LEDs on")
await asyncio.sleep(2)
print("Turning LEDs off...")
if await client.led_off():
print("LEDs turned off successfully")
else:
print("Failed to turn LEDs off")
finally:
await client.dispose()
print("Disconnected\n")
async def dual_mode_led_control():
"""
Example 2: Dual-Mode LED Control
Use this approach when using the HTTP API but need to fall back to TCP
for LED control. This is useful when:
- The printer has aftermarket LEDs not detected by the HTTP API
- The led_control capability flag is incorrectly False
- HTTP LED control fails but TCP works
"""
print("=== Dual-Mode LED Control (HTTP + TCP Fallback) ===\n")
ip_address = os.getenv("FLASHFORGE_IP", "192.168.1.100")
serial_number = os.getenv("FLASHFORGE_SERIAL", "").strip()
check_code = os.getenv("FLASHFORGE_CHECK_CODE", "").strip()
if not serial_number or not check_code:
print("Set FLASHFORGE_SERIAL and FLASHFORGE_CHECK_CODE before running this example")
return
client = FlashForgeClient(ip_address, serial_number, check_code)
try:
status = await client.get_printer_status()
if not status:
print("Failed to initialize HTTP client")
return
if not await client.init_control():
print("Failed to initialize control session")
return
print("Connected to printer")
print(f"LED control capability: {client.led_control}")
if client.led_control:
print("\nUsing HTTP API for LED control...")
await client.control.set_led_on()
await asyncio.sleep(2)
await client.control.set_led_off()
else:
print("\nHTTP LED control not available, using TCP fallback...")
print("Accessing internal TCP client...")
tcp_client = client.tcp_client
print("Turning LEDs on via TCP...")
if await tcp_client.led_on():
print("LEDs turned on successfully (TCP)")
else:
print("Failed to turn LEDs on (TCP)")
await asyncio.sleep(2)
print("Turning LEDs off via TCP...")
if await tcp_client.led_off():
print("LEDs turned off successfully (TCP)")
else:
print("Failed to turn LEDs off (TCP)")
print("\nLED control completed")
finally:
await client.dispose()
print("Disconnected\n")
async def main():
"""Run all LED control examples."""
print("FlashForge LED Control Examples\n")
print("These examples demonstrate TCP-based LED control,")
print("which uses M146 G-code commands instead of HTTP API.\n")
print("=" * 50)
print()
await standalone_tcp_led_control()
await asyncio.sleep(1)
await dual_mode_led_control()
print("=" * 50)
print("\nAll examples completed!")
if __name__ == "__main__":
asyncio.run(main())