Skip to content

Commit e85bd22

Browse files
committed
ready to test
1 parent 1f5777c commit e85bd22

13 files changed

+151
-150
lines changed

README.rst

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,46 @@ In most cases you just need a couple of imports.
3737
3838
# This is a mock example showing typical usage of the library for each kind of device.
3939
40-
# crickit is a singleton object
41-
from adafruit_crickit.crickit import crickit
42-
43-
# Terminals have simple names like SIGNAL1, SERVO2, TOUCH3, MOTOR1A, NEOPIXEL,
44-
# CPX_DRIVE1, and FEATHER_DRIVE2.
45-
# Because the Drive terminals are numbered in reverse on the CPX Crickit vs the FeatherWing Crickit,
46-
# there are separate DRIVE names for CPX and FeatherWing Drive terminals.
47-
from adafruit_crickit.terminals import (SERVO1,
48-
MOTOR1A, MOTOR1B, MOTOR2A, MOTOR2B,
49-
CPX_DRIVE1, NEOPIXEL, SIGNAL1, SIGNAL2, TOUCH1)
40+
from adafruit_crickit import crickit
5041
5142
# Add this import if using stepper motors.
5243
# It will expose constants saying how to step: stepper.FORWARD, stepper.BACKWARD, etc.
5344
from adafruit_motor import stepper
5445
55-
servo1 = crickit.servo(SERVO1)
56-
servo1.angle = 90
46+
# Set servo 1 to 90 degrees
47+
crickit.servo_1.angle = 90
5748
58-
cservo1 = crickit.continuous_servo(SERVO1)
59-
cservo1.throttle = -0.5
49+
# Change servo settings.
50+
crickit.servo_1.actuation_range = 135
51+
crickit.servo_1.set_pulse_widths(min_pulse=850, max_pulse=2100)
6052
61-
motor = crickit.dc_motor(MOTOR1A, MOTOR1B)
62-
motor.throttle = 0.5
53+
# You can assign a device to a variable to get a shorter name.
54+
servo_2 = crickit.servo_2
55+
servo_2.throttle = 0
6356
64-
drive1 = crickit.pwm_out(CPX_DRIVE1)
65-
drive1.fraction = 1.0
57+
# Run a continous servo on Servo 2 backwards at half speed.
58+
crickit.continuous_servo_2.throttle = -0.5
6659
67-
# Note: On CPX Crickit, NeoPixel pin is normally connected to A1, not to seesaw,
68-
# so this demo would not control the NeoPixel terminal.
60+
# Run the motor on Motor 1 terminals at half speed.
61+
crickit.dc_motor_1.throttle = 0.5
6962
70-
# Strip or ring of 8 NeoPixels
71-
neopixels = crickit.neopixel(NEOPIXEL, 8)
72-
neopixels.fill((100, 100, 100))
63+
# Set Drive 1 terminal to 3/4 strength.
64+
crickit.drive_1.fraction = 0.75
7365
74-
# Write Signal terminal 1 and read Signal terminal 2.
75-
ss = crickit.seesaw
76-
ss.pin_mode(SIGNAL1, ss.OUTPUT)
77-
ss.pin_mode(SIGNAL2, ss.INPUT)
78-
ss.digital_write(SIGNAL1, True)
79-
print(ss.digital_read(SIGNAL2))
66+
if crickit.touch_1.value:
67+
print("Touched terminal Touch 1")
8068
8169
# A single stepper motor uses up all the motor terminals.
82-
stepper_motor = crickit.stepper_motor(MOTOR1A, MOTOR1B, MOTOR2A, MOTOR2B)
83-
stepper_motor.onestep(direction=stepper.FORWARD)
70+
crickit.stepper_motor.onestep(direction=stepper.FORWARD)
8471
85-
touch1 = crickit.touch(TOUCH1)
86-
if touch1.value:
87-
print("Touched terminal Touch 1")
72+
# You can also use the Drive terminals for a stepper motor
73+
crickit.drive_stepper_motor.onestep(direction=stepper.BACKWARD)
74+
75+
# Note: On CPX Crickit, NeoPixel pin is normally connected to A1, not to seesaw,
76+
# so this part of the demo cannot control the NeoPixel terminal.
77+
# Strip or ring of 8 NeoPixels
78+
crickit.init_neopixel(8)
79+
crickit.neopixel.fill((100, 100, 100))
8880
8981
9082
Contributing

