-
Notifications
You must be signed in to change notification settings - Fork 1
examples: Add practical examples for mcp23009e driver. #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fcb3ebc
fix(mcp23009e): Remove test script from example
Charly-sketch 686baac
feat(mcp23009e): add reaction time example
Charly-sketch 775ac09
feat(mcp23009e): add simon game example
Charly-sketch 17236a4
feat(mcp23009e): add combination lock example
Charly-sketch e57ba37
feat(mcp23009e): add dpad counter example
Charly-sketch 5b98972
feat(mcp23009e): add morse code example
Charly-sketch a9b861c
feat(mcp23009e): add binary counter example
Charly-sketch b0ffc9b
feat(mcp23009e): add navigation menu example
Charly-sketch 9534996
feat(mcp23009e): add dpad piano example
Charly-sketch c85077b
docs(mcp23009e): add example to readme
Charly-sketch d49587b
feat(mcp23009e): add sleep on button example
Charly-sketch fb4d593
docs(mcp23009e): add sleep on example to readme
Charly-sketch 01134fb
docs(mcp23009e): fix examples errors.
Charly-sketch 1888d82
fix(mcp23009e): More reliable interrupt in sleep_on_button
Charly-sketch 975b127
fix(mcp23009e): replace wrong pin in dpad piano example
Charly-sketch ab1b2c6
docs(mcp23009e): clarify GPIO naming for binary counter output pins
Charly-sketch fb3a70a
fix(lis2mdl): added menu btn to exit dpad piano example
Charly-sketch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """ | ||
| Binary counter using D-PAD inputs and MCP23009E GPIO outputs. | ||
|
|
||
| UP -> increment | ||
| DOWN -> decrement | ||
| LEFT -> reset | ||
| RIGHT -> print current value | ||
|
|
||
| The value is displayed on GPIO1..GPIO4 (expander pins 0..3) | ||
| as a 4-bit binary number. | ||
| """ | ||
|
|
||
| from time import sleep_ms | ||
|
|
||
| from machine import I2C, Pin | ||
| from mcp23009e import MCP23009E | ||
| from mcp23009e.const import * | ||
|
|
||
| bus = I2C(1) | ||
| reset = Pin("RST_EXPANDER", Pin.OUT) | ||
|
|
||
| mcp = MCP23009E(bus, address=MCP23009_I2C_ADDR, reset_pin=reset) | ||
|
|
||
| BUTTONS = { | ||
| MCP23009_BTN_UP: "UP", | ||
| MCP23009_BTN_DOWN: "DOWN", | ||
| MCP23009_BTN_LEFT: "LEFT", | ||
| MCP23009_BTN_RIGHT: "RIGHT", | ||
| } | ||
|
|
||
| OUTPUT_PINS = [MCP23009_GPIO1, MCP23009_GPIO2, MCP23009_GPIO3, MCP23009_GPIO4] | ||
|
|
||
|
|
||
| def wait_all_released(): | ||
| while True: | ||
| if all(mcp.get_level(pin_number) == MCP23009_LOGIC_HIGH for pin_number in BUTTONS): | ||
| return | ||
| sleep_ms(20) | ||
|
|
||
|
|
||
| def wait_for_button(): | ||
| wait_all_released() | ||
| while True: | ||
| for pin_number, name in BUTTONS.items(): | ||
| if mcp.get_level(pin_number) == MCP23009_LOGIC_LOW: | ||
| while mcp.get_level(pin_number) == MCP23009_LOGIC_LOW: | ||
| sleep_ms(20) | ||
| return name | ||
| sleep_ms(20) | ||
|
|
||
|
|
||
| def update_outputs(value): | ||
| for bit_index, pin_number in enumerate(OUTPUT_PINS): | ||
| bit_value = (value >> bit_index) & 0x01 | ||
| mcp.set_level(pin_number, bit_value) | ||
|
|
||
|
|
||
| for pin_number in BUTTONS: | ||
| mcp.setup(pin_number, MCP23009_DIR_INPUT, pullup=MCP23009_PULLUP) | ||
|
|
||
| for pin_number in OUTPUT_PINS: | ||
| mcp.setup(pin_number, MCP23009_DIR_OUTPUT) | ||
| mcp.set_level(pin_number, MCP23009_LOGIC_LOW) | ||
|
|
||
| counter = 0 | ||
| update_outputs(counter) | ||
|
|
||
| print("=================================") | ||
| print("Binary Counter") | ||
| print("=================================\n") | ||
| print("Displaying the 4-bit value on GP0..GP3") | ||
| print("UP=+1 DOWN=-1 LEFT=reset RIGHT=print\n") | ||
|
|
||
| while True: | ||
| button = wait_for_button() | ||
|
|
||
| if button == "UP": | ||
| counter = (counter + 1) & 0x0F | ||
| elif button == "DOWN": | ||
| counter = (counter - 1) & 0x0F | ||
| elif button == "LEFT": | ||
| counter = 0 | ||
| elif button == "RIGHT": | ||
| pass | ||
|
|
||
| update_outputs(counter) | ||
| print("Value:", counter, " Binary:", "{:04b}".format(counter)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """ | ||
| Digital combination lock using the MCP23009E D-PAD. | ||
|
|
||
| Enter the secret button sequence to unlock. | ||
| A wrong input resets the attempt. | ||
| """ | ||
|
|
||
| from time import sleep_ms | ||
|
|
||
| from machine import I2C, Pin | ||
| from mcp23009e import MCP23009E | ||
| from mcp23009e.const import * | ||
|
|
||
| bus = I2C(1) | ||
| reset = Pin("RST_EXPANDER", Pin.OUT) | ||
|
|
||
| mcp = MCP23009E(bus, address=MCP23009_I2C_ADDR, reset_pin=reset) | ||
|
|
||
| BUTTONS = { | ||
| MCP23009_BTN_UP: "UP", | ||
| MCP23009_BTN_DOWN: "DOWN", | ||
| MCP23009_BTN_LEFT: "LEFT", | ||
| MCP23009_BTN_RIGHT: "RIGHT", | ||
| } | ||
|
|
||
| SECRET = ["UP", "UP", "DOWN", "LEFT", "RIGHT"] | ||
|
|
||
|
|
||
| def wait_all_released(): | ||
| while True: | ||
| if all(mcp.get_level(pin_number) == MCP23009_LOGIC_HIGH for pin_number in BUTTONS): | ||
| return | ||
| sleep_ms(20) | ||
|
|
||
|
|
||
| def wait_for_button(): | ||
| wait_all_released() | ||
| while True: | ||
| for pin_number, name in BUTTONS.items(): | ||
| if mcp.get_level(pin_number) == MCP23009_LOGIC_LOW: | ||
| while mcp.get_level(pin_number) == MCP23009_LOGIC_LOW: | ||
| sleep_ms(20) | ||
| return name | ||
| sleep_ms(20) | ||
|
|
||
|
|
||
| for pin_number in BUTTONS: | ||
| mcp.setup(pin_number, MCP23009_DIR_INPUT, pullup=MCP23009_PULLUP) | ||
|
|
||
| print("=================================") | ||
| print("Combination Lock") | ||
| print("=================================\n") | ||
| print("Enter the secret D-PAD sequence.\n") | ||
|
|
||
| entered = [] | ||
|
|
||
| while True: | ||
| button = wait_for_button() | ||
| entered.append(button) | ||
|
|
||
| print("Input:", "-".join(entered)) | ||
|
|
||
| expected_prefix = SECRET[: len(entered)] | ||
| if entered != expected_prefix: | ||
| print("WRONG") | ||
| entered = [] | ||
| print() | ||
| continue | ||
|
|
||
| if len(entered) == len(SECRET): | ||
| print("UNLOCKED") | ||
| entered = [] | ||
| print() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """ | ||
| Simple D-PAD counter. | ||
|
|
||
| UP -> increment | ||
| DOWN -> decrement | ||
| LEFT -> reset | ||
| RIGHT -> print current value | ||
| """ | ||
|
|
||
| from time import sleep_ms | ||
|
|
||
| from machine import I2C, Pin | ||
| from mcp23009e import MCP23009E | ||
| from mcp23009e.const import * | ||
|
|
||
| bus = I2C(1) | ||
| reset = Pin("RST_EXPANDER", Pin.OUT) | ||
|
|
||
| mcp = MCP23009E(bus, address=MCP23009_I2C_ADDR, reset_pin=reset) | ||
|
|
||
| BUTTONS = { | ||
| MCP23009_BTN_UP: "UP", | ||
| MCP23009_BTN_DOWN: "DOWN", | ||
| MCP23009_BTN_LEFT: "LEFT", | ||
| MCP23009_BTN_RIGHT: "RIGHT", | ||
| } | ||
|
|
||
|
|
||
| def wait_all_released(): | ||
| while True: | ||
| if all(mcp.get_level(pin_number) == MCP23009_LOGIC_HIGH for pin_number in BUTTONS): | ||
| return | ||
| sleep_ms(20) | ||
|
|
||
|
|
||
| def wait_for_button(): | ||
| wait_all_released() | ||
| while True: | ||
| for pin_number, name in BUTTONS.items(): | ||
| if mcp.get_level(pin_number) == MCP23009_LOGIC_LOW: | ||
| while mcp.get_level(pin_number) == MCP23009_LOGIC_LOW: | ||
| sleep_ms(20) | ||
| return name | ||
| sleep_ms(20) | ||
|
|
||
|
|
||
| for pin_number in BUTTONS: | ||
| mcp.setup(pin_number, MCP23009_DIR_INPUT, pullup=MCP23009_PULLUP) | ||
|
|
||
| value = 0 | ||
|
|
||
| print("=================================") | ||
| print("D-PAD Counter") | ||
| print("=================================\n") | ||
| print("UP=+1 DOWN=-1 LEFT=reset RIGHT=print\n") | ||
|
|
||
| while True: | ||
| button = wait_for_button() | ||
|
|
||
| if button == "UP": | ||
| value += 1 | ||
| print("Increment ->", value) | ||
| elif button == "DOWN": | ||
| value -= 1 | ||
| print("Decrement ->", value) | ||
| elif button == "LEFT": | ||
| value = 0 | ||
| print("Reset ->", value) | ||
| elif button == "RIGHT": | ||
| print("Current value ->", value) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.