How to Control Chipsee PC Backlight with Python and Browser
By Printfinn, last updated at 2023-04-13
Today let's see how we can adjust the screen backlight of a Chipsee industrial Pi PC.
So, a Chipsee engineer told me adjusting backlight is just writing some text file, and I thought, oh we have a Debian Linux operating system, writing text file is not hard. So I started to think about which file I should write to. And then luckily I found Chipsee has a document website, I then looked up if my model is in the document website. It turns out they have many docs online and my Industrial Pi model is among one of those.
So I opened the docs web page, and skimmed a little bit, then found the backlight part. It says I can get or set the brightness from these two files. So I went to the folder and checked what files are there, and if there are other files there which might be of interest to me.
Initial Attempt by Reading/Writing Linux Files
Luckily, there is a pwm-backlight folder in the backlight directory, there are files named max_brightness and brightness and some other files, according to the file names, which happen to be the same as those in the document.
So I cat a file, at first I tried the max_brightness, from its name, I assume it should list the maximum available brightness of this device, it returns a 99. It makes sense, the minimum be zero, the maximum be 100, or sometimes 99, so we have a 100 level pwm backlight.
Then I cat the brightness and actual_brightness files, they all returned 90 for me. I'm a little bit confused now, which one should I read to get the actual brightness? They all seem to be the same. My first assumption is they might have different Linux file permissions, or to avoid read/write conflicts on the same file. I'm not an expert on Linux drivers, so I just put it aside and proceeded. So I went back to the official document again, and the example Chipsee gives is writing to the brightness file to adjust the brightness. Let's try it, let's copy and paste to the terminal and OK, I can see from my Chipsee Pi, the screen is a lot darker now. Can I write something else? I then write 90 again to see if I can get it brighter. Cool. So I understand if I write to this file, then I can adjust the screen brightness of this device with any programming language.
Although not documented, I'd still love to see what's the difference between the actual_brightness and brightness file. I think they might not want me to write to the actual_brightness file, so I checked the file permission with ls -la, it gives only read permission, OK cool, so my assumption might be correct. But what about the brightness file, it has both read and write permission. But whatever, I'll just read the actual_brightness file to get the current value, and only write the brightness file to set a new value. I don't know the mechanism behind the files, maybe to avoid transaction problems? In the end it's just the backlight, so not a big deal, since it gives me the option, and the values are the same, then I'd pick a safe way to read.
Start Python Flask Server: Hello World
So now the first step investigation is done, let's write the actual GUI application.
Our aim is now to show the actual brightness value on a browser, and then try to modify this value in the browser, so we can adjust the brightness of our Chipsee industrial Pi.
I use Flask as a lightweight web server, because it uses Python, and can understand HTML and HTTP, we don't need to know everything about Flask to start, we will only use a tiny small part of it, I also try to avoid the framework specific conventions, and try only to use the plain JS and HTML in the browser, so you can reuse the code on your favorite frameworks.
Let's create a default Flask app. Then let's run flask, and see if our Chipsee Pi can display a hello world. Cool.
On our PC we can visit the http://IP:5000 (change IP to your Chipsee PC IP address, you can get this IP by entering ifconfig in your Chipsee PC terminal and look around) address and see Hello World. It means our Flask demo app is running on the Chipsee PC.
A Web Page to Show Dummy Brightness
Then let's create a brightness.html file in the templates folder. The templates folder is Flask's convention of storing html template files, this is one of a few things about Flask we need to memorize for building this app. An HTML file has some markup structures, you can refer to the Mozilla web doc to learn more, but for our use case, we don't need too much, will just copy from our demo code, they're the html, head, body, and that's it.
To add some CSS styles, let's also copy the CSS files from the static folder of our demo. Otherwise our page will look too tedious.
So let's change our code, we will return a brightness.html template, with a actual_brightness=actual_b, max_brightness=max_b, let assign a new variable named actual_b and give it a value, say 80. And also a max_b, say 100. And make sure that there is an actual_brightness, a max_brightness with double curly brackets in our HTML template file. I'll explain later what this means, let's first check what it will display in the browser.The brightness.html should look like this: The app.py should look like this:
Let's visit the http://IP:5000/brightness page of the browser, and see there is a 80 displaying alongside the slider. This 80 comes from the brightness inside curly brackets, and, the value is passed by the same name variable when we return the render template, see there is a actual_brightness=actual_b, it's the 80 that is passed to the actual_brightness inside the curly brackets in the brightness.html. Now you can play around and change this value.
For the max_brightness=max_b, this informs the slider the max value it can approach. Some Chipsee industrial PCs have 255 levels of backlight, and some 100 levels. This value should be obtained from the OS files we looked at in the beginning, remember the pwm-backlight folder? There is a max_brightness file.
So, things get easier now. If we can grep the actual brightness value from Chipsee operating system files, then we can display the actual brightness in our GUI application right? It's even easier, let's just read the file with Python.
Get the Actual Brightness from File with Python
Let's create a function to read this file. I'll just copy paste the file from our demo code. And import it in our app.py.
The models/brightness.py file should look like this:
After importing the Brightness class, the app.py file should look like this:
Then when we refresh the page, we should see on the webpage that the value is the current brightness value of our Chipsee industrial PC. And the slider should be in a place where it represents the same value.
The models/brightness.py does a few things: It gives Linux permission to the backlight driver file, and reads values from them. For example to get the maximum available brightness or current actual brightness. It also has a method to set brightness to the Chipsee PC. We will try this method now, and see if we can set a new brightness to this Chipsee PC.
Setting a New Brightness from Browser
So to set a value we should use a POST http request. Let's first ask our Flask server to listen to POST request, we can write a method like this in the app.py file:
And when Flask server receives this XML http request that the browser's JS sends, it will read the value this request carries with. And then call a Python method to write to a file, which is the file of Chipsee PC's brightness controller.
Then the screen backlight brightness will change to the value the slider indicates, thus setting a new brightness.
Not too complicated hah?
Now we can set our Chipsee PC's brightness through a browser GUI, woo hoo!
In the end, your files should look like this: First, you have an app.py, which is the entrypoint of your Flask server app:
Second, you have a templates/brightness.html, which is how your GUI looks like in the browser:
Third, you have a models/brightness.py, which deals with the reading and writing of Chipsee pwm-backlight:
Also, you should have static/css/style.css and bootstrap.min.css, which sets how things look like in the browser, you should copy them from the demo app. If you find your brightness web page looks weird, you should remember to check these two CSS files, see if you copied them currently.
To sum up, to get the actual brightness, we ask Python to read a file, and to set a new brightness, we touch the slider, which triggers a POST request to our Flask backend, which will then write to a Linux file. This way, we're able to get or set the Chipsee PC backlight brightness by reading or writing to Linux files.
So that's it, now you can set your Chipsee PC's brightness with an HMI!