User Interface Test on mbed RTOS
Dependencies: N5110 RotaryEncoder mbed-rtos mbed PinDetect
main.cpp@3:8c8020dfd82f, 2016-05-25 (annotated)
- Committer:
- ryood
- Date:
- Wed May 25 08:14:42 2016 +0000
- Revision:
- 3:8c8020dfd82f
- Parent:
- 2:8cc6dff1d7fd
- Child:
- 4:d9a72e07749f
?????????????2????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryood | 0:468e49a35876 | 1 | #include "mbed.h" |
ryood | 0:468e49a35876 | 2 | #include "rtos.h" |
ryood | 2:8cc6dff1d7fd | 3 | #include "PinDetect.h" |
ryood | 2:8cc6dff1d7fd | 4 | #include "RotaryEncoder.h" |
ryood | 0:468e49a35876 | 5 | #include "N5110.h" |
ryood | 2:8cc6dff1d7fd | 6 | |
ryood | 2:8cc6dff1d7fd | 7 | #define SEQUENCE_N 16 |
ryood | 2:8cc6dff1d7fd | 8 | #define OCTAVE_MIN -1 |
ryood | 2:8cc6dff1d7fd | 9 | #define OCTAVE_MAX 1 |
ryood | 2:8cc6dff1d7fd | 10 | #define PITCH_MAX 12 |
ryood | 0:468e49a35876 | 11 | |
ryood | 0:468e49a35876 | 12 | // SPI2 Morpho |
ryood | 0:468e49a35876 | 13 | // VCC, SCE, RST, D/C, MOSI, SCLK, LED |
ryood | 0:468e49a35876 | 14 | N5110 Lcd(PA_12, PB_1, PB_2, PB_12, PB_15, PB_13, PA_11); |
ryood | 2:8cc6dff1d7fd | 15 | |
ryood | 2:8cc6dff1d7fd | 16 | RotaryEncoder RotEnc1(D2, D3, 0, SEQUENCE_N - 1, 0); |
ryood | 2:8cc6dff1d7fd | 17 | RotaryEncoder RotEnc2(D4, D5, 0, PITCH_MAX, 0); |
ryood | 2:8cc6dff1d7fd | 18 | |
ryood | 3:8c8020dfd82f | 19 | AnalogIn Pots[] = { |
ryood | 3:8c8020dfd82f | 20 | AnalogIn(A0), |
ryood | 3:8c8020dfd82f | 21 | AnalogIn(A1), |
ryood | 3:8c8020dfd82f | 22 | AnalogIn(A2), |
ryood | 3:8c8020dfd82f | 23 | AnalogIn(A3), |
ryood | 3:8c8020dfd82f | 24 | AnalogIn(A4), |
ryood | 3:8c8020dfd82f | 25 | }; |
ryood | 3:8c8020dfd82f | 26 | |
ryood | 2:8cc6dff1d7fd | 27 | PinDetect Pins[] = { |
ryood | 3:8c8020dfd82f | 28 | PinDetect(PA_13, PullUp), |
ryood | 3:8c8020dfd82f | 29 | PinDetect(PA_14, PullUp), |
ryood | 3:8c8020dfd82f | 30 | PinDetect(PA_15, PullUp), |
ryood | 3:8c8020dfd82f | 31 | PinDetect(PB_7, PullUp), |
ryood | 3:8c8020dfd82f | 32 | PinDetect(PC_13, PullUp), |
ryood | 3:8c8020dfd82f | 33 | PinDetect(PB_10, PullUp), |
ryood | 3:8c8020dfd82f | 34 | PinDetect(PA_8, PullUp), |
ryood | 2:8cc6dff1d7fd | 35 | }; |
ryood | 2:8cc6dff1d7fd | 36 | |
ryood | 0:468e49a35876 | 37 | DigitalOut Led1(LED1); |
ryood | 0:468e49a35876 | 38 | |
ryood | 2:8cc6dff1d7fd | 39 | // Grobal Variables |
ryood | 2:8cc6dff1d7fd | 40 | struct Sequence { |
ryood | 2:8cc6dff1d7fd | 41 | bool noteOn; |
ryood | 2:8cc6dff1d7fd | 42 | int octave; |
ryood | 2:8cc6dff1d7fd | 43 | int pitch; |
ryood | 2:8cc6dff1d7fd | 44 | bool tie; |
ryood | 2:8cc6dff1d7fd | 45 | bool accent; |
ryood | 2:8cc6dff1d7fd | 46 | } Sequence[SEQUENCE_N]; |
ryood | 2:8cc6dff1d7fd | 47 | |
ryood | 3:8c8020dfd82f | 48 | int currentNote = 0; |
ryood | 3:8c8020dfd82f | 49 | int waveForm = 0; |
ryood | 3:8c8020dfd82f | 50 | bool isRunning = true; |
ryood | 3:8c8020dfd82f | 51 | bool isDirty = true; |
ryood | 2:8cc6dff1d7fd | 52 | |
ryood | 2:8cc6dff1d7fd | 53 | void ledThread(void const *argument) |
ryood | 0:468e49a35876 | 54 | { |
ryood | 0:468e49a35876 | 55 | while (true) { |
ryood | 0:468e49a35876 | 56 | Led1 = !Led1; |
ryood | 0:468e49a35876 | 57 | Thread::wait(500); |
ryood | 0:468e49a35876 | 58 | } |
ryood | 0:468e49a35876 | 59 | } |
ryood | 0:468e49a35876 | 60 | |
ryood | 2:8cc6dff1d7fd | 61 | void updateLCD() |
ryood | 0:468e49a35876 | 62 | { |
ryood | 0:468e49a35876 | 63 | char buff[20]; |
ryood | 0:468e49a35876 | 64 | |
ryood | 0:468e49a35876 | 65 | Lcd.clear(); |
ryood | 0:468e49a35876 | 66 | Lcd.printString("RTOS UI Test.", 0, 0); |
ryood | 2:8cc6dff1d7fd | 67 | sprintf(buff, "Note#: %d", currentNote); |
ryood | 0:468e49a35876 | 68 | Lcd.printString(buff, 0, 1); |
ryood | 2:8cc6dff1d7fd | 69 | sprintf(buff, "pitch: %d", Sequence[currentNote].pitch); |
ryood | 2:8cc6dff1d7fd | 70 | Lcd.printString(buff, 0, 2); |
ryood | 2:8cc6dff1d7fd | 71 | sprintf(buff, "octave: %d" ,Sequence[currentNote].octave); |
ryood | 2:8cc6dff1d7fd | 72 | Lcd.printString(buff, 0, 3); |
ryood | 3:8c8020dfd82f | 73 | sprintf(buff, "%d %d %d %d %d", |
ryood | 3:8c8020dfd82f | 74 | Sequence[currentNote].noteOn, Sequence[currentNote].tie, Sequence[currentNote].accent, |
ryood | 3:8c8020dfd82f | 75 | isRunning, waveForm); |
ryood | 2:8cc6dff1d7fd | 76 | Lcd.printString(buff, 0, 4); |
ryood | 0:468e49a35876 | 77 | Lcd.refresh(); |
ryood | 0:468e49a35876 | 78 | } |
ryood | 0:468e49a35876 | 79 | |
ryood | 2:8cc6dff1d7fd | 80 | // CallBack routines |
ryood | 2:8cc6dff1d7fd | 81 | void swOctaveUpPressed() |
ryood | 2:8cc6dff1d7fd | 82 | { |
ryood | 2:8cc6dff1d7fd | 83 | Sequence[currentNote].octave++; |
ryood | 2:8cc6dff1d7fd | 84 | isDirty = true; |
ryood | 2:8cc6dff1d7fd | 85 | printf("swOctaveUpPressed\r\n"); |
ryood | 2:8cc6dff1d7fd | 86 | } |
ryood | 2:8cc6dff1d7fd | 87 | |
ryood | 2:8cc6dff1d7fd | 88 | void swOctaveDownPressed() |
ryood | 2:8cc6dff1d7fd | 89 | { |
ryood | 2:8cc6dff1d7fd | 90 | Sequence[currentNote].octave--; |
ryood | 2:8cc6dff1d7fd | 91 | isDirty = true; |
ryood | 2:8cc6dff1d7fd | 92 | printf("swOctaveDownPressed\r\n"); |
ryood | 2:8cc6dff1d7fd | 93 | } |
ryood | 2:8cc6dff1d7fd | 94 | |
ryood | 2:8cc6dff1d7fd | 95 | void swNoteOnOffPressed() |
ryood | 2:8cc6dff1d7fd | 96 | { |
ryood | 2:8cc6dff1d7fd | 97 | Sequence[currentNote].noteOn = !Sequence[currentNote].noteOn; |
ryood | 2:8cc6dff1d7fd | 98 | isDirty = true; |
ryood | 2:8cc6dff1d7fd | 99 | printf("swNoteOnOffPressed\r\n"); |
ryood | 2:8cc6dff1d7fd | 100 | } |
ryood | 2:8cc6dff1d7fd | 101 | |
ryood | 2:8cc6dff1d7fd | 102 | void swTiePressed() |
ryood | 2:8cc6dff1d7fd | 103 | { |
ryood | 2:8cc6dff1d7fd | 104 | Sequence[currentNote].tie = !Sequence[currentNote].tie; |
ryood | 2:8cc6dff1d7fd | 105 | isDirty = true; |
ryood | 2:8cc6dff1d7fd | 106 | printf("swTiePressed\r\n"); |
ryood | 2:8cc6dff1d7fd | 107 | } |
ryood | 2:8cc6dff1d7fd | 108 | |
ryood | 2:8cc6dff1d7fd | 109 | void swAccentPressed() |
ryood | 2:8cc6dff1d7fd | 110 | { |
ryood | 2:8cc6dff1d7fd | 111 | Sequence[currentNote].accent = !Sequence[currentNote].accent; |
ryood | 2:8cc6dff1d7fd | 112 | isDirty = true; |
ryood | 2:8cc6dff1d7fd | 113 | printf("swAccentPressed\r\n"); |
ryood | 2:8cc6dff1d7fd | 114 | } |
ryood | 2:8cc6dff1d7fd | 115 | |
ryood | 3:8c8020dfd82f | 116 | void swRunStopPressed() |
ryood | 3:8c8020dfd82f | 117 | { |
ryood | 3:8c8020dfd82f | 118 | isRunning = !isRunning; |
ryood | 3:8c8020dfd82f | 119 | isDirty = true; |
ryood | 3:8c8020dfd82f | 120 | printf("swRunStopPressed\r\n"); |
ryood | 3:8c8020dfd82f | 121 | } |
ryood | 3:8c8020dfd82f | 122 | |
ryood | 3:8c8020dfd82f | 123 | void swWaveFormPressed() |
ryood | 3:8c8020dfd82f | 124 | { |
ryood | 3:8c8020dfd82f | 125 | waveForm++; |
ryood | 3:8c8020dfd82f | 126 | isDirty = true; |
ryood | 3:8c8020dfd82f | 127 | printf("swWaveFormPressed\r\n"); |
ryood | 3:8c8020dfd82f | 128 | } |
ryood | 3:8c8020dfd82f | 129 | |
ryood | 3:8c8020dfd82f | 130 | void pollingRotEncs() |
ryood | 3:8c8020dfd82f | 131 | { |
ryood | 3:8c8020dfd82f | 132 | int _note = RotEnc1.getVal(); |
ryood | 3:8c8020dfd82f | 133 | if (_note != currentNote) { |
ryood | 3:8c8020dfd82f | 134 | currentNote = _note; |
ryood | 3:8c8020dfd82f | 135 | isDirty = true; |
ryood | 3:8c8020dfd82f | 136 | } |
ryood | 3:8c8020dfd82f | 137 | int _pitch = RotEnc2.getVal(); |
ryood | 3:8c8020dfd82f | 138 | if (_pitch != Sequence[currentNote].pitch) { |
ryood | 3:8c8020dfd82f | 139 | Sequence[currentNote].pitch = _pitch; |
ryood | 3:8c8020dfd82f | 140 | isDirty = true; |
ryood | 3:8c8020dfd82f | 141 | } |
ryood | 3:8c8020dfd82f | 142 | } |
ryood | 3:8c8020dfd82f | 143 | |
ryood | 3:8c8020dfd82f | 144 | void pollingPots() |
ryood | 3:8c8020dfd82f | 145 | { |
ryood | 3:8c8020dfd82f | 146 | } |
ryood | 3:8c8020dfd82f | 147 | |
ryood | 0:468e49a35876 | 148 | int main() |
ryood | 0:468e49a35876 | 149 | { |
ryood | 0:468e49a35876 | 150 | printf("\n\n\r*** RTOS UI Test ***\r\n"); |
ryood | 0:468e49a35876 | 151 | |
ryood | 0:468e49a35876 | 152 | // Init devices |
ryood | 2:8cc6dff1d7fd | 153 | RotEnc1.setInterval(500); |
ryood | 2:8cc6dff1d7fd | 154 | RotEnc2.setInterval(500); |
ryood | 2:8cc6dff1d7fd | 155 | |
ryood | 2:8cc6dff1d7fd | 156 | Pins[0].attach_asserted(&swOctaveUpPressed); |
ryood | 2:8cc6dff1d7fd | 157 | Pins[1].attach_asserted(&swOctaveDownPressed); |
ryood | 2:8cc6dff1d7fd | 158 | Pins[2].attach_asserted(&swNoteOnOffPressed); |
ryood | 2:8cc6dff1d7fd | 159 | Pins[3].attach_asserted(&swTiePressed); |
ryood | 2:8cc6dff1d7fd | 160 | Pins[4].attach_asserted(&swAccentPressed); |
ryood | 3:8c8020dfd82f | 161 | Pins[5].attach_asserted(&swRunStopPressed); |
ryood | 3:8c8020dfd82f | 162 | Pins[6].attach_asserted(&swWaveFormPressed); |
ryood | 3:8c8020dfd82f | 163 | for (int i = 0; i < 7; i++) { |
ryood | 2:8cc6dff1d7fd | 164 | Pins[i].setAssertValue(0); |
ryood | 2:8cc6dff1d7fd | 165 | Pins[i].setSampleFrequency(); |
ryood | 2:8cc6dff1d7fd | 166 | } |
ryood | 0:468e49a35876 | 167 | |
ryood | 0:468e49a35876 | 168 | Lcd.init(); |
ryood | 0:468e49a35876 | 169 | Lcd.normalMode(); // normal colour mode |
ryood | 0:468e49a35876 | 170 | Lcd.setBrightness(0.5); // put LED backlight on 50% |
ryood | 0:468e49a35876 | 171 | |
ryood | 2:8cc6dff1d7fd | 172 | // Thread start |
ryood | 2:8cc6dff1d7fd | 173 | Thread thread1(ledThread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE); |
ryood | 0:468e49a35876 | 174 | |
ryood | 0:468e49a35876 | 175 | // Main loop |
ryood | 0:468e49a35876 | 176 | while (true) { |
ryood | 3:8c8020dfd82f | 177 | pollingRotEncs(); |
ryood | 3:8c8020dfd82f | 178 | pollingPots(); |
ryood | 0:468e49a35876 | 179 | if (isDirty) { |
ryood | 2:8cc6dff1d7fd | 180 | updateLCD(); |
ryood | 0:468e49a35876 | 181 | isDirty = false; |
ryood | 0:468e49a35876 | 182 | } |
ryood | 0:468e49a35876 | 183 | } |
ryood | 0:468e49a35876 | 184 | } |