Download the file
  1. #!/usr/bin/env python
  2.  
  3. import serial, os, time, sys
  4.  
  5. TIME_ADJUSTMENT_FACTOR = 0.9984
  6.  
  7. def set_timer(val = 2):
  8. port = os.path.join(os.environ['HOME'], 'dev/leffakone_timer')
  9. ser = serial.Serial(port, 57600, timeout=1.1)
  10. ser.setDTR(False) # Prevent resetting the arduino when closing the serial port
  11. time.sleep(2)
  12. val_s = "%d" % (val - 2)
  13. ser.write(val_s + "\n")
  14. echo = ser.readline().strip()
  15. ser.close()
  16. if echo != val_s:
  17. print "Error: sent data '%s' differs from echoed value '%s'" % (val_s, echo)
  18. sys.exit(1)
  19.  
  20. if __name__ == "__main__":
  21. if len(sys.argv) == 2:
  22. wakeup_time = time.mktime(time.strptime(sys.argv[1], "%Y-%m-%d %H:%M"))
  23. now = time.mktime(time.localtime())
  24. timeout_delay = int((wakeup_time - now) * TIME_ADJUSTMENT_FACTOR)
  25. if timeout_delay > 300:
  26. set_timer(timeout_delay)
  27. else:
  28. print "Error: timeout delay is less than 5 minutes (%d)" % timeout_delay
  29. elif len(sys.argv) == 1:
  30. set_timer()
  31. else:
  32. print "Usage: %s [%Y-%m-%d %H:%M]"
  33. sys.exit(1)
  34.