20
20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
# THE SOFTWARE.
22
22
"""
23
- `adafruit_crickit.crickit `
23
+ `adafruit_crickit`
24
24
==========================
25
25
26
26
Convenience library for using the Adafruit Crickit robotics boards.
51
51
#pylint: disable=wrong-import-position
52
52
sys .path .insert (0 , ".frozen" ) # Prefer frozen modules over local.
53
53
54
- from adafruit_seesaw .seesaw import Seesaw
55
- from adafruit_seesaw .pwmout import PWMOut
54
+ from adafruit_seesaw .seesaw import Seesaw , PWMOut
56
55
from adafruit_motor .servo import Servo , ContinuousServo
57
56
from adafruit_motor .motor import DCMotor
58
57
from adafruit_motor .stepper import StepperMotor
59
58
60
59
__version__ = "0.0.0-auto.0"
61
60
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Crickit.git"
62
61
63
- SIGNAL1 = const (2 )
64
- """Signal 1 terminal"""
65
- SIGNAL2 = const (3 )
66
- """Signal 2 terminal"""
67
- SIGNAL3 = const (40 )
68
- """Signal 3 terminal"""
69
- SIGNAL4 = const (41 )
70
- """Signal 4 terminal"""
71
- SIGNAL5 = const (11 )
72
- """Signal 5 terminal"""
73
- SIGNAL6 = const (10 )
74
- """Signal 6 terminal"""
75
- SIGNAL7 = const (9 )
76
- """Signal 7 terminal"""
77
- SIGNAL8 = const (8 )
78
- """Signal 8 terminal"""
79
62
80
63
_SERVO1 = const (17 )
81
64
_SERVO2 = const (16 )
84
67
85
68
_MOTOR1 = (22 , 23 )
86
69
_MOTOR2 = (19 , 18 )
87
- _MOTOR = _MOTOR1 + _MOTOR2
70
+ _MOTOR_ALL = _MOTOR1 + _MOTOR2
88
71
89
72
_DRIVE1 = const (42 )
90
73
_DRIVE2 = const (43 )
91
74
_DRIVE3 = const (12 )
92
75
_DRIVE4 = const (13 )
93
76
94
- _DRIVE_SET = (_DRIVE1 , _DRIVE2 , _DRIVE3 , _DRIVE4 )
95
-
96
- _PWM_SET = _MOTOR_SET | _SERVO_SET | _DRIVE_SET
77
+ _DRIVE_ALL = (_DRIVE1 , _DRIVE2 , _DRIVE3 , _DRIVE4 )
97
78
98
79
_TOUCH1 = const (4 )
99
80
_TOUCH2 = const (5 )
@@ -121,13 +102,56 @@ def value(self):
121
102
return self .raw_value > self .threshold
122
103
123
104
105
+ #pylint: disable=too-many-public-methods
124
106
class Crickit :
125
- """Represents a Crickit board."""
107
+ """Represents a Crickit board. Provides a number of devices available via properties, such as
108
+ ``servo_1``. Devices are created on demand the first time they are referenced.
109
+
110
+ It's fine to refer a device multiple times via its property, but it's faster and results
111
+ in more compact code to assign a device to a variable.
112
+
113
+ .. code-block:: python
114
+
115
+ import time
116
+ from adafruit_crickit import crickit
117
+
118
+ # This is fine:
119
+ crickit.servo_1.angle = 0
120
+ time.sleep(1)
121
+ crickit.servo_1.angle = 90
122
+ time.sleep(1)
123
+
124
+ # This is slightly faster and more compact:
125
+ servo_1 = crickit.servo_1
126
+ servo_1.angle = 0
127
+ time.sleep(1)
128
+ servo_1.angle = 90
129
+ time.sleep(1)
130
+ """
131
+
132
+ SIGNAL1 = 2
133
+ """Signal 1 terminal"""
134
+ SIGNAL2 = 3
135
+ """Signal 2 terminal"""
136
+ SIGNAL3 = 40
137
+ """Signal 3 terminal"""
138
+ SIGNAL4 = 41
139
+ """Signal 4 terminal"""
140
+ SIGNAL5 = 11
141
+ """Signal 5 terminal"""
142
+ SIGNAL6 = 10
143
+ """Signal 6 terminal"""
144
+ SIGNAL7 = 9
145
+ """Signal 7 terminal"""
146
+ SIGNAL8 = 8
147
+ """Signal 8 terminal"""
148
+
126
149
def __init__ (self , seesaw ):
127
150
self ._seesaw = seesaw
128
151
# Associate terminal(s) with certain devices.
129
152
# Used to find existing devices.
130
153
self ._devices = dict ()
154
+ self ._neopixel = None
131
155
132
156
@property
133
157
def seesaw (self ):
@@ -148,42 +172,42 @@ def seesaw(self):
148
172
@property
149
173
def servo_1 (self ):
150
174
"""``adafruit_motor.servo.Servo`` object on Servo 1 terminal"""
151
- return self ._servo (_SERVO1 )
175
+ return self ._servo (_SERVO1 , Servo )
152
176
153
177
@property
154
178
def servo_2 (self ):
155
179
"""``adafruit_motor.servo.Servo`` object on Servo 2 terminal"""
156
- return self ._servo (_SERVO2 )
180
+ return self ._servo (_SERVO2 , Servo )
157
181
158
182
@property
159
183
def servo_3 (self ):
160
184
"""``adafruit_motor.servo.Servo`` object on Servo 3 terminal"""
161
- return self ._servo (_SERVO3 )
185
+ return self ._servo (_SERVO3 , Servo )
162
186
163
187
@property
164
188
def servo_4 (self ):
165
189
"""``adafruit_motor.servo.Servo`` object on Servo 4 terminal"""
166
- return self ._servo (_SERVO4 )
190
+ return self ._servo (_SERVO4 , Servo )
167
191
168
192
@property
169
- def continous_servo_1 (self ):
170
- """``adafruit_motor.servo.Continous_Servo `` object on Servo 1 terminal"""
171
- return self ._servo (_SERVO1 , Continous_Servo )
193
+ def continuous_servo_1 (self ):
194
+ """``adafruit_motor.servo.ContinuousServo `` object on Servo 1 terminal"""
195
+ return self ._servo (_SERVO1 , ContinuousServo )
172
196
173
197
@property
174
- def continous_servo_2 (self ):
175
- """``adafruit_motor.servo.Continous_Servo `` object on Servo 2 terminal"""
176
- return self ._servo (_SERVO2 , Continous_Servo )
198
+ def continuous_servo_2 (self ):
199
+ """``adafruit_motor.servo.ContinuousServo `` object on Servo 2 terminal"""
200
+ return self ._servo (_SERVO2 , ContinuousServo )
177
201
178
202
@property
179
203
def continuous_servo_3 (self ):
180
- """``adafruit_motor.servo.Continuous_Servo `` object on Servo 3 terminal"""
181
- return self ._servo (_SERVO3 , Continuous_Servo )
204
+ """``adafruit_motor.servo.ContinuousServo `` object on Servo 3 terminal"""
205
+ return self ._servo (_SERVO3 , ContinuousServo )
182
206
183
207
@property
184
208
def continuous_servo_4 (self ):
185
- """``adafruit_motor.servo.Continuous_Servo `` object on Servo 4 terminal"""
186
- return self ._servo (_SERVO4 , Continuous_Servo )
209
+ """``adafruit_motor.servo.ContinuousServo `` object on Servo 4 terminal"""
210
+ return self ._servo (_SERVO4 , ContinuousServo )
187
211
188
212
def _servo (self , terminal , servo_class ):
189
213
device = self ._devices .get (terminal , None )
@@ -195,56 +219,56 @@ def _servo(self, terminal, servo_class):
195
219
return device
196
220
197
221
@property
198
- def motor_1 (self ):
222
+ def dc_motor_1 (self ):
199
223
"""``adafruit_motor.motor.DCMotor`` object on Motor 1 terminals"""
200
224
return self ._motor (_MOTOR1 , DCMotor )
201
225
202
226
@property
203
- def motor_2 (self ):
227
+ def dc_motor_2 (self ):
204
228
"""``adafruit_motor.motor.DCMotor`` object on Motor 2 terminals"""
205
229
return self ._motor (_MOTOR2 , DCMotor )
206
230
207
231
@property
208
232
def stepper_motor (self ):
209
233
"""``adafruit_motor.motor.StepperMotor`` object on Motor 1 and Motor 2 terminals"""
210
- return self ._stepper_motor (_MOTOR_ALL , StepperMotor )
234
+ return self ._motor (_MOTOR_ALL , StepperMotor )
211
235
212
236
@property
213
237
def drive_stepper_motor (self ):
214
238
"""``adafruit_motor.motor.StepperMotor`` object on Drive terminals"""
215
- return self ._stepper_motor (_DRIVE_ALL , StepperMotor )
239
+ return self ._motor (_DRIVE_ALL , StepperMotor )
216
240
217
241
@property
218
242
def feather_drive_stepper_motor (self ):
219
243
"""``adafruit_motor.motor.StepperMotor`` object on Drive terminals on Crickit FeatherWing"""
220
- return self ._stepper_motor (reversed (_DRIVE_ALL ), StepperMotor )
244
+ return self ._motor (reversed (_DRIVE_ALL ), StepperMotor )
221
245
222
246
def _motor (self , terminals , motor_class ):
223
247
device = self ._devices .get (terminals , None )
224
248
if not isinstance (device , motor_class ):
225
249
device = motor_class (* (PWMOut (self ._seesaw , terminal ) for terminal in terminals ))
226
- self ._devices [terminal ] = device
250
+ self ._devices [terminals ] = device
227
251
return device
228
252
229
253
@property
230
254
def drive_1 (self ):
231
255
"""``adafruit_seesaw.pwmout.PWMOut`` object on Drive 1 terminal, with ``frequency=1000``"""
232
- return _drive (_DRIVE1 )
256
+ return self . _drive (_DRIVE1 )
233
257
234
258
@property
235
259
def drive_2 (self ):
236
260
"""``adafruit_seesaw.pwmout.PWMOut`` object on Drive 2 terminal, with ``frequency=1000``"""
237
- return _drive (_DRIVE2 )
261
+ return self . _drive (_DRIVE2 )
238
262
239
263
@property
240
264
def drive_3 (self ):
241
265
"""``adafruit_seesaw.pwmout.PWMOut`` object on Drive 3 terminal, with ``frequency=1000``"""
242
- return _drive (_DRIVE3 )
266
+ return self . _drive (_DRIVE3 )
243
267
244
268
@property
245
269
def drive_4 (self ):
246
270
"""``adafruit_seesaw.pwmout.PWMOut`` object on Drive 4 terminal, with ``frequency=1000``"""
247
- return _drive (_DRIVE4 )
271
+ return self . _drive (_DRIVE4 )
248
272
249
273
feather_drive_1 = drive_4
250
274
"""``adafruit_seesaw.pwmout.PWMOut`` object on Crickit Featherwing Drive 1 terminal,
@@ -264,7 +288,7 @@ def drive_4(self):
264
288
"""
265
289
266
290
def _drive (self , terminal ):
267
- device = self ._devices .get (terminals , None )
291
+ device = self ._devices .get (terminal , None )
268
292
if not isinstance (device , PWMOut ):
269
293
device = PWMOut (self ._seesaw , terminal )
270
294
device .frequency = 1000
@@ -274,25 +298,25 @@ def _drive(self, terminal):
274
298
@property
275
299
def touch_1 (self ):
276
300
"""``adafruit_crickit.CrickitTouchIn`` object on Touch 1 terminal"""
277
- return _touch (_TOUCH1 )
301
+ return self . _touch (_TOUCH1 )
278
302
279
303
@property
280
304
def touch_2 (self ):
281
305
"""``adafruit_crickit.CrickitTouchIn`` object on Touch 2 terminal"""
282
- return _touch (_TOUCH2 )
306
+ return self . _touch (_TOUCH2 )
283
307
284
308
@property
285
309
def touch_3 (self ):
286
310
"""``adafruit_crickit.CrickitTouchIn`` object on Touch 3 terminal"""
287
- return _touch (_TOUCH3 )
311
+ return self . _touch (_TOUCH3 )
288
312
289
313
@property
290
314
def touch_4 (self ):
291
315
"""``adafruit_crickit.CrickitTouchIn`` object on Touch 4 terminal"""
292
- return _touch (_TOUCH4 )
316
+ return self . _touch (_TOUCH4 )
293
317
294
318
def _touch (self , terminal ):
295
- touch_in = self ._devices .get (terminals , None )
319
+ touch_in = self ._devices .get (terminal , None )
296
320
if not touch_in :
297
321
touch_in = CrickitTouchIn (self ._seesaw , terminal )
298
322
self ._devices [terminal ] = touch_in
@@ -301,7 +325,7 @@ def _touch(self, terminal):
301
325
@property
302
326
def neopixel (self ):
303
327
"""```adafruit_seesaw.neopixel`` object on NeoPixel terminal.
304
- Raises ValueError if `init_neopixel` has not been called.
328
+ Raises ValueError if `` init_neopixel` ` has not been called.
305
329
"""
306
330
if not self ._neopixel :
307
331
raise ValueError ("Call init_neopixel first" )
0 commit comments