rsjudka / intelligent-auto

41 stars 15 forks source link

[Enhancement] Set screen brightness #9

Closed leobel96 closed 4 years ago

leobel96 commented 4 years ago

I have seen in "Future Features" the possibility to adjust screen brightness. Using a touchscreen it is as easy as echo BRIGHTNESS > "/sys/class/backlight/rpi_backlight/brightness" where BRIGHTNESS is a number between 0 and 255. Give it a try. Maybe you have to run it as root.

rsjudka commented 4 years ago

is this with the official raspberry pi screen? I've been using a generic one which you cant adjust brightness on :( I definitely need some "modular" way of controlling brightness, where I would have to detect what function I can use to change brightness and that may take some time to implement hah

leobel96 commented 4 years ago

Yes it's for official raspberry pi screen but could also work with your screen.

leobel96 commented 4 years ago

Alternatively you could try using xrandr command. It works on Debian and Ubuntu so I think it will also work on raspbian.

rsjudka commented 4 years ago

just did some testing with my pi (on my generic touchscreen and my regular monitor) and it doesnt seem brightness can be changed using xrandr :/ after some more research it seems like that functionality is supported by the pi's videocore driver

I will definitely add support for adjusting brightness by changing the backlight tho, and have the simulated brightness as a fallback option

rsjudka commented 4 years ago

Sorry for taking so long with this! Ive been toying with a few ways of implementing it, mainly trying to allow people to add custom controls for it but haven't figured out a viable way to do that.

Its still not merged in yet, but you should be able to use the config-controls branch and let me know how it goes (don't have the official pi screen so haven't been able to test that yet lol)

rsjudka commented 4 years ago

@WardBenjamin @yoriaantje-dev @rhysmorgan134 @robert5974 for anyone with the official raspberry pi touchscreen (that supports setting the brightness), I was hoping someone could test out the config-controls branch to see if it correctly sets the brightness for the display. Thanks!

robert5974 commented 4 years ago

I will do so. I created a new build on a new sdcard but for some reason I keep getting an error when I make. It fails at 74% at 115: openauto/CMakeFiles/auto.dir/SRC/autoapp/Projection/Input device.cpp.o Error 2

Any clue what I did wrong? I've tried branches: Master, develop, config-controls

rsjudka commented 4 years ago

@robert5974 hmm haven't seen that one yet. have you tried a make clean just to get rid of any possible bad files? Are your openauto and aasdk branches on develop branches?

You might always want to try out @rhysmorgan134 's script if you're having troubles here

robert5974 commented 4 years ago

I'll try a make clean but yeah I did make sure I had the submodules on develop. Thanks. I'll try that script if not.

robert5974 commented 4 years ago

so I got it built on master and develop...I ended up trying the script and that worked. I tried building the config-controls branch but it fails at 88% with the following:

