Skip to content

Commit 56d6973

Browse files
committed
Edited Docs and improved Example code.
1 parent d01793d commit 56d6973

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# micropython-inputs
2-
This Micro Python library facilitates reading digital and analog inputs on the pyboard or other microcontrollers running [Micro Python](http://micropython.org/), a variant of the Python 3 programming language that runs on some microcontrollers. These library routines were tested on a [pyboard, available here](https://micropython.org/store/#/store). Some of the notable features of this library are:
2+
This Micro Python library facilitates reading digital and analog inputs on the pyboard or other microcontrollers running [Micro Python](http://micropython.org/), a variant of the Python 3 programming language. These library routines were tested on a [pyboard, available here](https://micropython.org/store/#/store). Some of the notable features of this library are:
33

44
* Digital input pins are debounced so transitions are detected cleanly. Debouncing parameters are controllable by the user.
55
* Digital input pins can be configured as counters for counting pulse trains. Either one or both edges of the pulses can be counted, and debouncing is present to clean up reed switch closures.
6-
* Analog readings are averaged across a user-selectable number of recent readings spaced at 2.1 ms (configurable) intervals. Noise on the analog line can be significantly suppressed with this averaging technique.
6+
* Analog readings are averaged across a user-selectable number of recent readings spaced at 2.1 ms (user configurable) intervals. Noise on the analog line can be significantly suppressed with this averaging technique.
77
* Current values from the pins are easily accessible through a Python dictionary, keyed by either the Pin name or a more descriptive name you can assign to the pin.
88

99
## Quickstart
@@ -28,9 +28,9 @@ mgr = Manager([Digital('Y1: button1', hl_func=hl_occurred),
2828
```
2929
The first argument passed to constructor of each Input object is the name of the pin to use for the input. For example, the Counter input uses the Y2 pin. Optionally, a more descriptive name can be added after a colon. The Digital input uses Pin Y1 and is labeled `button1`. If a descriptive name is provided, it will be used for all labeling and accessing of the input.
3030

31-
Each Input object type has a number of configuration options, all with default values except for the required `pin_name` argument. Some of the configuration options are shown in the example. For the Digital input, a function is passed in that will be called when the input makes a clean transition from High (1) to Low (0). For the Analog input, a conversion function is passed in that takes the raw 0 - 4095 reading from the Analog pin and converts it to a voltage value. Either a lambda function, as shown here, or a normal multi-line `def` function name can be passed.
31+
Each Input object type has a number of configuration options, all with default values except for the required `pin_name` argument. Some of the configuration options are shown in the example. For the Digital input in this example, a function is passed that will be called when the input makes a clean transition from High (1) to Low (0). For the Analog input, a conversion function is passed that takes the raw 0 - 4095 reading from the Analog pin and converts it to a voltage value. Either a lambda function, as shown here, or a normal multi-line `def` function name can be passed.
3232

33-
After the Manager object is created, it automatically starts polling the input objects at the default rate of 480 Hz (a 2x or greater multiple of 60 Hz will help 60 Hz noise to be filtered on Analog lines). Each input is read and processed according to the type of input it is.
33+
After the Manager object is created, it automatically starts polling the input objects at the default rate of 480 Hz (a 2x or greater multiple of 60 Hz will help filter 60 Hz noise on Analog lines). Each input is read and processed according to the type of input it is.
3434

3535
At any time, the current values of all the inputs can be read by executing the `values()` method on the Manager object. The return object is a Python dictionary with the added feature that values can be read as attributes of the object:
3636

@@ -108,7 +108,7 @@ Arguments for instantiating a Manager object include:
108108
`poll_freq`: The frequency that will be used to poll the inputs in Hz. The default of 480 Hz will poll each sensor every 2.08 ms, which is a convenient value for debounce routines discussed later and analog averaging routines that sample across an exact number of 60 Hz cycles.
109109

110110
**Manager.values**()
111-
This returns a snapshot of all of the current inputs. The return object is a Python dictionary with added feature of being able to use an attribute to access a value as well as standard dictionary syntax. If `vals` is the object returned by this method, these two read access means are equivalent: `vals['X1']` and `vals.X1`.
111+
This returns a snapshot of all of the current inputs. The return object is a Python dictionary with the added feature of being able to use an attribute to access a value as well as standard dictionary syntax. If `vals` is the object returned by this method, these two read access means are equivalent: `vals['X1']` and `vals.X1`.
112112

113113
**Manager.service_inputs**()
114114
This routine reads and services all of the inputs and does not normally need to be used; it is generally called from a Timer interrupt internally set up by the Manager object. However, if this internal Timer is disabled by passing `None` to the `timer_num` constructor argument, a user can use their own Timer interrupt to periodically call this `service_inputs()` method.
@@ -145,9 +145,13 @@ Arguments for instantiating a Digital object include:
145145

146146
`stable_read_count`: The digital input pin is read repeatedly at a rate determined by the `poll_freq` value passed to the Manager class. To debounce the input, a changed input value must remain the same for `stable_read_count` readings. If so, a state changed is deemed to occur. The default value is 12 readings in a row, and with the default polling frequency of 480 Hz (2.08 ms spacing), the reading must remain stable for about 25 ms to be considered valid. This argument must be set to a value of 30 stable readings or less.
147147

148-
`hl_func`: A callback function that will be run when the input stably transitions from a 1 value to a 0 value.
148+
`hl_func`: A callback function that will be run when the input stably transitions from a 1 value to a 0 value. This callback function is run
149+
from inside a Timer interrupt routine, so do not consume much time in
150+
the function you provide, as it will block future interrupts.
149151

150-
`lh_func`: A callback function that will be run when the input stably transitions from a 0 value to a 1 value.
152+
`lh_func`: A callback function that will be run when the input stably transitions from a 0 value to a 1 value. This callback function is run
153+
from inside a Timer interrupt routine, so do not consume much time in
154+
the function you provide, as it will block future interrupts.
151155

152156
---
153157

examples.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1+
# Example of using the "inputs" library with the Micro Python pyboard.
2+
13
import time
24
from inputs import Manager, Digital, Counter, Analog
35

6+
# List the desired inputs and pass them to a Manager object.
47
mgr = Manager([Digital('Y1:button1'),
58
Counter('Y2'),
69
Analog('X1:sensor1_volts', convert_func=lambda x: x / 4095 * 3.3)])
710

8-
# wait to fill the Analog buffer with readings before reading the first time.
11+
# The manager object immediately starts polling the inputs after instantiation.
12+
13+
# wait until the Analog input reading buffer before reading all the inputs the
14+
# first time.
915
time.sleep(0.4)
16+
1017
while True:
18+
# get a snapshot of all the current input values
1119
vals = mgr.values()
12-
print(vals)
20+
21+
print(vals) # prints the entire dictionary of readings
22+
23+
# prints two individual readings, using normal dictionary access and also
24+
# attribute access.
25+
print(vals['button1'], vals.sensor1_volts, '\n')
26+
1327
time.sleep(1)

0 commit comments

Comments
 (0)