Chromation’s Python project microspec
is available from PyPI
(the Python Package Index) at
https://pypi.org/project/microspec/.
The latest documentation for microspec
is here: https://microspec.readthedocs.io
We are adding a higher-level API to microspec
to simplify
application programming. It is already part of the latest version
of microspec
, but we don’t have any example scripts yet. The
documentation for the higher-level API is temporarily located here:
https://microspec-api.readthedocs.io/
There is also a Python GUI at https://pypi.org/project/microspecgui/.
The GUI does not have much functionality yet, but it at least gives a live plot with control over exposure time and a display of the peak pixel. It’s a good alternative when using the LabVIEW GUI is inconvenient but you need visual feedback to align your spectrometer. And unlike the LabVIEW GUI, the Python GUI works on Windows, Mac, and Linux, and Raspberry Pi 3 or higher.
See PYTHON-SETUP.md for steps starting on a Windows or Linux system without Python.
microspec is a Python package for USB communication with the spectrometer dev-kit.
Install microspec
with pip
:
$ pip install microspec
REPL Example:
>>> import microspec as usp
>>> kit = usp.Devkit()
>>> reply = kit.captureFrame()
>>> print(reply)
captureFrame_response(status='OK', num_pixels=392,
pixels=[...], frame={...})
Command-line example:
$ microspec-cmdline.exe captureframe
2021-02-11T22:56:17.133290,SensorCaptureFrame(status=0, num_pixels=392, pixels=[...])
The microspec
package has a command line utility and two
modules:
microspec_cmdline.py
:
microspec-cmdline.exe
followed by
the dev-kit command and argumentsmicrospec
:
microspeclib
for the purpose
of adding docstrings and simplifying the UI for sending
commands to the dev-kitmicrospec
microspeclib
:
microspeclib
microspec-cmdline.exe
utility to test that I’ve
revised the firmware to match the change in the
protocol – I don’t need to write any Python code to do
this testmicrospeclib
, but breaks the microspec
module (which is
good because having to update micropec
manually is my
reminder to update the docstrings)The same example from earlier looks like this using microspeclib
:
>>> from microspeclib.simple import MicroSpecSimpleInterface
>>> kit = MicroSpecSimpleInterface()
>>> reply = kit.captureFrame()
>>> print(reply)
SensorCaptureFrame(status=0, num_pixels=392, pixels=[...])
microspec
to talk to the dev-kitThe microspec
package make it easy to write Python applications
to control the dev-kit over USB without solving low-level USB
hardware issues such as:
Here is a short example script using microspeclib
that:
from microspeclib.simple import MicroSpecSimpleInterface
kit = MicroSpecSimpleInterface(timeout=2.0)
print(kit.getSensorConfig())
Running the above 3-line script results in the following output:
SensorGetSensorConfig(status=0, binning=1, gain=1, row_bitmap=31)
In the above 3-line script, the command is getSensorConfig
which reads the spectrometer chip’s internal analog
configuration.
What is going on:
MicroSpecSimpleInterface
class
kit
:kit = MicroSpecSimpleInterface()
MicroSpecSimpleInterface
instance
foo
by calling kit.foo()
If multiple dev-kits are connected, name the dev-kit by serial number when instantiating:
from microspeclib.simple import MicroSpecSimpleInterface
kit = MicroSpecSimpleInterface(serial_number='091103',timeout=2.0)
print(kit.serial.serial_number)
Running the above 3-line script results in the following output (the output depends on the kit’s serial number):
CHROMATION091103
If there is no dev-kit connected with serial number 091103
(or
if that dev-kit is already opened in another application), the
following exception is raised:
MicroSpecConnectionException: Cannot find CHROMATION device
microspec_cmdline.py
runs a single command and prints the reply
to stdout, optionally in CSV format.
led_num=0
or
cycles=100
For example, the following sequence of commands will:
microspec_cmdline.py setsensorconfig binning=true gain=gain1x row_bitmap=0x1f
microspec_cmdline.py setexposure cycles=100
microspec_cmdline.py captureframe -t 0.2 -r 3 -w 1.5 --csv
-t
timeout:
None
and moves on-
r repeat
-w
wait:
All timeout and wait times are in fractional seconds.
.exe
on WindowsOn Windows, replace microspec_cmdline.py
in the above lines
with microspec_cmdline.exe
.
For example:
> microspec-cmdline.exe captureframe -t 0.2 -r 3 -w 1.5 --csv
The repository has some examples:
one-command
runs one dev-kit command. The script docstring
has a short summary of all the commands.system-tests
runs every dev-kit command.using microspec
:
using microspeclib
:
A GUI is necessary for real-time feedback when optically aligning the spectrometer in a measurement setup.
Chromation provides microspecgui
. This is a Python GUI to
replace the legacy LabVIEW GUI. microspecgui
uses the
microspeclib
package for communication and SDL interface
pygame
for display and user control.
Install:
$ pip install microspecgui
Run:
$ microspec-gui
On Windows:
> microspec-gui.exe
Use:
X - decrease exposure
Shift+X - increase exposure
Space - autoexpose
Ctrl+Q - quit
All dev-kit firmware, dev-kit design files, and Python code is
open-source under a standard MIT License. For example, users are
free to develop their own GUI reusing anything from the
microspecgui
source code, or develop their own
hardware/firmware reusing any of the schematics, PCB layout, or
firmware provided by Chromation.
microspec
is activeChromation actively maintains microspec
. We use this library
to create all of our applications, both for internal work and for
external example applications. If you make a modification you
want to share, or find a bug, or if you have a feature request,
please let us know.
If you’d like to reuse microspeclib
for your own custom
communication protocol, the setup is described below, and is
described in much greater detail in
PYTHON-SETUP.md. Extra details on Linux Mint
setup are in PYTHON-SETUP.md in the section Setup for modifying
microspeclib.
microspec
repository from the project homepage$ git clone https://github.com/microspectrometer/microspec.git
venv
pip install -e .
,
this:
setup.py
pip uninstall microspec
, or
by simply deleting the virtual environmentInstall the packages for running the unit tests and rebuilding documentation:
$ pip install microspec[dev]
Commands defined in the JSON file are automatically picked up by
the microspec
unit tests to check that the hardware responds
with the expected response. The unit tests figure out the
expected response from the JSON file.
Enter the microspec
local repository folder, then run the tests
with pytest
:
$ pytest
The documentation automatically includes some basic information. But it is still up to the user to add specific docstrings.
Since the command methods are created by meta-class factories,
they are not defined in code, so there are no docstrings to
edit. This is why I ended up creating the microspec
module to wrap microspeclib
.
src/microspeclib/internal/docstrings.py
is a place to put
documentation that would normally go in the method docstring.
Then enter the microspec/doc
folder and run the Sphinx
Makefile to generate the new HTML documentation:
$ cd microspec/doc
$ make clean html
I build the documentation on Linux. I cannot get the build to run
on Windows. The .rst
files use the autodoc
Sphinx extension,
and something about the complicated path setup prevents autodoc
from finding the submodules in the tests
folder.