Shortly speaking, sometimes I think custom extensions can live outside of the Klippy/Klipper process.
Here is the API, and it should be enough for most basic stuff.
I see the Moonraker as the perfect example of a klipper extension.
This library is supposed to be used as part of the daemon/service that connects to the Klipper socket.
Declares supported functions and probably loads macros to call those methods.
Some missing pieces, for now:
- Macros should be loaded by the external installer into the printer config.
- The only way to communicate with the user and the UI consoles is to use
RESPONDg-code.
This example should cover something; it is also available inside the repo:
from queue import Queue
from KlippyRPCShim import KlippyRPCShim
def main():
krpc = KlippyRPCShim()
info = {
"method": "info",
"params": {
"client_info": {
"program": "KRPC", "version": "0.0.1"
}
}
}
# Register itself and test connection
resp = krpc.query(info)
print(f"sync response: {resp}")
promise = krpc.query_async(info)
print(f"async response: {promise()}")
# Test subscription
import statistics
request = {"method": "adxl345/dump_adxl345", "params": {"sensor": "adxl345"}}
pkgs = 5
generator, cancel = krpc.subscribe(request)
for resp in generator():
params = resp.get("params")
if params is None:
continue
d = params["data"]
val = [row[1] for row in d]
# Compute mean and standard deviation
mean_val = statistics.mean(val)
stddev_val = statistics.stdev(val) if len(val) > 1 else 0.0
print(f"Mean value: {mean_val:.3f}, StdDev: {stddev_val:.3f}")
pkgs -= 1
if pkgs <= 0:
cancel()
params_q = Queue(1)
krpc.register_remote_method(callback=params_q.put, remote_method="noop")
while True:
print(f"{params_q.get()}")
if __name__ == "__main__":
And so the call of the custom method is:
[gcode_macro NOOP]
gcode:
{action_call_remote_method("noop", frequency=300, duration=1.0)}
Probably it would be nice if Klippy would print call errors in the console of gcode is called from the console.
I think the next step would be to implement a simple server to generate calibrate_shaper graphs, as an example.
Because my initial motivation was help shake tune be less intrusive and less dependent on the Klipper internals.
Hope someone finds it useful.
-Timofey