Skip to content

Commit a58f1ed

Browse files
committed
Merge development branch changes.
2 parents 9dbcacc + b0e4c67 commit a58f1ed

File tree

7 files changed

+109
-161
lines changed

7 files changed

+109
-161
lines changed

myDevices/devices/bus.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,24 @@ def close(self):
111111
if self.fd > 0:
112112
os.close(self.fd)
113113

114-
def read(self, size=1):
114+
def readDevice(self, size=1):
115115
if self.fd > 0:
116116
return os.read(self.fd, size)
117117
raise Exception("Device %s not open" % self.device)
118118

119119
def readBytes(self, size=1):
120-
return bytearray(self.read(size))
120+
return bytearray(self.readDevice(size))
121121

122122
def readByte(self):
123123
return self.readBytes()[0]
124124

125-
def write(self, string):
125+
def writeDevice(self, string):
126126
if self.fd > 0:
127127
return os.write(self.fd, string)
128128
raise Exception("Device %s not open" % self.device)
129129

130130
def writeBytes(self, data):
131-
return self.write(bytearray(data))
131+
return self.writeDevice(bytearray(data))
132132

133133
def writeByte(self, value):
134134
self.writeBytes([value])

myDevices/devices/digital/helper.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from myDevices.devices.digital.gpio import NativeGPIO as GPIO
1919

2020

21-
2221
class DigitalSensor():
2322

2423
def __init__(self, gpio, channel, invert=False):
@@ -53,9 +52,8 @@ def read(self):
5352
self.setGPIOInstance()
5453
value = self.gpio.digitalRead(self.channel)
5554
if self.invert:
56-
return not value
57-
else:
58-
return value
55+
value = not value
56+
return int(value)
5957

6058
class MotionSensor(DigitalSensor):
6159
def __init__(self, gpio, channel, invert=False):
@@ -79,7 +77,7 @@ def __family__(self):
7977
@response("%d")
8078
def write(self, value):
8179
if self.invert:
82-
value = not value
80+
value = int(not value)
8381
self.gpio.digitalWrite(self.channel, value)
8482
return self.read()
8583

myDevices/devices/manager.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def addDeviceJSON(json):
8888
if 'args' in json:
8989
args = json["args"]
9090
else:
91-
args = []
91+
args = {}
9292

9393
if 'description' in json:
9494
description = json["description"]
@@ -145,7 +145,6 @@ def addDevice(name, device, description, args, origin):
145145

146146
instance = None
147147
try:
148-
149148
if len(args) > 0:
150149
instance = constructor(**args)
151150
else:
@@ -162,7 +161,7 @@ def addDevice(name, device, description, args, origin):
162161
def addDeviceConf(devices, origin):
163162
for (name, params) in devices:
164163
values = params.split(" ")
165-
driver = values[0];
164+
driver = values[0]
166165
description = name
167166
args = {}
168167
i = 1

myDevices/devices/shield/piface.py

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,72 +17,15 @@
1717
from myDevices.decorators.rest import request, response
1818

1919

20-
class PiFaceDigital():
20+
class PiFaceDigital(MCP23S17):
2121
def __init__(self, board=0):
22-
mcp = MCP23S17(0, 0x20+toint(board))
23-
mcp.writeRegister(mcp.getAddress(mcp.IODIR, 0), 0x00) # Port A as output
24-
mcp.writeRegister(mcp.getAddress(mcp.IODIR, 8), 0xFF) # Port B as input
25-
mcp.writeRegister(mcp.getAddress(mcp.GPPU, 0), 0x00) # Port A PU OFF
26-
mcp.writeRegister(mcp.getAddress(mcp.GPPU, 8), 0xFF) # Port B PU ON
27-
self.mcp = mcp
2822
self.board = toint(board)
23+
MCP23S17.__init__(self, 0, 0x20 + self.board)
24+
self.writeRegister(self.getAddress(self.IODIR, 0), 0x00) # Port A as output
25+
self.writeRegister(self.getAddress(self.IODIR, 8), 0xFF) # Port B as input
26+
self.writeRegister(self.getAddress(self.GPPU, 0), 0x00) # Port A PU OFF
27+
self.writeRegister(self.getAddress(self.GPPU, 8), 0xFF) # Port B PU ON
2928

