BaseMachine Sequencer

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

Committer:
ryood
Date:
Mon May 01 08:24:22 2017 +0000
Revision:
24:ddc42cfb9384
Parent:
23:099d8f1b0914
Child:
25:ed2079e645fc
Add SpiSVFilterController Class

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