Skip to content

Commit 078c1aa

Browse files
author
Charles Weir
committed
Changed initialization of sensors to avoid need for import *
1 parent 2670dcf commit 078c1aa

10 files changed

Lines changed: 64 additions & 34 deletions

File tree

BrickPython/BrickPiWrapper.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44

55
from Motor import Motor
66
from Sensor import Sensor
7-
from BrickPi import *
7+
import BrickPi as BP
88
from Scheduler import Scheduler
99

10-
1110
class BrickPiWrapper(Scheduler):
1211
'''
1312
This extends the Scheduler with functionality specific to the BrickPi
1413
15-
The constructor takes a map giving the sensor type connected to each port: PORT_1 through PORT_4.
16-
E.g. BrickPiWrapper( {PORT_1: TYPE_SENSOR_ULTRASONIC_CONT} )
14+
The constructor takes a map giving the sensor type connected to each port: 1 through 5.
15+
E.g. BrickPiWrapper( {'1': Sensor.TYPE_SENSOR_ULTRASONIC_CONT} )
1716
1817
Motors and sensors are identified by their port names: motors are A to D; sensors 1 to 5.
1918
'''
20-
def __init__(self, portTypes = [] ):
19+
def __init__(self, portTypes = {} ):
2120
Scheduler.__init__(self)
22-
self.motors = { 'A': Motor(PORT_A, self), 'B': Motor(PORT_B, self), 'C': Motor(PORT_C, self), 'D': Motor(PORT_D, self) }
23-
self.sensors = { '1': Sensor( PORT_1 ), '2': Sensor( PORT_2 ), '3': Sensor( PORT_3 ), '4': Sensor( PORT_4 ) }
24-
BrickPiSetup() # setup the serial port for communication
21+
self.motors = { 'A': Motor(BP.PORT_A, self), 'B': Motor(BP.PORT_B, self), 'C': Motor(BP.PORT_C, self), 'D': Motor(BP.PORT_D, self) }
22+
self.sensors = { '1': Sensor( BP.PORT_1 ), '2': Sensor( BP.PORT_2 ), '3': Sensor( BP.PORT_3 ), '4': Sensor( BP.PORT_4 ) }
23+
BP.BrickPiSetup() # setup the serial port for communication
2524

2625
for port in portTypes:
27-
BrickPi.SensorType[port] = portTypes[port]
28-
BrickPiSetupSensors() #Send the properties of sensors to BrickPi
26+
portNum = Sensor.portNumFromId(port)
27+
BP.BrickPi.SensorType[portNum] = portTypes[port]
28+
BP.BrickPiSetupSensors() #Send the properties of sensors to BrickPi
2929

3030
self.setUpdateCoroutine( self.updaterCoroutine() )
3131

@@ -43,21 +43,21 @@ def update(self):
4343
# Communicates with the BrickPi processor, sending current motor settings, and receiving sensor values.
4444
global BrickPi
4545
for motor in self.motors.values():
46-
BrickPi.MotorEnable[motor.port] = int(motor.enabled())
47-
BrickPi.MotorSpeed[motor.port] = motor.power()
46+
BP.BrickPi.MotorEnable[motor.port] = int(motor.enabled())
47+
BP.BrickPi.MotorSpeed[motor.port] = motor.power()
4848

4949
# Updates sensor readings, motor locations, and motor power settings.
5050
# Takes about 6ms.
51-
BrickPiUpdateValues()
51+
BP.BrickPiUpdateValues()
5252

5353
for motor in self.motors.values():
54-
position = BrickPi.Encoder[motor.port]
54+
position = BP.BrickPi.Encoder[motor.port]
5555
if not isinstance( position, ( int, long ) ): # For mac
5656
position = 0
5757
motor.updatePosition( position )
5858

5959
for sensor in self.sensors.values():
60-
value = BrickPi.Sensor[sensor.port]
60+
value = BP.BrickPi.Sensor[sensor.port]
6161
if not isinstance( value, ( int, long ) ): # For mac
6262
value = 0
6363
sensor.updateValue( value )

BrickPython/CommandLineApplication.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
#
44
# Copyright (c) 2014 Charles Weir. Shared under the MIT Licence.
55

6-
import Tkinter as tk
76

8-
from BrickPiWrapper import *
9-
import logging
7+
from BrickPiWrapper import BrickPiWrapper
8+
import time
109

