Nucleo SPI Sequencer

Dependencies:   AverageAnalogIn N5110 Nucleo_rtos_UI_Test PinDetect RotaryEncoder Sequence mbed-rtos mbed FilterController

Fork of Nucleo_rtos_UI_Test by Ryo Od

Committer:
ryood
Date:
Wed May 25 07:08:12 2016 +0000
Revision:
2:8cc6dff1d7fd
Parent:
0:468e49a35876
Child:
3:8c8020dfd82f
LCD, RotaryEncoder, Pin???Arduino Header????

Who changed what in which revision?

UserRevisionLine numberNew 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 2:8cc6dff1d7fd 19 PinDetect Pins[] = {
ryood 2:8cc6dff1d7fd 20 PinDetect(D8, PullUp),
ryood 2:8cc6dff1d7fd 21 PinDetect(D9, PullUp),
ryood 2:8cc6dff1d7fd 22 PinDetect(D10, PullUp),
ryood 2:8cc6dff1d7fd 23 PinDetect(D11, PullUp),
ryood 2:8cc6dff1d7fd 24 PinDetect(D12, PullUp)
ryood 2:8cc6dff1d7fd 25 };
ryood 2:8cc6dff1d7fd 26
ryood 0:468e49a35876 27 DigitalOut Led1(LED1);
ryood 0:468e49a35876 28
ryood 2:8cc6dff1d7fd 29 // Grobal Variables
ryood 2:8cc6dff1d7fd 30 struct Sequence {
ryood 2:8cc6dff1d7fd 31 bool noteOn;
ryood 2:8cc6dff1d7fd 32 int octave;
ryood 2:8cc6dff1d7fd 33 int pitch;
ryood 2:8cc6dff1d7fd 34 bool tie;
ryood 2:8cc6dff1d7fd 35 bool accent;
ryood 2:8cc6dff1d7fd 36 } Sequence[SEQUENCE_N];
ryood 2:8cc6dff1d7fd 37
ryood 2:8cc6dff1d7fd 38 int currentNote;
ryood 2:8cc6dff1d7fd 39 bool isDirty;
ryood 2:8cc6dff1d7fd 40
ryood 2:8cc6dff1d7fd 41 void ledThread(void const *argument)
ryood 0:468e49a35876 42 {
ryood 0:468e49a35876 43 while (true) {
ryood 0:468e49a35876 44 Led1 = !Led1;
ryood 0:468e49a35876 45 Thread::wait(500);
ryood 0:468e49a35876 46 }
ryood 0:468e49a35876 47 }
ryood 0:468e49a35876 48
ryood 2:8cc6dff1d7fd 49 void updateLCD()
ryood 0:468e49a35876 50 {
ryood 0:468e49a35876 51 char buff[20];
ryood 0:468e49a35876 52
ryood 0:468e49a35876 53 Lcd.clear();
ryood 0:468e49a35876 54 Lcd.printString("RTOS UI Test.", 0, 0);
ryood 2:8cc6dff1d7fd 55 sprintf(buff, "Note#: %d", currentNote);
ryood 0:468e49a35876 56 Lcd.printString(buff, 0, 1);
ryood 2:8cc6dff1d7fd 57 sprintf(buff, "pitch: %d", Sequence[currentNote].pitch);
ryood 2:8cc6dff1d7fd 58 Lcd.printString(buff, 0, 2);
ryood 2:8cc6dff1d7fd 59 sprintf(buff, "octave: %d" ,Sequence[currentNote].octave);
ryood 2:8cc6dff1d7fd 60 Lcd.printString(buff, 0, 3);
ryood 2:8cc6dff1d7fd 61 sprintf(buff, "%d %d %d",
ryood 2:8cc6dff1d7fd 62 Sequence[currentNote].noteOn, Sequence[currentNote].tie, Sequence[currentNote].accent);
ryood 2:8cc6dff1d7fd 63 Lcd.printString(buff, 0, 4);
ryood 0:468e49a35876 64 Lcd.refresh();
ryood 0:468e49a35876 65 }
ryood 0:468e49a35876 66
ryood 2:8cc6dff1d7fd 67 // CallBack routines
ryood 2:8cc6dff1d7fd 68 void swOctaveUpPressed()
ryood 2:8cc6dff1d7fd 69 {
ryood 2:8cc6dff1d7fd 70 Sequence[currentNote].octave++;
ryood 2:8cc6dff1d7fd 71 isDirty = true;
ryood 2:8cc6dff1d7fd 72 printf("swOctaveUpPressed\r\n");
ryood 2:8cc6dff1d7fd 73 }
ryood 2:8cc6dff1d7fd 74
ryood 2:8cc6dff1d7fd 75 void swOctaveDownPressed()
ryood 2:8cc6dff1d7fd 76 {
ryood 2:8cc6dff1d7fd 77 Sequence[currentNote].octave--;
ryood 2:8cc6dff1d7fd 78 isDirty = true;
ryood 2:8cc6dff1d7fd 79 printf("swOctaveDownPressed\r\n");
ryood 2:8cc6dff1d7fd 80 }
ryood 2:8cc6dff1d7fd 81
ryood 2:8cc6dff1d7fd 82 void swNoteOnOffPressed()
ryood 2:8cc6dff1d7fd 83 {
ryood 2:8cc6dff1d7fd 84 Sequence[currentNote].noteOn = !Sequence[currentNote].noteOn;
ryood 2:8cc6dff1d7fd 85 isDirty = true;
ryood 2:8cc6dff1d7fd 86 printf("swNoteOnOffPressed\r\n");
ryood 2:8cc6dff1d7fd 87 }
ryood 2:8cc6dff1d7fd 88
ryood 2:8cc6dff1d7fd 89 void swTiePressed()
ryood 2:8cc6dff1d7fd 90 {
ryood 2:8cc6dff1d7fd 91 Sequence[currentNote].tie = !Sequence[currentNote].tie;
ryood 2:8cc6dff1d7fd 92 isDirty = true;
ryood 2:8cc6dff1d7fd 93 printf("swTiePressed\r\n");
ryood 2:8cc6dff1d7fd 94 }
ryood 2:8cc6dff1d7fd 95
ryood 2:8cc6dff1d7fd 96 void swAccentPressed()
ryood 2:8cc6dff1d7fd 97 {
ryood 2:8cc6dff1d7fd 98 Sequence[currentNote].accent = !Sequence[currentNote].accent;
ryood 2:8cc6dff1d7fd 99 isDirty = true;
ryood 2:8cc6dff1d7fd 100 printf("swAccentPressed\r\n");
ryood 2:8cc6dff1d7fd 101 }
ryood 2:8cc6dff1d7fd 102
ryood 0:468e49a35876 103 int main()
ryood 0:468e49a35876 104 {
ryood 0:468e49a35876 105 printf("\n\n\r*** RTOS UI Test ***\r\n");
ryood 0:468e49a35876 106
ryood 0:468e49a35876 107 // Init devices
ryood 2:8cc6dff1d7fd 108 RotEnc1.setInterval(500);
ryood 2:8cc6dff1d7fd 109 RotEnc2.setInterval(500);
ryood 2:8cc6dff1d7fd 110
ryood 2:8cc6dff1d7fd 111 Pins[0].attach_asserted(&swOctaveUpPressed);
ryood 2:8cc6dff1d7fd 112 Pins[1].attach_asserted(&swOctaveDownPressed);
ryood 2:8cc6dff1d7fd 113 Pins[2].attach_asserted(&swNoteOnOffPressed);
ryood 2:8cc6dff1d7fd 114 Pins[3].attach_asserted(&swTiePressed);
ryood 2:8cc6dff1d7fd 115 Pins[4].attach_asserted(&swAccentPressed);
ryood 2:8cc6dff1d7fd 116 for (int i = 0; i < 5; i++) {
ryood 2:8cc6dff1d7fd 117 Pins[i].setAssertValue(0);
ryood 2:8cc6dff1d7fd 118 Pins[i].setSampleFrequency();
ryood 2:8cc6dff1d7fd 119 }
ryood 0:468e49a35876 120
ryood 0:468e49a35876 121 Lcd.init();
ryood 0:468e49a35876 122 Lcd.normalMode(); // normal colour mode
ryood 0:468e49a35876 123 Lcd.setBrightness(0.5); // put LED backlight on 50%
ryood 0:468e49a35876 124
ryood 2:8cc6dff1d7fd 125 // Thread start
ryood 2:8cc6dff1d7fd 126 Thread thread1(ledThread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
ryood 0:468e49a35876 127
ryood 0:468e49a35876 128 // Main loop
ryood 2:8cc6dff1d7fd 129 isDirty = true;
ryood 0:468e49a35876 130 while (true) {
ryood 2:8cc6dff1d7fd 131 int note = RotEnc1.getVal();
ryood 2:8cc6dff1d7fd 132 if (note != currentNote) {
ryood 2:8cc6dff1d7fd 133 currentNote = note;
ryood 2:8cc6dff1d7fd 134 isDirty = true;
ryood 2:8cc6dff1d7fd 135 }
ryood 2:8cc6dff1d7fd 136 int pitch = RotEnc2.getVal();
ryood 2:8cc6dff1d7fd 137 if (pitch != Sequence[currentNote].pitch) {
ryood 2:8cc6dff1d7fd 138 Sequence[currentNote].pitch = pitch;
ryood 0:468e49a35876 139 isDirty = true;
ryood 0:468e49a35876 140 }
ryood 0:468e49a35876 141 if (isDirty) {
ryood 2:8cc6dff1d7fd 142 updateLCD();
ryood 0:468e49a35876 143 isDirty = false;
ryood 0:468e49a35876 144 }
ryood 0:468e49a35876 145 }
ryood 0:468e49a35876 146 }