Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
msp430.py
Go to the documentation of this file.
1 # coding: utf-8
2 
3 """
4  MSP module
5 
6  A Interface with the hardware
7 """
8 try:
9  # Terminal Class
10  from serial.tools.miniterm import Miniterm
11  from serial.serialutil import SerialException
12 except Exception as error:
13  try:
14  import serial
15  except ImportError as error:
16  print 'Install Pyserial'
17  raise error
18  if float(serial.VERSION) < 2.6:
19  print 'Upgrade Pyserial'
20  raise error
21 
22 import device as sensor
23 from mock import MagicMock
24 import util
25 
26 
27 class MSP(Miniterm):
28 
29  """docstring for MSP
30 
31  Done with the ideia that all the interface happens
32  by UART
33 
34  """
35  port = 0
36  alive = False
37  modules={}
38 
39  def __init__(self, tty, baud=9600):
40  if tty is not []:
41  try:
42  super(MSP, self).__init__(tty, baud, 'N',
43  False, False)
44  except SerialException:
45  print util.ROOT_MESSAGE
46  exit(-1)
47  self.serial.setTimeout(1)
48  else:
49  """FIXME: remove the code bellow in
50  production environment"""
51  self.serial = MagicMock()
52  self.serial.readline = util.randomstring
53  # raise Exception
54  self.port = tty
55  self.enable()
56 
57  def __getitem__(self,key):
58  """ Return the value of a item """
59  try:
60  return getattr(self,key).read_data()
61  except TypeError:
62  return getattr(self,key).read_data('t')
63  except IndexError:
64  return getattr(self,key).data
65  except Exception, e:
66  raise e
67 
68  def __setitem__(self,key,item):
69  """ Set a value of a item """
70  # getattr(self,key).write_data('r',str(item))
71  getattr(self,key).write_data(str(item))
72 
73  def enable(self):
74  """ Method to enable the micro """
75  self.alive = True
76 
77  def desable(self):
78  """ Method to desable the micro """
79  self.alive = False
80  self.serial.close()
81 
82 if __name__ == '__main__':
83 
84  # list available ports
85  print 'Available ports:'
86  PORTS_AVAILABLE = util.available_ports()
87  try:
88  for i in PORTS_AVAILABLE:
89  print '>>> %s' % i
90  print '---'
91  except TypeError, error:
92  print "None device connected"
93  exit()
94 
95  # choose a port
96  if len(PORTS_AVAILABLE) == 1:
97  msp430 = MSP(PORTS_AVAILABLE[0])
98  print 'Ready'
99 
100  # make 10 reads from adc
101  # print '\nRaw data \n----------'
102  # for x in xrange(1, 11):
103  # print x, msp430.adc.read_data('t')
104 
105  # # define a new reading method
106  # def thridfirst(item):
107  # """ Rewritng method """
108  # data = msp430.serial.readline()
109  # return data.split(',')[:3]
110 
111  # print msp430['adc']
112  # msp430.adc.read_data = thridfirst
113 
114  # # make 10 reads from adc using the new method
115  # print '\nProcessed data \n----------'
116  # for x in xrange(1, 11):
117  # print x, msp430.adc.flush()
118 
119  # # example of using a active sensor
120  # print '\nActive sensor\'s data \n----------'
121  # for x in xrange(1, 11):
122  # print x, msp430.pwm.write_data('r', 't')
123 
124  # new microcontroller interface
125  msp430.adc = sensor.Direction(msp430.serial,0)
126  print '\nADC data \n----------'
127  for x in xrange(1,10):
128  print msp430['adc']
129 
130  msp430.guidao = sensor.Direction(msp430.serial,2)
131  print '\nGuidão data \n----------'
132  for x in xrange(1,10):
133  print msp430['guidao']
134 
135  msp430.freio = sensor.Freio(msp430.serial,3)
136  print '\nFreio data \n----------'
137  for x in xrange(1,11):
138  print "Writinh %d..." %x,
139  msp430['freio']=x
140  print msp430['freio']
141 
142  # teste number larger than 9
143  msp430.teste = sensor.Direction(msp430.serial,13)
144  print '\nTeste data \n----------'
145  for x in xrange(1,10):
146  print msp430['teste']
147 
148 
149  # closes msp430 dependecies
150  msp430.desable()