Buttons are one form of input, but often we want to read data from a GPIO line driven by an electronic device and decode the data. This usually means measuring the width of the pulses and this raises the question of how fast can we accept input?
The simplest way to find out how quickly we can take a measurement is to perform a pulse width measurement. Applying a square wave to GP22 we can measure the time that the pulse is high using:
#include <stdio.h>
#include "pico/stdlib.h"
int main() {
stdio_init_all();
gpio_set_function(22, GPIO_FUNC_SIO);
gpio_set_dir(22, false);
uint64_t t;
while (true) {
while (gpio_get(22) == 1) {};
while (gpio_get(22) == 0) {};
t = time_us_64();
while (gpio_get(22) == 1) {};
t = (uint64_t)(time_us_64() - t);
printf("%llu\n", t);
sleep_ms(1000);
} }
This might look a little strange at first. The inner while loops are responsible for getting us to the right point in the waveform. First we loop until the line goes low, then we loop until it goes high again and finally measure how long before it goes low. You might think that we simply have to wait for it to go high and then measure how long till it goes low, but this misses the possibility that the signal might be part way through a high period when we first measure it. This can be measured down to around 1 microsecond with an accuracy of 0.5 µs with the accuracy poor at the low end of the range.
Notice that in either case if you try measuring pulse widths much shorter than the lower limit that works, you will get results that look like longer pulses are being applied. The reason is simply that the Pico will miss the first transition to zero but will detect a second or third or later transition. This is the digital equivalent of the aliasing effect found in the Fourier Transform or general signal processing.
In chapter but not in this extract
The Finite State Machine
FSM Button
FSM Hold Button
FSM Ring Counter
Summary
Input is hard because things happen at any time, irrespective of what your program might be doing.
You can call gpio_get at any time to discover the state of a GPIO line – the problem is when and how often to call it.
You can chose between external or internal pull-up/pull-down resistors.
Mechanical input devices such as buttons have to be debounced.
The power of software is that it can enhance a simple device. A simple button is either pushed or released, but you can use this to generate a third “held” state.
Using a polling loop you can handle inputs as short as a few tens of microseconds.
Most IoT programs are best written as a polling loop.
The Finite State Machine (FSM) is one way of organizing a complex polling loop so that inputs are tested and outputs are set once for each time through the loop.
Ideally the states of a FSM should not be simple statements of the inputs of outputs that determine the state, but for simple systems this can be difficult to achieve.
It can be difficult to work out what constitutes the events of a FSM. Ideally they should be localized in time so that they indicate the moment that something happens.
TestSprite has announced an early access beta program for its end-to-end QA tool, along with $1.5 million pre-seed funding aimed at accelerating product development, expanding the team, and scaling op [ ... ]