Skip to content

Commit

Permalink
Merge pull request #1 from nouser2013/nouser2013-prepare-v1.2
Browse files Browse the repository at this point in the history
Nouser2013 prepare v1.2
  • Loading branch information
nouser2013 authored May 6, 2020
2 parents 087ac55 + 768e20a commit d338e37
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
# Manager for V2 Lighthouses by Valve/HTC
This python script helps you switch your Valve Lighthouses V2 on and into stand-by. Unfortunately, my Pimax 5K XR needs the lighthouses up and running before I turn it on, so the awesome work done by [@mann1x]( https://github.com/mann1x ) in his project [Pimax BS Manager](https://github.com/mann1x/pimax_bs_manager) is of little use to me. This may be different with your particular Pimax HMD.

**Note:** In the meantime, there are binary releases for Windows [here](https://github.com/nouser2013/lighthouse-v2-manager/releases).
## installation (Windows binary release)
1. Create a folder where you want to store the manager's files, e.g. `C:\Program Files\LH-Manager`.
1. Download and copy the files (executable and both icons) from [this repository's releases page](https://github.com/nouser2013/lighthouse-v2-manager/releases) into the target folder from step 1. Depending on the destination, administrative privileges may be required for copying.
1. Make sure your Bluetooth Low-Energy dongle is connected.
1. Open a command prompt and navigate to the folder from step 1, e.g. `C:\Program Files\LH-Manager`. From there, follow the instructions in the chapter "Usage".

## installation & prerequisites
## installation & prerequisites (python script version)
Make sure that you have the following:
* Windows 10, at least 17xx build
* BLE / Bluetooth 4.0 dongle installed and connected (*not* a BGAPI one!)
* Python 3, I used Python 3.8.2
* Pythonnet installation from [here](https://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet)
1. download `pythonnet‑2.4.1.dev0‑cp38‑cp38‑win_amd64.whl`
2. `pip3 install pythonnet‑2.4.1.dev0‑cp38‑cp38‑win_amd64.whl`
* bleak installed `pip3 install bleak`
* bleak installed `pip3 install bleak`

## usage with command line arguments
The script provides three usage options: discovery, turning on and switch to standby of a lighthouse V2.

**Note:** if you installed the binary version, simply call the program by executing it. Instead of `python3 .\lighthouse-v2-manager.py` type `lighthouse-v2-manager.exe` in the command prompt window for the commands below.

### display instructions
If you call the executable/script with no command line arguments or with an invalid command, the useage instructions are returned. Choose one of the commands `discover`, `on` or `off` to interact with your Lighthouses V2.

**usage:** `python3 .\lighthouse-v2-manager.py`

### discovery
If you don't call it with any arguments, it tries to open your BLE device and scans for BLE servers in range. Once found, it looks for the service and characteristic which allow for the power-up and power-down of a lighthouse V2. Look for the MAC addresses and the results on stdout.
If you call the program with the `discover` command, it tries to open your BLE device and scans for BLE servers in range. Once found, it looks for the service and characteristic which allow for the power-up and power-down of a lighthouse V2. Look for the MAC addresses and the results on the console output.

Optionally, since version 1.1, you can specify the command line option `-cs` or `--create-shortcuts` with the discovery mode. The script then tries to create suitable shortcuts for your installation and your Lighthouses' MAC addresses. This works with both the script version and the binary stand-alone version.
Optionally, since version 1.1, you can specify the command line option `-cs` or `--create-shortcuts` with the discovery command. The program then tries to create suitable shortcuts for your installation and your Lighthouses' MAC addresses. This works with both the script version and the binary stand-alone version.

**usage:** `python3 .\lighthouse-v2-manager.py [-cs,--create-shortcuts]`
**usage:** `python3 .\lighthouse-v2-manager.py discover [-cs,--create-shortcuts]`

### switch lighthouses into standby
If you want to switch a lighthouse off ("stand-by"), specify either "off" as first argument and then each MAC address consecutively as further arguments like so:
Expand Down
62 changes: 35 additions & 27 deletions lighthouse-v2-manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__PWR_ON = bytearray([0x01])
__PWR_STANDBY = bytearray([0x00])

command = "discover"
command = ""
lh_macs = [] # hard code mac addresses here if you want, otherwise specify in command line

print(" ")
Expand All @@ -23,33 +23,19 @@
if cmdStr.find(".py")>0:
cmdStr = '"'+ sys.executable +'" "' + cmdPath + '"'

if len(sys.argv)==1 or sys.argv[1] in ["--create-shortcuts","-cs"]:
print(">> MODE: discover suitable V2 lighthouses")
print(" ")
elif sys.argv[1] in ["on","off"]:
if len(sys.argv)>1 and sys.argv[1] in ["on", "off", "discover"]:
command = sys.argv[1]
print(">> MODE: switch lighthouses "+ command.upper())
lh_macs.extend(sys.argv[2:])
for mac in list(lh_macs):
if re.match("[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}", mac):
continue
print(" * Invalid MAC address format: "+mac)
lh_macs.remove(mac)
if len(lh_macs) == 0:
print(" ")
print(">> ERROR: no (valid) base station MAC addresses given.")
sys.exit()
for mac in lh_macs:
print(" * "+mac)

if len(sys.argv)==1 or command=="":
print(" Invalid or no command given. Usage:")
print(" ")
else:
print(" discover lighthouses V2:")
print(" "+cmdStr+" [--create-shortcuts, -cs]")
print(" * discover lighthouses V2:")
print(" "+cmdStr+" discover [--create-shortcuts, -cs]")
print(" ")
print(" power one or more lighthoses V2 ON:")
print(" * power one or more lighthoses V2 ON:")
print(" "+cmdStr +" on [MAC1] [MAC2] [...MACn]")
print(" ")
print(" power one or more lighthoses V2 OFF:")
print(" * power one or more lighthoses V2 OFF:")
print(" "+cmdStr +" off [MAC1] [MAC2] [...MACn]")
print(" ")
sys.exit()
Expand All @@ -59,6 +45,10 @@
async def run(loop, lh_macs):
if command == "discover":
lh_macs = []
createShortcuts = True if ("-cs" in sys.argv or "--create-shortcuts" in sys.argv) else False
print(">> MODE: discover suitable V2 lighthouses")
if createShortcuts: print(" and create desktop shortcuts")
print(" ")
print (">> Discovering BLE devices...")
devices = await discover()
for d in devices:
Expand Down Expand Up @@ -88,9 +78,9 @@ async def run(loop, lh_macs):
print(">> This is likely NOT a suitable Lighthouse V2.")
print(" ")
if len(lh_macs)>0:
print(">> At least one compatible V2 lighthouse was found.")
print(">> OK: At least one compatible V2 lighthouse was found.")
print(" ")
if "--create-shortcuts" in sys.argv or "-cs" in sys.argv:
if createShortcuts:
print(">> Trying to create Desktop Shortcuts...")
import winshell
from win32com.client import Dispatch
Expand Down Expand Up @@ -122,7 +112,7 @@ async def run(loop, lh_macs):
shortcut.save()
print(" * OK: LHv2-OFF.lnk was created successfully.")
else:
print(">> OK, you need to manually create two links, for example on your desktop:")
print(" OK, you need to manually create two links, for example on your desktop:")
print(" ")
print(" To turn your lighthouses ON:")
print(" * Link Target: "+ cmdStr +" on "+ " ".join(lh_macs))
Expand All @@ -131,7 +121,24 @@ async def run(loop, lh_macs):
print(" * Link Target: "+ cmdStr +" off "+ " ".join(lh_macs))
else:
print(">> Sorry, not suitable V2 Lighthouses found.")
else:
print(" ")

if command in ["on", "off"]:
print(">> MODE: switch lighthouses "+ command.upper())
lh_macs.extend(sys.argv[2:])
for mac in list(lh_macs):
if re.match("[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}", mac):
continue
print(" * Invalid MAC address format: "+mac)
lh_macs.remove(mac)
if len(lh_macs) == 0:
print(" ")
print(">> ERROR: no (valid) base station MAC addresses given.")
print(" ")
sys.exit()
for mac in lh_macs:
print(" * "+mac)
print(" ")
for mac in lh_macs:
print(">> Trying to connect to BLE MAC '"+ mac +"'...")
try:
Expand All @@ -145,5 +152,6 @@ async def run(loop, lh_macs):
except Exception as e:
print(">> ERROR: "+ str(e))
print(" ")

loop = asyncio.get_event_loop()
loop.run_until_complete(run(loop, lh_macs))

0 comments on commit d338e37

Please sign in to comment.