BaseMachine Sequencer V2
Dependencies: AT24C1024 Amp AverageAnalogIn BaseMachineUIController Envelope ExioBufferdController FilterController MCP23S17 OscController PinDetect RotaryEncoder Sequence SequencerDisplay mbed-rtos mbed st7567LCD
Fork of BaseMachine by
main.cpp@26:fa2bd913530b, 2017-05-04 (annotated)
- Committer:
- ryood
- Date:
- Thu May 04 09:22:08 2017 +0000
- Revision:
- 26:fa2bd913530b
- Parent:
- 25:ed2079e645fc
- Child:
- 27:173ad2312d03
SpiSvfController, SpiVcvsLpfController Class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:1afb83a21a25 | 1 | /* |
ryood | 0:1afb83a21a25 | 2 | * main.cpp |
ryood | 0:1afb83a21a25 | 3 | * BaseMachine Sequencer |
ryood | 0:1afb83a21a25 | 4 | * |
ryood | 20:bc7bd7faa8d5 | 5 | * mbed Rev 135 / mbed-rtos Rev 123 |
ryood | 0:1afb83a21a25 | 6 | * |
ryood | 20:bc7bd7faa8d5 | 7 | * 2017.02.14 mbed Rev 135 / mbed-rtos Rev 123に変更 |
ryood | 0:1afb83a21a25 | 8 | * 2016.11.07 UIを統合 |
ryood | 0:1afb83a21a25 | 9 | * |
ryood | 0:1afb83a21a25 | 10 | */ |
ryood | 0:1afb83a21a25 | 11 | |
ryood | 0:1afb83a21a25 | 12 | #include "mbed.h" |
ryood | 0:1afb83a21a25 | 13 | #include "rtos.h" |
ryood | 0:1afb83a21a25 | 14 | |
ryood | 0:1afb83a21a25 | 15 | #define UART_TRACE (0) |
ryood | 0:1afb83a21a25 | 16 | #include "BaseMachineCommon.h" |
ryood | 0:1afb83a21a25 | 17 | #include "BaseMachineUIController.h" |
ryood | 21:0bbca9e3ba30 | 18 | #include "SequenceSender.h" |
ryood | 0:1afb83a21a25 | 19 | #include "EnvelopeGenerator.h" |
ryood | 21:0bbca9e3ba30 | 20 | #include "SpiOscController.h" |
ryood | 0:1afb83a21a25 | 21 | #include "SpiAmpController.h" |
ryood | 0:1afb83a21a25 | 22 | #include "SpiFilterController.h" |
ryood | 26:fa2bd913530b | 23 | #include "SpiVcvsLpfController.h" |
ryood | 26:fa2bd913530b | 24 | #include "SpiSvfController.h" |
ryood | 0:1afb83a21a25 | 25 | |
ryood | 0:1afb83a21a25 | 26 | #define SPI1_RATE (1000000) |
ryood | 10:0b7f4eb37197 | 27 | #define I2C1_RATE (100000) |
ryood | 0:1afb83a21a25 | 28 | |
ryood | 0:1afb83a21a25 | 29 | const int samplingPeriod = 1; // ms |
ryood | 0:1afb83a21a25 | 30 | const int bpm = 120; |
ryood | 0:1afb83a21a25 | 31 | const int envelopeLength = (60 * 1000 / (bpm * 4)) / samplingPeriod; |
ryood | 0:1afb83a21a25 | 32 | |
ryood | 0:1afb83a21a25 | 33 | // Devices |
ryood | 0:1afb83a21a25 | 34 | // |
ryood | 0:1afb83a21a25 | 35 | BaseMachineUIController UIController; |
ryood | 0:1afb83a21a25 | 36 | |
ryood | 0:1afb83a21a25 | 37 | //SPI1 (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) |
ryood | 0:1afb83a21a25 | 38 | SPI SpiMaster(PA_7, PA_6, PA_5); |
ryood | 17:bc1f0fea5bc9 | 39 | DigitalOut SpiMasterReset(PB_10, 0); |
ryood | 0:1afb83a21a25 | 40 | |
ryood | 0:1afb83a21a25 | 41 | Sequence sequences[SEQUENCE_N]; |
ryood | 21:0bbca9e3ba30 | 42 | SequenceSender SequenceSender(sequences, SEQUENCE_N, samplingPeriod, bpm); |
ryood | 0:1afb83a21a25 | 43 | |
ryood | 0:1afb83a21a25 | 44 | Envelope envelope(4095, envelopeLength, envelopeLength*3/4, envelopeLength/2, 2047); |
ryood | 0:1afb83a21a25 | 45 | EnvelopeGenerator EnvelopeGenerator; |
ryood | 21:0bbca9e3ba30 | 46 | |
ryood | 21:0bbca9e3ba30 | 47 | SpiOscController OscController(&SpiMaster, D9); |
ryood | 0:1afb83a21a25 | 48 | SpiAmpController AmpController(&SpiMaster, D8, D7); |
ryood | 0:1afb83a21a25 | 49 | |
ryood | 26:fa2bd913530b | 50 | SpiVcvsLpfController FilterController(&SpiMaster, D10); |
ryood | 26:fa2bd913530b | 51 | //SpiSvfController FilterController(&SpiMaster, D10); |
ryood | 0:1afb83a21a25 | 52 | |
ryood | 23:099d8f1b0914 | 53 | InterruptIn SyncIn(PC_4); |
ryood | 23:099d8f1b0914 | 54 | //DigitalOut led(D13); |
ryood | 23:099d8f1b0914 | 55 | |
ryood | 0:1afb83a21a25 | 56 | // Grobal Variables |
ryood | 0:1afb83a21a25 | 57 | // |
ryood | 0:1afb83a21a25 | 58 | int playingStep = 0; |
ryood | 0:1afb83a21a25 | 59 | int editingStep = 0; |
ryood | 0:1afb83a21a25 | 60 | bool isRunning = false; |
ryood | 0:1afb83a21a25 | 61 | |
ryood | 0:1afb83a21a25 | 62 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 63 | // Callback functions |
ryood | 0:1afb83a21a25 | 64 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 65 | void updateTicks(int ticks) |
ryood | 0:1afb83a21a25 | 66 | { |
ryood | 6:fd15586f72ff | 67 | if (sequences[SequenceSender.getStep()].isNoteOn()) |
ryood | 5:e909232c913e | 68 | { |
ryood | 25:ed2079e645fc | 69 | uint16_t level = EnvelopeGenerator.update(sequences[SequenceSender.getStep()].isTie()); |
ryood | 8:4e38bb44c72e | 70 | level = level >> 1; |
ryood | 8:4e38bb44c72e | 71 | if (sequences[SequenceSender.getStep()].isAccent()) |
ryood | 5:e909232c913e | 72 | { |
ryood | 14:16d53ac0a61a | 73 | // Todo: 小数点演算を整数に |
ryood | 14:16d53ac0a61a | 74 | level *= 1.0f + ((float)UIController.getAccentLevel()) / 128.0f; |
ryood | 5:e909232c913e | 75 | } |
ryood | 5:e909232c913e | 76 | AmpController.outDca(level); |
ryood | 5:e909232c913e | 77 | } |
ryood | 5:e909232c913e | 78 | else |
ryood | 5:e909232c913e | 79 | { |
ryood | 5:e909232c913e | 80 | AmpController.outDca(0); |
ryood | 5:e909232c913e | 81 | } |
ryood | 5:e909232c913e | 82 | |
ryood | 21:0bbca9e3ba30 | 83 | OscController.setFrequency10(SequenceSender.getFrequency10()); |
ryood | 21:0bbca9e3ba30 | 84 | OscController.outDco(); |
ryood | 21:0bbca9e3ba30 | 85 | |
ryood | 5:e909232c913e | 86 | FilterController.outDcf(); |
ryood | 5:e909232c913e | 87 | |
ryood | 0:1afb83a21a25 | 88 | if (ticks == 0) |
ryood | 0:1afb83a21a25 | 89 | { |
ryood | 0:1afb83a21a25 | 90 | EnvelopeGenerator.init(envelope); |
ryood | 0:1afb83a21a25 | 91 | playingStep = SequenceSender.getStep(); |
ryood | 0:1afb83a21a25 | 92 | UIController.setPlayingStep(playingStep); |
ryood | 0:1afb83a21a25 | 93 | } |
ryood | 0:1afb83a21a25 | 94 | } |
ryood | 0:1afb83a21a25 | 95 | |
ryood | 0:1afb83a21a25 | 96 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 97 | // Functions |
ryood | 0:1afb83a21a25 | 98 | //------------------------------------------------------------------------ |
ryood | 1:87f7d2e0a123 | 99 | void setParams() |
ryood | 1:87f7d2e0a123 | 100 | { |
ryood | 1:87f7d2e0a123 | 101 | UIController.getSequences(&sequences); |
ryood | 0:1afb83a21a25 | 102 | |
ryood | 1:87f7d2e0a123 | 103 | SequenceSender.setBpm(UIController.getBpm()); |
ryood | 0:1afb83a21a25 | 104 | |
ryood | 1:87f7d2e0a123 | 105 | OscillatorParam osc; |
ryood | 1:87f7d2e0a123 | 106 | UIController.getOscillatorParam(&osc); |
ryood | 21:0bbca9e3ba30 | 107 | OscController.setWaveShape(osc.waveShape); |
ryood | 21:0bbca9e3ba30 | 108 | OscController.setPulseWidth(osc.pulseWidth << 1); |
ryood | 1:87f7d2e0a123 | 109 | |
ryood | 1:87f7d2e0a123 | 110 | FilterParam flt; |
ryood | 1:87f7d2e0a123 | 111 | UIController.getFilterParam(&flt); |
ryood | 1:87f7d2e0a123 | 112 | FilterController.setCutoff(flt.cutoff << 1); |
ryood | 1:87f7d2e0a123 | 113 | FilterController.setResonance(flt.resonance << 1); |
ryood | 1:87f7d2e0a123 | 114 | |
ryood | 1:87f7d2e0a123 | 115 | EnvelopeParam env; |
ryood | 1:87f7d2e0a123 | 116 | UIController.getEnvelopeParam(&env); |
ryood | 1:87f7d2e0a123 | 117 | envelope.setLevel(env.level << 5); |
ryood | 1:87f7d2e0a123 | 118 | //ToDo: Impl. envelope length |
ryood | 1:87f7d2e0a123 | 119 | envelope.setLength(envelopeLength); |
ryood | 1:87f7d2e0a123 | 120 | envelope.setDuration((float)env.duration / 128.0f * envelopeLength); |
ryood | 1:87f7d2e0a123 | 121 | envelope.setDecay((float)env.decay / 128.0f * envelopeLength); |
ryood | 1:87f7d2e0a123 | 122 | envelope.setSustain(env.sustain << 5); |
ryood | 1:87f7d2e0a123 | 123 | |
ryood | 1:87f7d2e0a123 | 124 | bool _isRunning = UIController.getIsRunning(); |
ryood | 5:e909232c913e | 125 | if (_isRunning != isRunning) |
ryood | 5:e909232c913e | 126 | { |
ryood | 1:87f7d2e0a123 | 127 | isRunning = _isRunning; |
ryood | 5:e909232c913e | 128 | if (isRunning) |
ryood | 5:e909232c913e | 129 | { |
ryood | 23:099d8f1b0914 | 130 | playingStep = 0; |
ryood | 1:87f7d2e0a123 | 131 | SequenceSender.run(playingStep); |
ryood | 5:e909232c913e | 132 | } |
ryood | 5:e909232c913e | 133 | else |
ryood | 5:e909232c913e | 134 | { |
ryood | 1:87f7d2e0a123 | 135 | AmpController.outDca(0); |
ryood | 1:87f7d2e0a123 | 136 | SequenceSender.stop(); |
ryood | 1:87f7d2e0a123 | 137 | } |
ryood | 1:87f7d2e0a123 | 138 | } |
ryood | 1:87f7d2e0a123 | 139 | } |
ryood | 0:1afb83a21a25 | 140 | |
ryood | 23:099d8f1b0914 | 141 | void syncFunction() |
ryood | 23:099d8f1b0914 | 142 | { |
ryood | 23:099d8f1b0914 | 143 | //led = !led; |
ryood | 23:099d8f1b0914 | 144 | SequenceSender.sync(); |
ryood | 23:099d8f1b0914 | 145 | } |
ryood | 23:099d8f1b0914 | 146 | |
ryood | 0:1afb83a21a25 | 147 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 148 | // Main routine |
ryood | 0:1afb83a21a25 | 149 | //------------------------------------------------------------------------ |
ryood | 0:1afb83a21a25 | 150 | int main() |
ryood | 0:1afb83a21a25 | 151 | { |
ryood | 0:1afb83a21a25 | 152 | #if (UART_TRACE) |
ryood | 0:1afb83a21a25 | 153 | printf("*** BaseMachine Sequencer ***\r\n"); |
ryood | 0:1afb83a21a25 | 154 | #endif |
ryood | 0:1afb83a21a25 | 155 | |
ryood | 0:1afb83a21a25 | 156 | //-------------------------------------------------------------------- |
ryood | 7:a47420a0c4bf | 157 | // Initialize objects |
ryood | 7:a47420a0c4bf | 158 | // |
ryood | 7:a47420a0c4bf | 159 | Sequence::setBaseNoteNumber(baseNoteNumber); |
ryood | 7:a47420a0c4bf | 160 | |
ryood | 7:a47420a0c4bf | 161 | //-------------------------------------------------------------------- |
ryood | 0:1afb83a21a25 | 162 | // Setup Devices |
ryood | 0:1afb83a21a25 | 163 | // |
ryood | 17:bc1f0fea5bc9 | 164 | SpiMasterReset = 1; |
ryood | 0:1afb83a21a25 | 165 | SpiMaster.format(8, 0); |
ryood | 0:1afb83a21a25 | 166 | SpiMaster.frequency(SPI1_RATE); |
ryood | 0:1afb83a21a25 | 167 | |
ryood | 0:1afb83a21a25 | 168 | // Mute output |
ryood | 0:1afb83a21a25 | 169 | AmpController.outDca(0); |
ryood | 0:1afb83a21a25 | 170 | |
ryood | 0:1afb83a21a25 | 171 | UIController.init(); |
ryood | 7:a47420a0c4bf | 172 | setParams(); |
ryood | 0:1afb83a21a25 | 173 | |
ryood | 0:1afb83a21a25 | 174 | EnvelopeGenerator.init(envelope); |
ryood | 23:099d8f1b0914 | 175 | |
ryood | 23:099d8f1b0914 | 176 | // Sync Interrupt |
ryood | 23:099d8f1b0914 | 177 | SyncIn.rise(&syncFunction); |
ryood | 0:1afb83a21a25 | 178 | |
ryood | 0:1afb83a21a25 | 179 | SequenceSender.attachUpdate(&updateTicks); |
ryood | 17:bc1f0fea5bc9 | 180 | |
ryood | 0:1afb83a21a25 | 181 | //-------------------------------------------------------------------- |
ryood | 0:1afb83a21a25 | 182 | // Main loop |
ryood | 0:1afb83a21a25 | 183 | // |
ryood | 0:1afb83a21a25 | 184 | for (;;) |
ryood | 0:1afb83a21a25 | 185 | { |
ryood | 0:1afb83a21a25 | 186 | UIController.update(); |
ryood | 1:87f7d2e0a123 | 187 | setParams(); |
ryood | 2:6056b9559541 | 188 | |
ryood | 2:6056b9559541 | 189 | Thread::wait(10); |
ryood | 0:1afb83a21a25 | 190 | } |
ryood | 0:1afb83a21a25 | 191 | } |