From 143be8f592edb5b4d09156fd953b611a7ea9d951 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Wed, 7 Nov 2018 15:06:41 +0000 Subject: [PATCH] Started integrating into Microscope class --- openflexure_microscope/__init__.py | 2 + openflexure_microscope/camera/base.py | 1 + openflexure_microscope/microscope.py | 26 +++++++++++++ tests/test_microscope.py | 55 +++++++++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 openflexure_microscope/microscope.py create mode 100644 tests/test_microscope.py diff --git a/openflexure_microscope/__init__.py b/openflexure_microscope/__init__.py index 3dc1f76b..b5bdcd1f 100644 --- a/openflexure_microscope/__init__.py +++ b/openflexure_microscope/__init__.py @@ -1 +1,3 @@ +from .microscope import Microscope + __version__ = "0.1.0" diff --git a/openflexure_microscope/camera/base.py b/openflexure_microscope/camera/base.py index 2f02ee85..0649cb71 100644 --- a/openflexure_microscope/camera/base.py +++ b/openflexure_microscope/camera/base.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import time import io import threading diff --git a/openflexure_microscope/microscope.py b/openflexure_microscope/microscope.py new file mode 100644 index 00000000..7762a821 --- /dev/null +++ b/openflexure_microscope/microscope.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +import numpy as np + +from openflexure_stage import OpenFlexureStage +from .camera.pi import StreamingCamera + + +class Microscope(object): + def __init__(self, camera: StreamingCamera, stage: OpenFlexureStage): + """Create the microscope object. The camera and stage should already be initialised.""" + self.camera = camera + self.stage = stage + self.stage.backlash = np.zeros(3, dtype=np.int) + + def __enter__(self): + """Create microscope on context enter.""" + return self + + def __exit__(self, exc_type, exc_value, traceback): + """Close microscope on context exit.""" + self.close() + + def close(self): + """Shut down the microscope hardware.""" + self.camera.close() + self.stage.close() diff --git a/tests/test_microscope.py b/tests/test_microscope.py new file mode 100644 index 00000000..06b0622e --- /dev/null +++ b/tests/test_microscope.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +from openflexure_microscope.camera.pi import StreamingCamera +from openflexure_stage import OpenFlexureStage +from openflexure_microscope import Microscope + +import os +import io +import sys +import time +import numpy as np +import uuid + +from PIL import Image + +import unittest +from pprint import pprint + +import logging, sys +logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) + +success_string = """ + /O + | | + _____) \\ + (__0) \\______ + (____0) + (____0) EVERYTHING IS OK! + (__o)___________ + """ + + +class TestMicroscope(unittest.TestCase): + + def test_startup(self): + """Tests capturing still images to a BytesIO stream.""" + global microscope + pass + + +if __name__ == '__main__': + with StreamingCamera() as camera, OpenFlexureStage(None) as stage: + + microscope = Microscope(camera, stage) + + suites = [ + unittest.TestLoader().loadTestsFromTestCase(TestMicroscope), + ] + + alltests = unittest.TestSuite(suites) + + result = unittest.TextTestRunner(verbosity=2).run(alltests) + + if result.wasSuccessful(): + print(success_string) +