{"id":19167390,"url":"https://github.com/aditya-a-garwal/mbed-keypad","last_synced_at":"2026-04-16T00:32:27.603Z","repository":{"id":213042657,"uuid":"732878195","full_name":"Aditya-A-garwal/mbed-Keypad","owner":"Aditya-A-garwal","description":"A simple library to use popular 4x4 Keypads with ARM MBed OS and ARM Cortex M Microcontrollers ","archived":false,"fork":false,"pushed_at":"2023-12-23T17:11:37.000Z","size":115,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-05T10:45:43.674Z","etag":null,"topics":["arm","async","button","c","cortex-m","cpp","embedded","keypad","mbed","mbed-os","rtos","sensor"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Aditya-A-garwal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-12-18T04:11:25.000Z","updated_at":"2024-08-09T17:52:07.000Z","dependencies_parsed_at":"2023-12-18T05:47:01.768Z","dependency_job_id":"0a6118a5-4ba1-4fd1-85a5-625e63e33476","html_url":"https://github.com/Aditya-A-garwal/mbed-Keypad","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":"0.11111111111111116","last_synced_commit":"eda06729329d3f88ca972f4cc8ca198fe1e14d1c"},"previous_names":["aditya-a-garwal/mbed-keypad"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Aditya-A-garwal/mbed-Keypad","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aditya-A-garwal%2Fmbed-Keypad","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aditya-A-garwal%2Fmbed-Keypad/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aditya-A-garwal%2Fmbed-Keypad/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aditya-A-garwal%2Fmbed-Keypad/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Aditya-A-garwal","download_url":"https://codeload.github.com/Aditya-A-garwal/mbed-Keypad/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Aditya-A-garwal%2Fmbed-Keypad/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31866329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-15T15:24:51.572Z","status":"ssl_error","status_checked_at":"2026-04-15T15:24:39.138Z","response_time":63,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arm","async","button","c","cortex-m","cpp","embedded","keypad","mbed","mbed-os","rtos","sensor"],"created_at":"2024-11-09T09:37:12.310Z","updated_at":"2026-04-16T00:32:27.582Z","avatar_url":"https://github.com/Aditya-A-garwal.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mbed-Keypad\n\n![GitHub License](https://img.shields.io/github/license/Aditya-A-garwal/mbed-Keypad)\n![GitHub forks](https://img.shields.io/github/forks/Aditya-A-garwal/mbed-Keypad?style=flat-square\u0026color=blue)\n![GitHub Repo stars](https://img.shields.io/github/stars/Aditya-A-garwal/mbed-Keypad?style=flat-square\u0026color=blue)\n![GitHub issues](https://img.shields.io/github/issues-raw/Aditya-A-garwal/mbed-Keypad?style=flat-square\u0026color=indianred)\n![GitHub closed issues](https://img.shields.io/github/issues-closed-raw/Aditya-A-garwal/mbed-Keypad?style=flat-square)\n![GitHub pull requests](https://img.shields.io/github/issues-pr/Aditya-A-garwal/mbed-Keypad?style=flat-square\u0026color=indianred)\n![GitHub closed pull requests](https://img.shields.io/github/issues-pr-closed/Aditya-A-garwal/mbed-Keypad?style=flat-square)\n\n## Overview\n\nThis repository contains a simple library to use a *4x4 Keypad* with MBed OS. The library **automatically debounces** the button presses and **can distinguish between short and long presses**. The library's design allows only recognizes a single button being pressed at a time. Pressing multiple buttons while a single one is held down will cause the latter pressed to be rejected. A state-machine diagram is included in this README to explain this.\n\nThe library provides *blocking* \u0026 *non-blocking* APIs, and allows different keypads connected to the same MCU to use different techniques at the same time. The dimensions of they keypad can also be increased or reduced as required. A more detailed explanation is found in the next sections.\n\n## Usage\n\nThe process to use the library in your own *MBed CLI/MBed CE* projects consists of a few simple steps. This section assumes that CMake is being used as the build system. For other build systems/IDEs, use the source and header files directly and refer to their manuals.\n\nThe steps to use the library in your own projects are shown below -\n\n1. Create a file named ```external/CMakeLists.txt``` in your project root with the following content. This will fetch the repository and add its targets to the project.\n\n    ```diff\n    FetchContent_Declare(mbed-Keypad\n        GIT_REPOSITORY\n            https://github.com/Aditya-A-garwal/mbed-Keypad.git\n        GIT_TAG\n            latest\n    )\n\n    FetchContent_MakeAvailable(mbed-Keypad)\n    ```\n\n    ```latest``` after ```GIT_TAG``` uses the latest commit to the main branch of this repository. Replace it with the hash of the commit or the tag of the release that needs to be used in your project.\n\n    More information about the FetchContent set of commands can be found [in the official CMake Docs](https://cmake.org/cmake/help/latest/module/FetchContent.html).\n\n2. Add the following line to to the ```CMakeLists.txt``` in your project root. This will add the ```external``` directory to your project. Make sure to insert it before creating the ```APP_TARGET``` executable and after including the MBed OS directory.\n\n    ```diff\n    add_subdirectory(${MBED_PATH})\n\n    + add_subdirectory(external)\n\n    add_executable(${APP_TARGET}\n        main.cpp\n    )\n    ```\n\n3. Link the library with APP_TARGET (or other targets as required) by adding updating the following line -\n\n    ```diff\n    - target_link_libraries(${APP_TARGET} mbed-os)\n    + target_link_libraries(${APP_TARGET} mbed-os mbed-Keypad)\n    ```\n\n    This also updates the include path for the linked targets.\n\n4. Configure and build the project by running the following commands in the root-directory of the project. This fetches the repository and makes the code available to the intellisense and autocomplete features in most IDEs.\n\n    ```bash\n    # Configure (fetches the repository and prepares the build-system)\n    mbed-tools configure --toolchain \u003cTOOLCHAIN\u003e --mbed-target \u003cTARGET_NAME\u003e --profile \u003cPROFILE\u003e\n    cmake -S . -B cmake_build/\u003cTARGET_NAME\u003e/\u003cPROFILE\u003e/\u003cTOOLCHAIN\u003e\n\n    # Builds the code\n    cmake --build cmake_build/\u003cTARGET_NAME\u003e/\u003cPROFILE\u003e/\u003cTOOLCHAIN\u003e\n    ```\n\n    Make sure to replace ```\u003cTOOLCHAIN\u003e```, ```\u003cTARGET_NAME\u003e``` and ```\u003cPROFILE\u003e``` with the appropriate\n    values.\n\n    **To change the version of the library being used, update the ```GIT_TAG``` parameter in ```external/CMakeLists.txt``` and re-run the configure and build commands. Re-building the project is not enough as the ```FetchContent_Declare``` command fetches the library while configuring the project only, and not while building.**\n\n5. Include the header files in ```main.cpp``` (and other files as required) -\n\n    ```cpp\n    #include \"keypad.h\" // non-blocking APIs\n    #include \"keypadBlocking.h\" // blocking APIs\n    ```\n\n## Organization of the Library\n\nThe library consists of two header files - ```keypad.h``` for non-blocking APIs and ```keypadBlocking.h``` for blocking APIs, each containing a single class. The classes are implemented in two source files - ```keypad.cpp``` and ```keypadBlocking.cpp```.\n\nThe steps to use the Blocking APIs are as follows -\n\n1. Instantiate the ```KeypadBlocking``` class.\n2. Initialize the object by calling the ```initialize()``` method.\n3. Get the number of unread events using the ```press_available()```, ```release_available()``` and ```longpress_available()``` methods. **These return immediately the number of events.**\n4. Peek the coordinates of each event using the ```peek_press(*r, *c)```, ```peek_release(*r, *c)``` and ```peek_longpress(*r, *c)``` methods. Events are processed in FIFO-order. **These calls return immediately, and return false if the requested event has not occured since the last call to its respective *pop* method.**\n5. Pop each event using the ```pop_press(*r, *c)```, ```pop_release(*r, *c)``` and ```pop_longpress(*r, *c)``` methods. Events are processed in the same order as the peek methods. **These calls return immediately, and return false if the requested event has not occured since the last call to its respective *pop* method.**\n6. Finalize the object by calling the ```finalize()``` method. **Missing this step before the destructor is called will cause memory-leaks and zombie-threads.**\n7. The object is destructed.\n\nThe steps to use the Non-Blocking APIs are as follows -\n\n1. Instantiate the ```Keypad``` class.\n2. Initialize the object by calling the ```initialize()``` method.\n3. Register callbacks for each type of event using the ```register_onpress(cb)```, ```register_onrelease(cb)``` and ```register_onlongpress(cb)``` methods. **These methods register the callback and return immediately. If any of the above events occur, the callback is called in a separate thread and the event processed.**\n4. Finalize the object by calling the ```finalize()``` method. **Missing this step before the destructor is called will cause memory-leaks and zombie-threads.**\n5. The object is destructed.\n\nThe library defines the following constants, whose values can be altered to change its behaviour -\n\n1. Number of rows on a keypad (present in ```keypad.h```)\n    ```cpp\n    /** Number of rows on a keypad */\n    constexpr auto      KEYPAD_NUM_ROWS     = 4;\n    ```\n2. Number of columns on a keypad (presnet in ```keypad.h```)\n    ```cpp\n    /** Number of columns on a keypad */\n    constexpr auto      KEYPAD_NUM_COLS     = 4;\n    ```\n3. Duration of time between a button bounces for while transitioning between pressed and released states (found in ```keypad.cpp```)\n    ```cpp\n    /** Duration of time a button spends bouncing before stabilizing */\n    constexpr auto DEBOUNCE_THRESH = 60ms;\n    ```\n4. Duration of time a button needs to be held-down before the press is considered as a long-press (found in ```keypad.cpp```)\n    ```cpp\n    /** Duration of time a button must be pressed to be considered long-pressed */\n    constexpr auto LONG_PRESS_THRESH = 300ms;\n    ```\n5. Number of events of each type (press/release/longpress) to queue up per keypad in blocking mode before old values are overwritten (found in ```keypad.h```)\n    ```cpp\n    /** Maximum number of presses/releases/long-presses to queue up before overwriting */\n    constexpr auto      KEYPAD_BUFFER_LEN   = 16;\n    ```\n\nDetailed information is available as inline documentation within the header files.\n\n## Design of the Library\n\nThe library recognizes three distinct events on the keypad -\n\n|Event|Meaning|\n|-|-|\n| Button Release | A button that was pressed just got released (all buttons were previously not held-down except one) |\n| Button Press | A button just got pressed (all buttons were previously not held-down) |\n| Button Longpress | A button that was previously pressed has just been held down beyond a threshold point. |\n\nA state-machine is used to manage these events along with debouncing and callbacks/\n\n![State Machine Diagram](/state_machine.png)\n\nThe state machine has the following states -\n\n|State|Meaning|\n|-|-|\n| ```RELEASED``` | All buttons are released |\n| ```PRESSED_BOUNCING``` | A button was pressed and is now bouncing |\n| ```PRESSED``` | A button that was previously bouncing is confirmed to be held-down |\n| ```LONG_PRESSED``` | A button that was held-down has been held-down for beyond a threshold |\n| ```RELEASED_BOUNCING``` | A button that was held-down has been released and is now bouncing |\n\nBecause of the design of the state-machine, only a single button maybe pressed at a time. Pressing multiple buttons at the same will cause only the first one to be accepted (the keypad will not be in the ```RELEASED``` state after the first button is pressed).\n\nA separate thread is used by the Keypad object for servicing callbacks, which is spawned in the ```initialize()``` method and joined in the ```finalize()``` method. **This might be an important consideration for many applications.**\n\nMultiple keypad objects maybe declared at the same time for different keypads connected to the microcontroller, but each keypad will spawn its own thread to run callbacks.\n\n## Documentation\n\nThe ```.h``` header files contain inline documentation for all classes, structs, functions and enums within it. This repository uses the Doxygen standard for inline-documentation. Regular comments explaining implementation details can be found in the ```.cpp``` source files.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faditya-a-garwal%2Fmbed-keypad","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faditya-a-garwal%2Fmbed-keypad","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faditya-a-garwal%2Fmbed-keypad/lists"}