ChordJoy is a keyboard application that accepts chordal input from a set of digital ports, outputting letters and the chords that correspond to the piano keyboard keys the user has pressed.

Dependencies:   PinDetect_KL25Z mbed-rtos mbed

Fork of ChordJoy by Interactive Device Design

main.cpp

Committer:
andrewhead
Date:
2014-09-18
Revision:
0:5ffec551c755
Child:
1:b6b866a58a87

File content as of revision 0:5ffec551c755:

#include "mbed.h"
#include "PinDetect.h"
#include "rtos.h"

const int BUZZERS = 3;
const int LETTERS = 27;

Serial pc(USBTX, USBRX);
PinDetect buttons [3] = {
    PinDetect(D15),
    PinDetect(D14),
    PinDetect(D13)
};

// Note: there are only THREE PWMs on the board. So this is all we can use
// for this particular method of sound output.
PwmOut buzzers [3] = {
    PwmOut(D0),
    PwmOut(D2),
    PwmOut(D3)
};
// Periods of notes in microseconds
int notePeriods [8] = {
    3818,   // C4
    3412,   // D4
    3030,   // E4
    2895,   // F4
    2551,   // G4
    2273,   // A4
    2024,   // B4
    10000000 // inaudible pitch
};
int chords [LETTERS][BUZZERS] = {
    {0, 7, 7}, // single notes
    {1, 7, 7},
    {2, 7, 7},
    {3, 7, 7},
    {4, 7, 7},
    {5, 7, 7},
    {6, 7, 7},
    {0, 2, 7}, // diads
    {0, 3, 7},
    {0, 4, 7},
    {0, 5, 7},
    {1, 3, 7},
    {1, 4, 7},
    {1, 5, 7},
    {1, 6, 7},
    {2, 4, 7},
    {2, 5, 7},
    {2, 6, 7},
    {3, 4, 7},
    {3, 5, 7},
    {4, 6, 7},
    {0, 2, 4}, // triads
    {0, 2, 5},
    {0, 3, 5},
    {1, 4, 6},
    {1, 3, 4},
    {1, 3, 6}
};

void playAllChords(void) {
    for (int i = 0; i < LETTERS; i++) {
        int *chord = chords[i];
        for (int j = 0; j < BUZZERS; j++) {
            int noteIndex = chord[j];
            if (noteIndex == 7) {
                buzzers[j] = 0;
            }
            else {
                buzzers[j] = .5f;
                int period = notePeriods[noteIndex];
                buzzers[j].period_us(period);
            }
        }
        wait_ms(1000);
    }
}

int main() {
    // Initialize all duty cycles to off, so we hear nothing.
    for (int i = 0; i < BUZZERS; i++) {
        buzzers[i] = 0.0f;
    }
    pc.printf("%c\n", 97);
    // playAllChords();
}