BaseMachine Sequencer

Dependencies:   Amp AverageAnalogIn BaseMachineUIController Envelope ExioBufferdController FilterController MCP23S17 PinDetect RotaryEncoder Sequence SequencerDisplay mbed-rtos mbed st7567LCD AT24C1024 OscController

Committer:
ryood
Date:
Thu May 04 09:42:51 2017 +0000
Revision:
27:173ad2312d03
Parent:
26:fa2bd913530b
Child:
29:5ca5aef9e7ca
Display DCF version

Who changed what in which revision?

UserRevisionLine numberNew 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 27:173ad2312d03 50 #if USE_VCVS
ryood 27:173ad2312d03 51 SpiVcvsLpfController FilterController(&SpiMaster, D10);
ryood 27:173ad2312d03 52 #else if USE_SVF
ryood 27:173ad2312d03 53 SpiSvfController FilterController(&SpiMaster, D10);
ryood 27:173ad2312d03 54 #endif
ryood 0:1afb83a21a25 55
ryood 23:099d8f1b0914 56 InterruptIn SyncIn(PC_4);
ryood 23:099d8f1b0914 57 //DigitalOut led(D13);
ryood 23:099d8f1b0914 58
ryood 0:1afb83a21a25 59 // Grobal Variables
ryood 0:1afb83a21a25 60 //
ryood 0:1afb83a21a25 61 int playingStep = 0;
ryood 0:1afb83a21a25 62 int editingStep = 0;
ryood 0:1afb83a21a25 63 bool isRunning = false;
ryood 0:1afb83a21a25 64
ryood 0:1afb83a21a25 65 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 66 // Callback functions
ryood 0:1afb83a21a25 67 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 68 void updateTicks(int ticks)
ryood 0:1afb83a21a25 69 {
ryood 6:fd15586f72ff 70 if (sequences[SequenceSender.getStep()].isNoteOn())
ryood 5:e909232c913e 71 {
ryood 25:ed2079e645fc 72 uint16_t level = EnvelopeGenerator.update(sequences[SequenceSender.getStep()].isTie());
ryood 8:4e38bb44c72e 73 level = level >> 1;
ryood 8:4e38bb44c72e 74 if (sequences[SequenceSender.getStep()].isAccent())
ryood 5:e909232c913e 75 {
ryood 14:16d53ac0a61a 76 // Todo: 小数点演算を整数に
ryood 14:16d53ac0a61a 77 level *= 1.0f + ((float)UIController.getAccentLevel()) / 128.0f;
ryood 5:e909232c913e 78 }
ryood 5:e909232c913e 79 AmpController.outDca(level);
ryood 5:e909232c913e 80 }
ryood 5:e909232c913e 81 else
ryood 5:e909232c913e 82 {
ryood 5:e909232c913e 83 AmpController.outDca(0);
ryood 5:e909232c913e 84 }
ryood 5:e909232c913e 85
ryood 21:0bbca9e3ba30 86 OscController.setFrequency10(SequenceSender.getFrequency10());
ryood 21:0bbca9e3ba30 87 OscController.outDco();
ryood 21:0bbca9e3ba30 88
ryood 5:e909232c913e 89 FilterController.outDcf();
ryood 5:e909232c913e 90
ryood 0:1afb83a21a25 91 if (ticks == 0)
ryood 0:1afb83a21a25 92 {
ryood 0:1afb83a21a25 93 EnvelopeGenerator.init(envelope);
ryood 0:1afb83a21a25 94 playingStep = SequenceSender.getStep();
ryood 0:1afb83a21a25 95 UIController.setPlayingStep(playingStep);
ryood 0:1afb83a21a25 96 }
ryood 0:1afb83a21a25 97 }
ryood 0:1afb83a21a25 98
ryood 0:1afb83a21a25 99 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 100 // Functions
ryood 0:1afb83a21a25 101 //------------------------------------------------------------------------
ryood 1:87f7d2e0a123 102 void setParams()
ryood 1:87f7d2e0a123 103 {
ryood 1:87f7d2e0a123 104 UIController.getSequences(&sequences);
ryood 0:1afb83a21a25 105
ryood 1:87f7d2e0a123 106 SequenceSender.setBpm(UIController.getBpm());
ryood 0:1afb83a21a25 107
ryood 1:87f7d2e0a123 108 OscillatorParam osc;
ryood 1:87f7d2e0a123 109 UIController.getOscillatorParam(&osc);
ryood 21:0bbca9e3ba30 110 OscController.setWaveShape(osc.waveShape);
ryood 21:0bbca9e3ba30 111 OscController.setPulseWidth(osc.pulseWidth << 1);
ryood 1:87f7d2e0a123 112
ryood 1:87f7d2e0a123 113 FilterParam flt;
ryood 1:87f7d2e0a123 114 UIController.getFilterParam(&flt);
ryood 1:87f7d2e0a123 115 FilterController.setCutoff(flt.cutoff << 1);
ryood 1:87f7d2e0a123 116 FilterController.setResonance(flt.resonance << 1);
ryood 1:87f7d2e0a123 117
ryood 1:87f7d2e0a123 118 EnvelopeParam env;
ryood 1:87f7d2e0a123 119 UIController.getEnvelopeParam(&env);
ryood 1:87f7d2e0a123 120 envelope.setLevel(env.level << 5);
ryood 1:87f7d2e0a123 121 //ToDo: Impl. envelope length
ryood 1:87f7d2e0a123 122 envelope.setLength(envelopeLength);
ryood 1:87f7d2e0a123 123 envelope.setDuration((float)env.duration / 128.0f * envelopeLength);
ryood 1:87f7d2e0a123 124 envelope.setDecay((float)env.decay / 128.0f * envelopeLength);
ryood 1:87f7d2e0a123 125 envelope.setSustain(env.sustain << 5);
ryood 1:87f7d2e0a123 126
ryood 1:87f7d2e0a123 127 bool _isRunning = UIController.getIsRunning();
ryood 5:e909232c913e 128 if (_isRunning != isRunning)
ryood 5:e909232c913e 129 {
ryood 1:87f7d2e0a123 130 isRunning = _isRunning;
ryood 5:e909232c913e 131 if (isRunning)
ryood 5:e909232c913e 132 {
ryood 23:099d8f1b0914 133 playingStep = 0;
ryood 1:87f7d2e0a123 134 SequenceSender.run(playingStep);
ryood 5:e909232c913e 135 }
ryood 5:e909232c913e 136 else
ryood 5:e909232c913e 137 {
ryood 1:87f7d2e0a123 138 AmpController.outDca(0);
ryood 1:87f7d2e0a123 139 SequenceSender.stop();
ryood 1:87f7d2e0a123 140 }
ryood 1:87f7d2e0a123 141 }
ryood 1:87f7d2e0a123 142 }
ryood 0:1afb83a21a25 143
ryood 23:099d8f1b0914 144 void syncFunction()
ryood 23:099d8f1b0914 145 {
ryood 23:099d8f1b0914 146 //led = !led;
ryood 23:099d8f1b0914 147 SequenceSender.sync();
ryood 23:099d8f1b0914 148 }
ryood 23:099d8f1b0914 149
ryood 0:1afb83a21a25 150 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 151 // Main routine
ryood 0:1afb83a21a25 152 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 153 int main()
ryood 0:1afb83a21a25 154 {
ryood 0:1afb83a21a25 155 #if (UART_TRACE)
ryood 0:1afb83a21a25 156 printf("*** BaseMachine Sequencer ***\r\n");
ryood 0:1afb83a21a25 157 #endif
ryood 0:1afb83a21a25 158
ryood 0:1afb83a21a25 159 //--------------------------------------------------------------------
ryood 7:a47420a0c4bf 160 // Initialize objects
ryood 7:a47420a0c4bf 161 //
ryood 7:a47420a0c4bf 162 Sequence::setBaseNoteNumber(baseNoteNumber);
ryood 7:a47420a0c4bf 163
ryood 7:a47420a0c4bf 164 //--------------------------------------------------------------------
ryood 0:1afb83a21a25 165 // Setup Devices
ryood 0:1afb83a21a25 166 //
ryood 17:bc1f0fea5bc9 167 SpiMasterReset = 1;
ryood 0:1afb83a21a25 168 SpiMaster.format(8, 0);
ryood 0:1afb83a21a25 169 SpiMaster.frequency(SPI1_RATE);
ryood 0:1afb83a21a25 170
ryood 0:1afb83a21a25 171 // Mute output
ryood 0:1afb83a21a25 172 AmpController.outDca(0);
ryood 0:1afb83a21a25 173
ryood 0:1afb83a21a25 174 UIController.init();
ryood 7:a47420a0c4bf 175 setParams();
ryood 0:1afb83a21a25 176
ryood 0:1afb83a21a25 177 EnvelopeGenerator.init(envelope);
ryood 23:099d8f1b0914 178
ryood 23:099d8f1b0914 179 // Sync Interrupt
ryood 23:099d8f1b0914 180 SyncIn.rise(&syncFunction);
ryood 0:1afb83a21a25 181
ryood 0:1afb83a21a25 182 SequenceSender.attachUpdate(&updateTicks);
ryood 17:bc1f0fea5bc9 183
ryood 0:1afb83a21a25 184 //--------------------------------------------------------------------
ryood 0:1afb83a21a25 185 // Main loop
ryood 0:1afb83a21a25 186 //
ryood 0:1afb83a21a25 187 for (;;)
ryood 0:1afb83a21a25 188 {
ryood 0:1afb83a21a25 189 UIController.update();
ryood 1:87f7d2e0a123 190 setParams();
ryood 2:6056b9559541 191
ryood 2:6056b9559541 192 Thread::wait(10);
ryood 0:1afb83a21a25 193 }
ryood 0:1afb83a21a25 194 }