Skip to content

MagicMirrorModules/MMM-Buttons

 
 

Repository files navigation

MMM-Buttons

This is a module for MagicMirror² to act based on button presses via GPIO.

logo

It is capable of connecting multiple buttons at once, which can be individually configured. It is basically a generalized version of the Button module, original idea comes from @PtrBld.

However it only sends out notifications to other modules.

For example this can be used to send notifications to the following modules:

Installation

Clone this repository into your modules directory:

cd ~/MagicMirror/modules # adapt directory if you are using a different one
git clone https://github.com/MagicMirrorModules/MMM-Buttons

That's it! No npm install needed - this module has no external Node.js dependencies.

**Note:**This module uses gpiod tools for GPIO access, which is usually pre-installed on Raspberry Pi OS. If not, you can install it with:

sudo apt install gpiod

Update

To update the module, navigate to the module directory and pull the latest changes from GitHub:

cd ~/MagicMirror/modules/MMM-Buttons
git pull

Configuration

Add the module to your modules array in your config.js.

Below is a simple example (needs MMM-Remote-Control installed), with two buttons connected, on pins 24 and 25.

One switches on the display on a short press, and switches it off on a long press. The other does not do anything on a short press, but shuts down the system after keeping it pressed for 3 seconds with an explanatory user alert.

    {
      module: "MMM-Buttons",
      position: "bottom_left",
      config: {
        buttons: [
          {
            pin: 25,
            name: "monitor_control",
            longPress: [
              {
                title: "Monitor off",
                message: "Keep pressed for 3 seconds to switch the monitor down",
                imageFA: "display",
                notification: "REMOTE_ACTION",
                payload: {action: "MONITOROFF"}
              }
            ],
            shortPress: [
              {
                notification: "REMOTE_ACTION",
                payload: {action: "MONITORON"}
              }
            ]
          },
          {
            pin: 24,
            name: "power",
            longPress: [
              {
                title: "Power off",
                message: "Keep pressed for 3 seconds to shut down",
                imageFA: "power-off",
                notification: "REMOTE_ACTION",
                payload: {action: "SHUTDOWN"}
              }
            ]
          }
        ]
      }
    },

Module Configuration

Here is full documentation of options for the modules configuration:

Option Description
buttons An array of button configurations. See Button Configuration below. Default is [] (no buttons registered).
minShortPressTime Minimum duration to trigger a short press in ms. Default is 0.
maxShortPressTime Maximum duration to trigger a short press in ms. Default is 1000.
minLongPressTime Minimum time needed to trigger a long press in ms. Default is 3000. Any press duration between maxShortPressTime and minLongPressTime does not do anything.
bounceTimeout Hardware debounce in milliseconds passed to gpiomon (-p flag).
debugLimit How many events to keep in the debug list. Default is 5.

Debug View: If you set a position for the module (e.g., position: "bottom_left"), a debug list will be displayed showing recent button events with timing and actions. This is useful for testing your button setup. Remove the position to hide the debug view.

Debug View

Button Configuration

Each button configuration is an object with the following properties:

Property Description
pin Pin number of the button input (use BCM numbering).
name Name of the button for easier identification and log output.
activeLow Set to true if your button is active low (connects to GND when pressed, which is the typical setup with internal pull-up resistors). Set to false for active high buttons. Default is true.
longPress Choose what notification to send on a long press. See Notification Configuration below. Omit if nothing should trigger.
shortPress Choose what notification to send on a short press. See Notification Configuration below. Omit if nothing should trigger.

Notification Configuration

Each notification configuration is an array of objects with the following properties:

Property Description
notification Notification name.
payload Notification payload. Can be anything, for example a string or an object.
title, message, and imageFA Optional (only for long press notifications): If you want to display a message before executing set its options here. See Alert documentation for their meaning.

Alert Examples:

Monitor Off Shutdown
Alert Monitor Off Alert Shutdown

Contributing

If you find any problems, bugs or have questions, please open a GitHub issue in this repository.

Pull requests are of course also very welcome 🙂

Code of Conduct

Please see the Code of Conduct. By contributing you agree to its terms.

Developer commands

  • node --run lint - Run linting and formatter checks.
  • node --run lint:fix - Fix linting and formatter issues.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

All notable changes to this project will be documented in the CHANGELOG.md file.

About

This module can be used to to connect buttons to your MagicMirror².

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Contributors

Languages

  • JavaScript 100.0%