3131__version__ = "0.0.0-auto.0"
3232__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git"
3333
34- from adafruit_motor import motor , stepper
3534from adafruit_pca9685 import PCA9685
3635
3736from adafruit_featherwing import shared
@@ -50,6 +49,23 @@ def __init__(self):
5049 self ._pca = PCA9685 (shared .I2C_BUS , address = 0x60 )
5150 self ._pca .frequency = 1600
5251
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+
5369 @property
5470 def motor1 (self ):
5571 """:py:class:`~adafruit_motor.motor.DCMotor` controls for motor 1.
@@ -71,12 +87,7 @@ def motor1(self):
7187
7288 wing.motor1.throttle = 0
7389 """
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 )
8091
8192 @property
8293 def motor2 (self ):
@@ -99,12 +110,7 @@ def motor2(self):
99110
100111 wing.motor2.throttle = 0
101112 """
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 )
108114
109115 @property
110116 def motor3 (self ):
@@ -127,12 +133,7 @@ def motor3(self):
127133
128134 wing.motor3.throttle = 0
129135 """
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 )
136137
137138 @property
138139 def motor4 (self ):
@@ -155,12 +156,7 @@ def motor4(self):
155156
156157 wing.motor4.throttle = 0
157158 """
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 )
164160
165161 @property
166162 def stepper1 (self ):
@@ -181,6 +177,7 @@ def stepper1(self):
181177 for i in range(100):
182178 wing.stepper1.onestep()"""
183179 if not self ._stepper1 :
180+ from adafruit_motor import stepper
184181 if self ._motor1 or self ._motor2 :
185182 raise RuntimeError ("Cannot use stepper1 at the same time as motor1 or motor2." )
186183 self ._pca .channels [8 ].duty_cycle = 0xffff
@@ -209,6 +206,7 @@ def stepper2(self):
209206 wing.stepper2.onestep()
210207 """
211208 if not self ._stepper2 :
209+ from adafruit_motor import stepper
212210 if self ._motor3 or self ._motor4 :
213211 raise RuntimeError ("Cannot use stepper2 at the same time as motor3 or motor4." )
214212 self ._pca .channels [7 ].duty_cycle = 0xffff
0 commit comments