-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge my adafruithat changes with the pscopehat version
- Loading branch information
Showing
7 changed files
with
572 additions
and
83 deletions.
There are no files selected for viewing
420 changes: 397 additions & 23 deletions
420
control/adafruithat/planktoscope/imagernew/__init__.py
Large diffs are not rendered by default.
Oops, something went wrong.
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
66 changes: 66 additions & 0 deletions
66
control/adafruithat/planktoscope/imagernew/picam_threading.py
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,66 @@ | ||
################################################################################ | ||
# Practical Libraries | ||
################################################################################ | ||
|
||
# Library to execute picamera in a separate thread within the imager process | ||
import threading | ||
|
||
# Logger library compatible with multiprocessing | ||
from loguru import logger | ||
|
||
# Library to create a queue for commands coming to the camera | ||
#import queue | ||
|
||
# Library to manage time commands and delay execution for a given time | ||
import time | ||
|
||
################################################################################ | ||
# Class for the implementation of picamera2 thread | ||
################################################################################ | ||
|
||
class PicamThread(threading.Thread): | ||
"""This class contains the main definitions of picamera thread""" | ||
|
||
def __init__(self, camera, command_queue, stop_event): | ||
"""Initialize the picamera thread class | ||
Args: | ||
camera: picamera instance | ||
command_queue (queue.Queue): queue for commands, when info must be exchanged safely between several threads | ||
stop_event (multiprocessing.Event or threading.Event): shutdown event | ||
""" | ||
super().__init__() | ||
self.__picam = camera | ||
self.command_queue = command_queue #FIXME remove the queue for now if not used | ||
self.stop_event = stop_event | ||
|
||
@logger.catch | ||
def run(self): | ||
try: | ||
self.__picam.start() | ||
except Exception as e: | ||
logger.exception( | ||
f"An exception has occured when starting up picamera2: {e}" | ||
) | ||
try: | ||
self.__picam.start(True) | ||
except Exception as e: | ||
logger.exception( | ||
f"A second exception has occured when starting up picamera2: {e}" | ||
) | ||
logger.error("This error can't be recovered from, terminating now") | ||
raise e | ||
try: | ||
while not self.stop_event.is_set(): | ||
"""if not self.command_queue.empty(): | ||
try: | ||
# Retrieve a command from the queue with a timeout to avoid indefinite blocking | ||
command = self.command_queue.get(timeout=0.1) | ||
except Exception as e: | ||
logger.exception(f"An error has occurred while handling a command: {e}")""" | ||
pass | ||
time.sleep(0.01) | ||
finally: | ||
self.__picam.stop() | ||
self.__picam.close() | ||
|
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
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