PWM Error After Closing a Linux Desktop App and Restarting the App
My tests are on my Model 4B and 5 Raspberry Pi, running Raspberry Pi Desktop OS (Debian bookworm).
Error - Cannot open PWM Channel After Closing an App
I am using the window_manage package to listen for the X closing the window.
When the window is closed, I dispose of the pwm service.
When I close a desktop app with the X and dispose of the pwmchip0, pwm0, pwm0 is now unexported. When I restart the app, the first time i get an error from dart_periphery that it cannot open the pwm0. I close the app with the X and now pwm0 has been exported. This is not acceptable performance and I need the app to be able to stop and restart without this error.
My solution example - I change it for different pwm channels:
Added code to check at initialization for pwm0. If it existing do nothing. If it does not exist I added code to send a bash script to export pwm0 Wait 500mS and then do the rest of the initialization. Note: need to add a check to ensure that pwm0 was exported.
/// Ensures PWM is exported before opening it
void _exportPwm() {
try {
if (!File('/sys/class/pwm/pwmchip2/pwm0').existsSync()) {
debugPrint('Exporting PWM0...');
Process.runSync('sh', ['-c', 'echo 0 > /sys/class/pwm/pwmchip2/export']);
sleep(Duration(milliseconds: 500)); // Wait for the system to process
}
} catch (e) {
debugPrint('Error exporting PWM0: $e');
}
}