1110
class CommandLineApplication(BrickPiWrapper):
1211
'''
1312
Main application class for command-line only apps. Doesn't support user input.
1413
'''
1514

1615
def __init__(self, sensorConfiguration={}):
17-
'''Initialization: *sensorConfiguration* is a map, e.g. {PORT_1: TYPE_SENSOR_ULTRASONIC_CONT }
16+
'''Initialization: *sensorConfiguration* is a map, e.g. {'1': Sensor.TYPE_SENSOR_ULTRASONIC_CONT}
1817
as passed to BrickPiWrapper'''
1918
BrickPiWrapper.__init__(self, sensorConfiguration )
2019

BrickPython/Sensor.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22
#
33
# Copyright (c) 2014 Charles Weir. Shared under the MIT Licence.
44

5-
#TODO: Remove need for clients to import * from BrickPython by
6-
# getting rid of the use of PORT_* values in the interfaces,
7-
# and duplicating TYPE_SENSOR_ULTRASONIC_CONT etc as static members of class Sensor.
5+
import BrickPi
86

97
class Sensor():
10-
'''Sensor, representing a sensor attached to one of the BrickPi ports.'''
8+
'''Sensor, representing a sensor attached to one of the BrickPi ports.
9+
*port* may be either a PORT_ value or an integer '1'-'5'
10+
11+
There are class attributes with the types defined in the BrickPi module:
12+
Sensor.TYPE_SENSOR_ULTRASONIC_CONT
13+
These are configured in the initialization parameters to BrickPiWrapper (and derived classes)
14+
15+
'''
16+
17+
@staticmethod
18+
def portNumFromId(portNumOrIdChar):
19+
# Answers the port number given either value.
20+
if (not isinstance(portNumOrIdChar, int)):
21+
return int(portNumOrIdChar) - 1
22+
return portNumOrIdChar
23+
1124
def __init__(self, port):
12-
self.port = port
25+
self.port = Sensor.portNumFromId(port)
1326
#: Character identifying the sensor: 1 through 5.
14-
self.idChar = chr(port + ord('1'))
27+
self.idChar = chr(self.port + ord('1'))
1528
#: Array of the *maxRecentValues* most recent sensor readings
1629
self.recentValues = [0]
1730
#: How many sensor readings to store
@@ -33,4 +46,8 @@ def value(self):
3346
def __repr__(self):
3447
return "Sensor %s: %r" % (self.idChar, self.recentValues)
3548

49+
# Put the sensor types (which are globals in BrickPi) to be attributes of Sensor
3650

51+
for name in dir(BrickPi):
52+
if name.startswith('TYPE_'):
53+
setattr(Sensor, name, getattr(BrickPi, name))

BrickPython/TkApplication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TkApplication(BrickPiWrapper):
2121
'''
2222

2323
def __init__(self, sensorConfiguration={}):
24-
'''Initialization: *sensorConfiguration* is a map, e.g. {PORT_1: TYPE_SENSOR_ULTRASONIC_CONT }
24+
'''Initialization: *sensorConfiguration* is a map, e.g. {'1': Sensor.TYPE_SENSOR_ULTRASONIC_CONT}
2525
as passed to BrickPiWrapper'''
2626
BrickPiWrapper.__init__(self, sensorConfiguration )
2727
self.root = tk.Tk()

ExamplePrograms/DoorControl.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
# Copyright (c) 2014 Charles Weir. Shared under the MIT Licence.
55

66
import sortOutPythonPaths
7-
from BrickPython.TkApplication import *
7+
from BrickPython.TkApplication import TkApplication
8+
from BrickPython.Sensor import Sensor
89
import logging
910

1011
class DoorControlApp(TkApplication):
@@ -17,7 +18,7 @@ class DoorControlApp(TkApplication):
1718
'''
1819

1920
def __init__(self):
20-
TkApplication.__init__(self, {PORT_1: TYPE_SENSOR_ULTRASONIC_CONT })
21+
TkApplication.__init__(self, {'1': Sensor.TYPE_SENSOR_ULTRASONIC_CONT }) # Compiler doesn't know these types.
2122
self.doorLocked = False
2223
self.addSensorCoroutine( self.openDoorWhenSensorDetected() )
2324

ExamplePrograms/MotorController.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
# Copyright (c) 2014 Charles Weir. Shared under the MIT Licence.
55

