mnishiguchi
Nerves Core Team
I detect the button push by using Circuits.GPIO.set_interrupts(gpio_ref, :rising); however it is too sensitive that I want to debounce it. I am thinking about setting a condition using the second data item in the message tuple {:circuits_gpio, 17, 1233456, 1} for controlling the sensitivity. Is it good? Is there a better way?
My target is Rpi4. My button is a 6mm mini button.
defmodule LedBlinker.Button do
use GenServer, restart: :temporary
@debounce_time 200_000_000
def start_link({gpio_pin, on_push_fn}) when is_number(gpio_pin) and is_function(on_push_fn) do
GenServer.start_link(__MODULE__, {gpio_pin, on_push_fn})
end
def init({gpio_pin, on_push_fn}) do
# Get a ref to the GPIO pin.
{:ok, gpio_ref} = Circuits.GPIO.open(gpio_pin, :input)
# Get messages every time the button is pushed.
Circuits.GPIO.set_interrupts(gpio_ref, :rising)
{
:ok,
%{
gpio_pin: gpio_pin,
gpio_ref: gpio_ref,
last_pushed_at: 0,
on_push_fn: on_push_fn
}
}
end
def handle_info({:circuits_gpio, _, at, 1} = message, state) do
%{last_pushed_at: last_pushed_at, on_push_fn: on_push_fn} = state
# Debounce the message.
if @debounce_time < at - last_pushed_at do
on_push_fn.(message)
end
{:noreply, %{state | last_pushed_at: at}}
end
end
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
ondrej-tucek
I’d use send_after & cancel_timer or sleep functions instead of if statement. For example:
Process.send_after(self(), {:debounce, on_push_fn, msg}, @debounce_time)+handle_infofor:debounce.mattludwigs
I think either way should get you what you need and probably more up to person prefference.
I like the
.
ifstatement appoarch. Speaking from my expereince, having to manage timers and sleeps normally does not work out too well for me. However, that might just be user error on my partmnishiguchi
@ondrej-tucek @mattludwigs Thank you for your input. It was helpful.
After all, I realized that I could use Rpi’s internal pull-down resister, which I had not known is used for at that time.
Circuits.GPIO.set_pull_mode(gpio, pull_mode)handles it all without adding any physical resister! I mean sensitivity feels right without any tuning. It makes my genserver simpler.It was still a great opportunity to learn how I could control the sensitivity of incoming data.
dominicletz
Cool, I think you’ve found the best solution. Just in case though I wanted to provider another option. I’ve had a couple of cases where I needed debounce behaviour and implement a module:
if you add this module to your dependencies:
you can just do:
And it will ensure that the event identified by the first argument (
:circuits_gpio) is not triggered more often than every 5_000 millisecond.There is actually a range of different debounce behaviors I needed in some of my projects so the module implements four different functions. All of them take 3 argument: (event_key, function, timeout)
Or here in a text graph:
Hope this helps some readers.
mnishiguchi
Wow, nice! Thanks. I will definitely keep that in my toolbox.