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@0:5ffec551c755, 2014-09-18 (annotated)
- Committer:
- andrewhead
- Date:
- Thu Sep 18 05:48:12 2014 +0000
- Revision:
- 0:5ffec551c755
- Child:
- 1:b6b866a58a87
Play all chords on 3 buzzers.
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 "PinDetect.h" |
andrewhead | 0:5ffec551c755 | 3 | #include "rtos.h" |
andrewhead | 0:5ffec551c755 | 4 | |
andrewhead | 0:5ffec551c755 | 5 | const int BUZZERS = 3; |
andrewhead | 0:5ffec551c755 | 6 | const int LETTERS = 27; |
andrewhead | 0:5ffec551c755 | 7 | |
andrewhead | 0:5ffec551c755 | 8 | Serial pc(USBTX, USBRX); |
andrewhead | 0:5ffec551c755 | 9 | PinDetect buttons [3] = { |
andrewhead | 0:5ffec551c755 | 10 | PinDetect(D15), |
andrewhead | 0:5ffec551c755 | 11 | PinDetect(D14), |
andrewhead | 0:5ffec551c755 | 12 | PinDetect(D13) |
andrewhead | 0:5ffec551c755 | 13 | }; |
andrewhead | 0:5ffec551c755 | 14 | |
andrewhead | 0:5ffec551c755 | 15 | // Note: there are only THREE PWMs on the board. So this is all we can use |
andrewhead | 0:5ffec551c755 | 16 | // for this particular method of sound output. |
andrewhead | 0:5ffec551c755 | 17 | PwmOut buzzers [3] = { |
andrewhead | 0:5ffec551c755 | 18 | PwmOut(D0), |
andrewhead | 0:5ffec551c755 | 19 | PwmOut(D2), |
andrewhead | 0:5ffec551c755 | 20 | PwmOut(D3) |
andrewhead | 0:5ffec551c755 | 21 | }; |
andrewhead | 0:5ffec551c755 | 22 | // Periods of notes in microseconds |
andrewhead | 0:5ffec551c755 | 23 | int notePeriods [8] = { |
andrewhead | 0:5ffec551c755 | 24 | 3818, // C4 |
andrewhead | 0:5ffec551c755 | 25 | 3412, // D4 |
andrewhead | 0:5ffec551c755 | 26 | 3030, // E4 |
andrewhead | 0:5ffec551c755 | 27 | 2895, // F4 |
andrewhead | 0:5ffec551c755 | 28 | 2551, // G4 |
andrewhead | 0:5ffec551c755 | 29 | 2273, // A4 |
andrewhead | 0:5ffec551c755 | 30 | 2024, // B4 |
andrewhead | 0:5ffec551c755 | 31 | 10000000 // inaudible pitch |
andrewhead | 0:5ffec551c755 | 32 | }; |
andrewhead | 0:5ffec551c755 | 33 | int chords [LETTERS][BUZZERS] = { |
andrewhead | 0:5ffec551c755 | 34 | {0, 7, 7}, // single notes |
andrewhead | 0:5ffec551c755 | 35 | {1, 7, 7}, |
andrewhead | 0:5ffec551c755 | 36 | {2, 7, 7}, |
andrewhead | 0:5ffec551c755 | 37 | {3, 7, 7}, |
andrewhead | 0:5ffec551c755 | 38 | {4, 7, 7}, |
andrewhead | 0:5ffec551c755 | 39 | {5, 7, 7}, |
andrewhead | 0:5ffec551c755 | 40 | {6, 7, 7}, |
andrewhead | 0:5ffec551c755 | 41 | {0, 2, 7}, // diads |
andrewhead | 0:5ffec551c755 | 42 | {0, 3, 7}, |
andrewhead | 0:5ffec551c755 | 43 | {0, 4, 7}, |
andrewhead | 0:5ffec551c755 | 44 | {0, 5, 7}, |
andrewhead | 0:5ffec551c755 | 45 | {1, 3, 7}, |
andrewhead | 0:5ffec551c755 | 46 | {1, 4, 7}, |
andrewhead | 0:5ffec551c755 | 47 | {1, 5, 7}, |
andrewhead | 0:5ffec551c755 | 48 | {1, 6, 7}, |
andrewhead | 0:5ffec551c755 | 49 | {2, 4, 7}, |
andrewhead | 0:5ffec551c755 | 50 | {2, 5, 7}, |
andrewhead | 0:5ffec551c755 | 51 | {2, 6, 7}, |
andrewhead | 0:5ffec551c755 | 52 | {3, 4, 7}, |
andrewhead | 0:5ffec551c755 | 53 | {3, 5, 7}, |
andrewhead | 0:5ffec551c755 | 54 | {4, 6, 7}, |
andrewhead | 0:5ffec551c755 | 55 | {0, 2, 4}, // triads |
andrewhead | 0:5ffec551c755 | 56 | {0, 2, 5}, |
andrewhead | 0:5ffec551c755 | 57 | {0, 3, 5}, |
andrewhead | 0:5ffec551c755 | 58 | {1, 4, 6}, |
andrewhead | 0:5ffec551c755 | 59 | {1, 3, 4}, |
andrewhead | 0:5ffec551c755 | 60 | {1, 3, 6} |
andrewhead | 0:5ffec551c755 | 61 | }; |
andrewhead | 0:5ffec551c755 | 62 | |
andrewhead | 0:5ffec551c755 | 63 | void playAllChords(void) { |
andrewhead | 0:5ffec551c755 | 64 | for (int i = 0; i < LETTERS; i++) { |
andrewhead | 0:5ffec551c755 | 65 | int *chord = chords[i]; |
andrewhead | 0:5ffec551c755 | 66 | for (int j = 0; j < BUZZERS; j++) { |
andrewhead | 0:5ffec551c755 | 67 | int noteIndex = chord[j]; |
andrewhead | 0:5ffec551c755 | 68 | if (noteIndex == 7) { |
andrewhead | 0:5ffec551c755 | 69 | buzzers[j] = 0; |
andrewhead | 0:5ffec551c755 | 70 | } |
andrewhead | 0:5ffec551c755 | 71 | else { |
andrewhead | 0:5ffec551c755 | 72 | buzzers[j] = .5f; |
andrewhead | 0:5ffec551c755 | 73 | int period = notePeriods[noteIndex]; |
andrewhead | 0:5ffec551c755 | 74 | buzzers[j].period_us(period); |
andrewhead | 0:5ffec551c755 | 75 | } |
andrewhead | 0:5ffec551c755 | 76 | } |
andrewhead | 0:5ffec551c755 | 77 | wait_ms(1000); |
andrewhead | 0:5ffec551c755 | 78 | } |
andrewhead | 0:5ffec551c755 | 79 | } |
andrewhead | 0:5ffec551c755 | 80 | |
andrewhead | 0:5ffec551c755 | 81 | int main() { |
andrewhead | 0:5ffec551c755 | 82 | // Initialize all duty cycles to off, so we hear nothing. |
andrewhead | 0:5ffec551c755 | 83 | for (int i = 0; i < BUZZERS; i++) { |
andrewhead | 0:5ffec551c755 | 84 | buzzers[i] = 0.0f; |
andrewhead | 0:5ffec551c755 | 85 | } |
andrewhead | 0:5ffec551c755 | 86 | pc.printf("%c\n", 97); |
andrewhead | 0:5ffec551c755 | 87 | // playAllChords(); |
andrewhead | 0:5ffec551c755 | 88 | } |