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

Committer:
andrewhead
Date:
Sat Sep 20 21:19:25 2014 +0000
Revision:
2:49e2b1273f16
Parent:
1:b6b866a58a87
Child:
3:ed89297af2ce
Measure time on seconds resolution

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewhead 0:5ffec551c755 1 #include "mbed.h"
andrewhead 0:5ffec551c755 2 #include "rtos.h"
andrewhead 0:5ffec551c755 3
andrewhead 1:b6b866a58a87 4
andrewhead 1:b6b866a58a87 5 const int BUTTONS = 7;
andrewhead 0:5ffec551c755 6 const int BUZZERS = 3;
andrewhead 0:5ffec551c755 7 const int LETTERS = 27;
andrewhead 0:5ffec551c755 8
andrewhead 1:b6b866a58a87 9 // Flag that says whether we're waiting to collect input from other buttons
andrewhead 1:b6b866a58a87 10 // following one upward edge.
andrewhead 1:b6b866a58a87 11 bool flag = false;
andrewhead 2:49e2b1273f16 12 time_t startSeconds = time(NULL);
andrewhead 1:b6b866a58a87 13
andrewhead 1:b6b866a58a87 14 InterruptIn buttons [BUTTONS] = {
andrewhead 2:49e2b1273f16 15 InterruptIn(D2),
andrewhead 1:b6b866a58a87 16 InterruptIn(D4),
andrewhead 1:b6b866a58a87 17 InterruptIn(D5),
andrewhead 1:b6b866a58a87 18 InterruptIn(D8),
andrewhead 1:b6b866a58a87 19 InterruptIn(D9),
andrewhead 1:b6b866a58a87 20 InterruptIn(D10),
andrewhead 2:49e2b1273f16 21 InterruptIn(D11)
andrewhead 1:b6b866a58a87 22 };
andrewhead 0:5ffec551c755 23 Serial pc(USBTX, USBRX);
andrewhead 1:b6b866a58a87 24
andrewhead 1:b6b866a58a87 25 // Note: there are only THREE PWMs on the board. So this is all we can use
andrewhead 1:b6b866a58a87 26 // for this particular method of sound output. The pins chosen are important
andrewhead 1:b6b866a58a87 27 // because the same PWM will map to multiple outputs. See the reference
andrewhead 1:b6b866a58a87 28 // manual for Mbed for which pins map to distinct PWMs.
andrewhead 1:b6b866a58a87 29 PwmOut buzzers [3] = {
andrewhead 1:b6b866a58a87 30 PwmOut(D0),
andrewhead 1:b6b866a58a87 31 PwmOut(D7),
andrewhead 1:b6b866a58a87 32 PwmOut(D3)
andrewhead 0:5ffec551c755 33 };
andrewhead 0:5ffec551c755 34
andrewhead 0:5ffec551c755 35 // Periods of notes in microseconds
andrewhead 0:5ffec551c755 36 int notePeriods [8] = {
andrewhead 0:5ffec551c755 37 3818, // C4
andrewhead 0:5ffec551c755 38 3412, // D4
andrewhead 0:5ffec551c755 39 3030, // E4
andrewhead 0:5ffec551c755 40 2895, // F4
andrewhead 0:5ffec551c755 41 2551, // G4
andrewhead 0:5ffec551c755 42 2273, // A4
andrewhead 0:5ffec551c755 43 2024, // B4
andrewhead 0:5ffec551c755 44 10000000 // inaudible pitch
andrewhead 0:5ffec551c755 45 };
andrewhead 0:5ffec551c755 46 int chords [LETTERS][BUZZERS] = {
andrewhead 1:b6b866a58a87 47 {0, 2, 4}, // single notes
andrewhead 0:5ffec551c755 48 {1, 7, 7},
andrewhead 0:5ffec551c755 49 {2, 7, 7},
andrewhead 0:5ffec551c755 50 {3, 7, 7},
andrewhead 0:5ffec551c755 51 {4, 7, 7},
andrewhead 0:5ffec551c755 52 {5, 7, 7},
andrewhead 0:5ffec551c755 53 {6, 7, 7},
andrewhead 0:5ffec551c755 54 {0, 2, 7}, // diads
andrewhead 0:5ffec551c755 55 {0, 3, 7},
andrewhead 0:5ffec551c755 56 {0, 4, 7},
andrewhead 0:5ffec551c755 57 {0, 5, 7},
andrewhead 0:5ffec551c755 58 {1, 3, 7},
andrewhead 0:5ffec551c755 59 {1, 4, 7},
andrewhead 0:5ffec551c755 60 {1, 5, 7},
andrewhead 0:5ffec551c755 61 {1, 6, 7},
andrewhead 0:5ffec551c755 62 {2, 4, 7},
andrewhead 0:5ffec551c755 63 {2, 5, 7},
andrewhead 0:5ffec551c755 64 {2, 6, 7},
andrewhead 0:5ffec551c755 65 {3, 4, 7},
andrewhead 0:5ffec551c755 66 {3, 5, 7},
andrewhead 0:5ffec551c755 67 {4, 6, 7},
andrewhead 0:5ffec551c755 68 {0, 2, 4}, // triads
andrewhead 0:5ffec551c755 69 {0, 2, 5},
andrewhead 0:5ffec551c755 70 {0, 3, 5},
andrewhead 0:5ffec551c755 71 {1, 4, 6},
andrewhead 0:5ffec551c755 72 {1, 3, 4},
andrewhead 0:5ffec551c755 73 {1, 3, 6}
andrewhead 0:5ffec551c755 74 };
andrewhead 0:5ffec551c755 75
andrewhead 0:5ffec551c755 76 void playAllChords(void) {
andrewhead 0:5ffec551c755 77 for (int i = 0; i < LETTERS; i++) {
andrewhead 0:5ffec551c755 78 int *chord = chords[i];
andrewhead 0:5ffec551c755 79 for (int j = 0; j < BUZZERS; j++) {
andrewhead 0:5ffec551c755 80 int noteIndex = chord[j];
andrewhead 0:5ffec551c755 81 if (noteIndex == 7) {
andrewhead 0:5ffec551c755 82 buzzers[j] = 0;
andrewhead 0:5ffec551c755 83 }
andrewhead 0:5ffec551c755 84 else {
andrewhead 0:5ffec551c755 85 buzzers[j] = .5f;
andrewhead 0:5ffec551c755 86 int period = notePeriods[noteIndex];
andrewhead 0:5ffec551c755 87 buzzers[j].period_us(period);
andrewhead 0:5ffec551c755 88 }
andrewhead 0:5ffec551c755 89 }
andrewhead 1:b6b866a58a87 90 wait_ms(10000);
andrewhead 0:5ffec551c755 91 }
andrewhead 0:5ffec551c755 92 }
andrewhead 0:5ffec551c755 93
andrewhead 1:b6b866a58a87 94 void setFlag() {
andrewhead 1:b6b866a58a87 95 flag = true;
andrewhead 1:b6b866a58a87 96 }
andrewhead 1:b6b866a58a87 97
andrewhead 1:b6b866a58a87 98 void silenceBuzzers(void) {
andrewhead 0:5ffec551c755 99 // Initialize all duty cycles to off, so we hear nothing.
andrewhead 0:5ffec551c755 100 for (int i = 0; i < BUZZERS; i++) {
andrewhead 0:5ffec551c755 101 buzzers[i] = 0.0f;
andrewhead 0:5ffec551c755 102 }
andrewhead 1:b6b866a58a87 103 }
andrewhead 1:b6b866a58a87 104
andrewhead 1:b6b866a58a87 105 void registerButtonInterrupts(void) {
andrewhead 1:b6b866a58a87 106 for (int i = 0; i < BUTTONS; i++) {
andrewhead 1:b6b866a58a87 107 buttons[i].rise(&setFlag);
andrewhead 1:b6b866a58a87 108 }
andrewhead 1:b6b866a58a87 109 }
andrewhead 1:b6b866a58a87 110
andrewhead 1:b6b866a58a87 111 void waitForChord(void) {
andrewhead 1:b6b866a58a87 112 /* Should be run in the main loop so that it can print */
andrewhead 1:b6b866a58a87 113 while (true) {
andrewhead 2:49e2b1273f16 114 time_t currentSeconds = time(NULL);
andrewhead 2:49e2b1273f16 115 time_t secondsPast = currentSeconds - startSeconds;
andrewhead 1:b6b866a58a87 116 if (flag == true) {
andrewhead 1:b6b866a58a87 117 pc.printf("Pressed\n");
andrewhead 1:b6b866a58a87 118 flag = false;
andrewhead 1:b6b866a58a87 119 }
andrewhead 1:b6b866a58a87 120 else {
andrewhead 1:b6b866a58a87 121 pc.printf("Not pressed\n");
andrewhead 1:b6b866a58a87 122 }
andrewhead 2:49e2b1273f16 123 pc.printf("Seconds: %d\n", secondsPast);
andrewhead 1:b6b866a58a87 124 wait_ms(500);
andrewhead 1:b6b866a58a87 125 }
andrewhead 1:b6b866a58a87 126 }
andrewhead 1:b6b866a58a87 127
andrewhead 1:b6b866a58a87 128 int main() {
andrewhead 1:b6b866a58a87 129 silenceBuzzers();
andrewhead 1:b6b866a58a87 130 registerButtonInterrupts();
andrewhead 1:b6b866a58a87 131 waitForChord();
andrewhead 0:5ffec551c755 132 }