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
main.cpp@1:b6b866a58a87, 2014-09-20 (annotated)
- Committer:
- andrewhead
- Date:
- Sat Sep 20 19:57:18 2014 +0000
- Revision:
- 1:b6b866a58a87
- Parent:
- 0:5ffec551c755
- Child:
- 2:49e2b1273f16
Listens for button interrupts from 7 ports.
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:b6b866a58a87 | 12 | |
andrewhead | 1:b6b866a58a87 | 13 | InterruptIn buttons [BUTTONS] = { |
andrewhead | 1:b6b866a58a87 | 14 | InterruptIn(D4), |
andrewhead | 1:b6b866a58a87 | 15 | InterruptIn(D5), |
andrewhead | 1:b6b866a58a87 | 16 | InterruptIn(D8), |
andrewhead | 1:b6b866a58a87 | 17 | InterruptIn(D9), |
andrewhead | 1:b6b866a58a87 | 18 | InterruptIn(D10), |
andrewhead | 1:b6b866a58a87 | 19 | InterruptIn(D11), |
andrewhead | 1:b6b866a58a87 | 20 | InterruptIn(D2) |
andrewhead | 1:b6b866a58a87 | 21 | }; |
andrewhead | 0:5ffec551c755 | 22 | Serial pc(USBTX, USBRX); |
andrewhead | 1:b6b866a58a87 | 23 | |
andrewhead | 1:b6b866a58a87 | 24 | // Note: there are only THREE PWMs on the board. So this is all we can use |
andrewhead | 1:b6b866a58a87 | 25 | // for this particular method of sound output. The pins chosen are important |
andrewhead | 1:b6b866a58a87 | 26 | // because the same PWM will map to multiple outputs. See the reference |
andrewhead | 1:b6b866a58a87 | 27 | // manual for Mbed for which pins map to distinct PWMs. |
andrewhead | 1:b6b866a58a87 | 28 | PwmOut buzzers [3] = { |
andrewhead | 1:b6b866a58a87 | 29 | PwmOut(D0), |
andrewhead | 1:b6b866a58a87 | 30 | PwmOut(D7), |
andrewhead | 1:b6b866a58a87 | 31 | PwmOut(D3) |
andrewhead | 0:5ffec551c755 | 32 | }; |
andrewhead | 0:5ffec551c755 | 33 | |
andrewhead | 0:5ffec551c755 | 34 | // Periods of notes in microseconds |
andrewhead | 0:5ffec551c755 | 35 | int notePeriods [8] = { |
andrewhead | 0:5ffec551c755 | 36 | 3818, // C4 |
andrewhead | 0:5ffec551c755 | 37 | 3412, // D4 |
andrewhead | 0:5ffec551c755 | 38 | 3030, // E4 |
andrewhead | 0:5ffec551c755 | 39 | 2895, // F4 |
andrewhead | 0:5ffec551c755 | 40 | 2551, // G4 |
andrewhead | 0:5ffec551c755 | 41 | 2273, // A4 |
andrewhead | 0:5ffec551c755 | 42 | 2024, // B4 |
andrewhead | 0:5ffec551c755 | 43 | 10000000 // inaudible pitch |
andrewhead | 0:5ffec551c755 | 44 | }; |
andrewhead | 0:5ffec551c755 | 45 | int chords [LETTERS][BUZZERS] = { |
andrewhead | 1:b6b866a58a87 | 46 | {0, 2, 4}, // single notes |
andrewhead | 0:5ffec551c755 | 47 | {1, 7, 7}, |
andrewhead | 0:5ffec551c755 | 48 | {2, 7, 7}, |
andrewhead | 0:5ffec551c755 | 49 | {3, 7, 7}, |
andrewhead | 0:5ffec551c755 | 50 | {4, 7, 7}, |
andrewhead | 0:5ffec551c755 | 51 | {5, 7, 7}, |
andrewhead | 0:5ffec551c755 | 52 | {6, 7, 7}, |
andrewhead | 0:5ffec551c755 | 53 | {0, 2, 7}, // diads |
andrewhead | 0:5ffec551c755 | 54 | {0, 3, 7}, |
andrewhead | 0:5ffec551c755 | 55 | {0, 4, 7}, |
andrewhead | 0:5ffec551c755 | 56 | {0, 5, 7}, |
andrewhead | 0:5ffec551c755 | 57 | {1, 3, 7}, |
andrewhead | 0:5ffec551c755 | 58 | {1, 4, 7}, |
andrewhead | 0:5ffec551c755 | 59 | {1, 5, 7}, |
andrewhead | 0:5ffec551c755 | 60 | {1, 6, 7}, |
andrewhead | 0:5ffec551c755 | 61 | {2, 4, 7}, |
andrewhead | 0:5ffec551c755 | 62 | {2, 5, 7}, |
andrewhead | 0:5ffec551c755 | 63 | {2, 6, 7}, |
andrewhead | 0:5ffec551c755 | 64 | {3, 4, 7}, |
andrewhead | 0:5ffec551c755 | 65 | {3, 5, 7}, |
andrewhead | 0:5ffec551c755 | 66 | {4, 6, 7}, |
andrewhead | 0:5ffec551c755 | 67 | {0, 2, 4}, // triads |
andrewhead | 0:5ffec551c755 | 68 | {0, 2, 5}, |
andrewhead | 0:5ffec551c755 | 69 | {0, 3, 5}, |
andrewhead | 0:5ffec551c755 | 70 | {1, 4, 6}, |
andrewhead | 0:5ffec551c755 | 71 | {1, 3, 4}, |
andrewhead | 0:5ffec551c755 | 72 | {1, 3, 6} |
andrewhead | 0:5ffec551c755 | 73 | }; |
andrewhead | 0:5ffec551c755 | 74 | |
andrewhead | 0:5ffec551c755 | 75 | void playAllChords(void) { |
andrewhead | 0:5ffec551c755 | 76 | for (int i = 0; i < LETTERS; i++) { |
andrewhead | 0:5ffec551c755 | 77 | int *chord = chords[i]; |
andrewhead | 0:5ffec551c755 | 78 | for (int j = 0; j < BUZZERS; j++) { |
andrewhead | 0:5ffec551c755 | 79 | int noteIndex = chord[j]; |
andrewhead | 0:5ffec551c755 | 80 | if (noteIndex == 7) { |
andrewhead | 0:5ffec551c755 | 81 | buzzers[j] = 0; |
andrewhead | 0:5ffec551c755 | 82 | } |
andrewhead | 0:5ffec551c755 | 83 | else { |
andrewhead | 0:5ffec551c755 | 84 | buzzers[j] = .5f; |
andrewhead | 0:5ffec551c755 | 85 | int period = notePeriods[noteIndex]; |
andrewhead | 0:5ffec551c755 | 86 | buzzers[j].period_us(period); |
andrewhead | 0:5ffec551c755 | 87 | } |
andrewhead | 0:5ffec551c755 | 88 | } |
andrewhead | 1:b6b866a58a87 | 89 | wait_ms(10000); |
andrewhead | 0:5ffec551c755 | 90 | } |
andrewhead | 0:5ffec551c755 | 91 | } |
andrewhead | 0:5ffec551c755 | 92 | |
andrewhead | 1:b6b866a58a87 | 93 | void setFlag() { |
andrewhead | 1:b6b866a58a87 | 94 | flag = true; |
andrewhead | 1:b6b866a58a87 | 95 | } |
andrewhead | 1:b6b866a58a87 | 96 | |
andrewhead | 1:b6b866a58a87 | 97 | void silenceBuzzers(void) { |
andrewhead | 0:5ffec551c755 | 98 | // Initialize all duty cycles to off, so we hear nothing. |
andrewhead | 0:5ffec551c755 | 99 | for (int i = 0; i < BUZZERS; i++) { |
andrewhead | 0:5ffec551c755 | 100 | buzzers[i] = 0.0f; |
andrewhead | 0:5ffec551c755 | 101 | } |
andrewhead | 1:b6b866a58a87 | 102 | } |
andrewhead | 1:b6b866a58a87 | 103 | |
andrewhead | 1:b6b866a58a87 | 104 | void registerButtonInterrupts(void) { |
andrewhead | 1:b6b866a58a87 | 105 | for (int i = 0; i < BUTTONS; i++) { |
andrewhead | 1:b6b866a58a87 | 106 | buttons[i].rise(&setFlag); |
andrewhead | 1:b6b866a58a87 | 107 | } |
andrewhead | 1:b6b866a58a87 | 108 | } |
andrewhead | 1:b6b866a58a87 | 109 | |
andrewhead | 1:b6b866a58a87 | 110 | void waitForChord(void) { |
andrewhead | 1:b6b866a58a87 | 111 | /* Should be run in the main loop so that it can print */ |
andrewhead | 1:b6b866a58a87 | 112 | while (true) { |
andrewhead | 1:b6b866a58a87 | 113 | if (flag == true) { |
andrewhead | 1:b6b866a58a87 | 114 | pc.printf("Pressed\n"); |
andrewhead | 1:b6b866a58a87 | 115 | flag = false; |
andrewhead | 1:b6b866a58a87 | 116 | } |
andrewhead | 1:b6b866a58a87 | 117 | else { |
andrewhead | 1:b6b866a58a87 | 118 | pc.printf("Not pressed\n"); |
andrewhead | 1:b6b866a58a87 | 119 | } |
andrewhead | 1:b6b866a58a87 | 120 | wait_ms(500); |
andrewhead | 1:b6b866a58a87 | 121 | } |
andrewhead | 1:b6b866a58a87 | 122 | } |
andrewhead | 1:b6b866a58a87 | 123 | |
andrewhead | 1:b6b866a58a87 | 124 | int main() { |
andrewhead | 1:b6b866a58a87 | 125 | silenceBuzzers(); |
andrewhead | 1:b6b866a58a87 | 126 | registerButtonInterrupts(); |
andrewhead | 1:b6b866a58a87 | 127 | waitForChord(); |
andrewhead | 0:5ffec551c755 | 128 | } |