Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
device.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # coding: utf-8
3 
4 """
5  Device module
6 
7  Define the basic classes and rotines to a sensor
8 """
9 
10 from startx import BREAK_MSP, DIRECTION_MSP, VELOCITY_MSP, ALL_VALUES
11 
12 class Device(object):
13 
14  """docstring for Device"""
15 
16  type = ''
17  name = 'base'
18  data = '-----'
19  live = False
20 
21  def __init__(self, terminal):
22  super(Device, self).__init__()
23  self.serial = terminal
24  self.enable()
25  self.flush()
26 
27  def enable(self):
28  """ Enable the sensor """
29 
30  self.live = True
31 
32  def desable(self):
33  """ Desable the sensor """
34 
35  self.live = False
36 
37  def flush(self):
38  """ Read data """
39 
40  return self.serial.readline()
41 
42 
43 class Active(Device):
44 
45  """docstring for Passive"""
46 
47  def __init__(self, terminal):
48  super(Active, self).__init__(terminal)
49  self.data = self.serial.readline()
50  self.data = self.data.split('\n')[0]
51 
52  def write_data(self, command, data):
53  """ Return actual data from device """
54  self.serial.write(str(command))
55  self.serial.write(str(data))
56 
57  def read_data(self, command):
58  return None #self.serial.readline()
59 
60 
61 class Passive(Device):
62 
63  """docstring for Active"""
64 
65  def __init__(self, terminal):
66  super(Passive, self).__init__(terminal)
67  self.data = self.serial.readline()
68  self.data = self.data.split('\n')[0]
69 
70  def read_data(self, command):
71  """ Send some data to device """
72  self.serial.write(str(command))
73  self.data = self.serial.readline()
74  self.data = self.data.split('\n')[0]
75  return self.data
76 
77  def write_data(self, command, data):
78  return None
79 
80 
81 class Break(Active):
82  """docstring for Break"""
83  def __init__(self,terminal, arg=BREAK_MSP):
84  super(Break, self).__init__(terminal)
85  self.arg = arg
86 
87  def write_data(self,data):
88  data = int(data)
89  if (data >= 0) and (data <= 9):
90  super(Break, self).write_data(self.arg,data)
91 
92 
94  """docstring for Direction"""
95  def __init__(self,terminal, arg=DIRECTION_MSP):
96  super(Direction, self).__init__(terminal)
97  self.arg = arg
98 
99  def read_data(self):
100  return super(Direction, self).read_data(self.arg)
101 
102 
104  """docstring for Velocity"""
105  def __init__(self,terminal, arg=VELOCITY_MSP):
106  super(Velocity, self).__init__(terminal)
107  self.arg = arg
108 
109  def read_data(self):
110  return super(Velocity, self).read_data(self.arg)
111 
112 
114  """docstring for Passives"""
115  def __init__(self,terminal, arg=ALL_VALUES):
116  super(Passives, self).__init__(terminal)
117  self.arg = arg
118 
119  def read_data(self):
120  return super(Passives, self).read_data(self.arg)
121 
122 if __name__ == '__main__':
123  import serial
124  from glob import glob
125  PORTS_AVAILABLE = glob('/dev/ttyUSB*') + glob('/dev/ttyACM*')
126  print PORTS_AVAILABLE
127  ser = serial.Serial(PORTS_AVAILABLE[0], 4800)
128  act = Active(ser)
129  print act.write_data('r', 't')
130  passive = Passive(ser)
131  print passive.read_data('g')
132  ser.close()