Nucleo Sequencer of my Base Machine
Dependencies: Amp AverageAnalogIn Envelope FilterController Sequence BaseMachineComon mbed-rtos mbed
Fork of SpiSequenceSender_Test by
main.cpp@16:b0419e3c9079, 2016-08-23 (annotated)
- Committer:
- ryood
- Date:
- Tue Aug 23 10:02:18 2016 +0000
- Revision:
- 16:b0419e3c9079
- Parent:
- 15:9813d8eaf57e
- Child:
- 17:557658db3e81
Add: ST7565 library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 15:9813d8eaf57e | 1 | /* |
ryood | 15:9813d8eaf57e | 2 | * main.cpp |
ryood | 15:9813d8eaf57e | 3 | * SpiSequencerSender_test |
ryood | 15:9813d8eaf57e | 4 | * |
ryood | 15:9813d8eaf57e | 5 | * 2016.08.20 mbed Rev 121 / mbed-rtos Rev 117で動作確認 |
ryood | 15:9813d8eaf57e | 6 | * |
ryood | 15:9813d8eaf57e | 7 | */ |
ryood | 15:9813d8eaf57e | 8 | |
ryood | 0:21e39bc75e31 | 9 | #include "mbed.h" |
ryood | 5:4abac408b827 | 10 | #include "rtos.h" |
ryood | 16:b0419e3c9079 | 11 | #include "st7565LCD.h" |
ryood | 0:21e39bc75e31 | 12 | |
ryood | 6:fc10eac60b91 | 13 | #define UART_TRACE (0) |
ryood | 6:fc10eac60b91 | 14 | #include "SpiSequenceSender.h" |
ryood | 9:3e4e08578e1c | 15 | #include "EnvelopeGenerator.h" |
ryood | 9:3e4e08578e1c | 16 | #include "SpiAmpController.h" |
ryood | 12:c248781608af | 17 | #include "SpiFilterController.h" |
ryood | 0:21e39bc75e31 | 18 | |
ryood | 16:b0419e3c9079 | 19 | #define TITLE_STR1 ("BaseMachine Sequencer") |
ryood | 16:b0419e3c9079 | 20 | #define TITLE_STR2 ("20160823") |
ryood | 16:b0419e3c9079 | 21 | |
ryood | 6:fc10eac60b91 | 22 | #define SEQUENCE_N (16) |
ryood | 6:fc10eac60b91 | 23 | #define SPI_RATE (8000000) |
ryood | 0:21e39bc75e31 | 24 | |
ryood | 14:c173e03ee3ad | 25 | const int samplingPeriod = 1; // ms |
ryood | 10:fb04f9959fd9 | 26 | const int bpm = 120; |
ryood | 10:fb04f9959fd9 | 27 | const int envelopeLength = (60 * 1000 / (bpm * 4)) / samplingPeriod; |
ryood | 14:c173e03ee3ad | 28 | const int waveShape = SpiSequenceSender::WAVESHAPE_SAW; |
ryood | 14:c173e03ee3ad | 29 | const int baseNoteNumber = 48; |
ryood | 14:c173e03ee3ad | 30 | |
ryood | 14:c173e03ee3ad | 31 | // Initial Sequence |
ryood | 14:c173e03ee3ad | 32 | const int noteOn[SEQUENCE_N] = { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0 }; |
ryood | 14:c173e03ee3ad | 33 | const int octave[SEQUENCE_N] = {-1,-1,-1, 0, 0,-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }; |
ryood | 14:c173e03ee3ad | 34 | const int pitch[SEQUENCE_N] = { 9, 7, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 }; |
ryood | 14:c173e03ee3ad | 35 | const int tie[SEQUENCE_N] = { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }; |
ryood | 10:fb04f9959fd9 | 36 | |
ryood | 16:b0419e3c9079 | 37 | // Devices |
ryood | 16:b0419e3c9079 | 38 | // |
ryood | 16:b0419e3c9079 | 39 | //SPI (PinName mosi, PinName miso, PinName sclk, PinName ssel=NC) |
ryood | 16:b0419e3c9079 | 40 | //SPI spiMaster(PA_7, PA_6, PA_5); |
ryood | 16:b0419e3c9079 | 41 | SPI spiMaster(SPI_MOSI, SPI_MISO, SPI_SCK); |
ryood | 16:b0419e3c9079 | 42 | |
ryood | 16:b0419e3c9079 | 43 | //ST7565(PinName mosi, PinName sclk, PinName cs, PinName rst, PinName a0); |
ryood | 16:b0419e3c9079 | 44 | ST7565 gLCD(PB_15, PB_13, PB_12, PB_2, PB_1); |
ryood | 16:b0419e3c9079 | 45 | |
ryood | 13:b0f988a239df | 46 | //AnalogIn levelIn(A0); |
ryood | 13:b0f988a239df | 47 | AnalogIn durationIn(A2); |
ryood | 13:b0f988a239df | 48 | AnalogIn decayIn(A1); |
ryood | 13:b0f988a239df | 49 | AnalogIn sustainIn(A0); |
ryood | 13:b0f988a239df | 50 | AnalogIn cutoffIn(A3); |
ryood | 13:b0f988a239df | 51 | AnalogIn resonanceIn(A4); |
ryood | 10:fb04f9959fd9 | 52 | |
ryood | 16:b0419e3c9079 | 53 | // Grobal Variables |
ryood | 16:b0419e3c9079 | 54 | // |
ryood | 16:b0419e3c9079 | 55 | Sequence sequences[SEQUENCE_N]; |
ryood | 16:b0419e3c9079 | 56 | SpiSequenceSender sequenceSender(&spiMaster, D9, sequences, SEQUENCE_N, samplingPeriod, bpm); |
ryood | 6:fc10eac60b91 | 57 | |
ryood | 10:fb04f9959fd9 | 58 | Envelope envelope(4095, envelopeLength, envelopeLength*3/4, envelopeLength/2, 2047); |
ryood | 9:3e4e08578e1c | 59 | EnvelopeGenerator envelopeGenerator; |
ryood | 9:3e4e08578e1c | 60 | SpiAmpController ampController(&spiMaster, D8, D7); |
ryood | 6:fc10eac60b91 | 61 | |
ryood | 12:c248781608af | 62 | SpiFilterController filterController(&spiMaster, D10); |
ryood | 0:21e39bc75e31 | 63 | |
ryood | 16:b0419e3c9079 | 64 | volatile int currentStep = 0; |
ryood | 16:b0419e3c9079 | 65 | volatile bool isRunning = false; |
ryood | 16:b0419e3c9079 | 66 | volatile bool isDirty = false; |
ryood | 16:b0419e3c9079 | 67 | volatile uint8_t pinFlag = 0x00; |
ryood | 16:b0419e3c9079 | 68 | |
ryood | 16:b0419e3c9079 | 69 | enum PinBit { |
ryood | 16:b0419e3c9079 | 70 | bWaveShape = 0x01, |
ryood | 16:b0419e3c9079 | 71 | bModNumber = 0x02, |
ryood | 16:b0419e3c9079 | 72 | bOctaveUp = 0x04, |
ryood | 16:b0419e3c9079 | 73 | bOctaveDown = 0x08, |
ryood | 16:b0419e3c9079 | 74 | bNoteOnOff = 0x10, |
ryood | 16:b0419e3c9079 | 75 | bTie = 0x20, |
ryood | 16:b0419e3c9079 | 76 | bAccent = 0x40, |
ryood | 16:b0419e3c9079 | 77 | bRunStop = 0x80 |
ryood | 16:b0419e3c9079 | 78 | }; |
ryood | 16:b0419e3c9079 | 79 | |
ryood | 16:b0419e3c9079 | 80 | // とりあえずの変数(後でClassのメンバ変数に格納) |
ryood | 16:b0419e3c9079 | 81 | #define MOD_NUMBER_MAX 1 |
ryood | 16:b0419e3c9079 | 82 | volatile int modNumber = 0; |
ryood | 16:b0419e3c9079 | 83 | volatile uint8_t cutOff; |
ryood | 16:b0419e3c9079 | 84 | volatile uint8_t resonance; |
ryood | 16:b0419e3c9079 | 85 | volatile uint8_t envMod; |
ryood | 16:b0419e3c9079 | 86 | volatile uint8_t level; |
ryood | 16:b0419e3c9079 | 87 | volatile uint8_t duration; |
ryood | 16:b0419e3c9079 | 88 | volatile uint8_t decay; |
ryood | 16:b0419e3c9079 | 89 | volatile uint8_t sustain; |
ryood | 16:b0419e3c9079 | 90 | |
ryood | 16:b0419e3c9079 | 91 | //------------------------------------------------------------------------ |
ryood | 16:b0419e3c9079 | 92 | // Callback functions |
ryood | 16:b0419e3c9079 | 93 | //------------------------------------------------------------------------ |
ryood | 16:b0419e3c9079 | 94 | void updateFunction(int ticks) |
ryood | 8:de409197ff95 | 95 | { |
ryood | 8:de409197ff95 | 96 | if (ticks == 0) { |
ryood | 9:3e4e08578e1c | 97 | envelopeGenerator.init(envelope); |
ryood | 8:de409197ff95 | 98 | } |
ryood | 14:c173e03ee3ad | 99 | |
ryood | 15:9813d8eaf57e | 100 | if (sequenceSender.getSequences()[sequenceSender.getStep()].isNoteOn()) { |
ryood | 14:c173e03ee3ad | 101 | uint16_t level = envelopeGenerator.getModLevel(); |
ryood | 14:c173e03ee3ad | 102 | ampController.outDca(level); |
ryood | 14:c173e03ee3ad | 103 | } else { |
ryood | 14:c173e03ee3ad | 104 | ampController.outDca(0); |
ryood | 14:c173e03ee3ad | 105 | } |
ryood | 9:3e4e08578e1c | 106 | envelopeGenerator.update(); |
ryood | 12:c248781608af | 107 | |
ryood | 15:9813d8eaf57e | 108 | filterController.outDcf(); |
ryood | 8:de409197ff95 | 109 | } |
ryood | 8:de409197ff95 | 110 | |
ryood | 16:b0419e3c9079 | 111 | |
ryood | 16:b0419e3c9079 | 112 | //------------------------------------------------------------------------ |
ryood | 16:b0419e3c9079 | 113 | // Functions |
ryood | 16:b0419e3c9079 | 114 | //------------------------------------------------------------------------ |
ryood | 16:b0419e3c9079 | 115 | void dumpToLCD() |
ryood | 16:b0419e3c9079 | 116 | { |
ryood | 16:b0419e3c9079 | 117 | char buff[64]; |
ryood | 16:b0419e3c9079 | 118 | int col = 0; |
ryood | 16:b0419e3c9079 | 119 | |
ryood | 16:b0419e3c9079 | 120 | gLCD.clear(); |
ryood | 16:b0419e3c9079 | 121 | |
ryood | 16:b0419e3c9079 | 122 | sprintf(buff, "Run: %d", isRunning); |
ryood | 16:b0419e3c9079 | 123 | gLCD.drawstring(0, col++, buff); |
ryood | 16:b0419e3c9079 | 124 | sprintf(buff, "BPM: %d", sequenceSender.getBpm()); |
ryood | 16:b0419e3c9079 | 125 | gLCD.drawstring(0, col++, buff); |
ryood | 16:b0419e3c9079 | 126 | |
ryood | 16:b0419e3c9079 | 127 | sprintf(buff, "Step: %d", currentStep); |
ryood | 16:b0419e3c9079 | 128 | gLCD.drawstring(0, col++, buff); |
ryood | 16:b0419e3c9079 | 129 | sprintf(buff, "NoteOn: %d", sequences[currentStep].isNoteOn()); |
ryood | 16:b0419e3c9079 | 130 | gLCD.drawstring(0, col++, buff); |
ryood | 16:b0419e3c9079 | 131 | sprintf(buff, "Pitch: %d", sequences[currentStep].getPitch()); |
ryood | 16:b0419e3c9079 | 132 | gLCD.drawstring(0, col++, buff); |
ryood | 16:b0419e3c9079 | 133 | sprintf(buff, "Octave: %d", sequences[currentStep].getOctave()); |
ryood | 16:b0419e3c9079 | 134 | gLCD.drawstring(0, col++, buff); |
ryood | 16:b0419e3c9079 | 135 | sprintf(buff, "Tie: %d", sequences[currentStep].isTie()); |
ryood | 16:b0419e3c9079 | 136 | gLCD.drawstring(0, col++, buff); |
ryood | 16:b0419e3c9079 | 137 | sprintf(buff, "Accent: %d", sequences[currentStep].isAccent()); |
ryood | 16:b0419e3c9079 | 138 | gLCD.drawstring(0, col++, buff); |
ryood | 16:b0419e3c9079 | 139 | |
ryood | 16:b0419e3c9079 | 140 | col = 0; |
ryood | 16:b0419e3c9079 | 141 | sprintf(buff, "WavS: %d", sequenceSender.getWaveShape()); |
ryood | 16:b0419e3c9079 | 142 | gLCD.drawstring(64, col++, buff); |
ryood | 16:b0419e3c9079 | 143 | sprintf(buff, "ModN: %d", modNumber); |
ryood | 16:b0419e3c9079 | 144 | gLCD.drawstring(64, col++, buff); |
ryood | 16:b0419e3c9079 | 145 | |
ryood | 16:b0419e3c9079 | 146 | col = 2; |
ryood | 16:b0419e3c9079 | 147 | sprintf(buff, "PW%3d CO%3d", sequenceSender.getPulseWidth(), cutOff); |
ryood | 16:b0419e3c9079 | 148 | gLCD.drawstring(60, col++, buff); |
ryood | 16:b0419e3c9079 | 149 | sprintf(buff, "RS%3d EV%3d", resonance, envMod); |
ryood | 16:b0419e3c9079 | 150 | gLCD.drawstring(60, col++, buff); |
ryood | 16:b0419e3c9079 | 151 | sprintf(buff, "LV%3d DR%3d", level, duration); |
ryood | 16:b0419e3c9079 | 152 | gLCD.drawstring(60, col++, buff); |
ryood | 16:b0419e3c9079 | 153 | sprintf(buff, "DC%3d ST%3d", decay, sustain); |
ryood | 16:b0419e3c9079 | 154 | gLCD.drawstring(60, col++, buff); |
ryood | 16:b0419e3c9079 | 155 | |
ryood | 16:b0419e3c9079 | 156 | gLCD.display(); |
ryood | 16:b0419e3c9079 | 157 | } |
ryood | 16:b0419e3c9079 | 158 | |
ryood | 16:b0419e3c9079 | 159 | //------------------------------------------------------------------------ |
ryood | 16:b0419e3c9079 | 160 | // Main routine |
ryood | 16:b0419e3c9079 | 161 | //------------------------------------------------------------------------ |
ryood | 0:21e39bc75e31 | 162 | int main() |
ryood | 0:21e39bc75e31 | 163 | { |
ryood | 16:b0419e3c9079 | 164 | // Setup Devices |
ryood | 16:b0419e3c9079 | 165 | // |
ryood | 0:21e39bc75e31 | 166 | spiMaster.format(8, 0); |
ryood | 0:21e39bc75e31 | 167 | spiMaster.frequency(SPI_RATE); |
ryood | 0:21e39bc75e31 | 168 | |
ryood | 16:b0419e3c9079 | 169 | gLCD.begin(0x12); |
ryood | 16:b0419e3c9079 | 170 | gLCD.clear(); |
ryood | 16:b0419e3c9079 | 171 | gLCD.drawstring(0, 0, TITLE_STR1); |
ryood | 16:b0419e3c9079 | 172 | gLCD.drawstring(0, 1, TITLE_STR2); |
ryood | 16:b0419e3c9079 | 173 | gLCD.display(); |
ryood | 16:b0419e3c9079 | 174 | Thread::wait(1000); |
ryood | 16:b0419e3c9079 | 175 | |
ryood | 0:21e39bc75e31 | 176 | // Test SequencerSender Run |
ryood | 0:21e39bc75e31 | 177 | // |
ryood | 15:9813d8eaf57e | 178 | Sequence::setBaseNoteNumber(baseNoteNumber); |
ryood | 10:fb04f9959fd9 | 179 | sequenceSender.setBpm(bpm); |
ryood | 14:c173e03ee3ad | 180 | |
ryood | 0:21e39bc75e31 | 181 | for (int i = 0; i < SEQUENCE_N; i++) { |
ryood | 0:21e39bc75e31 | 182 | Sequence& seq = sequenceSender.getSequences()[i]; |
ryood | 14:c173e03ee3ad | 183 | seq.setPitch(pitch[i]); |
ryood | 14:c173e03ee3ad | 184 | seq.setOctave(octave[i]); |
ryood | 15:9813d8eaf57e | 185 | seq.setNoteOn(noteOn[i]); |
ryood | 15:9813d8eaf57e | 186 | seq.setTie(tie[i]); |
ryood | 0:21e39bc75e31 | 187 | } |
ryood | 6:fc10eac60b91 | 188 | |
ryood | 9:3e4e08578e1c | 189 | envelopeGenerator.init(envelope); |
ryood | 12:c248781608af | 190 | |
ryood | 16:b0419e3c9079 | 191 | sequenceSender.attachUpdate(&updateFunction); |
ryood | 14:c173e03ee3ad | 192 | sequenceSender.setWaveShape(waveShape); |
ryood | 0:21e39bc75e31 | 193 | sequenceSender.run(0); |
ryood | 5:4abac408b827 | 194 | |
ryood | 5:4abac408b827 | 195 | for (;;) { |
ryood | 10:fb04f9959fd9 | 196 | /* |
ryood | 5:4abac408b827 | 197 | sequenceSender.setPulseWidth(sequenceSender.getPulseWidth() + 4); |
ryood | 6:fc10eac60b91 | 198 | Thread::wait(500); |
ryood | 6:fc10eac60b91 | 199 | sequenceSender.setWaveShape(SpiSequenceSender::WAVESHAPE_SAW); |
ryood | 6:fc10eac60b91 | 200 | Thread::wait(500); |
ryood | 6:fc10eac60b91 | 201 | sequenceSender.setWaveShape(SpiSequenceSender::WAVESHAPE_SQUARE); |
ryood | 10:fb04f9959fd9 | 202 | */ |
ryood | 13:b0f988a239df | 203 | //envelope.setLevel(levelIn * 4095); |
ryood | 13:b0f988a239df | 204 | envelope.setLevel(4095); |
ryood | 10:fb04f9959fd9 | 205 | envelope.setDuration(durationIn * envelopeLength); |
ryood | 10:fb04f9959fd9 | 206 | envelope.setDecay(decayIn * envelopeLength); |
ryood | 10:fb04f9959fd9 | 207 | envelope.setSustain(sustainIn * 4095); |
ryood | 12:c248781608af | 208 | |
ryood | 12:c248781608af | 209 | filterController.setCutoff(cutoffIn * 255); |
ryood | 12:c248781608af | 210 | filterController.setResonance(resonanceIn * 255); |
ryood | 12:c248781608af | 211 | |
ryood | 14:c173e03ee3ad | 212 | #if(UART_TRACE) |
ryood | 12:c248781608af | 213 | printf("%d\t%d\t%d\t%d\t%d\t%d\r\n", |
ryood | 12:c248781608af | 214 | filterController.getCutoff(), |
ryood | 12:c248781608af | 215 | filterController.getResonance(), |
ryood | 12:c248781608af | 216 | envelope.getLevel(), |
ryood | 12:c248781608af | 217 | envelope.getDuration(), |
ryood | 12:c248781608af | 218 | envelope.getDecay(), |
ryood | 12:c248781608af | 219 | envelope.getSustain() |
ryood | 12:c248781608af | 220 | ); |
ryood | 14:c173e03ee3ad | 221 | #endif |
ryood | 16:b0419e3c9079 | 222 | |
ryood | 16:b0419e3c9079 | 223 | //dumpToLCD(); |
ryood | 5:4abac408b827 | 224 | } |
ryood | 0:21e39bc75e31 | 225 | } |