#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import pygame
from os.path import dirname, abspath
sys.path.append(dirname(dirname(dirname(dirname(abspath(__file__))))))
reload(sys)
sys.setdefaultencoding('utf-8')

import rospy
from std_msgs.msg import String

from EmeraldAI.Logic.Modules import Pid
from EmeraldAI.Config.Config import *
from EmeraldAI.Logic.GPIO.GPIOProxy import GPIOProxy


class HardwareTrigger:
    def __init__(self, gpioTiggerName="GPIO"):
        rospy.init_node('emerald_trigger_node', anonymous=True)
        rospy.Rate(10) # 10hz

        pygame.init()
        
        self.__GPIOInputChannel = Config().GetInt("Trigger", "GPIOPin")
        self.__GPIOTriggerName = gpioTiggerName

        self.__Publisher = rospy.Publisher('/emerald_ai/io/hardware_trigger', String, queue_size=5)

        GPIO = GPIOProxy(None, (self.__GPIOInputChannel))
        GPIO.add_event_detect(self.__GPIOInputChannel, GPIO.RISING, self.GPIOTrigger, 100)

    
        # TODO: Keyboard Trigger
        # TODO: STT Keyword Trigger - snowboy

        print "Keypress..."
        while 1:
            """
            if(pygame.key.get_pressed()[pygame.K_ESCAPE]):
                print "Up pressed"
            pygame.event.pump()
            """
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    print "Quit"

        #rospy.spin()


    def GPIOTrigger(callback):
        triggerData = "TRIGGER|{0}|{1}".format(self.__GPIOTriggerName, rospy.Time.now().to_sec())
        rospy.loginfo(triggerData)
        self.__Publisher.publish(triggerData)



if __name__ == "__main__":
    if(Pid.HasPid("Trigger")):
        print "Process is already runnung. Bye!"
        sys.exit()
    Pid.Create("Trigger")
    try:

        tmpTriggername = None
        if len(sys.argv) > 1:
            for arg in sys.argv:
                if (arg.lower().startswith("-name")):
                    tmpTriggername = int(arg.lower().replace("-name", ""))

        if tmpTriggername is None:
            HardwareTrigger()
        else:
            HardwareTrigger(tmpTriggername)
    
    except KeyboardInterrupt:
        print "End"
    finally:
        Pid.Remove("Trigger")

@@ -575,24 +575,68 @@
 game.init()%0A
+        pygame.display.set_mode((200,200))%0A%0A
         %0A   
@@ -1008,154 +1008,240 @@
 0)%0A%0A
+%0A
     
-%0A        # TODO: Keyboard Trigger%0A        # TODO: STT Keyword Trigger - snowboy%0A%0A        print %22Keypress...%22%0A        while 1:%0A
+    while 1:%0A            for event in pygame.event.get():%0A                #if event.type == pygame.QUIT: sys.exit()%0A                #if event.type == pygame.KEYDOWN and event.dict%5B'key'%5D == 27: sys.exit()%0A         
        
+%0A
      
-%22%22%22%0A
     
@@ -1251,54 +1251,39 @@
     
- 
 if
-(pygame.key.get_pressed()%5Bpygame.K_ESCAPE%5D)
+ event.type == pygame.KEYDOWN
 :%0A  
@@ -1300,120 +1300,391 @@
     
+    #
 print 
-%22Up pressed%22%0A            pygame.event.pump()%0A            %22%22%22%0A            for eve
+event.key, event.unicode%0A%0A                    # Enter%0A                    if event.key == 13:%0A                        self.SendTrigger(%22KEY%22, %22ENTER%22)%0A                        co
 nt
- 
 in
- pygame.event.get():%0A
+ue%0A%0A                    # Space%0A                    if event.key == 32:%0A                        self.SendTrigger(%22KEY%22, %22SPACE%22)%0A                        continue%0A%0A                    # Esc%0A  
     
@@ -1687,32 +1687,34 @@
                 
+  
 if event.type ==
@@ -1706,35 +1706,25 @@
 f event.
-type == pygame.QUIT
+key == 27
 :%0A      
@@ -1741,42 +1741,130 @@
     
-print %22Quit%22%0A%0A        #rospy.spin(
+    self.SendTrigger(%22KEY%22, %22ESC%22)%0A                        continue%0A%0A                    self.SendTrigger(%22KEY%22, event.key
 )%0A%0A%0A
@@ -1875,20 +1875,20 @@
 def 
-GPIO
+Send
 Trigger(
 call
@@ -1887,16 +1887,27 @@
 ger(
-callback
+self, name, pressId
 ):%0A 
@@ -1943,16 +1943,20 @@
 %7C%7B0%7D%7C%7B1%7D
+%7C%7B2%7D
 %22.format
@@ -1956,38 +1956,29 @@
 .format(
-self.__GPIOTriggerName
+name, pressId
 , rospy.
@@ -2081,16 +2081,123 @@
 Data)%0A%0A%0A
+    def GPIOTrigger(callback):%0A        self.SendTrigger(self.__GPIOTriggerName, self.__GPIOInputChannel)%0A%0A%0A
 %0Aif __na

Update HardwareTrigger.py