{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cab82157-e90d-4e54-947b-ed18d660e771",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Check FPGA [OK].\n",
      "Red Pitaya initialized.\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "from rp_overlay import overlay\n",
    "import rp\n",
    "\n",
    "fpga = overlay()\n",
    "rp.rp_Init()\n",
    "print(\"Red Pitaya initialized.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "51db24f0-c3ae-4ff5-b6b3-993882e0d0e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reset generator and acquisition.\n"
     ]
    }
   ],
   "source": [
    "rp.rp_GenReset()\n",
    "rp.rp_AcqReset()\n",
    "print(\"Reset generator and acquisition.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5924a7c6-2f34-48d6-92ce-29ec6db30abd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Gen_start\n",
      "Generator started.\n"
     ]
    }
   ],
   "source": [
    "# Generator parameters\n",
    "channel = rp.RP_CH_1\n",
    "channel2 = rp.RP_CH_2\n",
    "waveform = rp.RP_WAVEFORM_SINE\n",
    "freq = 1\n",
    "ampl = 1.0\n",
    "\n",
    "print(\"Gen_start\")\n",
    "rp.rp_GenWaveform(channel, waveform)\n",
    "rp.rp_GenFreqDirect(channel, freq)\n",
    "rp.rp_GenAmp(channel, ampl)\n",
    "\n",
    "rp.rp_GenWaveform(channel2, waveform)\n",
    "rp.rp_GenFreqDirect(channel2, freq)\n",
    "rp.rp_GenAmp(channel2, ampl)\n",
    "\n",
    "rp.rp_GenTriggerSource(channel, rp.RP_GEN_TRIG_SRC_INTERNAL)\n",
    "\n",
    "rp.rp_GenOutEnableSync(True)\n",
    "rp.rp_GenSynchronise()\n",
    "print(\"Started generator.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "13eaa97b-127e-435c-81ff-216004f617aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GainVoltage [0, 20.0]\n",
      "Acq_start\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 21224 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Set Decimation\n",
    "rp.rp_AcqSetDecimationFactor(16384)\n",
    "rp.rp_AcqSetAveraging(True)\n",
    "rp.rp_AcqSetGain(rp.RP_CH_1, rp.RP_HIGH)\n",
    "\n",
    "V=rp.rp_AcqGetGainV(rp.RP_CH_1)\n",
    "print(\"GainVoltage\", V)\n",
    "\n",
    "# Set trigger level and delay\n",
    "rp.rp_AcqSetTriggerLevel(rp.RP_T_CH_1, 0.5)\n",
    "rp.rp_AcqSetTriggerDelay(0)\n",
    "\n",
    "# Start Acquisition\n",
    "print(\"Acq_start\")\n",
    "rp.rp_AcqStart()\n",
    "\n",
    "time.sleep(3)\n",
    "\n",
    "# Specify trigger - immediately\n",
    "rp.rp_AcqSetTriggerSrc(rp.RP_TRIG_SRC_NOW)\n",
    "\n",
    "# Trigger state\n",
    "while 1:\n",
    "    trig_state = rp.rp_AcqGetTriggerState()[1]\n",
    "    if trig_state == rp.RP_TRIG_STATE_TRIGGERED:\n",
    "        break\n",
    "\n",
    "# Fill state\n",
    "while 1:\n",
    "    if rp.rp_AcqGetBufferFillState()[1]:\n",
    "        break\n",
    "\n",
    "\n",
    "### Get data ###\n",
    "\n",
    "# Volts\n",
    "N = 16384\n",
    "fbuff = rp.fBuffer(N)\n",
    "res = rp.rp_AcqGetOldestDataV(rp.RP_CH_1, N, fbuff)[1]\n",
    "\n",
    "data_V = np.zeros(N, dtype = float)\n",
    "X = np.arange(0, N, 1)\n",
    "\n",
    "for i in range(0, N, 1):\n",
    "    data_V[i] = fbuff[i]\n",
    "\n",
    "plt.plot(X, data_V) \n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "489a59ca-59e6-4f96-9728-3d8ee20b5df4",
   "metadata": {},
   "source": [
    "Only run the `rp_Release()` below when you do not want to further acquire data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b3f32b3-4a7c-4ba5-a675-d592550deed8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Release resources\n",
    "rp.rp_Release()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (RP api)",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
