1
1
# 0. Introduction
2
2
3
- Drivers for switches and pushbuttons are provided, plus a retriggerable delay
4
- class . The switch and button drivers support debouncing. The switch driver
5
- provides for running a callback or launching a coroutine (coro) on contact
6
- closure and/or opening .
3
+ Drivers for switches and pushbuttons are provided. Switch and button drivers
4
+ support debouncing . The switch driver provides for running a callback or
5
+ launching a coroutine (coro) on contact closure and/or opening. The pushbutton
6
+ driver extends this to support long-press and double-click events .
7
7
8
- The pushbutton driver extends this to support long-press and double-click
9
- events.
8
+ An ` Encoder ` class is provided to support rotary control knobs based on
9
+ quadrature encoder switches. This is not intended for high throughput encoders
10
+ as used in CNC machines where
11
+ [ an interrupt based solution] ( https://github.com/peterhinch/micropython-samples#47-rotary-incremental-encoder )
12
+ is required.
10
13
11
14
The asynchronous ADC supports pausing a task until the value read from an ADC
12
15
goes outside defined bounds.
@@ -24,9 +27,11 @@ goes outside defined bounds.
24
27
5 . [ ADC monitoring] ( ./DRIVERS.md#5-adc-monitoring ) Pause until an ADC goes out of bounds
25
28
5.1 [ AADC class] ( ./DRIVERS.md#51-aadc-class )
26
29
5.2 [ Design note] ( ./DRIVERS.md#52-design-note )
27
- 6 . [ Additional functions] ( ./DRIVERS.md#6-additional-functions )
28
- 6.1 [ launch] ( ./DRIVERS.md#61-launch ) Run a coro or callback interchangeably
29
- 6.2 [ set_global_exception] ( ./DRIVERS.md#62-set_global_exception ) Simplify debugging with a global exception handler
30
+ 6 . [ Quadrature encoders] ( ./DRIVERS.md#6-quadrature-encoders )
31
+ 6.1 [ Encoder class] ( ./DRIVERS.md#61-encoder-class )
32
+ 7 . [ Additional functions] ( ./DRIVERS.md#7-additional-functions )
33
+ 7.1 [ launch] ( ./DRIVERS.md#71-launch ) Run a coro or callback interchangeably
34
+ 7.2 [ set_global_exception] ( ./DRIVERS.md#72-set_global_exception ) Simplify debugging with a global exception handler
30
35
31
36
###### [ Tutorial] ( ./TUTORIAL.md#contents )
32
37
@@ -333,9 +338,56 @@ this for applications requiring rapid response.
333
338
334
339
###### [ Contents] ( ./DRIVERS.md#1-contents )
335
340
336
- # 6. Additional functions
341
+ # 6. Quadrature encoders
342
+
343
+ The ` Encoder ` class is an asynchronous driver for control knobs based on
344
+ quadrature encoder switches such as
345
+ [ this Adafruit product] ( https://www.adafruit.com/product/377 ) . This is not
346
+ intended for high throughput encoders such as those used in CNC machines where
347
+ [ an interrupt based solution] ( https://github.com/peterhinch/micropython-samples#47-rotary-incremental-encoder )
348
+ is required. This is because the driver works by polling the switches. The
349
+ latency between successive readings of the switch state will depend on the
350
+ behaviour of other tasks in the application, but if changes occur rapidly it is
351
+ likely that transitions will be missed.
352
+
353
+ In the context of a rotary dial this is usually not a problem, firstly because
354
+ changes occur at a relatively low rate and secondly because there is usually
355
+ some form of feedback to the user. A single missed increment on a CNC machine
356
+ is a fail. In a user interface it usually is not.
357
+
358
+ The API uses a callback which occurs whenever the value changes. Alternatively
359
+ the ` Encoder ` may be queried to retrieve the current position.
360
+
361
+ A high throughput solution can be used with rotary dials but there is a
362
+ difference in the way contact bounce (or vibration induced jitter) are handled.
363
+ The high throughput solution results in +-1 count jitter with the callback
364
+ repeatedly occurring. This driver uses hysteresis to ensure that transitions
365
+ due to contact bounce are ignored.
366
+
367
+ ## 6.1 Encoder class
368
+
369
+ Constructor arguments:
370
+ 1 . ` pin_x ` Initialised ` machine.Pin ` instances for the switch. Should be set
371
+ as ` Pin.IN ` and have pullups.
372
+ 2 . ` pin_y ` Ditto.
373
+ 3 . ` v=0 ` Initial value.
374
+ 4 . ` vmin=None ` By default the ` value ` of the encoder can vary without limit.
375
+ Optionally maximum and/or minimum limits can be set.
376
+ 5 . ` vmax=None `
377
+ 6 . ` callback=lambda *_ : None ` Optional callback function. The callback
378
+ receives two args, ` v ` being the encoder's current value and ` fwd ` being
379
+ ` True ` if the value has incremented of ` False ` if it decremented. Further args
380
+ may be appended by the following.
381
+ 7 . ` args=() ` An optional tuple of args for the callback.
382
+
383
+ Synchronous method:
384
+ * ` value ` No args. Returns an integer being the ` Encoder ` current value.
337
385
338
- ## 6.1 Launch
386
+ ###### [ Contents] ( ./DRIVERS.md#1-contents )
387
+
388
+ # 7. Additional functions
389
+
390
+ ## 7.1 Launch
339
391
340
392
Import as follows:
341
393
``` python
@@ -347,7 +399,7 @@ runs it and returns the callback's return value. If a coro is passed, it is
347
399
converted to a ` task ` and run asynchronously. The return value is the ` task `
348
400
instance. A usage example is in ` primitives/switch.py ` .
349
401
350
- ## 6 .2 set_global_exception
402
+ ## 7 .2 set_global_exception
351
403
352
404
Import as follows:
353
405
``` python
0 commit comments