User Interface Test on mbed RTOS
Dependencies: N5110 RotaryEncoder mbed-rtos mbed PinDetect
main.cpp
00001 /* 00002 * Nucleo RTOS Sequencer User Interface Test. 00003 * 00004 * 2016.05.27 00005 * 00006 */ 00007 00008 #include "mbed.h" 00009 #include "rtos.h" 00010 #include "PinDetect.h" 00011 #include "RotaryEncoder.h" 00012 #include "N5110.h" 00013 #include "AverageAnalogIn.h" 00014 00015 #define SEQUENCE_N 16 00016 #define OCTAVE_MIN -1 00017 #define OCTAVE_MAX 1 00018 #define PITCH_MAX 12 00019 00020 // SPI2 Morpho 00021 // VCC, SCE, RST, D/C, MOSI, SCLK, LED 00022 N5110 Lcd(PA_12, PB_1, PB_2, PB_12, PB_15, PB_13, PA_11); 00023 00024 RotaryEncoder RotEnc1(D2, D3, 0, SEQUENCE_N - 1, 0); 00025 RotaryEncoder RotEnc2(D4, D5, 0, PITCH_MAX, 0); 00026 00027 AverageAnalogIn Pots[] = { 00028 AverageAnalogIn(A0), 00029 AverageAnalogIn(A1), 00030 AverageAnalogIn(A2), 00031 AverageAnalogIn(A3), 00032 AverageAnalogIn(A4), 00033 }; 00034 00035 PinDetect Pins[] = { 00036 PinDetect(PA_13, PullUp), 00037 PinDetect(PA_14, PullUp), 00038 PinDetect(PA_15, PullUp), 00039 PinDetect(PB_7, PullUp), 00040 PinDetect(PC_13, PullUp), 00041 PinDetect(PB_10, PullUp), 00042 PinDetect(PA_8, PullUp), 00043 }; 00044 00045 DigitalOut Led1(LED1); 00046 DigitalOut CheckPin(PC_8); 00047 00048 // Grobal Variables 00049 struct Sequence { 00050 bool noteOn; 00051 int octave; 00052 int pitch; 00053 bool tie; 00054 bool accent; 00055 } Sequence[SEQUENCE_N]; 00056 00057 struct Oscillator { 00058 int waveForm; 00059 int pulseWidth; 00060 } Oscillator; 00061 00062 struct Filter { 00063 int cutOff; 00064 int resonance; 00065 int envMod; 00066 } Filter; 00067 00068 int currentNote = 0; 00069 int tempo = 120; 00070 bool isRunning = true; 00071 bool isDirty = true; 00072 00073 void updateLCD() 00074 { 00075 char buff[20]; 00076 00077 //Lcd.clear(); 00078 sprintf(buff, "Note#: %d ", currentNote); 00079 Lcd.printString(buff, 0, 0); 00080 sprintf(buff, "pitch: %d ", Sequence[currentNote].pitch); 00081 Lcd.printString(buff, 0, 1); 00082 sprintf(buff, "octave: %d " ,Sequence[currentNote].octave); 00083 Lcd.printString(buff, 0, 2); 00084 sprintf(buff, "%1d %1d %1d %1d %3d", 00085 Sequence[currentNote].noteOn, Sequence[currentNote].tie, Sequence[currentNote].accent, 00086 isRunning, Oscillator.waveForm); 00087 Lcd.printString(buff, 0, 3); 00088 sprintf(buff, "%3d %3d %3d", Oscillator.pulseWidth, Filter.envMod, tempo); 00089 Lcd.printString(buff, 0, 4); 00090 sprintf(buff, "%3d %3d", Filter.cutOff, Filter.resonance); 00091 Lcd.printString(buff, 0, 5); 00092 Lcd.refresh(); 00093 } 00094 00095 // CallBack routines 00096 void swOctaveUpPressed() 00097 { 00098 Sequence[currentNote].octave++; 00099 isDirty = true; 00100 printf("swOctaveUpPressed\r\n"); 00101 } 00102 00103 void swOctaveDownPressed() 00104 { 00105 Sequence[currentNote].octave--; 00106 isDirty = true; 00107 printf("swOctaveDownPressed\r\n"); 00108 } 00109 00110 void swNoteOnOffPressed() 00111 { 00112 Sequence[currentNote].noteOn = !Sequence[currentNote].noteOn; 00113 isDirty = true; 00114 printf("swNoteOnOffPressed\r\n"); 00115 } 00116 00117 void swTiePressed() 00118 { 00119 Sequence[currentNote].tie = !Sequence[currentNote].tie; 00120 isDirty = true; 00121 printf("swTiePressed\r\n"); 00122 } 00123 00124 void swAccentPressed() 00125 { 00126 Sequence[currentNote].accent = !Sequence[currentNote].accent; 00127 isDirty = true; 00128 printf("swAccentPressed\r\n"); 00129 } 00130 00131 void swRunStopPressed() 00132 { 00133 isRunning = !isRunning; 00134 isDirty = true; 00135 printf("swRunStopPressed\r\n"); 00136 } 00137 00138 void swWaveFormPressed() 00139 { 00140 Oscillator.waveForm++; 00141 isDirty = true; 00142 printf("swWaveFormPressed\r\n"); 00143 } 00144 00145 // Thread 00146 void ledThread(void const *argument) 00147 { 00148 while (true) { 00149 Led1 = !Led1; 00150 Thread::wait(500); 00151 } 00152 } 00153 00154 void pollingRotEncs(void const *argument) 00155 { 00156 while (true) { 00157 int _note = RotEnc1.getVal(); 00158 if (_note != currentNote) { 00159 currentNote = _note; 00160 isDirty = true; 00161 } 00162 int _pitch = RotEnc2.getVal(); 00163 if (_pitch != Sequence[currentNote].pitch) { 00164 Sequence[currentNote].pitch = _pitch; 00165 isDirty = true; 00166 } 00167 Thread::wait(10); 00168 } 00169 } 00170 00171 void pollingPots(void const *argument) 00172 { 00173 unsigned short tmp; 00174 00175 while (true) { 00176 // pulse width 00177 tmp = Pots[0].read_u16() >> 9; // 7bit witdth 00178 if (tmp != Oscillator.pulseWidth) { 00179 Oscillator.pulseWidth = tmp; 00180 isDirty = true; 00181 } 00182 // filter envelope moduration 00183 tmp = Pots[1].read_u16() >> 9; // 7bit witdth 00184 if (tmp != Filter.envMod) { 00185 Filter.envMod = tmp; 00186 isDirty = true; 00187 } 00188 // tempo 00189 tmp = Pots[2].read_u16() >> 9; // 7bit witdth 00190 if (tmp != tempo) { 00191 tempo = tmp; 00192 isDirty = true; 00193 } 00194 // cutoff 00195 tmp = Pots[3].read_u16() >> 10; // 6bit witdth 00196 if (tmp != Filter.cutOff) { 00197 Filter.cutOff = tmp; 00198 isDirty = true; 00199 } 00200 // resonance 00201 tmp = Pots[4].read_u16() >> 10; // 6bit witdth 00202 if (tmp != Filter.resonance) { 00203 Filter.resonance = tmp; 00204 isDirty = true; 00205 } 00206 Thread::wait(20); 00207 } 00208 } 00209 00210 int main() 00211 { 00212 printf("\n\n\r*** RTOS UI Test ***\r\n"); 00213 00214 // Init devices 00215 RotEnc1.setInterval(500); 00216 RotEnc2.setInterval(500); 00217 00218 Pins[0].attach_asserted(&swOctaveUpPressed); 00219 Pins[1].attach_asserted(&swOctaveDownPressed); 00220 Pins[2].attach_asserted(&swNoteOnOffPressed); 00221 Pins[3].attach_asserted(&swTiePressed); 00222 Pins[4].attach_asserted(&swAccentPressed); 00223 Pins[5].attach_asserted(&swRunStopPressed); 00224 Pins[6].attach_asserted(&swWaveFormPressed); 00225 for (int i = 0; i < 7; i++) { 00226 Pins[i].setAssertValue(0); 00227 Pins[i].setSampleFrequency(); 00228 } 00229 00230 Lcd.init(); 00231 Lcd.normalMode(); // normal colour mode 00232 Lcd.setBrightness(0.5); // put LED backlight on 50% 00233 00234 // Thread start 00235 Thread thLed(ledThread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); 00236 Thread thRotEnc(pollingRotEncs, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); 00237 Thread thPots(pollingPots, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); 00238 00239 // Main loop 00240 while (true) { 00241 CheckPin = !CheckPin; 00242 if (isDirty) { 00243 updateLCD(); 00244 isDirty = false; 00245 } 00246 } 00247 }
Generated on Mon Jul 18 2022 20:09:13 by 1.7.2