Something went wrong. Try again.
python api for controlling an arctis-nova-pro device
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170import numpyimport hidimport timefrom PIL import Image,ImageDraw
VID=0x1038PID=0x12e0
class ArctisNova: def __init__(self,input_path=None, output_path=None): if (not input_path == None) or (not output_path == None): print(input_path) print(output_Path) assert not input_path == None, "must set both input_path and output_path" assert not output_path == None, "must set both input_path and output_path" self.input_path = input_path self.output_path = output_path else: for dev in hid.enumerate(0x1038,0x12e0): if dev['interface_number']==4: self.input_path = dev['path'] if dev['interface_number']==3: self.output_path = dev['path'] self._dev_input = hid.device() self._dev_output = hid.device() self._open_count = 0 self.game_volume = -1 self.chat_volume = -1 self.bt_default = -1 self.bt_auto_mute = -1 self.bt_power = -1 self.bt_connection = -1 self.headset_battery = -1 self.charge_battery = -1 self.noise_cancel_level = -1 self.muted = -1 self.noise_cancel = -1 self.mic_led = -1 self.auto_off = -1 self.wireless_mode = -1 self.pairing = -1 self.headset_status = -1
def __enter__(self): if self._open_count < 1: self._dev_input.open_path(self.input_path) self._dev_output.open_path(self.output_path) self._open_count += 1
def __exit__(self, exception_type, exception_value, exception_traceback): self._open_count -= 1 if self._open_count < 1: self._dev_input.close() self._dev_output.close()
def drawImage(self,img): assert self._open_count > 0, "Device is not open" #Image is a 1-bit color image. imgBytes = img.tobytes() npPack = numpy.frombuffer(imgBytes,dtype=numpy.uint8) #because image is 1-bit color, this breaks the uint8 entries into individual bits to work with npPixels = numpy.unpackbits(npPack) #The next two lines handle swapping the x and y axis of the bits, since the display receives pixels by columns npPixelRows = numpy.split(npPixels,64) npMergedColumns = numpy.stack(npPixelRows,1) #Pillow returns images in big-eiden format, but our display reads in little-eiden format. npFixedPack = numpy.packbits(npMergedColumns,bitorder='little') imgFixedBytes = npFixedPack.tobytes() #Split the bytes into 2 arrays, exactly in half, so we get 2 arrays of 64x64 pixels (4096 bits) #This is because the HID message maxes out at 1024 bytes, and 6 of our bytes are eaten up by the magic numbers needed below a = list(imgFixedBytes)[:len(imgFixedBytes)//2] b = list(imgFixedBytes)[len(imgFixedBytes)//2:] #0x06 is the data direction... (I think? it's HID weirdness) #0x93 is the ... command code? for writing to the display... I think... again. #0,0 is the x,y to start drawing to the screen. #64,64 is the width and height of the block of pixels being sent message1 = tuple([0x06,0x93,0,0,64,64]+a) message2 = tuple([0x06,0x93,64,0,64,64]+b) self._dev_input.send_feature_report(message1) self._dev_input.send_feature_report(message2)
def chat_mixer(self, enable=True): assert self._open_count > 0, "Device is not open" enable_var = 0x00 if enable: enable_var = 0x01 msg = bytes([0x06,0x49,enable_var]) self._dev_input.write(msg)
def updateData(self): assert self._open_count > 0, "Device is not open" msg = bytes([0x06,0xb0]).ljust(64,b"0") self._dev_input.write(msg)
def attenuate(self,amount): assert self._open_count > 0, "Device is not open" msg = bytes([0x06,0x37,amount]) self._dev_input.write(msg)
def sidetone(self,amount): assert self._open_count > 0, "Device is not open" assert 0 <= amount < 11, "Invalid sidetone value" msg = bytes([0x06,0x39,amount]) self._dev_input.write(msg)
def readData(self): assert self._open_count > 0, "Device is not open" data = self._dev_input.read(16,1) if len(data)==0: return None if (data[0] == 0x06): if (data[1] == 0xb0): self.bt_default = data[2] self.bt_auto_mute = data[3] self.bt_power = data[4] self.bt_connection = data[5] self.headset_battery = data[6] self.charge_battery = data[7] self.noise_cancel_level = data[8] self.muted = data[9] self.noise_cancel = data[10] self.mic_led = data[11] self.auto_off = data[12] self.wireless_mode = data[13] self.pairing = data[14] self.headset_status = data[15] if (data[0] == 0x07): if (data[1] == 0x25): self.volume = data[2] #self._flags["volume"] == True; if (data[1] == 0x49): self.chat_volume = data[2] self.game_volume = data[3] if (data[1] == 0xb7): self.headset_battery = data[2] self.charge_battery = data[3] self.headset_status = data[4] if (data[1] == 0xb5): self.bt_power = data[2] self.bt_connection = data[2] if (data[1] == 0xbd): self.noise_cancel = data[2] if (data[1] == 0xbb): self.muted = data[2] if (data[1] == 0x37): self.mic_led = data[2] if (data[1] == 0xb2): self.bt_default = data[2] if (data[1] == 0xb3): self.bt_auto_mute = data[2] return data
if __name__=="__main__": headphones = ArctisNova() im = Image.new("1",(128,64)) draw = ImageDraw.Draw(im) with headphones: headphones.drawImage(im) while True: msg = bytes([0x06,0xb0]).ljust(64,b"0") devInput.write(msg) data = devInput.read(16,1) if(len(data)>0): if(data[0]== 0x07 and data[1] == 0x25): print("Volume Change") print(data[3]) elif(data[0] == 0x06 and data[1] == 0xb0): print("Bluetooth Powerup Status: {0}".format(data[2])) print("Auto Mute: {0}".format(data[3]))