Skip to content

Commit d39c1a2

Browse files
committed
docs(readme): Update and clarify instructions and formatting
1 parent 8e268dc commit d39c1a2

File tree

1 file changed

+62
-46
lines changed

1 file changed

+62
-46
lines changed

README.rst

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,72 @@ PrusaConnect SDK for Printer
33

44
Printer instance
55
----------------
6-
You can create a Printer instance using the constructor and passing `server` and `token` to it. These you can find in `prusa_printer_settings.ini`.
6+
You can create a Printer instance using the constructor passing printer type,
7+
serial number and printer fingerprint to it. Serial number can be found on the printer label.
8+
For I3MK3 and SL1, printer fingerprint is a SHA256 HEX digest of the serial number.
79

810
.. code:: python
911
1012
from prusa.connect.printer import Printer, const
1113
14+
SN = "Printer serial number"
15+
FINGERPRINT = sha256(SN.encode()).hexdigest()
16+
PRINTER_TYPE = const.PrinterType.I3MK3
17+
printer = Printer(PRINTER_TYPE, SN, FINGERPRINT)
18+
19+
For setting the connection you should call `set_connection` passing `server` and `token` to it. These you can find in the `prusa_printer_settings.ini` file. You can download it after printer registration from the Connect web by selecting your printer -> Settings -> LAN settings -> Download settings. If the printer has not been registered yet, refer to the Registration section below.
20+
21+
.. code:: python
22+
1223
SERVER = "https://connect.prusa3d.com"
13-
SN = 'SERIAL_NUMBER_FROM_PRINTER'
14-
FINGERPRINT = 'Printer fingerprint'
15-
TOKEN = 'secret token from prusa_printer_settings.ini'
16-
printer = Printer(const.PrinterType.I3MK3, SN, SERVER, TOKEN)
24+
TOKEN = "Secret token from prusa_printer_settings.ini"
1725
18-
printer.loop() # communication loop
26+
printer.set_connection(SERVER, TOKEN)
1927
20-
Or you can use `Printer.from_config()` method which reads these values from the ini file.
28+
printer.loop() # Communication loop
2129
22-
.. code:: python
30+
Or you can use `printer.connection_from_config()` method which reads these values directly from the .ini file.
2331

24-
from prusa.connect.printer import Printer, const
32+
.. code:: python
2533
26-
SERVER = "https://connect.prusa3d.com"
27-
SN = 'SERIAL_NUMBER_FROM_PRINTER'
28-
printer = Printer.from_config("./prusa_printer_settings.ini",
29-
const.PrinterType.I3MK3, SN)
34+
printer.connection_from_config("./prusa_printer_settings.ini")
3035
31-
printer.loop() # communication loop
36+
printer.loop() # Communication loop
3237
3338
3439
Registration
3540
------------
36-
If the printer has not been registered yet, you need to use `Printer.register()` to get a temporary code. This code is then used in the **Add Printer** form in Connect Web. After the printer
37-
has been added to Connect, `Printer.get_token()` will return printer's persistent token.
41+
If the printer has not been registered yet, you need to use `Printer.register()` to get a temporary code. Enter this code in the **Add Printer** form in Connect Web. After the printer has been added to Connect, your Printer instance will automatically retrieve the code in the background.
3842

3943
.. code:: python
4044
4145
from time import sleep
4246
from prusa.connect.printer import Printer, const
4347
4448
SERVER = "https://connect.prusa3d.com"
45-
SN = 'SERIAL_NUMBER_FROM_PRINTER'
46-
printer = Printer(const.PrinterType.I3MK3, SN, SERVER)
49+
SN = "Printer serial number"
50+
FINGERPRINT = "Printer fingerprint"
51+
PRINTER_TYPE = const.PrinterType.I3MK3
52+
printer = Printer(PRINTER_TYPE, SN, FINGERPRINT)
53+
54+
printer.set_connection(SERVER, None)
4755
4856
tmp_code = printer.register()
4957
print(f"Use this code `{tmp_code}` in add printer form "
5058
f"{SERVER}/printers/overview?code={tmp_code}.")
5159
52-
token = None
53-
while token is None:
54-
token = printer.get_token(tmp_code)
60+
while printer.token is None:
61+
print("Waiting for the printer registration on the Connect web...")
5562
sleep(1)
5663
57-
print("Printer is registered with token %s" % token)
64+
print(f"Printer is registered with token {printer.token}")
65+
66+
Note: For I3MK3 the Add Printer form does not allow you manually enter the temporary code.
67+
You can use this url to add it directly: https://connect.prusa3d.com:443/add-printer/connect/{PRINTER_TYPE}/{tmp_code}
5868

