HHF
Published © CC0

MPR121 Touch Pad an Calliope Mini über I2C

Um Eingaben für ein Mathe-Lernspiel auf dem Calliope Mini zu ermöglichen, habe ich das MPR121 Touch Pad über I2C angebunden.

IntermediateFull instructions provided1 hour333
MPR121 Touch Pad an Calliope Mini über I2C

Things used in this project

Hardware components

Calliope mini
Calliope mini
×1
SparkFun MPR121 Capacitive Touch Keypad
Amazon Produkt ID für MPR121 Touch Pad: B01M5JZRQX
×1
Seeed 4 Pin Female Jumper To Grove 4 Pin Conversion Cable
Amazon Produkt ID für das Grove/I2C Kabel: B01AD62W56
×1

Software apps and online services

Calliope Mini PXT Editor

Story

Read more

Code

MPR121 Calliope I2C v1

JavaScript
Initialisiert den MPR121 über I2C und gibt dann die Nummer des berührten Tastenfelds (0-11) aus.
let output = 0
pins.i2cWriteNumber(0x5A, 0x2B01, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x2C01, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x2D00, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x2E00, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x2F01, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x3001, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x31FF, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x3202, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4106, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x420A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4306, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x440A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4506, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x460A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4706, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x480A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4906, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4A0A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4B06, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4C0A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4D06, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4E0A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x4F06, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x500A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x5106, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x520A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x5306, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x540A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x5506, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x560A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x5706, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x580A, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x5D04, NumberFormat.UInt16BE)
pins.i2cWriteNumber(0x5A, 0x5E0C, NumberFormat.UInt16BE)
while (true) {
    control.waitMicros(10)
    pins.i2cWriteNumber(0x5A, 0, NumberFormat.UInt16BE)
    output = pins.i2cReadNumber(0x5A, NumberFormat.UInt16BE)
    if (output == 0) {
        basic.clearScreen()
    }
    if (output == 1) {
        music.playTone(256, 10)
        basic.showNumber(8)
    }
    if (output == 2) {
        music.playTone(256, 10)
        basic.showNumber(9)
    }
    if (output == 4) {
        music.playTone(256, 10)
        basic.showNumber(10)
    }
    if (output == 8) {
        music.playTone(256, 10)
        basic.showNumber(11)
    }
    if (output == 4096) {
        music.playTone(256, 10)
        basic.showNumber(4)
    }
    if (output == 8192) {
        music.playTone(256, 10)
        basic.showNumber(5)
    }
    if (output == 16384) {
        music.playTone(256, 10)
        basic.showNumber(6)
    }
    if (output == 32768) {
        music.playTone(256, 10)
        basic.showNumber(7)
    }
    if (output == 256) {
        music.playTone(256, 10)
        basic.showNumber(0)
    }
    if (output == 512) {
        music.playTone(256, 10)
        basic.showNumber(1)
    }
    if (output == 1024) {
        music.playTone(256, 10)
        basic.showNumber(2)
    }
    if (output == 2048) {
        music.playTone(256, 10)
        basic.showNumber(3)
    }
}

Initialisierung mit Javascript Array

JavaScript
Eleganter ist die Initialisierung mit einem Array und einer Schleife, das ist dann aber nicht mehr mit dem Blockeditor darstellbar.
let a = 0x5A // i2c Adresse des MPR121
let ini: number[] = [0x2B01, 0x2C01, 0x2D00, 0x2E00, 0x2F01, 0x3001, 0x31FF, 0x3202, 0x4106, 0x420A, 0x4306, 0x440A, 0x4506, 0x460A, 0x4706, 0x480A, 0x4906, 0x4A0A, 0x4B06, 0x4C0A, 0x4D06, 0x4E0A, 0x4F06, 0x500A, 0x5106, 0x520A, 0x5306, 0x540A, 0x5506, 0x560A, 0x5706, 0x580A, 0x5D04, 0x5E0C]
// Initialisierung des MPR121 mit den Werten aus Array ini
for (let i = 0; i < ini.length; i++) {
pins.i2cWriteNumber(a, ini[i], NumberFormat.UInt16BE)
}

Auslesen der gedrückten Taste mit Array

JavaScript
Der Code für das Auslesen der gedrückten Taste wird mit einem Array sehr viel eleganter.
let taste: number[] = [1, 2, 4, 8, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768]
let t = 0 // die gedrückte Taste
while (true) {
    control.waitMicros(10)
    pins.i2cWriteNumber(0x5A, 0, NumberFormat.UInt16BE)
    t = pins.i2cReadNumber(0x5A, NumberFormat.UInt16BE)
    if (t == 0) {
        basic.clearScreen()
    }
    if (t != 0) {
        music.playTone(256, 10)
        basic.showNumber(taste.indexOf(t))
    }
}

Credits

HHF

HHF

8 projects • 8 followers
"Papa, warum hast Du Dir auch einen Calliope gekauft?" - "Weil Ihr Eure ja nicht mehr hergebt!"

Comments

Add projectSign up / Login