#!/usr/bin/python
# -*- coding: latin-1 -*-

__author__ = "Jérôme Sautret <jerome @ sautret.org>"
__version__ = "1.0"
__date__ = "2008/08/16"
__license__ = "GPL v3"


"""
(c) 2007 Jérôme Sautret <jerome @ sautret.org>
Licensed under the GNU General Public License, version 3.
See the file http://www.gnu.org/licenses/gpl-3.0.txt

Sample script to bound functions to the remote controller.

"""

import time, sys
sys.path.append('/opt/tuxdroid/api/python')
from tux import *

##########################################################################
# Global settings

tux.tts.select_voice(SPK_FR_MALE, 100)

##########################################################################
# Keys binding definition

KEYS_BINDING = {
    'clock':                 K_STARTVOIP,
    'tux.cmd.eyes_on':       K_RED,
    'tux.cmd.wings_on':      K_GREEN,
    'leds':                  K_BLUE,
    'tux.cmd.mouth_on':      K_YELLOW,
    'tux.cmd.spinl_on_free': K_PREVIOUS,
    'tux.cmd.spinr_on_free': K_NEXT,
    'spinl':                 K_LEFT,
    'spinr':                 K_RIGHT,
    }

##########################################################################
# Functions bound to keys

def leds():
    if tux.status.lled():
        tux.cmd.leds_off()
    else:
        tux.cmd.leds_on()

def clock():
    # deactivate the key
    function = sys._getframe().f_code.co_name
    key = KEYS_BINDING[function]
    tux.event.set_on_remote_bt(key, None)
    # say time
    tux.tts.speak("Il est %s."% time.strftime("%H:%M:%S"))
    # activate the key
    tux.event.set_on_remote_bt(key, eval(function))

def spinl():
    function = sys._getframe().f_code.co_name
    key = KEYS_BINDING[function]
    tux.event.set_on_remote_bt(key, None)
    tux.cmd.spinl_on(1,1)
    tux.event.set_on_remote_bt(key, eval(function))
def spinr():
    function = sys._getframe().f_code.co_name
    key = KEYS_BINDING[function]
    tux.event.set_on_remote_bt(key, None)
    tux.cmd.spinr_on(1,1)
    tux.event.set_on_remote_bt(key, eval(function))

##########################################################################
# Main Program

def bind_keys():
    for binding in KEYS_BINDING.items():
        tux.event.set_on_remote_bt(binding[1], eval(binding[0]))

def main():
    tux.tts.on_sound_on=tux.cmd.mouth_open
    tux.tts.on_sound_off=tux.cmd.mouth_close
    bind_keys()
    while True:
        tux.sys.wait(99999)

if __name__ == "__main__":
    main()
