Skip to content

Commit

Permalink
FilamentBuddy now automatically disables Filament Sensor feature when…
Browse files Browse the repository at this point in the history
… not executed on a Raspberry Pi
  • Loading branch information
danieleborgo committed Feb 13, 2024
1 parent f76189d commit 1226f40
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 41 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ operations.
The _Filament Sensor_ module is different since it uses a Python module
specifically developed for the Raspberry boards, _gpiozero_, so it has
this board as prerequisite. Nevertheless, Marlin is not a requirement,
since it only uses OctoPrint functionalities.
since it only uses OctoPrint functionalities. In the case FilamentBuddy
is not running on a Raspberry Pi, the plugin will disable this features.

## Setup

Expand Down
3 changes: 2 additions & 1 deletion extra/filamentbuddy.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ operations.
The _Filament Sensor_ module is different since it uses a Python module
specifically developed for the Raspberry boards, _gpiozero_, so it has
this board as prerequisite. Nevertheless, Marlin is not a requirement,
since it only uses OctoPrint functionalities.
since it only uses OctoPrint functionalities. In the case FilamentBuddy
is not running on a Raspberry Pi, the plugin will disable this features.

## Setup

Expand Down
6 changes: 5 additions & 1 deletion octoprint_filamentbuddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import octoprint.plugin
from octoprint.events import Events
from octoprint_filamentbuddy import GenericFilamentSensorManager
from octoprint_filamentbuddy.manager import is_gpio_available
from octoprint_filamentbuddy.manager.PollingFilamentSensorManager import PollingFilamentSensorManager
from octoprint_filamentbuddy.manager.InterruptFilamentSensorManager import InterruptFilamentSensorManager

