BaseMachine Sequencer

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

Committer:
ryood
Date:
Fri Nov 25 09:08:14 2016 +0000
Revision:
10:0b7f4eb37197
Parent:
8:4e38bb44c72e
Child:
11:eb9e72bf529c
Add: AT24C1024 Simple Test

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 0:1afb83a21a25 5 * mbed Rev 121 / mbed-rtos Rev 117
ryood 0:1afb83a21a25 6 *
ryood 0:1afb83a21a25 7 * 2016.11.07 UIを統合
ryood 0:1afb83a21a25 8 *
ryood 0:1afb83a21a25 9 */
ryood 0:1afb83a21a25 10
ryood 0:1afb83a21a25 11 #include "mbed.h"
ryood 0:1afb83a21a25 12 #include "rtos.h"
ryood 10:0b7f4eb37197 13 #include "AT24C1024.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 0:1afb83a21a25 18 #include "SpiSequenceSender.h"
ryood 0:1afb83a21a25 19 #include "EnvelopeGenerator.h"
ryood 0:1afb83a21a25 20 #include "SpiAmpController.h"
ryood 0:1afb83a21a25 21 #include "SpiFilterController.h"
ryood 0:1afb83a21a25 22
ryood 0:1afb83a21a25 23 #define SPI1_RATE (1000000)
ryood 10:0b7f4eb37197 24 #define I2C1_RATE (100000)
ryood 0:1afb83a21a25 25
ryood 0:1afb83a21a25 26 const int samplingPeriod = 1; // ms
ryood 0:1afb83a21a25 27 const int bpm = 120;
ryood 0:1afb83a21a25 28 const int envelopeLength = (60 * 1000 / (bpm * 4)) / samplingPeriod;
ryood 0:1afb83a21a25 29 const int waveShape = SpiSequenceSender::WAVESHAPE_SQUARE;
ryood 0:1afb83a21a25 30
ryood 0:1afb83a21a25 31 // Devices
ryood 0:1afb83a21a25 32 //
ryood 0:1afb83a21a25 33 BaseMachineUIController UIController;
ryood 0:1afb83a21a25 34
ryood 0:1afb83a21a25 35 //SPI1 (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC)
ryood 0:1afb83a21a25 36 SPI SpiMaster(PA_7, PA_6, PA_5);
ryood 0:1afb83a21a25 37
ryood 10:0b7f4eb37197 38 I2C I2cMaster(PB_9, PB_8); // SDA, SCL
ryood 10:0b7f4eb37197 39 AT24C1024 At24c1024(I2cMaster); // Atmel 1Mbit EE-PROM
ryood 10:0b7f4eb37197 40
ryood 0:1afb83a21a25 41 Sequence sequences[SEQUENCE_N];
ryood 0:1afb83a21a25 42 SpiSequenceSender SequenceSender(&SpiMaster, D9, 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 0:1afb83a21a25 46 SpiAmpController AmpController(&SpiMaster, D8, D7);
ryood 0:1afb83a21a25 47
ryood 0:1afb83a21a25 48 SpiFilterController FilterController(&SpiMaster, D10);
ryood 0:1afb83a21a25 49
ryood 0:1afb83a21a25 50 // Grobal Variables
ryood 0:1afb83a21a25 51 //
ryood 0:1afb83a21a25 52 int playingStep = 0;
ryood 0:1afb83a21a25 53 int editingStep = 0;
ryood 0:1afb83a21a25 54 bool isRunning = false;
ryood 0:1afb83a21a25 55
ryood 0:1afb83a21a25 56 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 57 // Callback functions
ryood 0:1afb83a21a25 58 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 59 void updateTicks(int ticks)
ryood 0:1afb83a21a25 60 {
ryood 6:fd15586f72ff 61 if (sequences[SequenceSender.getStep()].isNoteOn())
ryood 5:e909232c913e 62 {
ryood 5:e909232c913e 63 uint16_t level = EnvelopeGenerator.update();
ryood 8:4e38bb44c72e 64 level = level >> 1;
ryood 8:4e38bb44c72e 65 if (sequences[SequenceSender.getStep()].isAccent())
ryood 5:e909232c913e 66 {
ryood 8:4e38bb44c72e 67 level += (UIController.getAccentLevel() << 4);
ryood 5:e909232c913e 68 }
ryood 5:e909232c913e 69 AmpController.outDca(level);
ryood 5:e909232c913e 70 //printf("%d %d %d\r\n", playingStep, ticks, level);
ryood 5:e909232c913e 71 }
ryood 5:e909232c913e 72 else
ryood 5:e909232c913e 73 {
ryood 5:e909232c913e 74 AmpController.outDca(0);
ryood 5:e909232c913e 75 }
ryood 5:e909232c913e 76
ryood 5:e909232c913e 77 FilterController.outDcf();
ryood 5:e909232c913e 78
ryood 0:1afb83a21a25 79 if (ticks == 0)
ryood 0:1afb83a21a25 80 {
ryood 0:1afb83a21a25 81 EnvelopeGenerator.init(envelope);
ryood 0:1afb83a21a25 82 playingStep = SequenceSender.getStep();
ryood 0:1afb83a21a25 83 UIController.setPlayingStep(playingStep);
ryood 0:1afb83a21a25 84 }
ryood 0:1afb83a21a25 85 }
ryood 0:1afb83a21a25 86
ryood 0:1afb83a21a25 87 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 88 // Functions
ryood 0:1afb83a21a25 89 //------------------------------------------------------------------------
ryood 1:87f7d2e0a123 90 void setParams()
ryood 1:87f7d2e0a123 91 {
ryood 1:87f7d2e0a123 92 UIController.getSequences(&sequences);
ryood 0:1afb83a21a25 93
ryood 1:87f7d2e0a123 94 SequenceSender.setBpm(UIController.getBpm());
ryood 0:1afb83a21a25 95
ryood 1:87f7d2e0a123 96 // ToDo: Impl. accentLevel
ryood 1:87f7d2e0a123 97
ryood 1:87f7d2e0a123 98 OscillatorParam osc;
ryood 1:87f7d2e0a123 99 UIController.getOscillatorParam(&osc);
ryood 1:87f7d2e0a123 100 SequenceSender.setWaveShape(osc.waveShape);
ryood 2:6056b9559541 101 SequenceSender.setPulseWidth(osc.pulseWidth << 1);
ryood 1:87f7d2e0a123 102
ryood 1:87f7d2e0a123 103 FilterParam flt;
ryood 1:87f7d2e0a123 104 UIController.getFilterParam(&flt);
ryood 1:87f7d2e0a123 105 FilterController.setCutoff(flt.cutoff << 1);
ryood 1:87f7d2e0a123 106 FilterController.setResonance(flt.resonance << 1);
ryood 1:87f7d2e0a123 107
ryood 1:87f7d2e0a123 108 EnvelopeParam env;
ryood 1:87f7d2e0a123 109 UIController.getEnvelopeParam(&env);
ryood 1:87f7d2e0a123 110 envelope.setLevel(env.level << 5);
ryood 1:87f7d2e0a123 111 //ToDo: Impl. envelope length
ryood 1:87f7d2e0a123 112 envelope.setLength(envelopeLength);
ryood 1:87f7d2e0a123 113 envelope.setDuration((float)env.duration / 128.0f * envelopeLength);
ryood 1:87f7d2e0a123 114 envelope.setDecay((float)env.decay / 128.0f * envelopeLength);
ryood 1:87f7d2e0a123 115 envelope.setSustain(env.sustain << 5);
ryood 1:87f7d2e0a123 116
ryood 1:87f7d2e0a123 117 bool _isRunning = UIController.getIsRunning();
ryood 5:e909232c913e 118 if (_isRunning != isRunning)
ryood 5:e909232c913e 119 {
ryood 1:87f7d2e0a123 120 isRunning = _isRunning;
ryood 5:e909232c913e 121 if (isRunning)
ryood 5:e909232c913e 122 {
ryood 1:87f7d2e0a123 123 SequenceSender.run(playingStep);
ryood 5:e909232c913e 124 }
ryood 5:e909232c913e 125 else
ryood 5:e909232c913e 126 {
ryood 1:87f7d2e0a123 127 AmpController.outDca(0);
ryood 1:87f7d2e0a123 128 SequenceSender.stop();
ryood 1:87f7d2e0a123 129 }
ryood 1:87f7d2e0a123 130 }
ryood 1:87f7d2e0a123 131 }
ryood 0:1afb83a21a25 132
ryood 0:1afb83a21a25 133 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 134 // Main routine
ryood 0:1afb83a21a25 135 //------------------------------------------------------------------------
ryood 0:1afb83a21a25 136 int main()
ryood 0:1afb83a21a25 137 {
ryood 0:1afb83a21a25 138 #if (UART_TRACE)
ryood 0:1afb83a21a25 139 printf("*** BaseMachine Sequencer ***\r\n");
ryood 0:1afb83a21a25 140 #endif
ryood 0:1afb83a21a25 141
ryood 0:1afb83a21a25 142 //--------------------------------------------------------------------
ryood 7:a47420a0c4bf 143 // Initialize objects
ryood 7:a47420a0c4bf 144 //
ryood 7:a47420a0c4bf 145 Sequence::setBaseNoteNumber(baseNoteNumber);
ryood 7:a47420a0c4bf 146
ryood 7:a47420a0c4bf 147 //--------------------------------------------------------------------
ryood 0:1afb83a21a25 148 // Setup Devices
ryood 0:1afb83a21a25 149 //
ryood 0:1afb83a21a25 150 SpiMaster.format(8, 0);
ryood 0:1afb83a21a25 151 SpiMaster.frequency(SPI1_RATE);
ryood 0:1afb83a21a25 152
ryood 0:1afb83a21a25 153 // Mute output
ryood 0:1afb83a21a25 154 AmpController.outDca(0);
ryood 0:1afb83a21a25 155
ryood 0:1afb83a21a25 156 UIController.init();
ryood 7:a47420a0c4bf 157 setParams();
ryood 0:1afb83a21a25 158
ryood 0:1afb83a21a25 159 EnvelopeGenerator.init(envelope);
ryood 0:1afb83a21a25 160
ryood 0:1afb83a21a25 161 SequenceSender.attachUpdate(&updateTicks);
ryood 0:1afb83a21a25 162 SequenceSender.setWaveShape(waveShape);
ryood 0:1afb83a21a25 163
ryood 10:0b7f4eb37197 164 // Byte Read/Write
ryood 10:0b7f4eb37197 165 //
ryood 10:0b7f4eb37197 166 uint8_t dt = 0xaa;
ryood 10:0b7f4eb37197 167 printf("Byte Write: %02x\r\n", dt);
ryood 10:0b7f4eb37197 168 At24c1024.write(0, dt); // write addr=0 data=dt
ryood 10:0b7f4eb37197 169 wait_ms(5);
ryood 10:0b7f4eb37197 170 dt = At24c1024.read(0); // read addr=0
ryood 10:0b7f4eb37197 171 printf("Byte Read: %02x\r\n", dt);
ryood 10:0b7f4eb37197 172
ryood 0:1afb83a21a25 173 //--------------------------------------------------------------------
ryood 0:1afb83a21a25 174 // Main loop
ryood 0:1afb83a21a25 175 //
ryood 0:1afb83a21a25 176 for (;;)
ryood 0:1afb83a21a25 177 {
ryood 0:1afb83a21a25 178 UIController.update();
ryood 1:87f7d2e0a123 179 setParams();
ryood 2:6056b9559541 180
ryood 2:6056b9559541 181 Thread::wait(10);
ryood 0:1afb83a21a25 182 }
ryood 0:1afb83a21a25 183 }