from __future__ import print_function
import sys
import math
import os
from datetime import datetime
import time

def read_signal():
    # This pin is also referred to as GPIO18
    INPUT_WIRE = 12

    env = os.environ['APP_ENV']
    t_end = time.time() + 60 / 30

    if env != 'development':
        import RPi.GPIO as GPIO
    else:
        print('%s - Waiting - %s' % (time.time(), t_end), file=sys.stderr)
        while time.time() < t_end:
            # print('ooooooo', file=sys.stderr)
            pass

        return False    

    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(INPUT_WIRE, GPIO.IN)


    # while time.time() < t_end:
    while time.time() < t_end:
        print('--- start ---', file=sys.stderr)
        value = 1
        # Loop until we read a 0
        while value:
            value = GPIO.input(INPUT_WIRE)

        print('--- signal ---', file=sys.stderr)
        # Grab the start time of the command
        startTime = datetime.now()

        # Used to buffer the command pulses
        command = []

        # The end of the "command" happens when we read more than
        # a certain number of 1s (1 is off for my IR receiver)
        numOnes = 0

        # Used to keep track of transitions from 1 to 0
        previousVal = 0

        while True:
            print('--- loop 2 ---', file=sys.stderr)
            if value != previousVal:
                # The value has changed, so calculate the length of this run
                now = datetime.now()
                pulseLength = now - startTime
                startTime = now

                command.append((previousVal, pulseLength.microseconds))

            if value:
                numOnes = numOnes + 1
            else:
                numOnes = 0

            # 10000 is arbitrary, adjust as necessary
            if numOnes > 10000:
                break

            previousVal = value
            value = GPIO.input(INPUT_WIRE)
        
            result = []

            for (val, pulse) in command:
                print(val, file=sys.stderr)
                print(pulse, file=sys.stderr)
                # print val, pulse

            for (val, pulse) in command:
                result.append(str(pulse))

            text = '-'.join(result)

            print(text, file=sys.stderr)
            print(result, file=sys.stderr)

            with open("ir_tmp_code.txt", "w") as text_file:
                text_file.write(text)

            return True

    return False

@@ -602,18 +602,16 @@
 N)%0A%0A%0A   
- #
  while t
@@ -637,34 +637,21 @@
 %0A   
+ #
  while 
-time.time() %3C t_end
+True
 :%0A  
@@ -1919,28 +1919,24 @@
    %0A        
-    
 result = %5B%5D%0A
@@ -1936,35 +1936,33 @@
 t = %5B%5D%0A%0A        
-   
+#
  for (val, pulse
@@ -1975,35 +1975,33 @@
 ommand:%0A        
-  
- 
+#
      print(val, 
@@ -2021,27 +2021,25 @@
 rr)%0A        
- 
-  
+#
      print(p
@@ -2065,27 +2065,25 @@
 rr)%0A        
-   
+#
      # print
@@ -2103,20 +2103,16 @@
         
-    
 for (val
@@ -2140,28 +2140,24 @@
             
-  
-  
 result.appen
@@ -2175,28 +2175,24 @@
 ))%0A%0A        
-    
 text = '-'.j
@@ -2208,28 +2208,24 @@
 t)%0A%0A        
- 
-   
 print(text, 
@@ -2245,28 +2245,24 @@
 rr)%0A        
-   
- 
 print(result
@@ -2281,20 +2281,16 @@
 tderr)%0A%0A
-    
         
@@ -2345,28 +2345,24 @@
             
-    
 text_file.wr
@@ -2368,28 +2368,24 @@
 rite(text)%0A%0A
-    
         retu

test 5