{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "82162890",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyfirmata import Arduino\n",
    "import serial\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fe78b7d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MCSArduino(Arduino):\n",
    "    pin_diode = 13\n",
    "    pin_TTL = 6\n",
    "    pin_LED_lights = 7\n",
    "    \n",
    "    def __init__(self, *args, **kwargs):\n",
    "        self.last_cmd = False  # False - Arduino LOW, True - Arduino HIGH\n",
    "        self.is_light_off = False\n",
    "        super(MCSArduino, self).__init__(*args, **kwargs)\n",
    "        \n",
    "    def start_or_stop(self):\n",
    "        self.last_cmd = not self.last_cmd\n",
    "        self.digital[MCSArduino.pin_diode].write(self.last_cmd)\n",
    "        self.digital[MCSArduino.pin_TTL].write(self.last_cmd)\n",
    "\n",
    "    def switch_light(self):\n",
    "        self.is_light_off = not self.is_light_off\n",
    "        self.digital[MCSArduino.pin_LED_lights].write(self.is_light_off)\n",
    "        \n",
    "class FakeArduino():\n",
    "    def __init__(self):\n",
    "        self.is_light_off = False\n",
    "        \n",
    "    def start_or_stop(self):\n",
    "        print(\"Fake Arduino - sending a TTL pulse\")\n",
    "        \n",
    "    def exit(self):\n",
    "        print(\"Fake Arduino - exiting...\")\n",
    "        \n",
    "    def switch_light(self):\n",
    "        self.is_light_off = not self.is_light_off\n",
    "        print(\"Fake Arduino - switching light on/off...\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "99209a2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "class FakeDevice:\n",
    "    def write(self):\n",
    "        pass\n",
    "    def close(self):\n",
    "        pass\n",
    "\n",
    "class Feeder:\n",
    "    def __init__(self, port, baudrate=9600):\n",
    "        if port == 'fake':\n",
    "            self.device = FakeDevice()\n",
    "        else:\n",
    "            self.device = serial.Serial(port, baudrate=baudrate)\n",
    "        \n",
    "    def feed(self):\n",
    "        self.device.write('SP200\\r'.encode())\n",
    "        self.device.write('LR6175\\r'.encode())\n",
    "        self.device.write('m\\r'.encode())\n",
    "        \n",
    "    def exit(self):\n",
    "        self.device.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56d33a31",
   "metadata": {},
   "source": [
    "### Testing Arduino TTL pulses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a8ee4492",
   "metadata": {},
   "outputs": [],
   "source": [
    "board = MCSArduino('COM10')  # Windows - 'COM10', Linux - '/dev/ttyACM0', check /dev/tty*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8aef8b20",
   "metadata": {},
   "outputs": [],
   "source": [
    "# check the light on-off\n",
    "board.switch_light()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "94466a46",
   "metadata": {},
   "outputs": [],
   "source": [
    "# testing sync with acquisition system\n",
    "board.start_or_stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c0687f02",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Arduino should blink 2 times\n",
    "for i in range(4):\n",
    "    board.start_or_stop()\n",
    "    time.sleep(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7e755da5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['PWM_CAPABLE',\n",
       " '__class__',\n",
       " '__delattr__',\n",
       " '__dict__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__le__',\n",
       " '__lt__',\n",
       " '__module__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " '__weakref__',\n",
       " '_get_mode',\n",
       " '_mode',\n",
       " '_set_mode',\n",
       " 'board',\n",
       " 'disable_reporting',\n",
       " 'enable_reporting',\n",
       " 'mode',\n",
       " 'pin_number',\n",
       " 'port',\n",
       " 'read',\n",
       " 'reporting',\n",
       " 'type',\n",
       " 'value',\n",
       " 'write']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(board.digital[5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bc432891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "board.digital[5].value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f883b19d",
   "metadata": {},
   "outputs": [],
   "source": [
    "board.exit()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54d9ffa1",
   "metadata": {},
   "source": [
    "### Testing feeder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "990f5a4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "feeder = Feeder('COM8')\n",
    "\n",
    "time.sleep(5)  # time to get inside the chamber and see that it works\n",
    "feeder.feed()\n",
    "\n",
    "feeder.exit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "982eb841",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
