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

main.cpp

Committer:
ryood
Date:
2016-05-25
Revision:
2:8cc6dff1d7fd
Parent:
0:468e49a35876
Child:
3:8c8020dfd82f

File content as of revision 2:8cc6dff1d7fd:

#include "mbed.h"
#include "rtos.h"
#include "PinDetect.h"
#include "RotaryEncoder.h"
#include "N5110.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);

PinDetect Pins[] = {
    PinDetect(D8,  PullUp),
    PinDetect(D9,  PullUp),
    PinDetect(D10, PullUp),
    PinDetect(D11, PullUp),
    PinDetect(D12, PullUp)
};

DigitalOut Led1(LED1);

// Grobal Variables
struct Sequence {
    bool noteOn;
    int octave;
    int pitch;
    bool tie;
    bool accent;
} Sequence[SEQUENCE_N];

int currentNote;
bool isDirty;

void ledThread(void const *argument)
{
    while (true) {
        Led1 = !Led1;
        Thread::wait(500);
    }
}

void updateLCD()
{
    char buff[20];
    
    Lcd.clear();
    Lcd.printString("RTOS UI Test.", 0, 0);
    sprintf(buff, "Note#: %d", currentNote);
    Lcd.printString(buff, 0, 1);
    sprintf(buff, "pitch: %d", Sequence[currentNote].pitch);
    Lcd.printString(buff, 0, 2);
    sprintf(buff, "octave: %d" ,Sequence[currentNote].octave);
    Lcd.printString(buff, 0, 3);
    sprintf(buff, "%d %d %d", 
        Sequence[currentNote].noteOn, Sequence[currentNote].tie, Sequence[currentNote].accent);
    Lcd.printString(buff, 0, 4);
    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");
}

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);
    for (int i = 0; i < 5; 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 thread1(ledThread, NULL, osPriorityNormal, DEFAULT_STACK_SIZE);
    
    // Main loop
    isDirty = true;
    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;
        }
        if (isDirty) {
            updateLCD();
            isDirty = false;
        }
    }
}