/*
 * File:   DACmain_withInterrupts_v3
 * Author: Zach Stauder
 *
 * Created on Mary 2, 2013, 7:10 PM
 */
#include <stdio.h>
#include <stdlib.h>
#include<xc.h>
#include<math.h>
#include<i2c.h>
#include "freqValues.h"
/*
 *
 */
//
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF  & POSCMD_NONE);
_FOSCSEL(FNOSC_FRC);
//#define PORTCbits.RC8 = 1      SHUTDOWN_OFF
//#define PORTCbits.RC8 = 1       SHUTDOWN_ON


//long long analyzeInputs(long long inputs) {
//
//    if((inputs&0x03)==0  {  // standard mode (bits 0-1 for mode)
//
//        long long outFreqs = (0x0000000007FFFFFC | inputs);  // bits 2-26 for piano keys
//        outFreqs = outFreqs|
//    }
//}

//void initRamps(){
//
//}

unsigned char getI2CByte(){
     unsigned char data;
     while(!I2C1STATbits.RBF);
     if(I2C1STATbits.D_A==0) // Wait until last byte was address
        data = I2C1RCV;
     return data;
}

void initiateI2C(){
    I2C1BRG = 156;  //132 // I21BRG = (1/I2C_CLK - 130ns)*SYS_CLK-2
    // made clock 400 kHz  // 156 makes it 100 kHz    // made clock 400 kHz
    I2C1CONbits.I2CSIDL = 0; // operate in idle mode
    I2C1CONbits.IPMIEN = 0; // ?????
    I2C1CONbits.A10M = 0; // 7 bit slave address
    I2C1ADD = 0b1010101; // set 7 bit slave address
    I2C1CONbits.I2CEN = 1; // enable I2C module
}


void initiateInterrupts(void){
    INTCON1bits.NSTDIS = 1; //Nested interrupts are not desired
    IEC1bits.SI2C1IE = 1; //Enable Slave Interrupt for I2C
    IFS1bits.SI2C1IF = 0; //Clear Slave Interrupt Flag
}

unsigned char address_rec = 0;
unsigned char buttonStates1 = 0;
unsigned char buttonStates2 = 0;
unsigned char byteCount = 0;
unsigned int freqs[36];
unsigned int eff[4];
unsigned char selection = 0;

unsigned char recByte1 = 0;
unsigned char recByte2 = 0;
unsigned char recByte3 = 0;
unsigned char recByte4 = 0;
unsigned char recByte5 = 0;
unsigned int numFreqs = 0;
unsigned int multFactor = 1;
unsigned int modulation = 0;

