Is there a way to switch the camera's LED via the web interface (Mainsail)?

It’s very pleasant to see that people are here to help each other.
Xmas before time.

1 Like

I agree! And it took less than 24 hours. Now I even got an answer from info@sovol3d.com. So it was good to ask the question here. Thanks again!

Keep us updated on your progress.

I apologize in advance for the German version of Orca-Slicer. I can’t see the English names right now.
Setting up the macros in Mainsail is quite simple. Under “Device” “Machine” select the Macro.cfg file and add this gCode at the end.

[gcode_macro LED_ON]
gcode: M42 P67 S255 ; LED einschalten

[gcode_macro LED_OFF]
gcode: M42 P67 S0 ; LED ausschalten

After saving and restarting, 2 new macros will appear in the dashboard. But at the moment there is still an error message and I’m still working on it. I’ll get back to you…

2 Likes

I couldn’t get it to work.

This should in theory work.

[output_pin cameralight]
pin: rpi:gpio67

But leads errors in klipper. Ultimately this error: klipper_mcu[860]: Got error -1 in gpio_out_reset get line: (22)Invalid argument

But I can do, as root, echo 1 > /sys/class/gpio/gpio67/value and the light turns on.

That M42 doesn’t work for me, at least not from the console, it just leads to Unknown command:"M42"

1 Like

I’m still working on it.
I probably have to let Klipper know about PIN67 and edit pin aliases. The entry is
[aliases]
pin_gpio67 = GPIO2_A3
Then it should find the pin
[output_pin cameralight]
pin: gpio67
in the printer.cfg.
That leaves the M42 command.
Always one step at a time.

You can make any number of gcode macros for a light. But for a button like macro:

[gcode_macro TOGGLE_CAMERALIGHT]
gcode: SET_PIN PIN=cameralight VALUE={(not printer['output_pin cameralight'].value)|int}

There are certainly many ways. At the moment I’m not there yet. The error “Unknown pin chip name ‘pin_gpio67 value’” is still there. Aliases didn’t help.

I think it should be

[output_pin cameralight]
pin: rpi:gpiochip2/gpio3

But that also doesn’t work.

I can also say that it is mksclient that does basically echo 67 > /sys/class/gpio/export which creates /sys/class/gpio/gpio67. And then writes to it by writing “0” or “1” to /sys/class/gpio/gpio67/value. Maybe that is in the way of klipper_mcu opening the same gpio via /dev/gpiochip2 and doing ioctl on that file handle.

I’m just giving a quick report. PIN67 is blocked by something. Whether in SSH or in the mainsail, PIN67 cannot be accessed. Moonraker warning:

"Failed to load power device [power camlight]

[power camlight]: Option ‘pin’ encountered the following error while parsing: [Errno 13] Opening GPIO chip: Permission denied"

Maybe I can find a solution.

sudo cat /sys/kernel/debug/gpio
shows the port. GPIO 67 exists but is blocked by something.

Indeed. I now have it working, with that pin: gpiochip2/gpio3. But first you need to do (as root):

killall mksclient
echo 67 > /sys/class/gpio/unexport

Then update printer.cfg. Save and reload. Then you’ll get a nice new button under Miscellaneous.

But that is unfortunate. Because that mksclient is the touch screen UI interface. And is closed source. Sovol should have configured the pin in klipper, and have mksclient send commands to mainsail to toggle the led. Not sure if the two can co-exist.

2 Likes

First of all, I have to say something important! I’m impressed with how onnlucky is putting in the effort here!

OK. That sounds good. Of course it would be a shame if it only works with root. Then of course I need the password for root. Otherwise I can’t get to it. But I really don’t want to mess around with the display. Can the two exist side by side? That’s the question. When I’m standing in front of the printer, the light from the LED isn’t enough for me. So I don’t really need to switch the light on the printer itself. But that’s just my personal opinion. Shall we start a poll?

