{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Libraries/env ready.\n"
     ]
    }
   ],
   "source": [
    "#C1\n",
    "import json\n",
    "import os\n",
    "from steamvr.lighthouse_console import LighthouseConsole as lh\n",
    "import subprocess\n",
    "\n",
    "# run lighthouse as admin!!\n",
    "os.environ[\"LH_CONSOLE_PATH\"] = r\"C:\\Users\\Bigscreen\\Documents\\GitHub\\valve-tools-wrapper\\steamvr\\tools\\bin\\win64\\lighthouse_console.exe\"\n",
    "\n",
    "print(\"Libraries/env ready.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {},
   "outputs": [],
   "source": [
    "#C2\n",
    "# Load JSON \n",
    "def load_steamvr_json(file_path):\n",
    "    try:\n",
    "        with open(file_path, 'r') as file:\n",
    "            config = json.load(file)\n",
    "        print(f\"Loaded config from {file_path}\")\n",
    "        return config\n",
    "    except Exception as e:\n",
    "        raise RuntimeError(f\"Error loading config JSON: {e}\")\n",
    "\n",
    "# Update the SN in the config\n",
    "def set_serial_number(config, serial_number):\n",
    "    config[\"device_serial_number\"] = serial_number\n",
    "    print(f\"Updated serial number to {serial_number}\")\n",
    "    return config\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lighthouse console ready.\n"
     ]
    }
   ],
   "source": [
    "#C3\n",
    "try:\n",
    "    # Initialize Lighthouse console\n",
    "    console = lh.create()\n",
    "    console.open()\n",
    "    print(\"Lighthouse console ready.\")\n",
    "except Exception as e:\n",
    "    print(f\"Error initializing Lighthouse console: {e}\")\n",
    "    raise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[b'lh> LHR-2252ED63: Read config of 1979 bytes from [vid:28de, pid:2300] (LHR-2252ED63) and inflated to 11004 bytes\\r\\n', b'DEVICEINFO serial=LHR-2252ED63 device_class=hmd\\r\\n']\n",
      "Detected HMD serial number: LHR-2252ED63\n",
      "Selected HMD with serial number: LHR-2252ED63\n"
     ]
    }
   ],
   "source": [
    "#C4 \n",
    "try:\n",
    "    # Detect connected device\n",
    "    hardware = console.list_hardware()\n",
    "    if not hardware:\n",
    "        raise RuntimeError(\"No devices connected. Ensure an HMD is plugged in.\")\n",
    "\n",
    "    # Find the HMD \n",
    "    target_device = next((device for device in hardware if device['device_class'] == 'hmd'), None)\n",
    "    if not target_device:\n",
    "        raise RuntimeError(\"No HMD found in connected devices.\")\n",
    "\n",
    "    # Get the sn\n",
    "    target_serial = target_device['serial']\n",
    "    print(f\"Detected HMD serial number: {target_serial}\")\n",
    "\n",
    "    # Select the HMD\n",
    "    console.select_device(target_serial)\n",
    "    print(f\"Selected HMD with serial number: {target_serial}\")\n",
    "\n",
    "except Exception as e:\n",
    "    print(f\"Error detecting or selecting device: {e}\")\n",
    "    raise\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded config from C:\\Users\\Bigscreen\\Documents\\GitHub\\big-notebooks\\BS2_TEST_CONFIG.json\n",
      "Updated serial number to LHR-2252ED63\n",
      "Updated Config to Upload:\n",
      "{\n",
      "    \"manufacturer\": \"Bigscreen\",\n",
      "    \"model_number\": \"Beyond\",\n",
      "    \"revision\": 0.12,\n",
      "    \"device\": {\n",
      "        \"eye_target_height_in_pixels\": 2544,\n",
      "        \"eye_target_width_in_pixels\": 2544,\n",
      "        \"first_eye\": \"eEYE_LEFT\",\n",
      "        \"last_eye\": \"eEYE_RIGHT\",\n",
      "        \"num_windows\": 1\n",
      "    },\n",
      "    \"device_class\": \"hmd\",\n",
      "    \"device_serial_number\": \"LHR-2252ED63\",\n",
      "    \"direct_mode_edid_pid\": \"1234\",\n",
      "    \"direct_mode_edid_vid\": \"BIG\",\n",
      "    \"dsc_version\": 17,\n",
      "    \"dsc_slices\": 4,\n",
      "    \"dsc_bppx16\": 128,\n",
      "    \"display_color_mult\": [\n",
      "        [\n",
      "            0.952545,\n",
      "            0.917645,\n",
      "            1.0\n",
      "        ],\n",
      "        [\n",
      "            0.952545,\n",
      "            0.917645,\n",
      "            1.0\n",
      "        ]\n",
      "    ],\n",
      "    \"seconds_from_photons_to_vblank\": 0.0138888888888889,\n",
      "    \"seconds_from_vsync_to_photons\": 0.002,\n",
      "    \"parallel_render_cameras\": true,\n",
      "    \"display_edid\": [\n",
      "        \"\",\n",
      "        \"\"\n",
      "    ],\n",
      "    \"expected_bases\": 2,\n",
      "    \"expected_controllers\": 2,\n",
      "    \"firmware_config\": {\n",
      "        \"imu_rate\": 1000\n",
      "    },\n",
      "    \"head\": {\n",
      "        \"plus_x\": [\n",
      "            1.0,\n",
      "            0.0,\n",
      "            0.0\n",
      "        ],\n",
      "        \"plus_z\": [\n",
      "            0.0,\n",
      "            0.0,\n",
      "            1.0\n",
      "        ],\n",
      "        \"position\": [\n",
      "            0.0,\n",
      "            0.001014,\n",
      "            0.011579\n",
      "        ]\n",
      "    },\n",
      "    \"imu\": {\n",
      "        \"acc_bias\": [\n",
      "            0.0,\n",
      "            0.0,\n",
      "            0.0\n",
      "        ],\n",
      "        \"acc_scale\": [\n",
      "            1.0,\n",
      "            1.0,\n",
      "            1.0\n",
      "        ],\n",
      "        \"gyro_bias\": [\n",
      "            0.0,\n",
      "            0.0,\n",
      "            0.0\n",
      "        ],\n",
      "        \"plus_x\": [\n",
      "            -1.0,\n",
      "            0.0,\n",
      "            0.0\n",
      "        ],\n",
      "        \"plus_z\": [\n",
      "            0.00067,\n",
      "            0.1961,\n",
      "            -0.98058\n",
      "        ],\n",
      "        \"position\": [\n",
      "            0.000575,\n",
      "            0.002048,\n",
      "            -0.031936\n",
      "        ]\n",
      "    },\n",
      "    \"ipd\": {\n",
      "        \"default_mm\": 63\n",
      "    },\n",
      "    \"lighthouse_config\": {\n",
      "        \"channelMap\": [\n",
      "            0,\n",
      "            1,\n",
      "            2,\n",
      "            3,\n",
      "            4,\n",
      "            5,\n",
      "            8,\n",
      "            9,\n",
      "            10,\n",
      "            11,\n",
      "            12,\n",
      "            21,\n",
      "            22,\n",
      "            23,\n",
      "            24,\n",
      "            25,\n",
      "            26,\n",
      "            27,\n",
      "            28,\n",
      "            29,\n",
      "            30,\n",
      "            31\n",
      "        ],\n",
      "        \"modelNormals\": [\n",
      "            [\n",
      "                0.00863834564,\n",
      "                0.21732679,\n",
      "                -0.976060688\n",
      "            ],\n",
      "            [\n",
      "                0.316527247,\n",
      "                -0.502134502,\n",
      "                -0.804780364\n",
      "            ],\n",
      "            [\n",
      "                -0.245055094,\n",
      "                -0.232033446,\n",
      "                -0.941333354\n",
      "            ],\n",
      "            [\n",
      "                -0.226516008,\n",
      "                -0.113739803,\n",
      "                -0.967343688\n",
      "            ],\n",
      "            [\n",
      "                -0.625500023,\n",
      "                -0.777267337,\n",
      "                -0.0678615049\n",
      "            ],\n",
      "            [\n",
      "                -0.99695015,\n",
      "                -0.0206311662,\n",
      "                -0.0752643719\n",
      "            ],\n",
      "            [\n",
      "                -0.350658387,\n",
      "                0.164657205,\n",
      "                -0.921914697\n",
      "            ],\n",
      "            [\n",
      "                -0.824497223,\n",
      "                0.561250746,\n",
      "                -0.0721244439\n",
      "            ],\n",
      "            [\n",
      "                -0.0427294932,\n",
      "                0.991020679,\n",
      "                -0.126697391\n",
      "            ],\n",
      "            [\n",
      "                0.0053204759,\n",
      "                0.167732835,\n",
      "                -0.985818148\n",
      "            ],\n",
      "            [\n",
      "                0.347153664,\n",
      "                0.936899841,\n",
      "                -0.0412678607\n",
      "            ],\n",
      "            [\n",
      "                -0.347153664,\n",
      "                0.936899841,\n",
      "                -0.0412678607\n",
      "            ],\n",
      "            [\n",
      "                -0.0053204759,\n",
      "                0.167732835,\n",
      "                -0.985818148\n",
      "            ],\n",
      "            [\n",
      "                0.0427294932,\n",
      "                0.991020679,\n",
      "                -0.126697391\n",
      "            ],\n",
      "            [\n",
      "                0.824497223,\n",
      "                0.561250746,\n",
      "                -0.0721244439\n",
      "            ],\n",
      "            [\n",
      "                0.350658387,\n",
      "                0.164657205,\n",
      "                -0.921914697\n",
      "            ],\n",
      "            [\n",
      "                0.99695015,\n",
      "                -0.0206311662,\n",
      "                -0.0752643719\n",
      "            ],\n",
      "            [\n",
      "                0.625500023,\n",
      "                -0.777267337,\n",
      "                -0.0678615049\n",
      "            ],\n",
      "            [\n",
      "                0.226516008,\n",
      "                -0.113739803,\n",
      "                -0.967343688\n",
      "            ],\n",
      "            [\n",
      "                0.245055094,\n",
      "                -0.232033446,\n",
      "                -0.941333354\n",
      "            ],\n",
      "            [\n",
      "                -0.316527247,\n",
      "                -0.502134502,\n",
      "                -0.804780364\n",
      "            ],\n",
      "            [\n",
      "                -0.00863834564,\n",
      "                0.21732679,\n",
      "                -0.976060688\n",
      "            ]\n",
      "        ],\n",
      "        \"modelPoints\": [\n",
      "            [\n",
      "                -0.0221471358,\n",
      "                -0.00981918257,\n",
      "                -0.0357203186\n",
      "            ],\n",
      "            [\n",
      "                -0.0193723422,\n",
      "                -0.0190552957,\n",
      "                -0.0333543345\n",
      "            ],\n",
      "            [\n",
      "                -0.0352776982,\n",
      "                -0.0159260165,\n",
      "                -0.0348371901\n",
      "            ],\n",
      "            [\n",
      "                -0.0449875444,\n",
      "                -0.0120951459,\n",
      "                -0.0332690328\n",
      "            ],\n",
      "            [\n",
      "                -0.0573436841,\n",
      "                -0.0198496152,\n",
      "                -0.0180949811\n",
      "            ],\n",
      "            [\n",
      "                -0.0619956963,\n",
      "                -0.00318449084,\n",
      "                -0.0223929957\n",
      "            ],\n",
      "            [\n",
      "                -0.0547361933,\n",
      "                0.00671611866,\n",
      "                -0.0300925747\n",
      "            ],\n",
      "            [\n",
      "                -0.0590967648,\n",
      "                0.0166177917,\n",
      "                -0.0165109243\n",
      "            ],\n",
      "            [\n",
      "                -0.0448669568,\n",
      "                0.0210559499,\n",
      "                -0.0204316359\n",
      "            ],\n",
      "            [\n",
      "                -0.0357390456,\n",
      "                0.0131900487,\n",
      "                -0.0313230641\n",
      "            ],\n",
      "            [\n",
      "                -0.028861437,\n",
      "                0.0191506986,\n",
      "                -0.0236520376\n",
      "            ],\n",
      "            [\n",
      "                0.0294065494,\n",
      "                0.0192690175,\n",
      "                -0.0236086827\n",
      "            ],\n",
      "            [\n",
      "                0.0363578871,\n",
      "                0.0130355135,\n",
      "                -0.0311700795\n",
      "            ],\n",
      "            [\n",
      "                0.0451859608,\n",
      "                0.0211474672,\n",
      "                -0.0199607294\n",
      "            ],\n",
      "            [\n",
      "                0.0591152981,\n",
      "                0.0168669578,\n",
      "                -0.0158297699\n",
      "            ],\n",
      "            [\n",
      "                0.0553800575,\n",
      "                0.0065053124,\n",
      "                -0.029556375\n",
      "            ],\n",
      "            [\n",
      "                0.0620423481,\n",
      "                -0.00281849271,\n",
      "                -0.0214772765\n",
      "            ],\n",
      "            [\n",
      "                0.0572336465,\n",
      "                -0.0198630951,\n",
      "                -0.0174160451\n",
      "            ],\n",
      "            [\n",
      "                0.04510561,\n",
      "                -0.0120098703,\n",
      "                -0.0329381824\n",
      "            ],\n",
      "            [\n",
      "                0.0358598083,\n",
      "                -0.0157476589,\n",
      "                -0.03446557\n",
      "            ],\n",
      "            [\n",
      "                0.0193569027,\n",
      "                -0.0192539971,\n",
      "                -0.0330599099\n",
      "            ],\n",
      "            [\n",
      "                0.021993516,\n",
      "                -0.00973772258,\n",
      "                -0.0355668366\n",
      "            ]\n",
      "        ]\n",
      "    },\n",
      "    \"tracking_to_eye_transform\": [\n",
      "        {\n",
      "            \"distortion\": {\n",
      "                \"coeffs\": [\n",
      "                    -0.22438791,\n",
      "                    -0.07465368,\n",
      "                    0.01471479,\n",
      "                    0.0\n",
      "                ],\n",
      "                \"type\": \"DISTORT_DPOLY3_SCALED\",\n",
      "                \"center_x\": -6.732e-05,\n",
      "                \"center_y\": 0.00906028\n",
      "            },\n",
      "            \"distortion_blue\": {\n",
      "                \"coeffs\": [\n",
      "                    -0.22438791,\n",
      "                    -0.07465368,\n",
      "                    0.01471479,\n",
      "                    0.01\n",
      "                ],\n",
      "                \"type\": \"DISTORT_DPOLY3_SCALED\",\n",
      "                \"center_x\": -6.732e-05,\n",
      "                \"center_y\": 0.00906028\n",
      "            },\n",
      "            \"distortion_red\": {\n",
      "                \"coeffs\": [\n",
      "                    -0.22438791,\n",
      "                    -0.07465368,\n",
      "                    0.01471479,\n",
      "                    -0.006903999805450439\n",
      "                ],\n",
      "                \"type\": \"DISTORT_DPOLY3_SCALED\",\n",
      "                \"center_x\": -6.732e-05,\n",
      "                \"center_y\": 0.00906028\n",
      "            },\n",
      "            \"eye_to_head\": [\n",
      "                [\n",
      "                    0.994178,\n",
      "                    -0.002892,\n",
      "                    0.107714\n",
      "                ],\n",
      "                [\n",
      "                    -0.007674,\n",
      "                    0.995201,\n",
      "                    0.097547\n",
      "                ],\n",
      "                [\n",
      "                    -0.107479,\n",
      "                    -0.097806,\n",
      "                    0.989385\n",
      "                ]\n",
      "            ],\n",
      "            \"grow_for_undistort\": 0.400000006,\n",
      "            \"intrinsics\": [\n",
      "                [\n",
      "                    1.41236968,\n",
      "                    0.0,\n",
      "                    6.732e-05\n",
      "                ],\n",
      "                [\n",
      "                    0.0,\n",
      "                    1.41363935,\n",
      "                    0.00906028\n",
      "                ],\n",
      "                [\n",
      "                    0.0,\n",
      "                    0.0,\n",
      "                    -1.0\n",
      "                ]\n",
      "            ],\n",
      "            \"undistort_r2_cutoff\": 1.0\n",
      "        },\n",
      "        {\n",
      "            \"distortion\": {\n",
      "                \"coeffs\": [\n",
      "                    -0.21911919,\n",
      "                    -0.09861112,\n",
      "                    0.03247762,\n",
      "                    0.0\n",
      "                ],\n",
      "                \"type\": \"DISTORT_DPOLY3_SCALED\",\n",
      "                \"center_x\": 0.00937917,\n",
      "                \"center_y\": 0.01237872\n",
      "            },\n",
      "            \"distortion_blue\": {\n",
      "                \"coeffs\": [\n",
      "                    -0.21911919,\n",
      "                    -0.09861112,\n",
      "                    0.03247762,\n",
      "                    0.01\n",
      "                ],\n",
      "                \"type\": \"DISTORT_DPOLY3_SCALED\",\n",
      "                \"center_x\": 0.00937917,\n",
      "                \"center_y\": 0.01237872\n",
      "            },\n",
      "            \"distortion_red\": {\n",
      "                \"coeffs\": [\n",
      "                    -0.21911919,\n",
      "                    -0.09861112,\n",
      "                    0.03247762,\n",
      "                    -0.006903999805450439\n",
      "                ],\n",
      "                \"type\": \"DISTORT_DPOLY3_SCALED\",\n",
      "                \"center_x\": 0.00937917,\n",
      "                \"center_y\": 0.01237872\n",
      "            },\n",
      "            \"eye_to_head\": [\n",
      "                [\n",
      "                    0.994178,\n",
      "                    0.002888,\n",
      "                    -0.107713\n",
      "                ],\n",
      "                [\n",
      "                    0.007665,\n",
      "                    0.995213,\n",
      "                    0.097433\n",
      "                ],\n",
      "                [\n",
      "                    0.107479,\n",
      "                    -0.097691,\n",
      "                    0.989396\n",
      "                ]\n",
      "            ],\n",
      "            \"grow_for_undistort\": 0.400000006,\n",
      "            \"intrinsics\": [\n",
      "                [\n",
      "                    1.41966885,\n",
      "                    0.0,\n",
      "                    -0.00937917\n",
      "                ],\n",
      "                [\n",
      "                    0.0,\n",
      "                    1.41992974,\n",
      "                    0.01237872\n",
      "                ],\n",
      "                [\n",
      "                    0.0,\n",
      "                    0.0,\n",
      "                    -1.0\n",
      "                ]\n",
      "            ],\n",
      "            \"undistort_r2_cutoff\": 1.0\n",
      "        }\n",
      "    ]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "#C5 \n",
    "try:\n",
    "    # Load the baseline configuration\n",
    "    config_path = r\"C:\\Users\\Bigscreen\\Documents\\GitHub\\big-notebooks\\BS2_TEST_CONFIG.json\"\n",
    "    baseline_config = load_steamvr_json(config_path)\n",
    "\n",
    "    # Update the sn in the configuration\n",
    "    updated_config = set_serial_number(baseline_config, target_serial)\n",
    "\n",
    "    # Log\n",
    "    print(\"Updated Config to Upload:\")\n",
    "    print(json.dumps(updated_config, indent=4))\n",
    "\n",
    "except Exception as e:\n",
    "    print(f\"Error loading or updating config: {e}\")\n",
    "    raise\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Config successfully uploaded to HMD with serial: LHR-2252ED63\n",
      "Config upload verified successfully.\n"
     ]
    }
   ],
   "source": [
    "#C6\n",
    "try:\n",
    "    # Upload\n",
    "    console.upload_config(updated_config)\n",
    "    print(f\"Config successfully uploaded to HMD with serial: {target_serial}\")\n",
    "\n",
    "    # Download and check the config\n",
    "    uploaded_config = console.download_config()\n",
    "    uploaded_serial = uploaded_config.get(\"device_serial_number\", \"unknown\")\n",
    "    if uploaded_serial == target_serial:\n",
    "        print(\"Config upload verified successfully.\")\n",
    "    else:\n",
    "        print(\"Config upload verification failed.\")\n",
    "        print(f\"Expected: {target_serial}, Found: {uploaded_serial}\")\n",
    "\n",
    "except Exception as e:\n",
    "    print(f\"Error uploading or verifying configuration: {e}\")\n",
    "    raise\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Config successfully uploaded.\n",
      "Device serial number: LHR-2252ED63 (confirmed).\n"
     ]
    }
   ],
   "source": [
    "#C7\n",
    "try:\n",
    "    # Verify the updated sn on device\n",
    "    device_config = console.download_config()\n",
    "    device_serial = device_config.get(\"device_serial_number\", \"unknown\")\n",
    "\n",
    "    if device_serial == updated_config.get(\"device_serial_number\"):\n",
    "        print(\"Config successfully uploaded.\")\n",
    "        print(f\"Device serial number: {device_serial} (confirmed).\")\n",
    "    else:\n",
    "        print(\"Config upload failed: Serial number mismatch.\")\n",
    "        print(f\"Expected: {updated_config.get('device_serial_number')}, Found: {device_serial}\")\n",
    "except Exception as e:\n",
    "    print(f\"Error confirming config upload: {e}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
