#!/usr/bin/env python2

#  Gnome15 - Suite of tools for the Logitech G series keyboards and headsets
#  Copyright (C) 2012 Brett Smith <tanktarta@blueyonder.co.uk>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
Utility to test the functioning of libg15
"""

import gnome15.drivers.pylibg15 as libg15
import gnome15.g15devices as g15devices
import sys

def clamp(val, min_val, max_val):
    return max(min_val, min(val, max_val))

print 'libg15 test\n'
print 'Choose a device:'
mono_devices = []
for idx, device in enumerate(g15devices.find_all_devices()):
    if device.bpp == 1:
        mono_devices.append(device)
        print '  %d %-10s - %-40s (%04x %04x)' % (idx, device.uid, device.model_fullname, device.usb_id[0],device.usb_id[1] )
        
device_idx = int(raw_input('\nDevice:'))
if device_idx < 0 or device_idx >= len(mono_devices):
    sys.stderr.write('Invalid device number\n')
    sys.exit(1)
device = mono_devices[device_idx]
    
print "WARNING: Reset sometimes doesn't work, try without it first"
reset_usb = raw_input('Reset USB Y/(N):').lower()[:1] == 'y'

libg15.set_debug(libg15.G15_LOG_INFO)
libg15.init(reset_usb, device.usb_id[0], device.usb_id[1])

print "Intialised\n"
print "WARNING: Some operations may not be appropriate for your hardware. "
print "Do try to use options your device does not have. This script is a"
print "bit dumb and won't try to stop you."

while True:
    print
    print "1 - Set the keyboard backlight brightness (G15,G11)"
    print "2 - Set the LCD brightness (G15,Z10?)"
    print "3 - Set the LCD contrast (G15,Z10?)"
    print "4 - Set the keyboard backlight color (G13,G510)"
    print "5 - Set the M-Key LEDs"
    print "6 - Test extra keys"
    print "0 - Exit"
    option = int(raw_input('\nDevice:'))
    if option == 0:
        break
    elif option == 1:
        libg15.set_keyboard_brightness(clamp(int(raw_input('Backlight brightness (0-2):')), 0, 2))
    elif option == 2:
        libg15.set_lcd_brightness(clamp(int(raw_input('LCD brightness (0-2):')), 0, 2))
    elif option == 3:
        libg15.set_contrast(clamp(int(raw_input('LCD contrast (0-2):')), 0, 2))
    elif option == 4:
        libg15.set_keyboard_color((clamp(int(raw_input('Red (0-255):')), 0, 255), \
                                  clamp(int(raw_input('Green (0-255):')), 0, 255), \
                                  clamp(int(raw_input('Blue (0-255):')), 0, 255)))
    elif option == 5:
        libg15.set_leds(clamp(int(raw_input('LED mask (0-15):')), 0, 15))
    elif option == 6:
        print "******************************************************************"
        print "* Now testing keys, abort with Ctrl+\\"
        print "******************************************************************"
        def callback(code, extended_code):
            print "%04x/%04x - %08d/%08d"% (code, extended_code, code, extended_code)
        libg15.grab_keyboard(callback).join()
        