#!/usr/bin/python
import hid
import time
from time import sleep
import os, sys

def init():
    global h
    print "Opening device"
    h = hid.device()
    h.open(0x10C4, 0xEA90)

    print "Manufacturer: %s" % h.get_manufacturer_string()
    print "Product: %s" % h.get_product_string()
    print "Serial No: %s" % h.get_serial_number_string()

    h.write([0x02, 0b00000110, 0x00, 0x00, 0x00]) # setup io pin direction 
    sleep( 1.00 )


def light(timea):
    h.write([0x04, 0xFF, 0xFF])
    time.sleep(timea)

def dark(timea):
    h.write([0x04, 0x00, 0xFF])
    time.sleep(timea)

def blik():
    dark(0.8)
    light(1)

def check_button():
    response = h.get_feature_report(0x03,2)
    return response[1]

def main():
    init()
    while True:
        try:

            while True:
                inputs = check_button()
                print bin(inputs) 
                if not (inputs & 0b00000001) and  (0b00000001):
                    blik()

                time.sleep(0.5)

        except IOError, ex:
            print ex

        except KeyboardInterrupt:
            print "Closing device"
            h.close()

        print "Done"


if __name__ == "__main__":
    main()