5969
Telemetry
6070
---------
61-
Printer must send telemetry to connect at least each second. Because obtaining telemetry values might not be atomic, this must be done in a different thread than `Printer.loop`.
71+
Printer must send telemetry to Connect at least once per second. Because obtaining telemetry values might not be atomic, this must be done in a separate thread from `Printer.loop`.
6272

6373
.. code:: python
6474
@@ -67,29 +77,32 @@ Printer must send telemetry to connect at least each second. Because obtaining t
6777
6878
...
6979
70-
# start communication loop
80+
# Start communication loop in a separate thread
7181
thread = Thread(target=printer.loop)
7282
thread.start()
7383
74-
# each second send telemetry to internal queue in the main-thread
84+
# Send telemetry to the main thread queue once per second
7585
while True:
7686
printer.telemetry(const.State.READY, temp_nozzle=24.1, temp_bed=23.2)
7787
sleep(1)
7888
79-
8089
Events
8190
------
82-
Events are a way to send information about the printer to Connect. They can be split into a few groups:
83-
84-
* **Command answers** - Response for Connect if the command was be ACCEPTED,
85-
REJECTED, etc. These are handled by the SDK in `Printer.loop` method or in `Command.__call__` method.
86-
* **State change** - indicating that the printer state has changed. This are sent
87-
by `Printer.set_state` method.
88-
* **FILE INFO** events which are created by `FileSystem` object.
89-
* Alternatively you can inform Connect about other events like attaching/detaching of storage.
90-
You can do this by calling `Printer.event_cb`.
91-
92-
Examples for these groups follow below.
91+
Events are a way to send information about the printer to Connect.
92+
They can be grouped into several categories:
93+
94+
- **Command answers**
95+
Responses to Connect indicating whether a command was ACCEPTED, REJECTED, etc.
96+
These are handled by the SDK in the `Printer.loop` method or the `Command.__call__` method.
97+
- **State changes**
98+
Indicate that the printer's state has changed. These are sent by the `Printer.set_state` method.
99+
- **FILE INFO**
100+
Events created by the FileSystem object.
101+
- **Other events**
102+
For example, informing Connect about storage being attached or detached.
103+
You can do this by calling `Printer.event_cb`.
104+
105+
Examples of each category are provided below.
93106

94107
Event callback
95108
--------------
@@ -101,15 +114,15 @@ You can inform Connect about some specific situation using events.
101114
102115
...
103116
104-
# start communication loop
117+
# Start communication loop
105118
thread = Thread(target=printer.loop)
106119
thread.start()
107120
108121
try:
109122
...
110123
except Exception as err:
111-
# send event to internal queue
112-
printer.event_cb(const.Event.ATTENTION, const.Source.WUI,
124+
# Send event to internal queue
125+
printer.event_cb(const.Event.FAILED, const.Source.WUI,
113126
reason=str(err))
114127
115128
Printer state
@@ -122,11 +135,11 @@ Printer state
122135
123136
...
124137
125-
# start communication loop
138+
# Start communication loop
126139
thread = Thread(target=printer.loop)
127140
thread.start()
128141
129-
# toggle the state each second
142+
# Toggle the state each second
130143
while True:
131144
if printer.state == const.State.READY:
132145
printer.set_state(const.State.BUSY, const.Source.MARLIN)
@@ -168,26 +181,29 @@ a dictionary as a value.
168181
...
169182
170183
@printer.handler(const.Command.START_PRINT)
171-
def start_print(args: List[str]):
184+
def start_print(args: list[str]):
172185
"""This handler will be called when START_PRINT command was sent to
173186
the printer."""
174187
printer.set_state(const.State.PRINTING, const.Source.CONNECT)
175188
print("Printing file: {args[0]}")
176189
...
177190
178191
@printer.handler(const.Command.STOP_PRINT)
179-
def start_print(args: List[str]):
192+
def start_print(args: list[str]):
180193
"""This handler will be called when STOP_PRINT command was sent to
181194
the printer."""
182195
printer.set_state(const.State.READY, const.Source.CONNECT)
183196
print("Printing stopped")
184197
...
185198
186-
# communication loop
199+
# Communication loop
187200
thread = Thread(target=printer.loop)
188201
thread.start()
189202
190-
# try run command handler each 100 ms
203+
# Set printer state to READY.
204+
printer.set_state(const.State.READY, const.Source.CONNECT)
205+
206+
# Try run command handler each 100 ms
191207
while True:
192208
printer.command()
193209
sleep(0.1)

0 commit comments

Comments
 (0)