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-20
Revision:
2:49e2b1273f16
Parent:
1:b6b866a58a87
Child:
3:ed89297af2ce

File content as of revision 2:49e2b1273f16:

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


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

// Flag that says whether we're waiting to collect input from other buttons
// following one upward edge.
bool flag = false;
time_t startSeconds = time(NULL);

InterruptIn buttons [BUTTONS] = {
    InterruptIn(D2),
    InterruptIn(D4),
    InterruptIn(D5),
    InterruptIn(D8),
    InterruptIn(D9),
    InterruptIn(D10),
    InterruptIn(D11)
};
Serial pc(USBTX, USBRX);

// Note: there are only THREE PWMs on the board. So this is all we can use
// for this particular method of sound output. The pins chosen are important
// because the same PWM will map to multiple outputs. See the reference
// manual for Mbed for which pins map to distinct PWMs.
PwmOut buzzers [3] = {
    PwmOut(D0),
    PwmOut(D7),
    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, 2, 4}, // 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(10000);
    }
}

void setFlag() {
    flag = true;
}

void silenceBuzzers(void) {
    // Initialize all duty cycles to off, so we hear nothing.
    for (int i = 0; i < BUZZERS; i++) {
        buzzers[i] = 0.0f;
    }
}

void registerButtonInterrupts(void) {
    for (int i =  0; i < BUTTONS; i++) {
        buttons[i].rise(&setFlag);
    }   
}

void waitForChord(void) {
     /* Should be run in the main loop so that it can print */
     while (true) {
        time_t currentSeconds = time(NULL);
        time_t secondsPast = currentSeconds - startSeconds;
        if (flag == true) {
            pc.printf("Pressed\n");
            flag = false;
        }
        else {
            pc.printf("Not pressed\n");   
        }
        pc.printf("Seconds: %d\n", secondsPast);
        wait_ms(500);
    }   
}

int main() {
    silenceBuzzers();
    registerButtonInterrupts();
    waitForChord();
}