XaiJu
The100
The100

patreon


Advanced Config Parser for Klipper & THEOS

Hey everyone,


The year may be winding down, but the new THEOS features aren’t! I managed to squeeze in some coding time over Christmas to overhaul the Klipper Config Parser and add a handful of new capabilities. These upgrades will make printer configuration much easier going forward. Here’s a quick rundown of what’s new:

Before we jump into the new stuff, let’s recall two features that current THEOS users already know—understanding them will help clarify my goals for extending the Config Parser.

[constants] Namespace

The purpose of the [constants] namespace is to provide a central place for defining and configuring core parameters. This approach moves us away from using _USER_VARIABLE namespaces, which became a workaround standard outside of THEOS for macro development:

Why move away from _USER_VARIABLE?

Variables defined in USER_VARIABLE were only available inside macros, which limited their scope and flexibility.

Reference Interpolation

To solve the issue of needing certain values outside of macros, I decided to give the Config Parser the ability to reference config values directly.

Example:

 

This means you can now assign properties by pulling in values from other sections or other properties.

Reference Includes

I’ve enhanced the existing reference interpolation logic so that it can be used anywhere in the configuration—even in include statements:

 For instance, you can now use constants to load the correct bed configuration for your printer. The Config Parser resolves these variables when loading includes. It doesn’t matter where the constants are defined—the parser is smart enough to figure it out.

Conditional Includes

To push the idea further, I thought it’d be cool if an include could decide whether it should be loaded or not. Now, you can use conditional logic in your config to include a file only when certain conditions are met.

Example:

 

In this Example only printer/t250.cfg will be loaded since the expression for the t100 is false.

This allows you to get rid definitions you might have seen in template files in other projects (e.g., klippain or rat_configuration):

With my parser, the code can be made smarter and more readable:

Plus, if there’s an error (for example, if a user sets a bed_size for which no configuration file exists), the parser will throw a clear error message telling you which file it couldn’t find.

Arithmetic Expressions

Macros are often overloaded in Klipper to adjust call parameters depending on variables. Here's an example from Klippain:

What the new "TEST_RESONANCES" does is automatically set the value of POINT to the center of the print bed if we don't overwrite it with a user variable. If the TEST_RESONANCES macro in Klipper changes in the future, it could make our macro override incompatible. Many of us experienced this during the update to Klipper 0.12 when cruise_ratio replaced max_accel_to_decel. So, what would a more robust version look like?

As you can see, overloading with my ConfigParser adjustments is no longer necessary. The new parser now supports basic arithmetic operations (+, -, *, /) and automatically calculates these for us when loading the configuration. It is also possible to reference variables in the calculations to dynamically compute values.

Another great example would be this one:

This config snippet automatically calculates the size of the bed mesh based on the size of the bed and a defined padding. The great thing about this is that the calculation happens when the config is loaded, meaning the entire dynamic process doesn’t cost us any CPU time. Compared to the macro approach, which is evaluated at the time of execution, our version is faster. The loaded config looks like this for Klipper:

All of this together enables entirely new ways of modularizing printer configurations. We no longer need to use crude constructs like the user templates from Klippain, nor do we need to overload every onboard macro from Klipper to incorporate our configuration modules into the macro. Most importantly, it allows us to remove many of these macros and replace them with simple property manipulations.

This makes the configuration more maintainable, as well-written configurations clearly state what they do at first glance.

For those who want to see the new parser in action, I have to ask for a little patience. My goal is to revise the THE100 configuration to leverage the new parser functionalities (and possibly fix a few bugs I notice along the way). Once that's complete, I plan to release the new THEOS version for my supporters.

This has been a lot of work, and it wouldn’t have been possible without you supporting me. Your support allows me to spend less time on projects for other clients and dedicate more time to the T250 and its operating system. Thank you for that!

Advanced Config Parser for Klipper & THEOS Advanced Config Parser for Klipper & THEOS Advanced Config Parser for Klipper & THEOS

Comments

Very thoughtful innovations, I can't wait to see the results!

Z-Man23

I love it. Definitely a programmer's way of doing it!

Alex B


More Creators