3029
def __str__(self):
3130
return "PiFaceDigital(%d)" % self.board
3231

33-
def __family__(self):
34-
return "GPIOPort"
35-
36-
def checkChannel(self, channel):
37-
# if not channel in range(8):
38-
if not channel in range(16):
39-
raise ValueError("Channel %d invalid" % channel)
40-
41-
#@request("GET", "%(channel)d/value")
42-
@response("%d")
43-
def digitalRead(self, channel):
44-
self.checkChannel(channel)
45-
# return not self.mcp.digitalRead(channel+8)
46-
return self.mcp.digitalRead(channel)
47-
48-
#@request("POST", "%(channel)d/value/%(value)d")
49-
@response("%d")
50-
def digitalWrite(self, channel, value):
51-
self.checkChannel(channel)
52-
return self.mcp.digitalWrite(channel, value)
53-
54-
# #@request("GET", "digital/output/%(channel)d")
55-
# @response("%d")
56-
# def digitalReadOutput(self, channel):
57-
# self.checkChannel(channel)
58-
# return self.mcp.digitalRead(channel)
59-
60-
# #@request("GET", "digital/*")
61-
# @response(contentType=M_JSON)
62-
# def readAll(self):
63-
# inputs = {}
64-
# outputs = {}
65-
# for i in range(8):
66-
# inputs[i] = self.digitalRead(i)
67-
# outputs[i] = self.digitalReadOutput(i)
68-
# return {"input": inputs, "output": outputs}
69-
70-
#@request("GET", "*")
71-
@response(contentType=M_JSON)
72-
def readAll(self):
73-
# inputs = {}
74-
# outputs = {}
75-
# for i in range(8):
76-
# inputs[i] = self.digitalRead(i)
77-
# outputs[i] = self.digitalReadOutput(i)
78-
# return {"input": inputs, "output": outputs}
79-
80-
values = {}
81-
for i in range(16):
82-
values[i] = {"function": self.mcp.getFunctionString(i), "value": int(self.mcp.digitalRead(i))}
83-
return values
84-
85-
#@request("GET", "count")
86-
@response("%d")
87-
def digitalCount(self):
88-
return 16

myDevices/sensors/sensors.py

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ def AppendToDeviceList(self, device_list, source, device_type):
230230

231231
def GetDevices(self):
232232
"""Return a list of current sensor/actuator devices"""
233+
manager.deviceDetector()
233234
device_list = manager.getDeviceList()
234235
devices = []
235236
for dev in device_list:
@@ -282,67 +283,65 @@ def BusInfo(self):
282283

