예제 #1
0
int main(int argc, char* argv[]) {
      
    uint32_t seconds = 0;
    char udpPacketIn[256];
    //uint8_t osc_packet_in[256];
    uint8_t i = 0;
    int len = 0;
    int page = 0;


    Timer screenFpsTimer, screenLineTimer, knobPollTimer, pingTimer, upTime;

    screenFpsTimer.reset();
    knobPollTimer.reset();
    screenLineTimer.reset();
    pingTimer.reset();
    upTime.reset();

    // set locale so sorting happens in right order
    //std::setlocale(LC_ALL, "en_US.UTF-8");

    // for setting real time scheduling
    /*struct sched_param par;

    par.sched_priority = 10;
    printf("settin priority to: %d\n", 10);
    if (sched_setscheduler(0,SCHED_FIFO,&par) < 0){
        printf("failed to set rt scheduling\n");
    }*/

    udpSock.setDestination(4000, "localhost");
    OSCMessage msgIn;

    ui.buildMenu();
    ui.drawPatchList();

    // send ready to wake up MCU
    // MCU is ignoring stuff over serial port until this message comes through
    // don't empty the message because it gets sent out periodically incase MCU resets
    OSCMessage rdyMsg("/ready");
    rdyMsg.add(1);
    rdyMsg.send(dump);
    // send it a few times just in case
    for(i = 0; i<4; i++) {
       slip.sendMessage(dump.buffer, dump.length, serial);
       usleep(20000); // wait 20 ms
    }
    
    //playFirst();
    quit = 0;

    // full udp -> serial -> serial -> udp
    for (;;){
        // receive udp, send to serial
        len = udpSock.readBuffer(udpPacketIn, 256, 0);
        if (len > 0){
            msgIn.empty();
            for (i = 0; i < len; i++){
                msgIn.fill(udpPacketIn[i]);
            }    
            if(!msgIn.hasError()){
                msgIn.dispatch("/oled/vumeter", vuMeter, 0);
                msgIn.dispatch("/oled/line/1", setPatchScreenLine1, 0);
                msgIn.dispatch("/oled/line/2", setPatchScreenLine2, 0);
                msgIn.dispatch("/oled/line/3", setPatchScreenLine3, 0);
                msgIn.dispatch("/oled/line/4", setPatchScreenLine4, 0);
                msgIn.dispatch("/oled/line/5", setPatchScreenLine5, 0);
                msgIn.dispatch("/oled/aux/line/1", setAuxScreenLine1, 0);
                msgIn.dispatch("/oled/aux/line/2", setAuxScreenLine2, 0);
                msgIn.dispatch("/oled/aux/line/3", setAuxScreenLine3, 0);
                msgIn.dispatch("/oled/aux/line/4", setAuxScreenLine4, 0);
                msgIn.dispatch("/oled/aux/line/5", setAuxScreenLine5, 0);
                msgIn.dispatch("/oled/aux/clear", auxScreenClear, 0);
                
                msgIn.dispatch("/ready", sendReady, 0);
                msgIn.dispatch("/shutdown", sendShutdown, 0);
                msgIn.dispatch("/led", setLED, 0);
                msgIn.dispatch("/oled/setscreen", setScreen, 0);
                msgIn.dispatch("/reload", reload, 0);
                msgIn.dispatch("/quitmother", quitMother, 0);
                msgIn.dispatch("/screenshot", screenShot, 0);
                msgIn.dispatch("/pgmchg", programChange, 0);
                msgIn.dispatch("/getPatchName", getPatchName, 0);
            }
            else {
                printf("bad message\n");
            }
            msgIn.empty();
        }   

        // receive serial, send udp
        if(slip.recvMessage(serial)) {
            udpSock.writeBuffer(slip.decodedBuf, slip.decodedLength);
            
            // check if we need to do something with this message
            msgIn.empty();
            msgIn.fill(slip.decodedBuf, slip.decodedLength);
            msgIn.dispatch("/enc", encoderInput, 0);
            msgIn.dispatch("/encbut", encoderButton, 0);
            msgIn.empty();
        }

        // sleep for .5ms
        usleep(750);
        
        if (ui.currentScreen == AUX) {
             // we can do a whole screen,  but not faster than 20fps
            if (screenFpsTimer.getElapsed() > 50.f){
                screenFpsTimer.reset();
                if (ui.newScreen){
                    ui.newScreen = 0;
                    updateScreenPage(0, ui.auxScreen);//menuScreen);
                    updateScreenPage(1, ui.auxScreen);
                    updateScreenPage(2, ui.auxScreen);
                    updateScreenPage(3, ui.auxScreen);
                    updateScreenPage(4, ui.auxScreen);
                    updateScreenPage(5, ui.auxScreen);
                    updateScreenPage(6, ui.auxScreen);
                    updateScreenPage(7, ui.auxScreen);
                }
            }
        }
        else if (ui.currentScreen == MENU) {
             // we can do a whole screen,  but not faster than 20fps
            if (screenFpsTimer.getElapsed() > 50.f){
                screenFpsTimer.reset();
                if (ui.newScreen){
                    ui.newScreen = 0;
                    updateScreenPage(0, ui.menuScreen);//menuScreen);
                    updateScreenPage(1, ui.menuScreen);
                    updateScreenPage(2, ui.menuScreen);
                    updateScreenPage(3, ui.menuScreen);
                    updateScreenPage(4, ui.menuScreen);
                    updateScreenPage(5, ui.menuScreen);
                    updateScreenPage(6, ui.menuScreen);
                    updateScreenPage(7, ui.menuScreen);
                }
                // if there is a patch running while on menu screen, switch back to patch screen after the timeout 
                if (ui.menuScreenTimeout > 0) ui.menuScreenTimeout -= 50;
                else ui.currentScreen = PATCH;
            }
        }
        else if (ui.currentScreen == PATCH) {
            if (ui.patchIsRunning) {
                // every 16 ms send a new screen page
                if (screenLineTimer.getElapsed() > 15.f){
                    screenLineTimer.reset();
                    updateScreenPage(page, ui.patchScreen);
                    page++;
                    page %= 8;
                }
            }
        }
       
        // every 1 second send a ping in case MCU resets
        if (pingTimer.getElapsed() > 1000.f){
          //  printf("pinged the MCU at %f ms.\n", upTime.getElapsed());
            pingTimer.reset();
            rdyMsg.send(dump);
            slip.sendMessage(dump.buffer, dump.length, serial);
        }

        // poll for knobs
        if (knobPollTimer.getElapsed() > 40.f){
            knobPollTimer.reset();
            sendGetKnobs();
        }
        
        // check exit flag
        if (quit) {
            printf("quitting\n");
            return 0;
        }
    } // for;;
}
예제 #2
0
int main(int argc, char* argv[]) {
      
    uint32_t seconds = 0;
    char udpPacketIn[256];
    //uint8_t osc_packet_in[256];
    uint8_t i = 0;
    int len = 0;


    UdpSocket udpSock(4001);
    udpSock.setDestination(4000, "localhost");
 
    Serial serial;
    SLIPEncodedSerial slip;

    SimpleWriter dump;

    OSCMessage msgIn;
    
    printf("cool\n");

    // make an osc object
    // the message wants an OSC address as first argument
    OSCMessage msg("/sys/renumber");
    // msg.add(888);
       
    // this just dumps it into the simple writer dump
    msg.send(dump);

    printf("\n msg is %d bytes\n", dump.length);

    // print it out
    for (i = 0; i < dump.length; i++){
        printf("%x ", dump.buffer[i]);
    }

    // UDP send
    udpSock.writeBuffer(dump.buffer, dump.length);

    // serial send
    slip.sendMessage(dump.buffer, dump.length, serial);

    printf("\ndone, now gonna receive\n");


    // full udp -> serial -> serial -> udp
    for (;;){
        // receive udp, send to serial
        len = udpSock.readBuffer(udpPacketIn, 256, 0);
        if (len > 0){
            for (i = 0; i < len; i++){
                msgIn.fill(udpPacketIn[i]);
            }
            if(!msgIn.hasError()){
                msgIn.dispatch("/thing/test", gotone, 0);
                // send it along
                msgIn.send(dump);
                slip.sendMessage(dump.buffer, dump.length, serial);
            }
            else {
                printf("bad message");
            }
            msgIn.empty();
        }   

        // receive serial, send udp
        if(slip.recvMessage(serial)) {
            udpSock.writeBuffer(slip.decodedBuf, slip.decodedLength);
        }

        // sleep for 1ms
        usleep(1000);
    } // for;;
}