-
Notifications
You must be signed in to change notification settings - Fork 2
adding Thorlabs DC4100 led driver #37
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| __author__ = """arielle leon""" | ||
| __email__ = 'ariellel@alleninstitute.org' | ||
| __version__ = '0.1.0' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import serial as s | ||
| from serial import SerialException | ||
| import logging | ||
| import os | ||
|
|
||
|
|
||
| COMMANDS = { | ||
| "set_brightness": "BP {} {}", | ||
| "get_brightness": "BP? {}", | ||
| "led_on": "O {} 1", | ||
| "led_off": "O {} 0", | ||
| "return_on_off": "O? {}", | ||
| "lock_led": "A {} {}", | ||
| "register_status": "R?", | ||
| "serial_number": "S?", | ||
| "firmware": "V?", | ||
| "manufacturer": "H?", | ||
| "error_status": "E?" | ||
| } | ||
| class ThorlabsDC4100: | ||
| def __init__(self,port,baudrate,timeout): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style comments:
|
||
| self.port = port | ||
| self.baudrate = baudrate | ||
| self.timeout = timeout | ||
| self.dev = None | ||
| self.escape = '\n\n' | ||
| self.read_buffer = [] | ||
|
|
||
| def led_on(self, channel: int): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will cause a syntax error in python 2. I hope python 3 support is coming soon, but currently micromanager is also py2 only and we don't know when that will be fixed.. |
||
| self._write_to_LED(COMMANDS["led_on"].format(channel)) | ||
|
|
||
| def led_off(self,channel: int): | ||
| self._write_to_LED(COMMANDS["led_off"].format(channel)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another minor suggestion: by rewriting _write_to_LED a little, you could turn this into ..which reduces the amount of repeated code and makes everything more readable. |
||
|
|
||
| def set_brightness(self,channel:int, brightness: float): | ||
| self._write_to_LED(COMMANDS["set_brightness"].format(channel,brightness)) | ||
|
|
||
| def get_brightness(self,channel: int): | ||
| self._write_to_LED(COMMANDS["get_brightness"].format(channel)) | ||
| return self._read_from_LED() | ||
|
|
||
| def check_if_on(self, channel: int): | ||
| self._write_to_LED(COMMANDS["return_on_off"].format(channel)) | ||
| return self._read_from_LED() | ||
|
|
||
| @property | ||
| def serial_number(self): | ||
| self._write_to_LED(COMMANDS["serial_number"]) | ||
| return self._read_from_LED() | ||
|
|
||
| @property | ||
| def firmware(self): | ||
| self._write_to_LED(COMMANDS["firmware"]) | ||
| return self._read_from_LED() | ||
|
|
||
| @property | ||
| def manufacturer(self): | ||
| self._write_to_LED(COMMANDS["manufacturer"]) | ||
| return self._read_from_LED() | ||
|
|
||
| def connect_device(self): | ||
| try: | ||
| self.dev = s.Serial(port=self.port,baudrate=self.baudrate,timeout=self.timeout) | ||
| except SerialException: | ||
| logging.error("Device connection could not be established") | ||
|
|
||
| def _write_to_LED(self, command: str): | ||
| self.dev.write(f"{command} {self.escape}".encode()) | ||
|
|
||
| def _read_from_LED(self): | ||
| while self.dev.is_open: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| output = self.dev.read().decode() | ||
| if len(self.read_buffer) == 0 and output == '\r': | ||
| self.dev.flush() | ||
| continue | ||
| if output == "\n": | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if I am not mistaken, it's possible for |
||
| ret_value = "".join(self.read_buffer) | ||
| self.dev.flush() | ||
| self.read_buffer = [] | ||
| return ret_value | ||
| else: | ||
| self.read_buffer.append(output) | ||
|
|
||
| def main(): | ||
| led = ThorlabsDC4100(port='com12',baudrate=115200,timeout=0.5) | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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.
init needs to import the Thorlabs DC4100 class.
Also: I recommend omitting the author/email/version.