[ 88%] Linking CXX executable ../bin/ia /usr/bin/ld: CMakeFiles/ia.dir/src/app/window.cpp.o: in function MainWindow::MainWindow()': /home/pi/intelligent-auto/src/app/window.cpp:26: undefined reference toMockedBrightnessModule::MockedBrightnessModule(QMainWindow*)' /usr/bin/ld: /home/pi/intelligent-auto/src/app/window.cpp:27: undefined reference to XBrightnessModule::XBrightnessModule()' /usr/bin/ld: CMakeFiles/ia.dir/src/app/window.cpp.o: in functionMainWindow::MainWindow()': /home/pi/intelligent-auto/include/app/modules/brightness.hpp:36: undefined reference to BrightnessModule::BrightnessModule(bool)' /usr/bin/ld: CMakeFiles/ia.dir/src/app/window.cpp.o: in functionMainWindow::MainWindow()': /home/pi/intelligent-auto/src/app/window.cpp:33: undefined reference to `vtable for RpiBrightnessModule' collect2: error: ld returned 1 exit status make[2]: [CMakeFiles/ia.dir/build.make:458: ../bin/ia] Error 1 make[1]: [CMakeFiles/Makefile2:108: CMakeFiles/ia.dir/all] Error 2 make: *** [Makefile:84: all] Error 2

rsjudka commented 4 years ago

hmm can you try just "modifying" the CMakeLists.txt file (or do a touch) to force CMake to pick up the new files. I've noticed this happen occasionally when jumping between branches.

robert5974 commented 4 years ago

I just triedit...I added an empty line to CMakeLists.txt. It moved past 88% this time. It is now at 90%. Good call and quick...I'll report back. Thanks!

robert5974 commented 4 years ago

Alright it built successfully. I tried the brightness on the official rpi 7" screen but unfortunately it didn't do anything. I tried every option (mocked, rpi7, x). None changed the brightness.

Is there a log somewhere I can provide?

rsjudka commented 4 years ago

Yeah i only would have expected the rip 7" to work (x is not supported on Raspberian).

Could you try just running this command (maybe run it with sudo also just to see if that's needed for this):

echo x > /sys/class/backlight/rpi_backlight/brightness

where x is any number from 0-255 (don't do anything too low or you might not be able to see the screen anymore if it works lol)

robert5974 commented 4 years ago

Sure thing. I got a "Permission denied" so I'm thinking it must be run as root, but nope I got "Permission denied" again.

leobel96 commented 4 years ago

Sure thing. I got a "Permission denied" so I'm thinking it must be run as root, but nope I got "Permission denied" again.

Try running su and, then, using that command. Sometimes it works for me.

robert5974 commented 4 years ago

Yeah running the command with sudo still didn't work so I added a rule so any user can change the brightness. The command worked after a reboot, but only in the terminal.

By default you must be root to write to that file, so I added a line to authorize this into:

/etc/udev/rules.d/51-iadash.rules

.

(For reference I borrowed this from a website for Yoctopuce devices which is ambient light sensors so the openauto portion might not be correct. This new build of mine, I haven't put everything into it yet.
Link to original: http://www.yoctopuce.com/EN/article/automated-brightness-control-for-the-raspberry-pi )

# udev rule to allow write access to Openauto USB
SUBSYSTEM=="usb", ATTR{idVendor}=="24e0", MODE="0666"

# udev rules to allow write access to all users for Raspberry Pi 7" Touch Screen
SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness"_
leobel96 commented 4 years ago

Yeah running the command with sudo still didn't work so I added a rule so any user can change the brightness. The command worked after a reboot, but only in the terminal.

By default you must be root to write to that file, so I added a line to authorize this into:

/etc/udev/rules.d/51-iadash.rules

.

(For reference I borrowed this from a website for Yoctopuce devices which is ambient light sensors so the openauto portion might not be correct. This new build of mine, I haven't put everything into it yet. Link to original: http://www.yoctopuce.com/EN/article/automated-brightness-control-for-the-raspberry-pi )

# udev rule to allow write access to Openauto USB
SUBSYSTEM=="usb", ATTR{idVendor}=="24e0", MODE="0666"

# udev rules to allow write access to all users for Raspberry Pi 7" Touch Screen
SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness"_

su is different from sudo (https://www.howtogeek.com/111479/htg-explains-whats-the-difference-between-sudo-su/)

robert5974 commented 4 years ago

Yeah, I know it is...I didn't want to have any part of the app needing to perform an action which requires root permissions. I found a way around it is all. Thanks though.

@rsjudka Any thoughts on the app performing the function with this information?

rsjudka commented 4 years ago

Yeah so it ended up being some bad code :p basically Qt does not handle redirects well in qrpocesses so I figured I would just do it the "right" way and interface directly with the sysfs

I was worried sudo might be an issue because of what I've read on some other forums, I'll make sure to document either needing to run with sudo for this feature or (and my preferred way also) add a rule for that file. Thanks for the find @robert5974!

Let me know how the latest changes work.

robert5974 commented 4 years ago

Thanks! It still didn't work within the app after adding the rule. I didn't get an official manual with the screen so I didn't know that one existed until just now.
Apparently there are steps to take to take advantage of things like brightness control with ease. It comes with a GUI, an API, and a specific set of commands to go with the package where the intervals are 0 to 100. I have installed that and after a reboot the commands work from the terminal.
I would try inserting that into the app to try but I was dumb and jumped the gun when I decided to make clean and rebuild the app lol. So now I have to wait for it to finish to try something.

I don't know if you've looked at this manual but here is a link to the pdf: https://www.google.com/url?sa=t&source=web&rct=j&url=https://buildmedia.readthedocs.org/media/pdf/rpi-backlight/latest/rpi-backlight.pdf&ved=2ahUKEwiG6qSF5YbpAhWtl-AKHc3qDv4QFjAHegQIAhAB&usg=AOvVaw3h6o8uC1bga-Exn203yZ5d

rsjudka commented 4 years ago

@robert5974 darn I was really hoping that would work. Just for my sanity, did you pull the latest changes for the config-controls branch?

Yeah I actually looked through that tool when I originally worked on this. It really just seems like its reading/writing to that sys file, which is why I'm confused it doesn't work in ia. I want to minimize the amount of processes ia spawns, which is why I'm trying to go for a solution that does it "natively".

Would you be able to run ia, set the module to rpi7, do a cat /sys/class/backlight/rpi_backlight/brightness, adjust the brightness in ia, do a cat /sys/class/backlight/rpi_backlight/brightness again to see if the value was changed at all?

robert5974 commented 4 years ago

understood. Yes, I did but I'll double check if I pulled what's current. Couldn't hurt. I can run those commands.

robert5974 commented 4 years ago

The cat command return the same value both times. In the app, I set it to 50% saved and then ran cat. After that I moved it to 100% and ran cat. It said 128 before and after I changed it in IA.

robert5974 commented 4 years ago

I'm making sure I've got the current code and then I'll report back.

robert5974 commented 4 years ago

Looks like I have the current code, it didn't change anything for brightness. I did a make clean and make again. If that doesn't work, I might start over to clean up the files to be sure. I'll keep you posted.

rsjudka commented 4 years ago

thanks for your help so far! so when its reading 128, your screen was at 50% brightness, and when you used the slider to move it to 100% it just didn't do anything?

Just as a test, could you try running ia with sudo to see if it'll work? That was we can rule out any sort of permission config stuff (and then just have to figure out how to get ia the right permissions).

EDIT: just added some debugging to help figure out where its failing

robert5974 commented 4 years ago

Yw!
I think the brightness was actually 128 (edit...128 is 50%...just occurred to me...doing too many things at once lol) and IA read that upon opening but using the slider did not change anything.

I'll be sure to try it with elevated permissions once my clean make is finished. It's at 85% so it shouldn't be much longer.

robert5974 commented 4 years ago

I see your edit about debugging. Good call. I tried it with sudo but that didn't seem to do anything either. I tried changing the command in the brightness.cpp but it didn't like it and wouldn't build past that point. I cleaned again and now making again.

Since you don't have the touchscreen, did you see there is an emulator? Perhaps that could be useful to you?

rsjudka commented 4 years ago

@robert5974 oh i actually havent thought about that. I'll poke around to see if there is one anywhere.

I tried changing the command in the brightness.cpp

wait what command are you talking about?

EDIT: couldn't find any sort of emulator, but I did try using umockdev to mock the touchscreen and it did seem to work, its just I'm not sure how accurate of a test that is.

rsjudka commented 4 years ago

I wonder if im just screwing with the file and "corrupting" it (I've been testing on a regular file so its not entirely the same).

@robert5974 this might be a lot, but would you be able to get a few data points for me before running ia (just in case it is corrupting it in any way)...

With that i can see what format the file is supposed to be and know how I should be writing to it.

rsjudka commented 4 years ago

ah lame actually I think i was able to reproduce it using the pi's led device which also has a brightness attribute!

Basically what I think is going on is Qt's file handling tries to get a bit too cute with how it actually writes to files, and just does something the sysfs doesn't like. Gunna have to implement a workaround for this that just writes to the file at a lower level.

robert5974 commented 4 years ago

I tried changing the "echo xxx > /sys...." To " rpi-backlight..." to see if it would work. It doesn't matter, please excuse that attempt to throw something against the wall and see if it would stick lol. The emulator is supposed to be here: https://github.com/linusg/rpi-backlight#emulator

Maybe it works??? Idk myself. Thought I'd throw it out there.

If you need me to still do those things I can...not a problem. Just let me know. Seems like you may have gotten it yourself.

rsjudka commented 4 years ago

Haha no worries I was more curious as to where you were sticking that command out of curiosity

but I think I got it! :tada:

If you could test out my latest changes that would be great, then i can put all this behind me :p

robert5974 commented 4 years ago

Sounds good. I'll get back with you...hopefully with some positive news

robert5974 commented 4 years ago

Good news! I finally got it ready and it works! For some reason when I would checkout, I wasn't always getting the updates. I did this that and another and finally got it correctly. I had to check those time stamps bc git status said I was up to date...thanks!

rsjudka commented 4 years ago

Just as a final check to make sure you're on the latest commit, could you do a git log -1 and verify it matches with what's the latest in Github? Then we can get this closed up and merged finally lol

Again thanks so much for your help!

robert5974 commented 4 years ago

Sure thing! I'll be home soon and I'll capture that info.

robert5974 commented 4 years ago

I was able to confirm the latest commit for the config-controls branch. Merge away.