-
Notifications
You must be signed in to change notification settings - Fork 193
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
Add an enclosure service for Picroft #122
Open
penrods
wants to merge
3
commits into
buster
Choose a base branch
from
feature/picroft-enclosure
base: buster
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
########################################################################## | ||
# file_watchdog.py | ||
# | ||
# Copyright 2019, Stephen Penrod | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
########################################################################## | ||
|
||
# Gain root access (needed for GPIO and Admin actions), load the Mycroft | ||
# virtual environment, and launch the enclosure script | ||
sudo -- bash -c 'source /home/pi/mycroft-core/venv-activate.sh; python3 my_enclosure.py' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python | ||
########################################################################## | ||
# file_watchdog.py | ||
# | ||
# Copyright 2019, Stephen Penrod | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
########################################################################## | ||
|
||
########################################################################## | ||
# This implements a watchdog to reload the given script(s) when changes | ||
# are made to the files. In other words, it automatically restarts the given | ||
# program when the program or other watched files are edited and saved to disk. | ||
# | ||
# Usage: | ||
# import lib.file_wachdog as watchdog | ||
# watchdog.watch(__file__) | ||
|
||
import os, sys | ||
import threading | ||
from os.path import getmtime | ||
|
||
__watched_files_mtimes = [] | ||
|
||
|
||
def __checkForModification(): | ||
for f, mtime in __watched_files_mtimes: | ||
if getmtime(f) != mtime: | ||
# Code modification detected, re-launch the main program to | ||
# pick up the changes. | ||
return os.execv(sys.executable, ['python'] + sys.argv) | ||
|
||
# Rerun the check in 5 seconds | ||
t = threading.Timer(5, __checkForModification) | ||
t.daemon = True # don't block the program from ending if main block exits | ||
t.start() | ||
|
||
|
||
def watch(files): | ||
"""Start watchdog to relaunch the program when the file(s) change | ||
|
||
Args: | ||
files (string or [string]): File(s) to monitor for changes | ||
""" | ||
global __watched_files_mtimes | ||
|
||
if isinstance(files, list): | ||
__watched_files = files | ||
else: | ||
__watched_files = [files] # support a single filename | ||
__watched_files_mtimes = [(f, getmtime(f)) for f in __watched_files] | ||
|
||
# Kick-off monitor which checks every 5 seconds for code changes | ||
__checkForModification() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/usr/bin/env python | ||
########################################################################## | ||
# gpio_button.py | ||
# | ||
# Copyright 2019, Stephen Penrod | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
########################################################################## | ||
|
||
import RPi.GPIO as GPIO | ||
from threading import Timer | ||
from time import time | ||
|
||
class GPIO_Button: | ||
|
||
def __init__(self, GPIO_BCM=23, on_press=None, on_release=None, | ||
on_hold=None, on_double_press=None): | ||
""" Interface for a button connected to a GPIO pin | ||
|
||
Respond to a button connected to a GPIO pin (and ground) | ||
|
||
Args: | ||
GPIO_BCM (int, optional): The BCM number of the GPIO pin to use. | ||
Defaults to 23. | ||
""" | ||
self.button_bcm = GPIO_BCM | ||
|
||
self._on_press = on_press | ||
self._on_release = on_release | ||
self._on_hold = on_hold | ||
self._on_double_press = on_double_press | ||
|
||
GPIO.setmode(GPIO.BCM) | ||
GPIO.setup(self.button_bcm, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||
GPIO.add_event_detect(23, GPIO.BOTH, self._on_gpio_button) | ||
self.__timer = None | ||
self.__timer2 = None | ||
self.__last_press = 0 | ||
self.__pressed = False | ||
self.__double_pressed = False | ||
|
||
def __del__(self): | ||
GPIO.remove_event_detect(self.button_bcm) | ||
|
||
def _on_gpio_button(self, channel): | ||
if GPIO.input(channel) == GPIO.HIGH: | ||
# High == button unpressed (pulled high to 3.3v) | ||
self.__pressed = False | ||
if self.__timer: | ||
self.__timer.cancel() | ||
if self.__timer2: | ||
self.__timer2.cancel() | ||
# Wait briefly before firing event to de-bounce | ||
self.__timer = Timer(0.05, self._on_gpio_released) | ||
self.__timer.start() | ||
else: | ||
# Low == button pressed (grounded) | ||
self.__pressed = True | ||
if self.__timer: | ||
self.__timer.cancel() | ||
if self.__timer2: | ||
self.__timer2.cancel() | ||
# Wait briefly before firing event to de-bounce | ||
self.__timer = Timer(0.05, self._on_gpio_pressed) | ||
|
||
# Hold for 1 second to get a "hold" action | ||
self.__timer2 = Timer(1, self._on_gpio_held) | ||
self.__timer.start() | ||
self.__timer2.start() | ||
|
||
def _on_gpio_pressed(self): | ||
self.__timer = None | ||
if self.__pressed: | ||
now = time() | ||
time_since_last = now - self.__last_press | ||
self.__last_press = now | ||
if time_since_last < 1: | ||
# Two presses within a second == double press event | ||
self.__double_pressed = True | ||
if self._on_double_press: | ||
self._on_double_press() | ||
else: | ||
# Single press event | ||
self.__double_pressed = False | ||
if self._on_press: | ||
self._on_press() | ||
|
||
def _on_gpio_released(self): | ||
self.__timer = None | ||
if not self.__pressed and not self.__double_pressed: | ||
if time() - self.__last_press < 1: | ||
# Release event | ||
if self._on_release: | ||
self._on_release() | ||
else: | ||
# Either button was held or a spurrious signal with no press | ||
pass | ||
|
||
def _on_gpio_held(self): | ||
self.__timer2 = None | ||
if self.__pressed: | ||
# Button held event | ||
if self._on_hold: | ||
self._on_hold() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that on systems with python2 AND python3, this might be needed .. BUT, afaict, since we should be within the picroft venv ... shouldn't technically be required; and anyway
pip
andpip3
are the same on picroft venv systems