283284
def SensorsInfo(self):
284285
"""Return a dict with current sensor states for all enabled sensors"""
285-
with self.sensorMutex:
286-
devices = self.GetDevices()
287-
debug(str(time()) + ' Got devices info ' + str(self.sensorsRefreshCount))
288-
if devices is None:
289-
return {}
290-
for value in devices:
291-
sensor = instance.deviceInstance(value['name'])
292-
if 'enabled' not in value or value['enabled'] == 1:
293-
sleep(SENSOR_INFO_SLEEP)
294-
try:
295-
if value['type'] == 'Temperature':
296-
value['Celsius'] = self.CallDeviceFunction(sensor.getCelsius)
297-
value['Fahrenheit'] = self.CallDeviceFunction(sensor.getFahrenheit)
298-
value['Kelvin'] = self.CallDeviceFunction(sensor.getKelvin)
299-
if value['type'] == 'Pressure':
300-
value['Pascal'] = self.CallDeviceFunction(sensor.getPascal)
301-
if value['type'] == 'Luminosity':
302-
value['Lux'] = self.CallDeviceFunction(sensor.getLux)
303-
if value['type'] == 'Distance':
304-
value['Centimeter'] = self.CallDeviceFunction(sensor.getCentimeter)
305-
value['Inch'] = self.CallDeviceFunction(sensor.getInch)
306-
if value['type'] in ('ADC', 'DAC', 'PWM'):
307-
value['channelCount'] = self.CallDeviceFunction(sensor.analogCount)
308-
value['maxInteger'] = self.CallDeviceFunction(sensor.analogMaximum)
309-
value['resolution'] = self.CallDeviceFunction(sensor.analogResolution)
310-
value['allInteger'] = self.CallDeviceFunction(sensor.analogReadAll)
311-
value['allVolt'] = self.CallDeviceFunction(sensor.analogReadAllVolt)
312-
value['allFloat'] = self.CallDeviceFunction(sensor.analogReadAllFloat)
313-
if value['type'] in 'DAC':
314-
value['vref'] = self.CallDeviceFunction(sensor.analogReference)
315-
if value['type'] == 'PWM':
316-
value['channelCount'] = self.CallDeviceFunction(sensor.pwmCount)
317-
value['maxInteger'] = self.CallDeviceFunction(sensor.pwmMaximum)
318-
value['resolution'] = self.CallDeviceFunction(sensor.pwmResolution)
319-
value['all'] = self.CallDeviceFunction(sensor.pwmWildcard)
320-
if value['type'] == 'Humidity':
321-
value['float'] = self.CallDeviceFunction(sensor.getHumidity)
322-
value['percent'] = self.CallDeviceFunction(sensor.getHumidityPercent)
323-
if value['type'] == 'PiFaceDigital':
324-
value['all'] = self.CallDeviceFunction(sensor.readAll)
325-
if value['type'] in ('DigitalSensor', 'DigitalActuator'):
326-
value['value'] = self.CallDeviceFunction(sensor.read)
327-
if value['type'] == 'GPIOPort':
328-
value['channelCount'] = self.CallDeviceFunction(sensor.digitalCount)
329-
value['all'] = self.CallDeviceFunction(sensor.wildcard)
330-
if value['type'] == 'AnalogSensor':
331-
value['integer'] = self.CallDeviceFunction(sensor.read)
332-
value['float'] = self.CallDeviceFunction(sensor.readFloat)
333-
value['volt'] = self.CallDeviceFunction(sensor.readVolt)
334-
if value['type'] == 'ServoMotor':
335-
value['angle'] = self.CallDeviceFunction(sensor.readAngle)
336-
if value['type'] == 'AnalogActuator':
337-
value['float'] = self.CallDeviceFunction(sensor.readFloat)
338-
except:
339-
exception("Sensor values failed: "+ value['type'] + " " + value['name'])
286+
devices = self.GetDevices()
287+
debug(str(time()) + ' Got devices info ' + str(self.sensorsRefreshCount))
288+
if devices is None:
289+
return {}
290+
for value in devices:
291+
sensor = instance.deviceInstance(value['name'])
292+
if 'enabled' not in value or value['enabled'] == 1:
293+
sleep(SENSOR_INFO_SLEEP)
340294
try:
341-
if 'hash' in value:
342-
value['sensor'] = value['hash']
343-
del value['hash']
344-
except KeyError:
345-
pass
295+
if value['type'] == 'Temperature':
296+
value['Celsius'] = self.CallDeviceFunction(sensor.getCelsius)
297+
value['Fahrenheit'] = self.CallDeviceFunction(sensor.getFahrenheit)
298+
value['Kelvin'] = self.CallDeviceFunction(sensor.getKelvin)
299+
if value['type'] == 'Pressure':
300+
value['Pascal'] = self.CallDeviceFunction(sensor.getPascal)
301+
if value['type'] == 'Luminosity':
302+
value['Lux'] = self.CallDeviceFunction(sensor.getLux)
303+
if value['type'] == 'Distance':
304+
value['Centimeter'] = self.CallDeviceFunction(sensor.getCentimeter)
305+
value['Inch'] = self.CallDeviceFunction(sensor.getInch)
306+
if value['type'] in ('ADC', 'DAC'):
307+
value['channelCount'] = self.CallDeviceFunction(sensor.analogCount)
308+
value['maxInteger'] = self.CallDeviceFunction(sensor.analogMaximum)
309+
value['resolution'] = self.CallDeviceFunction(sensor.analogResolution)
310+
value['allInteger'] = self.CallDeviceFunction(sensor.analogReadAll)
311+
value['allVolt'] = self.CallDeviceFunction(sensor.analogReadAllVolt)
312+
value['allFloat'] = self.CallDeviceFunction(sensor.analogReadAllFloat)
313+
if value['type'] == 'DAC':
314+
value['vref'] = self.CallDeviceFunction(sensor.analogReference)
315+
if value['type'] == 'PWM':
316+
value['channelCount'] = self.CallDeviceFunction(sensor.pwmCount)
317+
value['maxInteger'] = self.CallDeviceFunction(sensor.pwmMaximum)
318+
value['resolution'] = self.CallDeviceFunction(sensor.pwmResolution)
319+
value['all'] = self.CallDeviceFunction(sensor.pwmWildcard)
320+
if value['type'] == 'Humidity':
321+
value['float'] = self.CallDeviceFunction(sensor.getHumidity)
322+
value['percent'] = self.CallDeviceFunction(sensor.getHumidityPercent)
323+
if value['type'] in ('DigitalSensor', 'DigitalActuator'):
324+
value['value'] = self.CallDeviceFunction(sensor.read)
325+
if value['type'] == 'GPIOPort':
326+
value['channelCount'] = self.CallDeviceFunction(sensor.digitalCount)
327+
value['all'] = self.CallDeviceFunction(sensor.wildcard)
328+
if value['type'] == 'AnalogSensor':
329+
value['integer'] = self.CallDeviceFunction(sensor.read)
330+
value['float'] = self.CallDeviceFunction(sensor.readFloat)
331+
value['volt'] = self.CallDeviceFunction(sensor.readVolt)
332+
if value['type'] == 'ServoMotor':
333+
value['angle'] = self.CallDeviceFunction(sensor.readAngle)
334+
if value['type'] == 'AnalogActuator':
335+
value['float'] = self.CallDeviceFunction(sensor.readFloat)
336+
except:
337+
exception("Sensor values failed: "+ value['type'] + " " + value['name'])
338+
try:
339+
if 'hash' in value:
340+
value['sensor'] = value['hash']
341+
del value['hash']
342+
except KeyError:
343+
pass
344+
with self.sensorMutex:
346345
if self.currentSensorsInfo:
347346
del self.currentSensorsInfo
348347
self.currentSensorsInfo = None