Expand All @@ -45,6 +46,7 @@ class FilamentBuddyPlugin(

def __init__(self):
super().__init__()
self.__is_gpio_available = is_gpio_available()
self.__fs_manager = None
self.__fr_state = FilamentBuddyPlugin.FRState.INACTIVE

Expand All @@ -64,7 +66,7 @@ def __initialize_filament_sensor(self):
if self.__fs_manager is not None:
self.__fs_manager.close()
self.__fs_manager = None
if not self.__get_bool("fs", "en"):
if not self.__is_gpio_available or not self.__get_bool("fs", "en"):
return

if "polling".__eq__(self.__get_string("fs", "sensor_mode")):
Expand Down Expand Up @@ -344,11 +346,13 @@ def get_settings_defaults(self):
return {
**FilamentBuddyPlugin.DEFAULT_SETTINGS,
**{
"is_gpio_available": self.__is_gpio_available,
"default": FilamentBuddyPlugin.DEFAULT_SETTINGS
}
}

def on_settings_save(self, data):
data["is_gpio_available"] = self.__is_gpio_available
data["default"] = FilamentBuddyPlugin.DEFAULT_SETTINGS
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
self.__reset_plugin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
except ModuleNotFoundError:
from octoprint_filamentbuddy.manager import DigitalInputDeviceForOlderPy as DigitalInputDevice

from octoprint_filamentbuddy.manager import GPIONotFoundException
from octoprint_filamentbuddy.GenericFilamentSensorManager import GenericFilamentSensorManager


Expand All @@ -31,10 +32,14 @@ def __init__(self, logger, runout_f, pin: int, runout_time: int, empty_v: str):
self.__runout_time = runout_time
self.__is_empty_high = "high".__eq__(empty_v.lower())

self.__input_device = DigitalInputDevice(
pin=pin,
pull_up=self.__is_empty_high
)
try:
self.__input_device = DigitalInputDevice(
pin=pin,
pull_up=self.__is_empty_high
)
except ImportError:
raise GPIONotFoundException()

self.__running = False
self.__lock = Lock()
self.__runout_thread = None
Expand Down
15 changes: 10 additions & 5 deletions octoprint_filamentbuddy/manager/PollingFilamentSensorManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
except ModuleNotFoundError:
from octoprint_filamentbuddy.manager import DigitalInputDeviceForOlderPy as DigitalInputDevice

from octoprint_filamentbuddy.manager import GPIONotFoundException
from octoprint_filamentbuddy.GenericFilamentSensorManager import GenericFilamentSensorManager


Expand All @@ -33,11 +34,15 @@ def __init__(self, logger, runout_f, pin: int, polling_time: int, runout_time: i
self.__runout_time = runout_time
self.__is_empty_high = "high".__eq__(empty_v.lower())

self.__input_device = DigitalInputDevice(
pin=pin,
pull_up=self.__is_empty_high,
bounce_time=PollingFilamentSensorManager.BOUNCE_TIME
)
try:
self.__input_device = DigitalInputDevice(
pin=pin,
pull_up=self.__is_empty_high,
bounce_time=PollingFilamentSensorManager.BOUNCE_TIME
)
except ImportError:
raise GPIONotFoundException()

self.__event = None
self.__running = False
self.__verifying = False
Expand Down
32 changes: 30 additions & 2 deletions octoprint_filamentbuddy/manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,32 @@
"""

from typing import Callable
from RPi import GPIO

try:
from RPi import GPIO
is_imported = True
except RuntimeError:
is_imported = False


class GPIONotFoundException(ImportError):
def __init__(self):
super().__init__("Impossible to import the GPIO manager module")


def is_gpio_available() -> bool:
try:
from gpiozero import pi_info
pi_info()
# gpiozero is perfectly working on this Raspberry
return True
except ModuleNotFoundError:
# gpiozero is not supported, so the decision is delegated to RPi.GPIO
return is_imported
except ImportError:
# gpiozero is working but no GPIO scheme has been found
return False


"""
Some OctoPrint users still use out of life Python versions and some of these
Expand All @@ -28,7 +53,7 @@
Nevertheless, this solution has a major issue, which is related to the fact
that other plugins may require a newer gpiozero version. Consequently, if the
module version is downgraded, it may cause unwanted behavior or errors in these
plugin that, obviously, doesn't consider this case. Hence, this downgrade,
plugins that, obviously, doesn't consider this case. Hence, this downgrade,
despite being here explained, is highly discouraged, even if it works in most
cases. The first option, so upgrading Python, remains the most suggested one.
Expand Down Expand Up @@ -81,6 +106,9 @@ def when_deactivated(self, value: Callable):
self.__when_deactivated = value

def __init__(self, pin: int, pull_up: bool, bounce_time=1):
if not is_imported:
raise GPIONotFoundException()

self.__pin = pin
self.__pull_up = pull_up
self.__when_activated = None
Expand Down
5 changes: 5 additions & 0 deletions octoprint_filamentbuddy/static/css/filamentbuddy.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@
height: 21px;
padding-left: 5px;
padding-right: 5px;
}

.gpio-unavailable-notification{
font-weight: bold;
color: #F00;
}
2 changes: 1 addition & 1 deletion octoprint_filamentbuddy/static/js/filamentbuddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ $(function () {
}).done(function (data) {
if(data['state'])
self.is_filament_available(data['filament']);
self.is_filament_error(false);
self.is_filament_error(!data['state']);
}).fail(function () {
if(!self.is_filament_error())
self.notify("Error in retrieving filament status");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<div class="navbar-text" data-bind="visible: filamentbuddy.fs.en() && filamentbuddy.fs.toolbar_en()">
<div class="navbar-text" data-bind="visible: filamentbuddy.is_gpio_available() &&
filamentbuddy.fs.en() &&
filamentbuddy.fs.toolbar_en()">
<img src="/plugin/filamentbuddy/static/img/f_available.png" alt="error"
class="filament-sensor-navbar-icon"
data-bind="attr:{src: is_filament_error() ? '/plugin/filamentbuddy/static/img/f_error.png' :
Expand Down
Loading

0 comments on commit 1226f40

Please sign in to comment.