-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
167 lines (137 loc) · 6.94 KB
/
classes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Contains the two classes used in the system, Sensors and Actuators. Due to the
common functions they have they can have a parent class in future implementations. The action and setup functions
require the driver file to be in the /drivers location. Currently all drivers
are coded specifically for the system and no libraries are used.
"""
class Sensor:
"""
Sensor object contains all of the relative sensor data.
Args:
data: Data retrieved by sensor
is_literal_numeric: Set when the literal of the sensor has a X for numbers, e.g. dist(X)
default_literal_p: Default literal to for easier replacement of X
default_literal_n: Same as above
sensor_id (str): ID for each sensor, duplicates are currently allowed.
pin_or_channel (int or list): Can be an array or an int but that depends if adc_fcn is not set to None.
literal_p (str, optional): The positive literal used by the sensor for the Context. Defaults to "".
literal_n (str, optional): The negative literal used by the sensor for the Context. Defaults to "".
action_fcn (function, optional): The action done by the sensor, e.g. get distance or check for a button press. Defaults to None.
setup_fcn (function, optional): Used to set up the correct pins in the correct way to accept the sensor data. Defaults to None.
adc_fcn (function, optional): Must be set if using an ADC channel for measurements. Defaults to None.
"""
data = 0
is_literal_numeric = False
default_literal_p = ""
default_literal_n = ""
def __init__(self, sensor_id, pin_or_channel, literal_p, literal_n, action_fcn, setup_fcn, adc_fcn = None):
self.sensor_id = sensor_id
self.pin_or_channel = pin_or_channel
self.literal_p = literal_p
self.literal_n = literal_n
self.action_fcn = action_fcn
self.setup_fcn = setup_fcn
self.default_literal_p = literal_p
self.default_literal_n = literal_n
self.adc_fcn = adc_fcn
# use the setup function to assign the sensor to the correct pins, max 4 pins can be used
def sensor_setup(self):
if self.setup_fcn == None:
return
else:
size = len(self.pin_or_channel)
if size == 1:
self.setup_fcn(self.pin_or_channel[0])
elif size == 2:
self.setup_fcn(self.pin_or_channel[0],self.pin_or_channel[1])
elif size == 3:
self.setup_fcn(self.pin_or_channel[0],self.pin_or_channel[1],self.pin_or_channel[2])
elif size == 4:
self.setup_fcn(self.pin_or_channel[0],self.pin_or_channel[1],self.pin_or_channel[2],self.pin_or_channel[3])
else:
print("Error notify dev, allowed pins exceeded for " + self.sensor_id)
# function used to retrieve data from sensor
def sensor_action(self):
if self.adc_fcn != None:
self.data = self.adc_fcn(self.pin_or_channel)
self.data = self.action_fcn(self.data)
return
# if any mistake happens in initialisation and there is an empty entry, ignore it
elif self.action_fcn == None:
return
else:
size = len(self.pin_or_channel)
if size == 1:
self.data = self.action_fcn(self.pin_or_channel[0])
elif size == 2:
self.data = self.action_fcn(self.pin_or_channel[0],self.pin_or_channel[1])
elif size == 3:
self.data = self.action_fcn(self.pin_or_channel[0],self.pin_or_channel[1],self.pin_or_channel[2])
else:
self.data = self.action_fcn(self.pin_or_channel[0],self.pin_or_channel[1],self.pin_or_channel[2],self.pin_or_channel[3])
# fcn used to replace the X in a numerical input to the context
def data_in_literal(self):
if "X" in self.literal_p:
self.literal_p = self.literal_p.replace("X", str(int(self.data)))
self.is_literal_numeric = True
elif "X" in self.literal_n:
self.literal_n = self.literal_n.replace("X",str(int(self.data)))
self.is_literal_numeric = True
elif self.is_literal_numeric and any((num in set('0123456789')) for num in self.literal_p):
if self.literal_p != "":
self.literal_p = self.default_literal_p
self.literal_p = self.literal_p.replace("X", str(int(self.data)))
elif self.literal_n != "":
self.literal_n = self.default_literal_n
self.literal_n = self.literal_n.replace("X", str(int(self.data)))
else:
self.is_literal_numeric = False
class Actuator:
def __init__(self, actuator_id, pin, literal, action_fcn, setup_fcn):
"""Actuator object contains all of the actuator data.
Args:
actuator_id (str): ID for each actuator, duplicates are currently allowed. Defaults to "".
pin (list): Pin used by the actuator. Defaults to [0].
literal (str, optional): Literal to detect in order to use it. Defaults to "".
action_fcn (function, optional): The action done by the actuator, e.g. turn LED on. Defaults to None.
setup_fcn (function, optional): Used to set up the correct pins in the correct way to accept the sensor data. Defaults to None.
"""
self.actuator_id = actuator_id
self.pin = pin
self.literal = literal
self.action_fcn = action_fcn
self.setup_fcn = setup_fcn
# use the setup function to assign the actuator to the correct pins, max 4 pins can be used
def actuator_setup(self):
if self.setup_fcn == None:
return
elif self.pin == None:
self.setup_fcn()
else:
size = len(self.pin)
if size == 1:
self.setup_fcn(self.pin[0])
elif size == 2:
self.setup_fcn(self.pin[0],self.pin[1])
elif size == 3:
self.setup_fcn(self.pin[0],self.pin[1],self.pin[2])
elif size == 4:
self.setup_fcn(self.pin[0],self.pin[1],self.pin[2],self.pin[3])
else:
print("Error notify dev, allowed setup pins exceeded for " + self.actuator_id)
# function used to retrieve data from actuator
def actuator_action(self):
if self.pin == None:
self.action_fcn()
else:
size = len(self.pin)
if size == 1:
self.data = self.action_fcn(self.pin[0])
elif size == 2:
self.data = self.action_fcn(self.pin[0],self.pin[1])
elif size == 3:
self.data = self.action_fcn(self.pin[0],self.pin[1],self.pin[2])
elif size == 4:
self.data = self.action_fcn(self.pin[0],self.pin[1],self.pin[2],self.pin[3])
else:
print("Error notify dev, allowed action pins exceeded for " + self.actuator_id)