#!/usr/bin/python

# change the FILE= to point to the file you want mplayer to play
# change the very last line to change the command


# This work is licensed under the New BSD License
#
#Copyright (c) , Ross Bevington
#
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#    * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
#CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# start PyPanic 

import usb
import time
import os

DEVICEID = 0x1130
PRODUCTID = 0x0202
FILE = "/home/acid2000/Desktop/air_raid_siren.wav"

device = 0;

# find device
busses = usb.busses()

for bus in busses:
	devices = bus.devices
	for dev in devices:
		#print dev.idVendor, dev.idProduct
		if dev.idVendor==DEVICEID and dev.idProduct==PRODUCTID:
			device = dev
			conf = dev.configurations[0]
            		intf = conf.interfaces[0][0]
if device == 0:
	print "Could not find device"
	exit()

# device find, now open it and claim

interface = device.open()
interface.reset()
interface.detachKernelDriver(0)
interface.detachKernelDriver(1)
interface.setConfiguration(conf)
interface.claimInterface(intf)

bufferA = (0x55, 0x53, 0x42, 0x43,  0,  8,  1,  0)
bufferB = 0x08
while (True):
  time.sleep(0.2)
  interface.controlMsg(0x21, 0x09, bufferA, value=0x200, index=0x001)
  result = interface.controlMsg(0xa1, 0x01, bufferB, value=0x300, index=0)
  if result[0] == 0x1:
	os.spawnvp(os.P_WAIT,"/usr/bin/mplayer",["", FILE])
