31
31
__version__ = "0.0.0-auto.0"
32
32
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"
33
33
34
- from adafruit_motor import motor , stepper
35
34
from adafruit_pca9685 import PCA9685
36
35
37
36
from adafruit_featherwing import shared
@@ -50,6 +49,23 @@ def __init__(self):
50
49
self ._pca = PCA9685 (shared .I2C_BUS , address = 0x60 )
51
50
self ._pca .frequency = 1600
52
51
52
+ # We can save memory usage (~300 bytes) by deduplicating the construction of the objects for
53
+ # each motor. This saves both code size and the number of raw strings (the error message)
54
+ # stored. The same technique is a net loss for stepper because there is less duplication.
55
+ def _motor (self , motor_name , channels , stepper_name ):
56
+ from adafruit_motor import motor
57
+ motor_name = "_motor" + str (motor_name )
58
+ stepper_name = "_stepper" + str (stepper_name )
59
+ if not getattr (self , motor_name ):
60
+ if getattr (self , stepper_name ):
61
+ raise RuntimeError (
62
+ "Cannot use {} at the same time as {}." .format (motor_name [1 :],
63
+ stepper_name [1 :]))
64
+ self ._pca .channels [channels [0 ]].duty_cycle = 0xffff
65
+ setattr (self , motor_name , motor .DCMotor (self ._pca .channels [channels [1 ]],
66
+ self ._pca .channels [channels [2 ]]))
67
+ return getattr (self , motor_name )
68
+
53
69
@property
54
70
def motor1 (self ):
55
71
""":py:class:`~adafruit_motor.motor.DCMotor` controls for motor 1.
@@ -71,12 +87,7 @@ def motor1(self):
71
87
72
88
wing.motor1.throttle = 0
73
89
"""
74
- if not self ._motor1 :
75
- if self ._stepper1 :
76
- raise RuntimeError ("Cannot use motor1 at the same time as stepper1." )
77
- self ._pca .channels [8 ].duty_cycle = 0xffff
78
- self ._motor1 = motor .DCMotor (self ._pca .channels [9 ], self ._pca .channels [10 ])
79
- return self ._motor1
90
+ return self ._motor (1 , (8 , 9 , 10 ), 1 )
80
91
81
92
@property
82
93
def motor2 (self ):
@@ -99,12 +110,7 @@ def motor2(self):
99
110
100
111
wing.motor2.throttle = 0
101
112
"""
102
- if not self ._motor2 :
103
- if self ._stepper1 :
104
- raise RuntimeError ("Cannot use motor2 at the same time as stepper1." )
105
- self ._pca .channels [13 ].duty_cycle = 0xffff
106
- self ._motor2 = motor .DCMotor (self ._pca .channels [11 ], self ._pca .channels [12 ])
107
- return self ._motor2
113
+ return self ._motor (2 , (13 , 11 , 12 ), 1 )
108
114
109
115
@property
110
116
def motor3 (self ):
@@ -127,12 +133,7 @@ def motor3(self):
127
133
128
134
wing.motor3.throttle = 0
129
135
"""
130
- if not self ._motor3 :
131
- if self ._stepper2 :
132
- raise RuntimeError ("Cannot use motor3 at the same time as stepper2." )
133
- self ._pca .channels [2 ].duty_cycle = 0xffff
134
- self ._motor3 = motor .DCMotor (self ._pca .channels [3 ], self ._pca .channels [4 ])
135
- return self ._motor3
136
+ return self ._motor (3 , (2 , 3 , 4 ), 2 )
136
137
137
138
@property
138
139
def motor4 (self ):
@@ -155,12 +156,7 @@ def motor4(self):
155
156
156
157
wing.motor4.throttle = 0
157
158
"""
158
- if not self ._motor4 :
159
- if self ._stepper2 :
160
- raise RuntimeError ("Cannot use motor4 at the same time as stepper2." )
161
- self ._pca .channels [7 ].duty_cycle = 0xffff
162
- self ._motor4 = motor .DCMotor (self ._pca .channels [5 ], self ._pca .channels [6 ])
163
- return self ._motor4
159
+ return self ._motor (4 , (7 , 5 , 6 ), 2 )
164
160
165
161
@property
166
162
def stepper1 (self ):
@@ -181,6 +177,7 @@ def stepper1(self):
181
177
for i in range(100):
182
178
wing.stepper1.onestep()"""
183
179
if not self ._stepper1 :
180
+ from adafruit_motor import stepper
184
181
if self ._motor1 or self ._motor2 :
185
182
raise RuntimeError ("Cannot use stepper1 at the same time as motor1 or motor2." )
186
183
self ._pca .channels [8 ].duty_cycle = 0xffff
@@ -209,6 +206,7 @@ def stepper2(self):
209
206
wing.stepper2.onestep()
210
207
"""
211
208
if not self ._stepper2 :
209
+ from adafruit_motor import stepper
212
210
if self ._motor3 or self ._motor4 :
213
211
raise RuntimeError ("Cannot use stepper2 at the same time as motor3 or motor4." )
214
212
self ._pca .channels [7 ].duty_cycle = 0xffff
0 commit comments