Added a battery check to the menu.
This commit is contained in:
1 parent
e1434f6423
commit
835f7ca4af
1 file changed
+86
@@ -316,6 +316,88 @@ class VoicedMenu:
|
|||||||
print(message)
|
print(message)
|
||||||
self.speak(message)
|
self.speak(message)
|
||||||
|
|
||||||
|
def report_battery_status(self):
|
||||||
|
"""Report current battery status"""
|
||||||
|
try:
|
||||||
|
import pathlib
|
||||||
|
power_supply_dir = pathlib.Path('/sys/class/power_supply')
|
||||||
|
|
||||||
|
# Check if power supply directory exists
|
||||||
|
if not power_supply_dir.exists():
|
||||||
|
self.speak("No battery found.")
|
||||||
|
return
|
||||||
|
|
||||||
|
battery_found = False
|
||||||
|
battery_level = None
|
||||||
|
is_charging = False
|
||||||
|
|
||||||
|
# Look for battery
|
||||||
|
for item in power_supply_dir.iterdir():
|
||||||
|
type_file = item / 'type'
|
||||||
|
if type_file.exists():
|
||||||
|
try:
|
||||||
|
if type_file.read_text().strip() == 'Battery':
|
||||||
|
battery_found = True
|
||||||
|
|
||||||
|
# Get battery level
|
||||||
|
capacity_file = item / 'capacity'
|
||||||
|
if capacity_file.exists():
|
||||||
|
battery_level = int(capacity_file.read_text().strip())
|
||||||
|
|
||||||
|
# Check charging status
|
||||||
|
status_file = item / 'status'
|
||||||
|
if status_file.exists():
|
||||||
|
status = status_file.read_text().strip()
|
||||||
|
is_charging = status in ['Charging', 'Full']
|
||||||
|
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Check for AC power
|
||||||
|
ac_connected = False
|
||||||
|
for item in power_supply_dir.iterdir():
|
||||||
|
type_file = item / 'type'
|
||||||
|
online_file = item / 'online'
|
||||||
|
if type_file.exists() and online_file.exists():
|
||||||
|
try:
|
||||||
|
device_type = type_file.read_text().strip()
|
||||||
|
if device_type in ['ADP', 'Mains', 'AC']:
|
||||||
|
online = int(online_file.read_text().strip())
|
||||||
|
if online == 1:
|
||||||
|
ac_connected = True
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not battery_found:
|
||||||
|
self.speak("No battery found.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Build status message
|
||||||
|
if battery_level is not None:
|
||||||
|
message = f"Battery level: {battery_level} percent"
|
||||||
|
|
||||||
|
if is_charging:
|
||||||
|
message += ", charging"
|
||||||
|
elif ac_connected:
|
||||||
|
message += ", plugged in"
|
||||||
|
else:
|
||||||
|
message += ", on battery power"
|
||||||
|
|
||||||
|
# Add warning context for low levels
|
||||||
|
if battery_level <= 5:
|
||||||
|
message += ". Critical level!"
|
||||||
|
elif battery_level <= 10:
|
||||||
|
message += ". Low battery!"
|
||||||
|
else:
|
||||||
|
message = "Battery detected but unable to read level"
|
||||||
|
|
||||||
|
self.speak(message)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.speak(f"Error reading battery status: {e}")
|
||||||
|
|
||||||
def update_service_menu_items(self):
|
def update_service_menu_items(self):
|
||||||
"""Update all service-related menu items based on their current status"""
|
"""Update all service-related menu items based on their current status"""
|
||||||
# Remove any existing service items
|
# Remove any existing service items
|
||||||
@@ -566,6 +648,7 @@ class VoicedMenu:
|
|||||||
Right arrow: Next section.
|
Right arrow: Next section.
|
||||||
Enter: Launch selected item.
|
Enter: Launch selected item.
|
||||||
H key: Hear these instructions again.
|
H key: Hear these instructions again.
|
||||||
|
B key: Report battery status.
|
||||||
Left bracket: Decrease speech rate.
|
Left bracket: Decrease speech rate.
|
||||||
Right bracket: Increase speech rate.
|
Right bracket: Increase speech rate.
|
||||||
9 key: Decrease volume.
|
9 key: Decrease volume.
|
||||||
@@ -761,6 +844,9 @@ class VoicedMenu:
|
|||||||
elif key == ord('0'): # Increase volume
|
elif key == ord('0'): # Increase volume
|
||||||
self.increase_volume()
|
self.increase_volume()
|
||||||
|
|
||||||
|
elif key == ord('b') or key == ord('B'): # Battery status
|
||||||
|
self.report_battery_status()
|
||||||
|
|
||||||
elif key == 27: # Esc key - restart the application
|
elif key == 27: # Esc key - restart the application
|
||||||
try:
|
try:
|
||||||
# Announce restart first while speech still works
|
# Announce restart first while speech still works
|
||||||
|
|||||||
Reference in new issue
Block a user