myDevices/system/hardware.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,44 @@ class Hardware:
4040
def __init__(self):
4141
"""Initialize board revision and model dict"""
4242
self.Revision = CPU_REVISION
43-
self.model = {}
44-
self.model["Beta"] = "Model B (Beta)"
45-
self.model["000d"] = self.model["000e"] = self.model["000f"] = self.model["0002"] = self.model["0003"] = self.model["0004"] = self.model["0005"] = self.model["0006"] = "Model B"
46-
self.model["0007"] = self.model["0008"] = self.model["0009"] = "Model A"
47-
self.model["0010"] = "Model B+"
48-
self.model["0011"] = "Compute Module"
49-
self.model["0012"] = "Model A+"
50-
self.model["0013"] = "Model B+"
51-
self.model["a01041"] = "Pi 2 Model B"
52-
self.model["a21041"] = "Pi 2 Model B"
53-
self.model["900092"] = "Zero"
54-
self.model["a22082"] = self.model["a02082"] = "Pi 3 Model B"
43+
self.model = 'Unknown'
44+
if self.Revision == 'Beta':
45+
self.model = 'Model B (Beta)'
46+
if self.Revision in ('000d', '000e', '000f', '0002', '0003', '0004', '0005', '0006'):
47+
self.model = 'Model B'
48+
if self.Revision in ('0007', '0008', '0009'):
49+
self.model = 'Model A'
50+
if self.Revision in ('0010', '0013', '900032'):
51+
self.model = 'Model B +'
52+
if self.Revision in ('0011', '0014'):
53+
self.model = 'Compute Module'
54+
if self.Revision in ('0012', '0015'):
55+
self.model = 'Model A+'
56+
if self.Revision in ('a01041', 'a21041', 'a22042'):
57+
self.model = 'Pi 2 Model B'
58+
if self.Revision in ('900092', '900093'):
59+
self.model = 'Zero'
60+
if self.Revision in ('9000c1',):
61+
self.model = 'Zero W'
62+
if self.Revision in ('a02082', 'a22082'):
63+
self.model = 'Pi 3 Model B'
64+
self.manufacturer = 'Element14/Premier Farnell'
65+
if self.Revision in ('a01041', '900092', 'a02082', '0012', '0011', '0010', '000e', '0008', '0004'):
66+
self.manufacturer = 'Sony, UK'
67+
if self.Revision in ('0014', '0015', 'a21041', 'a22082'):
68+
self.manufacturer = 'Embest, China'
69+
if self.Revision in ('0005', '0009', '000f'):
70+
self.manufacturer = 'Qisda'
71+
if self.Revision in ('0006', '0007', '000d'):
72+
self.manufacturer = 'Egoman'
5573

