Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto backlight logic #60

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions doc/hardware_installation_pitft.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,6 @@ It is more reliable to solder the PiTFT directly to the header of Raspberry Pi Z

Follow [official setup guide](https://learn.adafruit.com/adafruit-2-4-pitft-hat-with-resistive-touchscreen-mini-kit/overview) of Adafruit, or [my setup guide (Japanese)](https://qiita.com/hishi/items/bdd630666277e4f8162a).

Additionally, install programs which to turn the PiTFT 2.4 backlight on and off.

```
$ sudo cp install/usr/local/bin/disable-pitft /usr/local/bin/
$ sudo cp install/usr/local/bin/enable-pitft /usr/local/bin/
```

Install the program which turns off the backlight at shutdown.

```
$ sudo cp install/etc/systemd/system/disable-pitft.service /etc/systemd/system/
$ sudo systemctl daemon-reload
$ sudo systemctl enable disable-pitft.service
```

If you run the program in a console, you need to build Qt5 and PyQt5 because the package python3-pyqt5 provided with Raspbian OS does not include a touchscreen library(tslib).

Note:
Expand Down
6 changes: 1 addition & 5 deletions modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,6 @@ class Config:
# stopwatch state
G_MANUAL_STATUS = "INIT"
G_STOPWATCH_STATUS = "INIT" # with Auto Pause
# quit status variable
G_QUIT = False

# Auto Pause Cutoff [m/s] (overwritten with setting.conf)
# G_AUTOSTOP_CUTOFF = 0
Expand Down Expand Up @@ -454,8 +452,7 @@ class Config:
G_DITHERING_CUTOFF_LOW_INDEX = 2
G_DITHERING_CUTOFF_HIGH_INDEX = 1

# auto backlight with spi mip display
# (PiTFT actually needs max brightness under sunlights, so there are no implementation with PiTFT)
# auto backlight
G_USE_AUTO_BACKLIGHT = True
G_AUTO_BACKLIGHT_CUTOFF = 30

Expand Down Expand Up @@ -950,7 +947,6 @@ async def quit(self):
if self.G_MANUAL_STATUS == "START":
self.logger.start_and_stop_manual()
self.display.quit()
self.G_QUIT = True

await self.logger.quit()
self.setting.write_config()
Expand Down
38 changes: 38 additions & 0 deletions modules/display/display_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,27 @@

# default display (X window)
class Display:
has_auto_brightness = False
has_color = True
has_touch = True
send = False

# current auto brightness status (on/off)
auto_brightness = False
brightness_index = 0
brightness_table = None

def __init__(self, config):
self.config = config

if self.has_auto_brightness:
# set initial status
self.auto_brightness = config.G_USE_AUTO_BACKLIGHT

# set index properly if on
if self.auto_brightness:
self.brightness_index = len(self.brightness_table)

@property
def resolution(self):
return getattr(self, "size", DEFAULT_RESOLUTION)
Expand All @@ -45,7 +59,31 @@ def screen_flash_long(self):
def screen_flash_short(self):
pass

# We can not have auto brightness and an empty brightness table
def change_brightness(self):
if self.brightness_table:
# brightness is changing as following if the display has auto_brightness feature
# [*self.brightness_table, self.auto_brightness]
if self.has_auto_brightness:
self.brightness_index = (self.brightness_index + 1) % (
len(self.brightness_table) + 1
)

# switch on auto_brightness
if self.brightness_index == len(self.brightness_table):
self.auto_brightness = True
# switch off auto_brightness and set requested brightness
else:
self.auto_brightness = False
self.set_brightness(self.brightness_table[self.brightness_index])
else:
# else we just loop over the brightness table
self.brightness_index = (self.brightness_index + 1) % len(
self.brightness_table
)
self.set_brightness(self.brightness_table[self.brightness_index])

def set_brightness(self, b):
pass


Expand Down
27 changes: 5 additions & 22 deletions modules/display/mip_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ class MipDisplay(Display):
pi = None
spi = None
interval = 0.25
brightness_index = 0
brightness_table = [0, 10, 100]
brightness = 0
mip_display_cpp = None

has_auto_brightness = True
has_touch = False
send = True

brightness_table = [0, 1, 2, 3, 5, 7, 10, 25, 50, 100]
brightness = 0

size = (400, 240)

def __init__(self, config, size=None):
Expand Down Expand Up @@ -94,10 +95,6 @@ def __init__(self, config, size=None):
# backlight
self.pi.set_mode(GPIO_BACKLIGHT, pigpio.OUTPUT)
self.pi.hardware_PWM(GPIO_BACKLIGHT, GPIO_BACKLIGHT_FREQ, 0)
if config.G_USE_AUTO_BACKLIGHT:
self.brightness_index = len(self.brightness_table)
else:
self.brightness_index = 0

def init_buffer(self):
self.buff_width = int(self.size[0] * 3 / 8) + 2 # for 3bit update mode
Expand Down Expand Up @@ -269,22 +266,8 @@ def conv_3bit_color_py(self, im_array):

return np.packbits(im_array_bin.reshape(self.size[1], self.size[0] * 3), axis=1)

def change_brightness(self):
# brightness is changing as following,
# [self.brightness_table(0, b1, b2, ..., bmax), self.display.G_USE_AUTO_BACKLIGHT]
self.brightness_index = (self.brightness_index + 1) % (
len(self.brightness_table) + 1
)

if self.brightness_index == len(self.brightness_table):
self.config.G_USE_AUTO_BACKLIGHT = True
else:
self.config.G_USE_AUTO_BACKLIGHT = False
b = self.brightness_table[self.brightness_index]
self.set_brightness(b)

def set_brightness(self, b):
if b == self.brightness or self.config.G_QUIT:
if b == self.brightness:
return
self.pi.hardware_PWM(GPIO_BACKLIGHT, GPIO_BACKLIGHT_FREQ, b * 10000)
self.brightness = b
Expand Down
3 changes: 0 additions & 3 deletions modules/display/mip_sharp_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ async def draw_worker(self):
self.draw_queue.task_done()

def update(self, im_array, direct_update):
if self.config.G_QUIT:
return

# self.config.check_time("mip_sharp_update start")
self.img_buff_rgb8[:, 2:] = ~im_array

Expand Down
53 changes: 25 additions & 28 deletions modules/display/pitft_28_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,32 @@


class PiTFT28r(Display):
spi = None

# brightness
brightness_index = True
brightness_cmd = {
0: ["sudo", "/usr/local/bin/disable-pitft"],
1: ["sudo", "/usr/local/bin/enable-pitft"],
}
# brightness_table = [0,100,1000]
# brightness_table = [0,100,400,700,1000]
# brightness_index = len(G_Brightness)-1
# brightness_cmd_init = ["/usr/bin/gpio", "-g", "mode", "18", "pwm"]
# brightness_cmd_base = ["/usr/bin/gpio", "-g", "pwm", "18"]
# PiTFT actually needs max brightness under sunlights, so there is no implementation of AUTO_BACKLIGHT
# There's also only two states enabled or disabled
brightness_index = 1 # we are on by default
brightness_table = [0, 100]

size = (320, 240)

def __init__(self, config):
super().__init__(config)
self.clear()

def clear(self):
pass

def quit(self):
self.clear()
# GPIO.output(GPIO_DISP, 1)
# time.sleep(0.1)

def change_brightness(self):
self.brightness_index = not self.brightness_index
cmd = self.brightness_cmd[int(self.brightness_index)]
exec_cmd(cmd)
self.set_brightness(0)

def set_brightness(self, b):
if b == 0:
exec_cmd(
[
"sudo",
"sh",
"-c",
r"echo 0 > /sys/class/backlight/soc\:backlight/brightness",
]
)
elif b == 100:
exec_cmd(
[
"sudo",
"sh",
"-c",
r"echo 1 /sys/class/backlight/soc\:backlight/brightness",
]
)
2 changes: 1 addition & 1 deletion modules/sensor/gps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def id_or_none(value):
):
self.values["track"] = int(track)
self.values["track_str"] = get_track_str(self.values["track"])
# for
# for GPS unable to get track
elif (
track is None
and speed is not None
Expand Down
23 changes: 12 additions & 11 deletions modules/sensor_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,18 +575,19 @@ async def integrate(self):
# time.sleep(1)

# auto backlight
if self.config.G_IS_RASPI and self.config.G_USE_AUTO_BACKLIGHT:
if self.config.G_DISPLAY in ("MIP", "MIP_640") and not np.isnan(
v["I2C"]["light"]
):
if v["I2C"]["light"] <= self.config.G_AUTO_BACKLIGHT_CUTOFF:
self.config.display.set_brightness(3)
else:
self.config.display.set_brightness(0)
if self.config.G_MANUAL_STATUS == "START":
if v["I2C"]["light"] <= self.config.G_AUTO_BACKLIGHT_CUTOFF:
if self.config.display.auto_brightness and not np.isnan(
v["I2C"]["light"]
):
if v["I2C"]["light"] <= self.config.G_AUTO_BACKLIGHT_CUTOFF:
self.config.display.set_brightness(3)

if self.config.G_MANUAL_STATUS == "START":
self.sensor_ant.set_light_mode("FLASH_LOW", auto=True)
else:

else:
self.config.display.set_brightness(0)

if self.config.G_MANUAL_STATUS == "START":
self.sensor_ant.set_light_mode("OFF", auto=True)

# cpu and memory
Expand Down
13 changes: 0 additions & 13 deletions scripts/install/etc/systemd/system/disable-pitft.service

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/install/usr/local/bin/disable-pitft

This file was deleted.

4 changes: 0 additions & 4 deletions scripts/install/usr/local/bin/enable-pitft

This file was deleted.