Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
main.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # coding: utf-8
3 
4 """
5  Package to the interface between the Bike-X and the
6  MSP430.
7 """
8 
9 import util
10 import sys
11 from msp430 import MSP
12 import device as sensor
13 from signal import signal, SIGINT, SIGALRM, SIGABRT, SIGQUIT
14 from startx import SIG1,SIG2,SIG3,ACTIVE_FILE, PASSIVE_FILE
15 from time import sleep
16 from os import getpid,kill
17 from mock import MagicMock
18 
19 # Bike-X PID
20 pid_bikex =0
21 # MSP430 object
22 msp430 = object()
23 
24 def safe_quit(signum, frame):
25  """ Care for a safe quit """
26  print "Exiting"
27  msp430.desable()
28  exit()
29 
30 def write_file(signum, frame):
31  """ was requisited new data from sensor"""
32  f = open(PASSIVE_FILE,'w')
33  print "requisited new data from sensor"
34  #f.write(msp430.read_data())
35  f.write(msp430['passives'])
36  f.close()
37  try:
38  # Without a real pid, could be dangerous
39  # kill(pid_bikex,SIG2)
40  pass
41  except Exception, e:
42  print 'Father not available'
43 
44 def read_file(signum, frame):
45  """ read file and send to mcu """
46  f = open(ACTIVE_FILE,'a+r')
47  data = f.readline()
48  print data,
49  msp430['curb'] = data
50  f.close()
51 
52 def main():
53  pid_bikex = sys.argv[1:2]
54  print "Father: %s" % pid_bikex
55  print "Mine: %d " % getpid()
56  print SIG1,SIG2,SIG3
57  msp430.curb = sensor.Break(msp430.serial,0)
58  msp430.passives = sensor.Passives(msp430.serial,0)
59 
60  signal(SIG1, write_file)
61  signal(SIG3, read_file)
62  signal(SIGINT, safe_quit)
63  signal(SIGQUIT, safe_quit)
64  signal(SIGABRT, safe_quit)
65  while True:
66  sleep(0.01)
67 
68 if __name__ == '__main__':
69  # list available ports
70  PORTS_AVAILABLE = util.available_ports()
71  try:
72  print 'Available ports:'
73  for i in PORTS_AVAILABLE:
74  print '>>> %s' % i
75  print '---'
76  # choose a port
77  if len(PORTS_AVAILABLE) > 0:
78  msp430 = MSP(PORTS_AVAILABLE[0])
79  except TypeError, error:
80  print "None device connected - test mode"
81  msp430 = MagicMock()
82  msp430.__getitem__ = MagicMock(return_value='Test Message\n')
83 
84  print 'Ready'
85  main()