Nucleo SPI Sequencer
Dependencies: AverageAnalogIn N5110 Nucleo_rtos_UI_Test PinDetect RotaryEncoder Sequence mbed-rtos mbed FilterController
Fork of Nucleo_rtos_UI_Test by
main.cpp
- Committer:
- ryood
- Date:
- 2016-05-27
- Revision:
- 4:d9a72e07749f
- Parent:
- 3:8c8020dfd82f
- Child:
- 5:e4b68faa6801
File content as of revision 4:d9a72e07749f:
/* * Nucleo RTOS Sequencer User Interface Test. * * 2016.05.27 * */ #include "mbed.h" #include "rtos.h" #include "PinDetect.h" #include "RotaryEncoder.h" #include "N5110.h" #include "AverageAnalogIn.h" #define SEQUENCE_N 16 #define OCTAVE_MIN -1 #define OCTAVE_MAX 1 #define PITCH_MAX 12 // SPI2 Morpho // VCC, SCE, RST, D/C, MOSI, SCLK, LED N5110 Lcd(PA_12, PB_1, PB_2, PB_12, PB_15, PB_13, PA_11); RotaryEncoder RotEnc1(D2, D3, 0, SEQUENCE_N - 1, 0); RotaryEncoder RotEnc2(D4, D5, 0, PITCH_MAX, 0); AverageAnalogIn Pots[] = { AverageAnalogIn(A0), AverageAnalogIn(A1), AverageAnalogIn(A2), AverageAnalogIn(A3), AverageAnalogIn(A4), }; PinDetect Pins[] = { PinDetect(PA_13, PullUp), PinDetect(PA_14, PullUp), PinDetect(PA_15, PullUp), PinDetect(PB_7, PullUp), PinDetect(PC_13, PullUp), PinDetect(PB_10, PullUp), PinDetect(PA_8, PullUp), }; DigitalOut Led1(LED1); DigitalOut CheckPin(PC_8); // Grobal Variables struct Sequence { bool noteOn; int octave; int pitch; bool tie; bool accent; } Sequence[SEQUENCE_N]; struct Oscillator { int waveForm; int pulseWidth; } Oscillator; struct Filter { int cutOff; int resonance; int envMod; } Filter; int currentNote = 0; int tempo = 120; bool isRunning = true; bool isDirty = true; void updateLCD() { char buff[20]; //Lcd.clear(); sprintf(buff, "Note#: %d ", currentNote); Lcd.printString(buff, 0, 0); sprintf(buff, "pitch: %d ", Sequence[currentNote].pitch); Lcd.printString(buff, 0, 1); sprintf(buff, "octave: %d " ,Sequence[currentNote].octave); Lcd.printString(buff, 0, 2); sprintf(buff, "%1d %1d %1d %1d %3d", Sequence[currentNote].noteOn, Sequence[currentNote].tie, Sequence[currentNote].accent, isRunning, Oscillator.waveForm); Lcd.printString(buff, 0, 3); sprintf(buff, "%3d %3d %3d", Oscillator.pulseWidth, Filter.envMod, tempo); Lcd.printString(buff, 0, 4); sprintf(buff, "%3d %3d", Filter.cutOff, Filter.resonance); Lcd.printString(buff, 0, 5); Lcd.refresh(); } // CallBack routines void swOctaveUpPressed() { Sequence[currentNote].octave++; isDirty = true; printf("swOctaveUpPressed\r\n"); } void swOctaveDownPressed() { Sequence[currentNote].octave--; isDirty = true; printf("swOctaveDownPressed\r\n"); } void swNoteOnOffPressed() { Sequence[currentNote].noteOn = !Sequence[currentNote].noteOn; isDirty = true; printf("swNoteOnOffPressed\r\n"); } void swTiePressed() { Sequence[currentNote].tie = !Sequence[currentNote].tie; isDirty = true; printf("swTiePressed\r\n"); } void swAccentPressed() { Sequence[currentNote].accent = !Sequence[currentNote].accent; isDirty = true; printf("swAccentPressed\r\n"); } void swRunStopPressed() { isRunning = !isRunning; isDirty = true; printf("swRunStopPressed\r\n"); } void swWaveFormPressed() { Oscillator.waveForm++; isDirty = true; printf("swWaveFormPressed\r\n"); } // Thread void ledThread(void const *argument) { while (true) { Led1 = !Led1; Thread::wait(500); } } void pollingRotEncs(void const *argument) { while (true) { int _note = RotEnc1.getVal(); if (_note != currentNote) { currentNote = _note; isDirty = true; } int _pitch = RotEnc2.getVal(); if (_pitch != Sequence[currentNote].pitch) { Sequence[currentNote].pitch = _pitch; isDirty = true; } Thread::wait(10); } } void pollingPots(void const *argument) { unsigned short tmp; while (true) { // pulse width tmp = Pots[0].read_u16() >> 9; // 7bit witdth if (tmp != Oscillator.pulseWidth) { Oscillator.pulseWidth = tmp; isDirty = true; } // filter envelope moduration tmp = Pots[1].read_u16() >> 9; // 7bit witdth if (tmp != Filter.envMod) { Filter.envMod = tmp; isDirty = true; } // tempo tmp = Pots[2].read_u16() >> 9; // 7bit witdth if (tmp != tempo) { tempo = tmp; isDirty = true; } // cutoff tmp = Pots[3].read_u16() >> 10; // 6bit witdth if (tmp != Filter.cutOff) { Filter.cutOff = tmp; isDirty = true; } // resonance tmp = Pots[4].read_u16() >> 10; // 6bit witdth if (tmp != Filter.resonance) { Filter.resonance = tmp; isDirty = true; } Thread::wait(20); } } int main() { printf("\n\n\r*** RTOS UI Test ***\r\n"); // Init devices RotEnc1.setInterval(500); RotEnc2.setInterval(500); Pins[0].attach_asserted(&swOctaveUpPressed); Pins[1].attach_asserted(&swOctaveDownPressed); Pins[2].attach_asserted(&swNoteOnOffPressed); Pins[3].attach_asserted(&swTiePressed); Pins[4].attach_asserted(&swAccentPressed); Pins[5].attach_asserted(&swRunStopPressed); Pins[6].attach_asserted(&swWaveFormPressed); for (int i = 0; i < 7; i++) { Pins[i].setAssertValue(0); Pins[i].setSampleFrequency(); } Lcd.init(); Lcd.normalMode(); // normal colour mode Lcd.setBrightness(0.5); // put LED backlight on 50% // Thread start Thread thLed(ledThread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); Thread thRotEnc(pollingRotEncs, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); Thread thPots(pollingPots, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); // Main loop while (true) { CheckPin = !CheckPin; if (isDirty) { updateLCD(); isDirty = false; } } }