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
Analog('X1:sensor1_volts', convert_func=lambdax: x /4095*3.3)])
27
+
Analog('X1:sensor1_volts', convert_func=lambdax: x /4095*3.3)])
28
28
```
29
29
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.
30
30
@@ -55,9 +55,9 @@ I will be using the pyboard as a data acquistion peripheral for the Raspberry Pi
55
55
import time
56
56
from inputs import Manager, Digital, Counter, Analog
57
57
58
-
mgr = Manager([Digital('Y1:button1'),
58
+
mgr = Manager([Digital('Y1:button1'),
59
59
Counter('Y2'),
60
-
Analog('X1:sensor1_volts', convert_func=lambdax: x /4095*3.3)])
60
+
Analog('X1:sensor1_volts', convert_func=lambdax: x /4095*3.3)])
61
61
62
62
# wait to fill the Analog buffer with readings before reading the first time.
63
63
time.sleep(0.4)
@@ -70,9 +70,10 @@ while True:
70
70
And here is a sample of the output from the program:
If you want to access one individual input object, perhaps to read its value alone or change its descriptive name, you can do that through attribute access on the Manager object:
@@ -98,15 +99,92 @@ This section documents the public interface to classes in the micropython-inputs
98
99
This class holds the configured Input objects, periodically polls each input to update the input's value, and provides convenient ways to return the current value of inputs.
99
100
100
101
**Manager**(inputs, timer_num=1, poll_freq=480)
101
-
102
-
Arguments include:
102
+
Arguments for instantiating a Manager object include:
103
103
104
104
`inputs`: This is the list of input objects that the Manager will periodically poll and manage.
105
105
106
106
`timer_num`: The number of the microcontroller Timer that will be used to generate an interrupt for polling the inputs. If `None` is passed, no automatic polling of inputs will occur, and you will need to periodically call the `service_inputs()` method of this object, perhaps from your own Timer interrupt routine.
107
107
108
108
`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.
109
109
110
-
**Manager.values**()
110
+
**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`.
112
+
113
+
**Manager.service_inputs**()
114
+
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.
115
+
116
+
---
117
+
118
+
### Methods Common to All Input Classes
119
+
120
+
A number of methods are present on all the Input classes below, including:
121
+
122
+
InputClass.**value**()
123
+
This returns the current value for the input, including all processing that occurs for that input, e.g. debouncing, averaging.
124
+
125
+
InputClass.**key_name**()
126
+
This returns the pin name, e.g. 'X1', or, if a descriptive name for the input was provided, the descriptive name is returned instead.
127
+
128
+
InputClass.**service_input**()
129
+
If you are using one of the Input objects without use of the Manager object, this is the method that must be periodically called to update and process new input values.
130
+
131
+
---
132
+
133
+
### Digital class
134
+
This class handles a digital input pin, providing debouncing and the ability to have callback functions that run when the pin changes state.
Arguments for instantiating a Digital object include:
139
+
140
+
`pin_name`: The microcontroller pin name, such as 'X1' or 'Y2'. Optionally a descriptive name can be provided after a separating colon, e.g. 'X1: button1'. If the descriptive name is provided, it will be used instead of the pin name for accessing the input.
141
+
142
+
`pull`: A pull up or pull down resistor can be enabled on the pin by setting this argument to one of the pyb.Pin.PULL_ constants.
143
+
144
+
`convert_func`: A Digital input normally reads a 0 or a 1 value. If you want these two values translated to something else (even a string), provide the name of a conversion function here, or enter a Python lambda function.
145
+
146
+
`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.
147
+
148
+
`hl_func`: A callback function that will be run when the input stably transitions from a 1 value to a 0 value.
149
+
150
+
`lh_func`: A callback function that will be run when the input stably transitions from a 0 value to a 1 value.
Arguments for instantiating a Digital object include:
161
+
162
+
`pin_name`: The microcontroller pin name, such as 'X1' or 'Y2'. Optionally a descriptive name can be provided after a separating colon, e.g. 'X1: button1'. If the descriptive name is provided, it will be used instead of the pin name for accessing the input.
163
+
164
+
`pull`: A pull up or pull down resistor can be enabled on the pin by setting this argument to one of the pyb.Pin.PULL_ constants.
165
+
166
+
`convert_func`: The value returned by the Input object is the count that has accumulated. If you want this count value translated to something else, provide the name of a conversion function here, or enter a Python lambda function.
167
+
168
+
`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 4 readings in a row, and with the default polling frequency of 480 Hz (2.08 ms spacing), the reading must remain stable for about 8.3 ms to be considered valid. Electronic generated pulses and reed switch pulses have little or no bounce, so a small stable read count can be used to increase the maximum pulse rate that can be read. This argument must be set to a value of 30 stable readings or less.
169
+
170
+
`edges`: This argument determines whether the falling edge alone of the pulse is counted, or whether both the falling and rising edges are counted. It must be either the constant `Counter.ONE_EDGE` or the constant `Counter.BOTH_EDGES`.
171
+
172
+
`reset_on_read`: If True, the count is reset to 0 every time the count value is read through use of the `value()` method.
173
+
174
+
`rollover`: If the count reaches this value it will reset to 0. The default rollover value of 1073741823 is the largest that is possible.
175
+
176
+
**Counter.reset_count**()
177
+
This method will reset the count to zero.
178
+
179
+
---
180
+
181
+
### Analog class
182
+
183
+
**Analog**()
184
+
185
+
186
+
---
187
+
188
+
### AnalogDeviation class
111
189
112
-
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`.
0 commit comments