66
import sortOutPythonPaths
7-
from BrickPython.TkApplication import *
7+
from BrickPython.TkApplication import TkApplication
88
from BrickPython.Motor import PIDSetting
9+
from BrickPython.Sensor import Sensor
910
import logging
1011

1112
class MotorControllerApp(TkApplication):
@@ -23,7 +24,7 @@ class MotorControllerApp(TkApplication):
2324
'''
2425

2526
def __init__(self):
26-
TkApplication.__init__(self, {PORT_1: TYPE_SENSOR_ULTRASONIC_CONT })
27+
TkApplication.__init__(self, {'1': Sensor.TYPE_SENSOR_ULTRASONIC_CONT }) # Compiler doesn't know these types.
2728
self.pidSetting = PIDSetting()
2829

2930
def rotate(self, degrees):

doBuild

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ python setup.py test
1313

1414
echo '>> Upload docs to pypi from' `pwd`/docs/generated/Archive.zip
1515
echo '>> Form at the bottom of https://pypi.python.org/pypi?%3Aaction=pkg_edit&name=BrickPython'
16-
echo '>> To release module to pypi: python setup.py sdist upload'
16+
echo '>> To release module to pypi: python setup.py sdist upload'
17+
echo '>> To install without releasing: pip install -e .'

docs/introduction.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ The :class:`.Motor` class implements methods to record and calculate the current
129129
to position itself accurately to a couple of degrees. There's also a 'constant speed' coroutine :meth:`.Motor.setSpeed()`.
130130

131131
The :class:`.Sensor` class simply keeps a record, :attr:`.Sensor.recentValues`, of the last few readings; its method :meth:`.Sensor.value()` answers the most recent one. The type of each sensor
132-
is set up via the initialization parameter to :class:`.BrickPiWrapper` (or :class:`.TkApplication`).
132+
is set up via the initialization parameter to :class:`.BrickPiWrapper` (via :class:`.TkApplication` or :class:`.CommandLineApplication`).
133133

134134
Example Applications
135135
====================

test/TestBrickPiWrapper.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515

1616
from BrickPython.BrickPiWrapper import BrickPiWrapper
17-
from BrickPython.BrickPi import *
17+
from BrickPython.BrickPi import BrickPi, PORT_1, TYPE_SENSOR_ULTRASONIC_CONT
18+
from BrickPython.Sensor import Sensor
1819
import unittest
1920

2021

@@ -45,6 +46,9 @@ def testSensorSetup(self):
4546
bp = BrickPiWrapper( {PORT_1: TYPE_SENSOR_ULTRASONIC_CONT} )
4647
assert( BrickPi.SensorType[PORT_1] == TYPE_SENSOR_ULTRASONIC_CONT)
4748

49+
def testSensorSetupBetterScopingSyntax(self):
50+
bp = BrickPiWrapper( {'1': Sensor.TYPE_SENSOR_ULTRASONIC_CONT} )
51+
self.assertEquals( BrickPi.SensorType[PORT_1], TYPE_SENSOR_ULTRASONIC_CONT)
4852

4953
if __name__ == '__main__':
5054
unittest.main()

test/TestSensor.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
# Copyright (c) 2014 Charles Weir. Shared under the MIT Licence.
44

55
import unittest
6-
from BrickPython.BrickPiWrapper import PORT_1
6+
from BrickPython.BrickPi import PORT_1
77
from BrickPython.Sensor import Sensor
88

99
class TestSensor(unittest.TestCase):
1010
'Test for the Sensor object'
1111

1212
def testSensor(self):
1313
sensor = Sensor( PORT_1 )
14+
self.assertEquals(sensor.port, PORT_1)
1415
assert( sensor.idChar == '1' )
1516
assert( sensor.value() == 0 )
1617
sensor.updateValue( 3 )
@@ -23,6 +24,12 @@ def testSensor(self):
2324
def testSensorTextRepresentation(self):
2425
self.assertEquals( repr(Sensor( PORT_1 ) ), 'Sensor 1: [0]')
2526

27+
def testBetterSensorInitSyntax(self):
28+
sensor = Sensor( '1' )
29+
self.assertEquals(sensor.port, PORT_1)
30+
self.assertEquals( sensor.idChar, '1' )
31+
assert( sensor.value() == 0 )
32+
2633
if __name__ == '__main__':
2734
unittest.main()
2835

0 commit comments

Comments
 (0)