Skip to content

Commit

Permalink
Stop error resulting from lack of pwm overlay support
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonhs committed Sep 17, 2024
1 parent 1a0b283 commit 303975c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
22 changes: 18 additions & 4 deletions backend_py/src/lights/rpi_pwm_hardware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rpi_hardware_pwm import HardwarePWM
from rpi_hardware_pwm import HardwarePWM, HardwarePWMException
from .pwm_controller import PWMController
from typing import Dict
import logging
Expand All @@ -9,6 +9,8 @@ class RPiHardwarePWMController(PWMController):
def __init__(self, chip=0, pins=None) -> None:
super().__init__()

self.pwm_supported = True

if pins is None:
pins = {
18: 0,
Expand All @@ -19,11 +21,23 @@ def __init__(self, chip=0, pins=None) -> None:
self.pwm_objects: Dict[int, HardwarePWM] = {}

for pin in self.PWM_PINS.keys():
self.pwm_objects[pin] = HardwarePWM(pwm_channel=self.PWM_PINS[pin], hz=7812.5, chip=chip)
try:
self.pwm_objects[pin] = HardwarePWM(pwm_channel=self.PWM_PINS[pin], hz=7812.5, chip=chip)
except HardwarePWMException:
logging.warning('Hardware PWM is not enabled. Need to add \'dtoverlay=pwm-2chan\' to /boot/config.txt and reboot.')
self.pwm_supported = False
break
self.pwm_objects[pin].start(0)

# Make sure the objects are not initialized
if not self.pwm_supported:
self.pwm_objects = {}

def is_pwm_pin(self, pin: int) -> bool:
return pin in self.PWM_PINS.keys()
'''
Return true if the pin is supported but will always return false if pwm is not supported entirely
'''
return pin in self.PWM_PINS.keys() if self.pwm_supported else False

def disable_pin(self, pin: int):
# FIXME: Not implemented
Expand All @@ -42,4 +56,4 @@ def cleanup(self):
pwm.stop()

def get_pins(self):
return self.PWM_PINS.keys()
return self.PWM_PINS.keys() if self.pwm_supported else []
48 changes: 35 additions & 13 deletions backend_py/src/lights/utils.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
import logging
from .fake_pwm import FakePWMController
# from .fake_pwm import FakePWMController
import re
import os

def get_rpi_model():
def is_overlay_loaded():
'''
Based on function from rpi_hardware_pwm
'''
chippath = '/sys/class/pwm/pwmchip0'
return os.path.isdir(chippath)

def get_rpi_version():
try:
with open('/sys/firmware/devicetree/base/model', 'r') as m:
model = m.read().lower()
if 'raspberry pi 3' in model:
return 3
elif 'raspberry pi 4' in model:
return 4
elif 'raspberry pi 5' in model:
return 5
# Read the device model from the file
with open('/sys/firmware/devicetree/base/model', 'r') as f:
model = f.read().strip()

# Check if the device is a Raspberry Pi
if "Raspberry Pi" in model:
# Extract the version number using regex
match = re.search(r'Raspberry Pi\s+(\d+)', model)
if match:
# Return the numeric model version
version = int(match.group(1))
return version
else:
# Fallback if no version number is found
return None
else:
return None
except:

except FileNotFoundError:
# In case the file doesn't exist or is not accessible
return None

def create_pwm_controllers():
pwm_controllers = []
version = get_rpi_model()
version = get_rpi_version()
if version is not None:
logging.info(f'Device is Raspberry Pi {version}')
if not is_overlay_loaded():
logging.warning('PWM Overlay not loaded. Need to add \'dtoverlay=pwm-2chan\' to /boot/config.txt and reboot')
return []
from .rpi_pwm_hardware import RPiHardwarePWMController
if version == 5:
pwm_controllers.append(RPiHardwarePWMController(chip=2, pins={
Expand All @@ -31,5 +53,5 @@ def create_pwm_controllers():
else:
pwm_controllers.append(RPiHardwarePWMController())
else:
pwm_controllers.append(FakePWMController())
logging.info('No supported PWM Controllers Found.')
return pwm_controllers

0 comments on commit 303975c

Please sign in to comment.