void __attribute__((__interrupt__, no_auto_psv)) _SI2C1Interrupt(void)
{
    IFS1bits.SI2C1IF = 0; // Clear Interrupt Flag
    if(I2C1STATbits.D_A==0){ //If last byte received was an address
        address_rec = I2C1RCV; //Unused variable to clear receive buffer
        byteCount = 0;
    }
    else if(I2C1STATbits.RBF==1){ //if receive buffer is full
        if(byteCount==0){
            recByte1 = I2C1RCV; //Set data byte
            byteCount = byteCount+1;
        }
        else if(byteCount==1){
            recByte2 = I2C1RCV; //Set data byte
            byteCount = byteCount+1;
        }
        else if(byteCount==2){
            recByte3 = I2C1RCV; //Set data byte
            byteCount = byteCount+1;
        }
        else if(byteCount==3){
            recByte4 = I2C1RCV; //Set data byte
            byteCount = byteCount+1;
        }
        else{
            recByte5 = I2C1RCV;
            byteCount = 0;
        }

            freqs[0] = (recByte1&128)==128;
            freqs[1] = (recByte1&64)==64;
            freqs[2] = (recByte1&32)==32;
            freqs[3] = (recByte1&16)==16;
            freqs[4] = (recByte1&8)==8;
            freqs[5] = (recByte1&4)==4;
            freqs[6] = (recByte1&2)==2;
            freqs[7] = recByte1&1;

            freqs[8] = (recByte2&128)==128;
            freqs[9] = (recByte2&64)==64;
            freqs[10] = (recByte2&32)==32;
            freqs[11] = (recByte2&16)==16;
            freqs[12] = (recByte2&8)==8;
            freqs[13] = (recByte2&4)==4;
            freqs[14] = (recByte2&2)==2;
            freqs[15] = recByte2&1;

            freqs[16] = (recByte3&128)==128;
            freqs[17] = (recByte3&64)==64;
            freqs[18] = (recByte3&32)==32;
            freqs[19] = (recByte3&16)==16;
            freqs[20] = (recByte3&8)==8;
            freqs[21] = (recByte3&4)==4;
            freqs[22] = (recByte3&2)==2;
            freqs[23] = recByte3&1;

            freqs[24] = (recByte4&128)==128;
            freqs[25] = (recByte4&64)==64;
            freqs[26] = (recByte4&32)==32;
            freqs[27] = (recByte4&16)==16;
            freqs[28] = (recByte4&8)==8;
            freqs[29] = (recByte4&4)==4;
            freqs[30] = (recByte4&2)==2;
            freqs[31] = recByte4&1;

            freqs[32] = (recByte5&128)==128;
            freqs[33] = (recByte5&64)==64;
            freqs[34] = (recByte5&32)==32;
            freqs[35] = (recByte5&16)==16;
            eff[0] = (recByte5&8)==8;
            eff[1] = (recByte5&4)==4;
            eff[2] = (recByte5&2)==2;
            eff[3] = recByte5&1;

            numFreqs = 0;
            int i = 0;
            for(i=0;i<36;i++){
                numFreqs = numFreqs+freqs[i];
            }

            if(eff[0]==1){
                modulation=0;
            }
            else if(eff[1]==1){
                modulation=1;
            }
            else if(eff[2]==1){
                modulation=3;
            }
            else{
                modulation=0;
            }

            switch (modulation){
                    case 0:
                    if(numFreqs==1){
                        multFactor=8;
                    }
                    else if(numFreqs==2){
                        multFactor=5;
                    }
                    else if(numFreqs==3){
                        multFactor=4;
                    }
                    else if(numFreqs==4){
                        multFactor=3;
                    }
                    else if(numFreqs==5){
                        multFactor=3;
                    }
                    else if(numFreqs==6){
                        multFactor=2;
                    }
                    else if(numFreqs==7){
                        multFactor=2;
                    }
                    else{
                        multFactor=1;
                    }
                        break;
                    default:
                    if(numFreqs==1){
                        multFactor=4;
                    }
                    else if(numFreqs==2){
                        multFactor=3;
                    }
                    else if(numFreqs==3){
                        multFactor=2;
                    }
                    else if(numFreqs==4){
                        multFactor=2;
                    }
                    else{
                        multFactor=1;
                    }
                    break;
                }
            }




    }