5674
def getManufacturer(self):
5775
"""Return manufacturer name as string"""
58-
if self.Revision in ["a01041","900092", "a02082", "0012", "0011", "0010", "000e", "0008", "0004"]:
59-
return "Sony, UK"
60-
if self.Revision == "a21041":
61-
return "Embest, China"
62-
if self.Revision in ["0005", "0009", "000f"]:
63-
return "Qisda"
64-
if self.Revision in ["0006", "0007", "000d"]:
65-
return "Egoman"
66-
return "Element14/Premier Farnell"
76+
return self.manufacturer
6777

6878
def getModel(self):
6979
"""Return model name as string"""
70-
try:
71-
model = self.model[self.Revision]
72-
except:
73-
model = "Unknown"
74-
return model
80+
return self.model
7581

7682
def getMac(self, format=2):
7783
"""Return MAC address as string"""

myDevices/test/sensors_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def testSensors(self):
6969

7070
def testSensorInfo(self):
7171
sensors = {'actuator' : {'description': 'Digital Output', 'device': 'DigitalActuator', 'args': {'gpio': 'GPIO', 'invert': False, 'channel': 16}, 'name': 'test_actuator'},
72+
'light_switch' : {'description': 'Light Switch', 'device': 'LightSwitch', 'args': {'gpio': 'GPIO', 'invert': True, 'channel': 15}, 'name': 'test_light_switch'},
7273
'MCP3004' : {'description': 'MCP3004', 'device': 'MCP3004', 'args': {'chip': '0'}, 'name': 'test_MCP3004'},
7374
'distance' : {'description': 'Analog Distance Sensor', 'device': 'DistanceSensor', 'args': {'adc': 'test_MCP3004', 'channel': 0}, 'name': 'test_distance'}}
7475
for sensor in sensors.values():
@@ -77,6 +78,8 @@ def testSensorInfo(self):
7778
#Test setting sensor values
7879
self.setSensorValue(sensors['actuator'], 1)
7980
self.setSensorValue(sensors['actuator'], 0)
81+
self.setSensorValue(sensors['light_switch'], 1)
82+
self.setSensorValue(sensors['light_switch'], 0)
8083
#Test getting analog value
8184
retrievedSensorInfo = next(obj for obj in SensorsClientTest.client.SensorsInfo() if obj['name'] == sensors['distance']['name'])
8285
self.assertEqual(retrievedSensorInfo['float'], 0.0)

0 commit comments

Comments
 (0)