Skip to the content.

Open this page at https://devegied.github.io/pxt-i2c-keypad/

I2C Keypad

Build Status

makecode I2C Keypad extension for micro:bit

Matrix keypad with custom controller connected to micro:bit through I2C.

Code is mostly duplicated from MakerBit Touch extension

Usage

In your micro:bit makecode project click on Extensions under the gearwheel menu search for devegied/pxt-i2c-keypad and import

API

i2cKeypad=github:devegied/pxt-i2c-keypad
i2cKeypad.initKeypadController("123A456B789C*0#D")

Optionaly initialize the keypad controller by defining key symbols

i2cKeypad.onKey(i2cKeypad.Keys.K1, i2cKeypad.KeyAction.Pressed, () => {})

Do something when a key is pressed or released.

i2cKeypad.onKeysPressed("*#", (theKey,stringBeforeTheKey) => {})

Do something when any of the symbols are entered.

i2cKeypad.currentKey()

Returns the key index of the last key event that was received. It could be either a key pressed or released event.

i2cKeypad.currentSymbol()

Returns the key symbol of the last key event that was received. It could be either a key pressed or released event.

i2cKeypad.isPressed(1)

Returns true if a specific key is currently pressed. False otherwise.

i2cKeypad.wasPressed()

Returns true if any key was pressed since the last call of this function. False otherwise.

Demo

First example shows key index and key symbol when keypad buttons are pressed

i2cKeypad.onKey(i2cKeypad.Keys.Any, i2cKeypad.KeyAction.Pressed, function () {
    basic.showString("" + convertToText(i2cKeypad.currentKey()) + "-" + i2cKeypad.currentSymbol())
})
i2cKeypad.onKey(i2cKeypad.Keys.Any, i2cKeypad.KeyAction.Released, function () {
    basic.clearScreen()
})
i2cKeypad.initKeypadController("123A456B789C*0#D")

First example code

Second example shows PINcode validation. PIN code entrance is finished by pressing #, half entered code can be cleared by pressing *

i2cKeypad.onKeysPressed("*#", function (theKey, stringBeforeTheKey) {
    if (theKey == "#" && stringBeforeTheKey == pinCode) {
        basic.showIcon(IconNames.Happy)
    } else {
        basic.showIcon(IconNames.Sad)
    }
})
let pinCode = ""
pinCode = "1234"

Second example code

Blocks preview

This image shows the blocks code from the last commit in master. This image may take a few minutes to refresh.

A rendered view of the blocks

i2cKeypad.initKeypadController("123A456B789C*0#D")
i2cKeypad.onKey(i2cKeypad.Keys.K1, i2cKeypad.KeyAction.Pressed, () => {

})
i2cKeypad.onKeysPressed("*#", (theKey,stringBeforeTheKey) => {

})
i2cKeypad.currentKey()
i2cKeypad.currentSymbol()
i2cKeypad.isPressed(1)
wasPressed()

License

Licensed under the MIT License (MIT). See LICENSE file for more details.

Copyright (c) 2022, devegied Copyright (c) 2020, MakerBit

Metadata (used for search, rendering)