You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-6Lines changed: 28 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ This micropython library facilitates reading digital and analog inputs on the py
8
8
9
9
## Quickstart
10
10
11
-
Here is some code that demonstrates how the library is used:
11
+
Suppose we need to set up an input to detect button presses, a counter to count the pulses from a water meter utilizing a dry contact reed switch, and an Analog input to measure a sensor voltage. Here is the setup code:
12
12
13
13
```Python
14
14
from inputs import Manager, Digital, Counter, Analog
@@ -20,11 +20,33 @@ def hl_occurred():
20
20
21
21
22
22
# the Manager class holds the input objects and polls them at a regular
23
-
# interval from a Timer interrupt.
24
-
# Here we set up a Digital Input, a Counter Input, and an Analog Input by
25
-
# passing a list of input objects to the Manager constructor.
23
+
# interval from a Timer interrupt. We pass a list of the three needed
Analog('X1:sensor1_volts', convert_func=lambdax: x /4095*3.3)])
28
+
```
29
+
The first parameter 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.
29
30
30
-
```
31
+
Each input type has a number of configuration options, all with default values except from the required pin name. 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.
32
+
33
+
After the Manager object is created, it automatically starts polling the input objects at the default rate of 480 Hz (an even multiple of 60 Hz to cause 60 Hz noise to be filtered on Analog lines). Each input is read and processed according to the type of input it is.
34
+
35
+
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 superset of a Python dictionary, also allowing access to values through attributes:
36
+
37
+
```Python
38
+
# get all of the current input values
39
+
vals = mgr.values()
40
+
41
+
# two different ways of accessing the current counter value on pin Y2.
42
+
print(vals['Y2'])
43
+
print(vals.Y2)
44
+
45
+
# for pins with descriptive names, you must use the descriptive name to access
46
+
# the input value. Note that if the descriptive name contains spaces,
47
+
# attribute access will not work, and instead, standard dictionary access
48
+
# should be used: vals['descriptive name']
49
+
print(vals.button1)
50
+
```
51
+
52
+
I will be using the pyboard as a data acquistion peripheral for the Raspberry Pi. A simple way to transfer the input value to the Pi is to print the `vals` dictionary. When the Pi receives the string representation of the dictionary, a call to `eval()` will convert the string back into a dictionary.
0 commit comments