int main(int argc, char** argv) {

//set auxillary clock

    OSCCONbits.COSC = 0b001;    // set clock to fast RC oscillator 7.37 MHz, divide by n, PLL
    OSCCONbits.NOSC = 0b001;    // new oscillator selection bits FRC oscillator, divide n, PLL (pg 147)
    PLLFBDbits.PLLDIV = 59;     // clock multiplier n+2
    CLKDIVbits.FRCDIV = 0;      // clock divider = 1
    CLKDIVbits.PLLPOST = 0;     // divide clock by two (2^(n+1))
    CLKDIVbits.PLLPRE = 2;      // divide clock by four (n+2)
    //Fosc = 7.37MHz*(59+2)/(4*2*1) = 56.196MHz
    // need 256*44.1kHz*5 = 56448000
    OSCTUNbits.TUN = 1;         // tune clock up .375 percent
    //Fosc = 7.37*1.00375*(59+2)/(4*2*1) = 56.4 MHz
    // Clock switch to incorporate PLL
	__builtin_write_OSCCONH(0x01);		// Initiate Clock Switch to
													// FRC with PLL (NOSC=0b001)
	__builtin_write_OSCCONL(0x01);		// Start clock switching
	while (OSCCONbits.COSC != 0b001);	// Wait for Clock switch to occur

    ACLKCONbits.AOSCMD = 0;     // auxillary clock disabled
    ACLKCONbits.SELACLK = 0;    // auxillary clock selects FOSC as source
    ACLKCONbits.APSTSCLR = 7;   // divide by 1

    DAC1CONbits.DACSIDL = 1;        // stop DAC module in idle mode
    DAC1CONbits.AMPON = 0;          // disable analog ouput amplifier in sleep/idle mode
    DAC1CONbits.FORM = 1;           // signed ints, midpoint at 0x8000
    DAC1CONbits.DACEN = 1;          // enable DAC
    DAC1CONbits.DACFDIV = 9;        // divide auxillary clock by 10 for DAC clock
                                    // The clock needs to be 256 times the sampling rate
                                    // making the clock 11.289 MHz
    // 56.4 Mhz/10/256*2 = 44100 Hz
    DAC1STATbits.LOEN = 0;          // left channel output DAC disabled
    DAC1STATbits.LMVOEN = 0;        // left channel midpoint DAC output disable
    DAC1STATbits.LITYPE = 0;        // left channel interrupt if FIFO empty
    //DAC1STATbits.LFULL = 1 if full, 0 if not full, read only
    //DAC1STATbits.LEMPTY = 1 if empty, 0 if not, read only
    DAC1STATbits.ROEN = 1;          // right channel output DAC enabled
    DAC1STATbits.RMVOEN = 1;        // right channel midpoint DAC output enable
    DAC1STATbits.RITYPE = 1;        // right channel interrupt if FIFO empty

    TRISCbits.TRISC8 = 0;           // OUTPUT
    PORTCbits.RC8 = 1;              // SET AS HIGH, SHUTDOWN OFF
    //DAC1STATbits.RFULL = 1 if full, 0 if not full, read only
    //DAC1STATbits.REMPTY = 1 if empty, 0 if not, read only

    DAC1DFLT = 0x0000;              // default DAC output
    DAC1RDAT = 0x0000;              // right data register
    DAC1LDAT = 0x0000;              // left data register


//
    int ramp[C1pts];


    //int increments[36] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    //int mods[36] = {C1pts,C1Spts,D1pts,D1Spts,E1pts,F1pts,F1Spts,G1pts,G1Spts,A1pts,A1Spts,B1pts,
    //                C2pts,C2Spts,D2pts,D2Spts,E2pts,F2pts,F2Spts,G2pts,G2Spts,A2pts,A2Spts,B2pts,
    //               C3pts,C3Spts,D3pts,D3Spts,E3pts,F3pts,F3Spts,G3pts,G3Spts,A3pts,A3Spts,B3pts};
    //unsigned int freqs[36];
    int c1i=0;
    int c1si=0;
    int d1i=0;
    int d1si=0;
    int e1i=0;
    int f1i=0;
    int f1si=0;
    int g1i=0;
    int g1si=0;
    int a1i=0;
    int a1si=0;
    int b1i=0;
    int c2i=0;
    int c2si=0;
    int d2i=0;
    int d2si=0;
    int e2i=0;
    int f2i=0;
    int f2si=0;
    int g2i=0;
    int g2si=0;
    int a2i=0;
    int a2si=0;
    int b2i=0;
    int c3i=0;
    int c3si=0;
    int d3i=0;
    int d3si=0;
    int e3i=0;
    int f3i=0;
    int f3si=0;
    int g3i=0;
    int g3si=0;
    int a3i=0;
    int a3si=0;
    int b3i=0;
    
    int c1o=0;
    int c1so=0;
    int d1o=0;
    int d1so=0;
    int e1o=0;
    int f1o=0;
    int f1so=0;
    int g1o=0;
    int g1so=0;
    int a1o=0;
    int a1so=0;
    int b1o=0;
    int c2o=0;
    int c2so=0;
    int d2o=0;
    int d2so=0;
    int e2o=0;
    int f2o=0;
    int f2so=0;
    int g2o=0;
    int g2so=0;
    int a2o=0;
    int a2so=0;
    int b2o=0;
    int c3o=0;
    int c3so=0;
    int d3o=0;
    int d3so=0;
    int e3o=0;
    int f3o=0;
    int f3so=0;
    int g3o=0;
    int g3so=0;
    int a3o=0;
    int a3so=0;
    int b3o=0;
    
   


    int factor=1;
    int index;

    for (index = 0; index<C1pts; index++) {
        ramp[index]=factor*index;
    }
//    for (index = 0; index<D1pts; index++) {
//        ramp[index]=factor*index;
//    }
//    for (index = 0; index<C1pts; index++) {
//        ramp[index]=factor*index;
//    }
//    for (index = 0; index<C1pts; index++) {
//        ramp[index]=factor*index;
//    }
//    for (index = 0; index<C1pts; index++) {
//        ramp[index]=factor*index;
//    }
//    for (index = 0; index<C1pts; index++) {
//        ramp[index]=factor*index;
//    }
//    for (index = 0; index<C1pts; index++) {
//        ramp[index]=factor*index;
//    }
//    for (index = 0; index<C1pts; index++) {
//        ramp[index]=factor*index;
//    }
//    for (index = 0; index<C1pts; index++) {
//        ramp[index]=factor*index;
//    }
    initiateI2C();
    initiateInterrupts();

    TRISBbits.TRISB6 = 0;
    LATBbits.LATB6 = 0;

    long yout;
    long long selection = 0;
    int time=0;


    while(1) {

       // selection1 = buttonStates1;
        selection = buttonStates2;

     /*   if (time==50) {
            //selection = getI2CByte();
            freqs[0] = selection&1;
            freqs[1] = (selection&2)==2;
            freqs[2] = (selection&4)==4;
            freqs[3] = (selection&8)==8;
            freqs[4] = (selection&16)==16;
            freqs[5] = (selection&32)==32;
            freqs[6] = (selection&64)==64;
            freqs[7] = (selection&128)==128;
            time=0;
        }
        */

        //LATCbits.LATC6=1;
//        yout =  C1factor*freqs[0]*ramp[c1i] + D1factor*freqs[2]*ramp[d1i] +
//                E1factor*freqs[4]*ramp[e1i] + F1factor*freqs[5]*ramp[f1i]+
//                G1factor*freqs[7]*ramp[g1i] + A1factor*0*ramp[a1i] +
//                B1factor*0*ramp[b1i] + C2factor*0*ramp[c2i] +
//                C1Sfactor*freqs[1]*ramp[c1si] + D1Sfactor*freqs[3]*ramp[d1si] +
//                F1Sfactor*freqs[6]*ramp[f1si] + G1Sfactor*0*ramp[g1si] +
//                A1Sfactor*0*ramp[a1si];
//        yout =  C1factor*freqs[0]*ramp[c1i++] + C1Sfactor*freqs[1]*ramp[c1si++] +
//                D1factor*freqs[2]*ramp[d1i++] + D1Sfactor*freqs[3]*ramp[d1si++] +
//                E1factor*freqs[4]*ramp[e1i++] + F1factor*freqs[5]*ramp[f1i++]+
//                F1Sfactor*freqs[6]*ramp[f1si++]+G1factor*freqs[7]*ramp[g1i++] +
//                G1Sfactor*0*ramp[g1si++]+A1factor*0*ramp[a1i++] +
//                A1Sfactor*0*ramp[a1i] +  B1factor*0*ramp[b1i++] + C2factor*0*ramp[c1i++];
//                C2factor*freqs[12]*ramp[c2i++];
        //yout=5000*(yref[(index%floor(SAMPFREQ/C1))*pointref/floor(SAMPFREQ/C1)]+yref[(index%*pointref/floor(SAMPFREQ/E1)]);
        while(DAC1STATbits.RFULL == 1);
        DAC1RDAT = yout;


        /*if (time==200) {
            //selection = getI2CByte();
            freqs[0] = selection&1;
            freqs[1] = (selection&2)==2;
            freqs[2] = (selection&4)==4;
            freqs[3] = (selection&8)==8;
            freqs[4] = (selection&16)==16;
            freqs[5] = (selection&32)==32;
            freqs[6] = (selection&64)==64;
            freqs[7] = (selection&128)==128;
            time=0;
        }*/

            yout=0;

                            if (freqs[0]==1){
                                c1i++;
                                if (c1i==C1pts)
                                    c1i=0;
                                yout=yout+C1factor*ramp[c1i]*multFactor;
                                if (!modulation==0){
                                    c1o++;
                                    if (c1o==C1pts-modulation)
                                        c1o=0;
                                    yout=yout+C1factor*ramp[c1o]*multFactor;
                                }
                            }
                            if (freqs[1]==1) {
                                c1si++;
                                if (c1si==C1Spts)
                                    c1si=0;
                                yout=yout+C1Sfactor*ramp[c1si]*multFactor;
                                if (!modulation==0){
                                    c1so++;
                                    if (c1so==C1Spts-modulation)
                                        c1so=0;
                                    yout=yout+C1Sfactor*ramp[c1so]*multFactor;
                                }
                            }
                            if (freqs[2]==1) {
                                d1i++;
                                if (d1i==D1pts)
                                    d1i=0;
                                yout=yout+D1Sfactor*ramp[d1i]*multFactor;
                                if (!modulation==0){
                                    d1o++;
                                    if (d1o==D1pts-modulation)
                                        d1o=0;
                                    yout=yout+D1factor*ramp[d1o]*multFactor;
                                }
                            }
                            if (freqs[3]==1) {
                                d1si++;
                                if (d1si==D1Spts)
                                    d1si=0;
                                yout=yout+D1Sfactor*ramp[d1si]*multFactor;
                                if (!modulation==0){
                                    d1so++;
                                    if (d1so==D1Spts-modulation)
                                        d1so=0;
                                    yout=yout+D1Sfactor*ramp[d1so]*multFactor;
                                }
                            }
                            if (freqs[4]==1) {
                                e1i++;
                                if (e1i==E1pts)
                                    e1i=0;
                                yout=yout+E1factor*ramp[e1i]*multFactor;
                                if (!modulation==0){
                                    e1o++;
                                    if (e1o==E1pts-modulation)
                                        e1o=0;
                                    yout=yout+E1factor*ramp[e1o]*multFactor;
                                }
                            }
                            if (freqs[5]==1) {
                                f1i++;
                                if (f1i==F1pts)
                                    f1i=0;
                                yout=yout+F1factor*ramp[f1i]*multFactor;
                                if (!modulation==0){
                                    f1o++;
                                    if (f1o==F1pts-modulation)
                                        f1o=0;
                                    yout=yout+F1factor*ramp[f1o]*multFactor;
                                }
                            }
                            if (freqs[6]==1) {
                                f1si++;
                                if (f1si==F1Spts)
                                    f1si=0;
                                yout=yout+F1Sfactor*ramp[f1si]*multFactor;
                                if (!modulation==0){
                                    f1so++;
                                    if (f1so==F1Spts-modulation)
                                        f1so=0;
                                    yout=yout+F1Sfactor*ramp[f1so]*multFactor;
                                }
                            }
                            if (freqs[7]==1) {
                                g1i++;
                                if (g1i==G1pts)
                                    g1i=0;
                                yout=yout+G1factor*ramp[g1i]*multFactor;
                            if (!modulation==0){
                                    g1o++;
                                    if (g1o==G1pts-modulation)
                                        g1o=0;
                                    yout=yout+G1factor*ramp[g1o]*multFactor;
                                }
                            }
                            if (freqs[8]==1) {
                                g1si++;
                                if (g1si==G1Spts)
                                    g1si=0;
                                yout=yout+G1Sfactor*ramp[g1si]*multFactor;
                                if (!modulation==0){
                                    g1so++;
                                    if (g1so==G1Spts-modulation)
                                        g1so=0;
                                    yout=yout+G1Sfactor*ramp[g1so]*multFactor;
                                }
                            }
                            if (freqs[9]==1) {
                                a1i++;
                                if (a1i==A1pts)
                                    a1i=0;
                                yout=yout+A1factor*ramp[a1i]*multFactor;
                                if (!modulation==0){
                                    a1o++;
                                    if (a1o==A1pts-modulation)
                                        a1o=0;
                                    yout=yout+A1factor*ramp[a1o]*multFactor;
                                }
                            }
                            if (freqs[10]==1) {
                                a1si++;
                                if (a1si==A1Spts)
                                    a1si=0;
                                yout=yout+A1Sfactor*ramp[a1si]*multFactor;
                                if (!modulation==0){
                                    a1so++;
                                    if (a1so==A1Spts-modulation)
                                        a1so=0;
                                    yout=yout+A1Sfactor*ramp[a1so]*multFactor;
                                }
                            }
                            if (freqs[11]==1) {
                                b1i++;
                                if (b1i==B1pts)
                                    b1i=0;
                                yout=yout+B1factor*ramp[b1i]*multFactor;
                                if (!modulation==0){
                                    b1o++;
                                    if (b1o==B1pts-modulation)
                                        b1o=0;
                                    yout=yout+B1factor*ramp[b1o]*multFactor;
                                }
                            }
                            if (freqs[12]==1) {
                                c2i++;
                                if (c2i==C2pts)
                                    c2i=0;
                                yout=yout+C2factor*ramp[c2i]*multFactor;
                                if (!modulation==0){
                                    c2o++;
                                    if (c2o==C2pts-modulation)
                                        c2o=0;
                                    yout=yout+C2factor*ramp[c2o]*multFactor;
                                }
                                
                            }
                            if (freqs[13]==1) {
                                c2si++;
                                if (c2si==C2Spts)
                                    c2si=0;
                                yout=yout+C2Sfactor*ramp[c2si]*multFactor;
                                if (!modulation==0){
                                    c2so++;
                                    if (c2so==C2Spts-modulation)
                                        c2so=0;
                                    yout=yout+C2Sfactor*ramp[c2so]*multFactor;
                                }
                            }
                            if (freqs[14]==1) {
                                d2i++;
                                if (d2i==D2pts)
                                    d2i=0;
                                yout=yout+D2factor*ramp[d2i]*multFactor;
                                if (!modulation==0){
                                    d2o++;
                                    if (d2o==D2pts-modulation)
                                        d2o=0;
                                    yout=yout+D2factor*ramp[d2o]*multFactor;
                                }
                            }
                            if (freqs[15]==1) {
                                d2si++;
                                if (d2si==D2Spts)
                                    d2si=0;
                                yout=yout+D2Sfactor*ramp[d2si]*multFactor;
                                if (!modulation==0){
                                    d2so++;
                                    if (d2so==D2Spts-modulation)
                                        d2so=0;
                                    yout=yout+D2Sfactor*ramp[d2so]*multFactor;
                                }
                            }
                            if (freqs[16]==1) {
                                e2i++;
                                if (e2i==E2pts)
                                    e2i=0;
                                yout=yout+E2factor*ramp[e2i]*multFactor;
                                if (!modulation==0){
                                    e2o++;
                                    if (e2o==E2pts-modulation)
                                        e2o=0;
                                    yout=yout+E2factor*ramp[e2o]*multFactor;
                                }
                            }
                            if (freqs[17]==1) {
                                f2i++;
                                if (f2i==F2pts)
                                    f2i=0;
                                yout=yout+F2factor*ramp[f2i]*multFactor;
                                if (!modulation==0){
                                    f2o++;
                                    if (f2o==F2pts-modulation)
                                        f2o=0;
                                    yout=yout+F2factor*ramp[f2o]*multFactor;
                                }
                            }
                            if (freqs[18]==1) {
                                f2si++;
                                if (f2si==F2Spts)
                                    f2si=0;
                                yout=yout+F2Sfactor*ramp[f2si]*multFactor;
                                if (!modulation==0){
                                    f2so++;
                                    if (f2so==F2Spts-modulation)
                                        f2so=0;
                                    yout=yout+F2Sfactor*ramp[f2so]*multFactor;
                                }
                            }
                            if (freqs[19]==1) {
                                g2i++;
                                if (g2i==G2pts)
                                    g2i=0;
                                yout=yout+G2factor*ramp[g2i]*multFactor;
                                if (!modulation==0){
                                    g2o++;
                                    if (g2o==G2pts-modulation)
                                        g2o=0;
                                    yout=yout+G2factor*ramp[g2o]*multFactor;
                                }
                            }
                            if (freqs[20]==1) {
                                g2si++;
                                if (g2si==G2Spts)
                                    g2si=0;
                                yout=yout+G2Sfactor*ramp[g2si]*multFactor;
                                if (!modulation==0){
                                    g2so++;
                                    if (g2so==G2Spts-modulation)
                                        g2so=0;
                                    yout=yout+G2Sfactor*ramp[g2so]*multFactor;
                                }
                            }
                            if (freqs[21]==1) {
                                a2i++;
                                if (a2i==A2pts)
                                    a2i=0;
                                yout=yout+A2factor*ramp[a2i]*multFactor;
                                if (!modulation==0){
                                    a2o++;
                                    if (a2o==A2pts-modulation)
                                        a2o=0;
                                    yout=yout+A2factor*ramp[a2o]*multFactor;
                                }
                            }
                            if (freqs[22]==1) {
                                a2si++;
                                if (a2si==A2Spts)
                                    a2si=0;
                                yout=yout+A2Sfactor*ramp[a2si]*multFactor;
                                if (!modulation==0){
                                    a2so++;
                                    if (a2so==A2Spts-modulation)
                                        a2so=0;
                                    yout=yout+A2Sfactor*ramp[a2so]*multFactor;
                                }
                            }
                            if (freqs[23]==1) {
                                b2i++;
                                if (b2i==B2pts)
                                    b2i=0;
                                yout=yout+B2factor*ramp[b2i]*multFactor;
                                if (!modulation==0){
                                    b2o++;
                                    if (b2o==B2pts-modulation)
                                        b2o=0;
                                    yout=yout+B2factor*ramp[b2o]*multFactor;
                                }
                            }
                            if (freqs[24]==1) {
                                c3i++;
                                if (c3i==C3pts)
                                    c3i=0;
                                yout=yout+C3factor*ramp[c3i]*multFactor;
                                if (!modulation==0){
                                    c3o++;
                                    if (c3o==C3pts-modulation)
                                        c3o=0;
                                    yout=yout+C3factor*ramp[c3o]*multFactor;
                                }
                            }
                            if (freqs[25]==1) {
                                c3si++;
                                if (c3si==C3Spts)
                                    c3si=0;
                                yout=yout+C3Sfactor*ramp[c3si]*multFactor;
                                if (!modulation==0){
                                    c3so++;
                                    if (c3so==C3Spts-modulation)
                                        c3so=0;
                                    yout=yout+C3Sfactor*ramp[c3so]*multFactor;
                                }
                            }
                            if (freqs[26]==1) {
                                d3i++;
                                if (d3i==D3pts)
                                    d3i=0;
                                yout=yout+D3factor*ramp[d3i]*multFactor;
                                if (!modulation==0){
                                    d3o++;
                                    if (d3o==D3pts-modulation)
                                        d3o=0;
                                    yout=yout+D3factor*ramp[d3o]*multFactor;
                                }
                            }
                            if (freqs[27]==1) {
                                d3si++;
                                if (d3si==D3Spts)
                                    d3si=0;
                                yout=yout+D3Sfactor*ramp[d3si]*multFactor;
                                if (!modulation==0){
                                    d3so++;
                                    if (d3so==D3Spts-modulation)
                                        d3so=0;
                                    yout=yout+D3Sfactor*ramp[d3so]*multFactor;
                                }
                            }
                            if (freqs[28]==1) {
                                e3i++;
                                if (e3i==E3pts)
                                    e3i=0;
                                yout=yout+E3factor*ramp[e3i]*multFactor;
                                if (!modulation==0){
                                    e3o++;
                                    if (e3o==E3pts-modulation)
                                        e3o=0;
                                    yout=yout+E3factor*ramp[e3o]*multFactor;
                                }
                            }
                            if (freqs[29]==1) {
                                f3i++;
                                if (f3i==F3pts)
                                    f3i=0;
                                yout=yout+F3factor*ramp[f3i]*multFactor;
                                if (!modulation==0){
                                    f3o++;
                                    if (f3o==F3pts-modulation)
                                        f3o=0;
                                    yout=yout+F3factor*ramp[f3o]*multFactor;
                                }
                            }
                            if (freqs[30]==1) {
                                f3si++;
                                if (f3si==F3Spts)
                                    f3si=0;
                                yout=yout+F3Sfactor*ramp[f3si]*multFactor;
                                if (!modulation==0){
                                    f3so++;
                                    if (f3so==F3Spts-modulation)
                                        f3so=0;
                                    yout=yout+F3Sfactor*ramp[f3so]*multFactor;
                                }
                            }
                            if (freqs[31]==1) {
                                g3i++;
                                if (g3i==G3pts)
                                    g3i=0;
                                yout=yout+G3factor*ramp[g3i]*multFactor;
                                if (!modulation==0){
                                    g3o++;
                                    if (g3o==G3pts-modulation)
                                        g3o=0;
                                    yout=yout+G3factor*ramp[g3o]*multFactor;
                                }
                            }
                            if (freqs[32]==1) {
                                g3si++;
                                if (g3si==G3Spts)
                                    g3si=0;
                                yout=yout+G3Sfactor*ramp[g3si]*multFactor;
                                if (!modulation==0){
                                    g3so++;
                                    if (g3so==G3Spts-modulation)
                                        g3so=0;
                                    yout=yout+G3Sfactor*ramp[g3so]*multFactor;
                                }
                            }
                            if (freqs[33]==1) {
                                a3i++;
                                if (a3i==A3pts)
                                    a3i=0;
                                yout=yout+A3factor*ramp[a3i]*multFactor;
                                if (!modulation==0){
                                    a3o++;
                                    if (a3o==A3pts-modulation)
                                        a3o=0;
                                    yout=yout+A3factor*ramp[a3o]*multFactor;
                                }
                            }
                            if (freqs[34]==1) {
                                a3si++;
                                if (a3si==A3Spts)
                                    a3si=0;
                                yout=yout+A3Sfactor*ramp[a3si]*multFactor;
                                if (!modulation==0){
                                    a3so++;
                                    if (a3so==A3Spts-modulation)
                                        a3so=0;
                                    yout=yout+A3Sfactor*ramp[a3so]*multFactor;
                                }
                            }
                            if (freqs[35]==1) {
                                b3i++;
                                if (b3i==B3pts)
                                    b3i=0;
                                yout=yout+B3factor*ramp[b3i]*multFactor;
                                if (!modulation==0){
                                    b3o++;
                                    if (b3o==B3pts-modulation)
                                        b3o=0;
                                    yout=yout+B3factor*ramp[b3o]*multFactor;
                                }
                            }






            time=time+1;

    }


    return (EXIT_SUCCESS);
}