adafruit_crickit.py

Lines changed: 79 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_crickit.crickit`
23+
`adafruit_crickit`
2424
==========================
2525
2626
Convenience library for using the Adafruit Crickit robotics boards.
@@ -51,31 +51,14 @@
5151
#pylint: disable=wrong-import-position
5252
sys.path.insert(0, ".frozen") # Prefer frozen modules over local.
5353

54-
from adafruit_seesaw.seesaw import Seesaw
55-
from adafruit_seesaw.pwmout import PWMOut
54+
from adafruit_seesaw.seesaw import Seesaw, PWMOut
5655
from adafruit_motor.servo import Servo, ContinuousServo
5756
from adafruit_motor.motor import DCMotor
5857
from adafruit_motor.stepper import StepperMotor
5958

6059
__version__ = "0.0.0-auto.0"
6160
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Crickit.git"
6261

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"""
7962

8063
_SERVO1 = const(17)
8164
_SERVO2 = const(16)
@@ -84,16 +67,14 @@
8467

8568
_MOTOR1 = (22, 23)
8669
_MOTOR2 = (19, 18)
87-
_MOTOR = _MOTOR1 + _MOTOR2
70+
_MOTOR_ALL = _MOTOR1 + _MOTOR2
8871

8972
_DRIVE1 = const(42)
9073
_DRIVE2 = const(43)
9174
_DRIVE3 = const(12)
9275
_DRIVE4 = const(13)
9376

94-
_DRIVE_SET = (_DRIVE1, _DRIVE2, _DRIVE3, _DRIVE4)
95-
96-
_PWM_SET = _MOTOR_SET | _SERVO_SET | _DRIVE_SET
77+
_DRIVE_ALL = (_DRIVE1, _DRIVE2, _DRIVE3, _DRIVE4)
9778

9879
_TOUCH1 = const(4)
9980
_TOUCH2 = const(5)
@@ -121,13 +102,56 @@ def value(self):
121102
return self.raw_value > self.threshold
122103

123104

105+
#pylint: disable=too-many-public-methods
124106
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+
126149
def __init__(self, seesaw):
127150
self._seesaw = seesaw
128151
# Associate terminal(s) with certain devices.
129152
# Used to find existing devices.
130153
self._devices = dict()
154+
self._neopixel = None
131155

132156
@property
133157
def seesaw(self):
@@ -148,42 +172,42 @@ def seesaw(self):
148172
@property
149173
def servo_1(self):
150174
"""``adafruit_motor.servo.Servo`` object on Servo 1 terminal"""
151-
return self._servo(_SERVO1)
175+
return self._servo(_SERVO1, Servo)
152176

153177
@property
154178
def servo_2(self):
155179
"""``adafruit_motor.servo.Servo`` object on Servo 2 terminal"""
156-
return self._servo(_SERVO2)
180+
return self._servo(_SERVO2, Servo)
157181

158182
@property
159183
def servo_3(self):
160184
"""``adafruit_motor.servo.Servo`` object on Servo 3 terminal"""
161-
return self._servo(_SERVO3)
185+
return self._servo(_SERVO3, Servo)
162186

163187
@property
164188
def servo_4(self):
165189
"""``adafruit_motor.servo.Servo`` object on Servo 4 terminal"""
166-
return self._servo(_SERVO4)
190+
return self._servo(_SERVO4, Servo)
167191

168192
@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)
172196

173197
@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)
177201

178202
@property
179203
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)
182206

183207
@property
184208
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)
187211

188212
def _servo(self, terminal, servo_class):
189213
device = self._devices.get(terminal, None)
@@ -195,56 +219,56 @@ def _servo(self, terminal, servo_class):
195219
return device
196220

197221
@property
198-
def motor_1(self):
222+
def dc_motor_1(self):
199223
"""``adafruit_motor.motor.DCMotor`` object on Motor 1 terminals"""
200224
return self._motor(_MOTOR1, DCMotor)
201225

202226
@property
203-
def motor_2(self):
227+
def dc_motor_2(self):
204228
"""``adafruit_motor.motor.DCMotor`` object on Motor 2 terminals"""
205229
return self._motor(_MOTOR2, DCMotor)
206230

207231
@property
208232
def stepper_motor(self):
209233
"""``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)
211235

212236
@property
213237
def drive_stepper_motor(self):
214238
"""``adafruit_motor.motor.StepperMotor`` object on Drive terminals"""
215-
return self._stepper_motor(_DRIVE_ALL, StepperMotor)
239+
return self._motor(_DRIVE_ALL, StepperMotor)
216240

217241
@property
218242
def feather_drive_stepper_motor(self):
219243
"""``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)
221245

222246
def _motor(self, terminals, motor_class):
223247
device = self._devices.get(terminals, None)
224248
if not isinstance(device, motor_class):
225249
device = motor_class(*(PWMOut(self._seesaw, terminal) for terminal in terminals))
226-
self._devices[terminal] = device
250+
self._devices[terminals] = device
227251
return device
228252

229253
@property
230254
def drive_1(self):
231255
"""``adafruit_seesaw.pwmout.PWMOut`` object on Drive 1 terminal, with ``frequency=1000``"""
232-
return _drive(_DRIVE1)
256+
return self._drive(_DRIVE1)
233257

234258
@property
235259
def drive_2(self):
236260
"""``adafruit_seesaw.pwmout.PWMOut`` object on Drive 2 terminal, with ``frequency=1000``"""
237-
return _drive(_DRIVE2)
261+
return self._drive(_DRIVE2)
238262

239263
@property
240264
def drive_3(self):
241265
"""``adafruit_seesaw.pwmout.PWMOut`` object on Drive 3 terminal, with ``frequency=1000``"""
242-
return _drive(_DRIVE3)
266+
return self._drive(_DRIVE3)
243267

244268
@property
245269
def drive_4(self):
246270
"""``adafruit_seesaw.pwmout.PWMOut`` object on Drive 4 terminal, with ``frequency=1000``"""
247-
return _drive(_DRIVE4)
271+
return self._drive(_DRIVE4)
248272

249273
feather_drive_1 = drive_4
250274
"""``adafruit_seesaw.pwmout.PWMOut`` object on Crickit Featherwing Drive 1 terminal,
@@ -264,7 +288,7 @@ def drive_4(self):
264288
"""
265289

266290
def _drive(self, terminal):
267-
device = self._devices.get(terminals, None)
291+
device = self._devices.get(terminal, None)
268292
if not isinstance(device, PWMOut):
269293
device = PWMOut(self._seesaw, terminal)
270294
device.frequency = 1000
@@ -274,25 +298,25 @@ def _drive(self, terminal):
274298
@property
275299
def touch_1(self):
276300
"""``adafruit_crickit.CrickitTouchIn`` object on Touch 1 terminal"""
277-
return _touch(_TOUCH1)
301+
return self._touch(_TOUCH1)
278302

279303
@property
280304
def touch_2(self):
281305
"""``adafruit_crickit.CrickitTouchIn`` object on Touch 2 terminal"""
282-
return _touch(_TOUCH2)
306+
return self._touch(_TOUCH2)
283307

284308
@property
285309
def touch_3(self):
286310
"""``adafruit_crickit.CrickitTouchIn`` object on Touch 3 terminal"""
287-
return _touch(_TOUCH3)
311+
return self._touch(_TOUCH3)
288312

289313
@property
290314
def touch_4(self):
291315
"""``adafruit_crickit.CrickitTouchIn`` object on Touch 4 terminal"""
292-
return _touch(_TOUCH4)
316+
return self._touch(_TOUCH4)
293317

294318
def _touch(self, terminal):
295-
touch_in = self._devices.get(terminals, None)
319+
touch_in = self._devices.get(terminal, None)
296320
if not touch_in:
297321
touch_in = CrickitTouchIn(self._seesaw, terminal)
298322
self._devices[terminal] = touch_in
@@ -301,7 +325,7 @@ def _touch(self, terminal):
301325
@property
302326
def neopixel(self):
303327
"""```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.
305329
"""
306330
if not self._neopixel:
307331
raise ValueError("Call init_neopixel first")

docs/api.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7-
.. automodule:: adafruit_crickit.terminals
8-
:members:
9-
10-
.. automodule:: adafruit_crickit.crickit
7+
.. automodule:: adafruit_crickit
118
:members:

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}
2828

2929
# Libraries we depend on but don't need for generating docs.
30-
autodoc_mock_imports = []
30+
autodoc_mock_imports = ['board', 'busio', 'micropython', 'adafruit_seesaw', 'adafruit_motor']
3131

3232
# Add any paths that contain templates here, relative to this directory.
3333
templates_path = ['_templates']

0 commit comments

Comments
 (0)