#!/usr/bin/python

import Touch_1inch28
import os
import sys
import time

# pin config
TP_INT = 4

# the touchscreen object
touch = Touch_1inch28.Touch_1inch28()

# the interrupt function
# WARN: may not work, because the touch object doesn't have the Gestures item,
#  may be replaced with the global variable
def int_callback(TP_INT):
 touch.Gestures = touch.Touch_Read_Byte(0x01)

# the main part
try:
 # init the touch
 touch.init()
 touch.int_irq(TP_INT, int_callback)

 # set mode to Gestures
 mode = 0
 touch.Set_Mode(mode)

 # the loop
 while True:
  if (touch.Gestures == 0x01):
   touch.Gestures = 0x00
   os.system("echo UP")
   sleep(0.5)
  if (touch.Gestures == 0x02):
   touch.Gestures = 0x00
   os.system("echo DOWN")
   sleep(0.5)
  if (touch.Gestures == 0x03):
   touch.Gestures = 0x00
   os.system("echo LEFT")
   sleep(0.5)
  if (touch.Gestures == 0x04):
   touch.Gestures = 0x00
   os.system("echo RIGHT")
   sleep(0.5)
  if (touch.Gestures == 0x0C):
   touch.Gestures = 0x00
   os.system("echo LONG PRESS")
   sleep(0.5)
  if (touch.Gestures == 0x0B):
   touch.Gestures = 0x00
   os.system("echo DOUBLE CLICK")
   sleep(0.5)

except IOError as e:
 print(e)
except KeyboardInterrupt:
 print("STOP")
 exit()