Thanks @Oliver

I checked that mksclient binary a bit, it does not do any error checking, so we can patch the binary easily to just open the wrong thing, and silently do nothing. I made this python script that does just that:

import mmap
import argparse

file_path = "/home/sovol/printer_data/build/mksclient"

def patch_binary(write_null):
    search = b"/sys/class/gpio/expor"
    searchlen = len(search)

    with open(file_path, 'r+b') as f:
        mm = mmap.mmap(f.fileno(), 0)

        pos1 = mm.find(search)
        pos2 = mm.find(search, pos1 + searchlen)

        if pos1 == -1:
            print("Missing first patch point")
            mm.close()
            return
        if pos2 == -1:
            print("Missing second patch point")
            mm.close()
            return

        print("found at:", pos1, pos2, "patching")
        mm[pos1 + searchlen] = 0 if write_null else ord('t')
        mm[pos2 + searchlen] = 0 if write_null else ord('t')
        mm.flush()
        mm.close()

    print("Patched:", file_path)

parser = argparse.ArgumentParser(description="Patch a binary file.")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--klipper", action="store_true", help="Let klipper control the camera led")
group.add_argument("--printer", action="store_true", help="Let the printer touch screen control the camera led")
args = parser.parse_args()

patch_binary(args.klipper)

Save it somewhere on the sovol machine. Then run these commands via ssh:

systemctl stop makerbase-client.service
cp printer_data/build/mksclient printer_data/build/mksclient.backup
python3 patch_led.py --klipper
sudo reboot

(And if you want to back to normal, run python3 patch_led.py --printer)

After the reboot, add this to the printer.cfg via the web interface in the “machine” section:

[output_pin cameralight]
pin: rpi:gpiochip2/gpio3

Then you’ll have a camera light toggle in the miscellaneous section of the dashboard.

And can run gcode like SET_PIN PIN=cameralight VALUE=1

And setup any gcode macro you like around this. Like turn it on for the whole print. Or configure it in machine.cfg to turn on when the stepper motors are engaged. Happy hacking :slight_smile:

2 Likes

That looks like a night shift! I would love to try it out right away. Unfortunately, that will have to wait until the weekend. Anyway, thank you for your great support. Contrary to expectations, this task turned out to be quite tricky. I would not have expected that a small LED could defend itself against a remote control like that. I will get in touch when I have implemented the instructions and report on the success. Thank you in advance for your great support. It would be best to integrate it into the SV06 Plus ACE straight away. That will surely follow soon.

Gruß Oliver

ChatGPT does half the work for me :slight_smile:

1 Like

Hm… I’m probably getting old. I actually tried to solve it with the alternative Copilot from Microsoft. First of all, you have to ask the right questions. That’s not easy. By the second question, the copilot no longer knows that you’re talking about Klipper and not Marlin… No! It doesn’t do the work for you. It creates other work. After 4-5 answers, I lose track.

Yeah, it is just making up stuff often, lol. But it is great at making first versions, like that python program, I didn’t have to google how to mmap, or arg parsing. I just had to tweak the initial program a bit.

Anyhow, I have the patch running and the light setup in klipper. I was also a bit disappointed the screen app is not open source. I think klipper screen would run on it as well, but it doesn’t look like it is all that great.

2 Likes

Klipper-Screen is a great open source project. But I understand the manufacturers’ desire to customize the menu on their own printer. Whether it really has to be closed like that is debatable. Most users don’t want to “hack” it. It’s only when they miss a function that they want to do so.

Even if ChatGPT helped with this, it’s still a good job on your part! Perhaps MKS can change the display software for the next printer and integrate the switch for the LED directly into Mainsail. Then no one will feel the need to install Klipper-Screen.

Thanks again for your help. It actually worked in the end. It would be nicer if Mainsail didn’t have to fight with the display for the port. MKS could improve that. I wish everyone a happy and peaceful Christmas!