TFT

Dependencies:   mbed

Fork of Ovation_Controller_1 by Andrew R

main.cpp

Committer:
andrewcrussell
Date:
2010-11-27
Revision:
0:3811b20051b1
Child:
1:ecf8078bf531

File content as of revision 0:3811b20051b1:

/******************Ovation SCA-1 Control Program *********************/
/*                  Nov 2010  V1.00                                 */



#include "mbed.h"
/* System Definitions */
/* the  follwoing are the pin assignments on the mbed controller */
DigitalOut pga2320(p30);        /* normally LOW; take HIGH  to select PGA2320 */
DigitalOut phono(p29);
DigitalIn IRx(p28);             /* need  to add p27  which is an interrupt pin */
DigitalIn IRxint(p27);
DigitalIn IPSELPB(p26);
DigitalIn IPSELB(p25);
DigitalIn IPSELA(p24);
DigitalIn VOLPB(p23);
DigitalIn VOLB(p22);
DigitalIn VOLA(p21);
AnalogIn ALS(p20);
DigitalOut SBUSON(p19);
DigitalOut NAOE(p18);
DigitalOut DC(p17);
DigitalOut RD(p16);    /* also ASTROBE */
DigitalOut WR(p15);    /* MDATA */
DigitalOut RS(p14);    /* ACLK */
DigitalOut CS(p13);
BusInOut DB(p12, p11, p10, p9, p8, p7, p6, p5);


/* global defines */
int volume=0;
int balance_left =0;
int balance_right=0;
//int power=0;
int bright=8;           /* this is the backlight brightness */
int control_bus=0;  /* this is the intial setting for all the  main board relays */
int relay=0;        /* this is  just a counter */
int control=0;      /* this the 8 bits  for the  control outputs on the CPU board. this is the intial setting */

/*lower case defines are bit positions and upper case are TRUE  or FALSE slect indicators */

#define POWERON 64;
#define POWEROFF 0;
int power=0;
#define ZLG 32
#define LG 0
int loopgain=0;
#define TRIGON 16
#define TRIGOFF 0
int trigger=0;
#define OPOFF 1
#define OPON 1
int mutebit=0;
#define HPOFF 0
#define HPON 128
int hpmutebit=0;
#define RECON 2
#define RECOFF 0
int recloop1bit=0;



/* the relay bits  occupy the top 8 MSB's and the control bites the 8 LSB's in the 16 */
/* bit serial data stream from the CPU */


/* system constants */

#define HIGH 1;
#define LOW 0;
#define TRUE 1;
#define FALSE 0;
#define incdec 1;
#define VOLA (p21);
#define VOLB (p22);
#define VOLPB (p23);
#define IPSELA (p24);
#define IPSELB (p25);
#define IPSELPB (p26);
#define SERIN (p27);
#define IRxINT (p28);
/* these are the input select relay  bit position assignments */
int inputrelay=1;       /* inputrelay holds the bit position of the selected relay */
#define Aux2    4
#define Aux1    8
#define Tuner    16
#define CD    32
#define Phono    64

/********************** declare all interrupts here****************************/
InterruptIn volumein(p21);        /* interuppt from the volume control encoder */
InterruptIn inputsel(p24);      /* for the input select encoder */
InterruptIn mutesw(p26);          /* this mutes the output - pb att to the sel encoder */
InterruptIn powersw(p23);         /* this turns the main power on-off.  Att. to the vol control encoder */


//void mutesw.fall(void);
//void volumein.fall(void);
//void inputsel.fall(void);
//void powersw.fall(void);

/*************** Serial bus routine for control relays and digital I/O ***************/
/* this routine takes the 8 bit relay data and the 8 bit  control data and joins them */
/* and writes 16 bits out on the serial bus */
/* it is called by the  relay function  or the control function */

void serialout(void) {

        mutesw.fall(NULL);
        volumein.fall(NULL);
        inputsel.fall(NULL);
    
    int unsigned serialdata=0;/* the 16 bits of serial data to go out to the  analog board - flush  it so its clean */
    int m=1;
    int unsigned shift_out_bit=0;
    int SCOUNT=0;
    int control=0;  /*control is temporary storage of the lower 8 bits of the  serial bitstream */


    /* put the hpmute, mute and recloop1 bits into the relay part of the output bitstream */
    inputrelay=(inputrelay|hpmutebit|mutebit|recloop1bit);

    //printf("1   %d   %d\n\r",relay,inputrelay);

    /* relay  now has the input selection and the status of the mute and recloop1 relays */
    /* next shift the relay data into the top 8 MSB's */

    serialdata=(inputrelay<<8);

    //printf("2   serialdata=%u\n\r",serialdata);

    /* top 8 MSB's now  have the relay data including mute, hpmute and recloop1 and the bottom 8 LSB's  are filled with 0's */
    /* now add the  bottom 8 control bits below */


    control=(control|bright|trigger|power|loopgain); /* last  bit position is unused in the control word */
   // printf("3   control=%d\n\r",control);
    serialdata=(serialdata|control); /* we now have all the data to send  to the analog board */


    RD=LOW;                     /* ASTROBE - it must be LOW on the analog board intitially */
    WR=HIGH;                     /* MDATA */
    RS=HIGH;                     /* ACLK  - so the clock line on the analog board is now LOW*/
    SBUSON=HIGH;                /* turn the SBUS on */
    wait_ms(0.5);
    do {

        shift_out_bit = (serialdata & m);       /* ADATA = the LSB (k=1)  */
        
        if (shift_out_bit==1) 
        {
            WR=HIGH;
        } 
        else if (shift_out_bit==0) 
        {
            WR=LOW;
        }
                
        wait_ms(1);
        RS=LOW;                /* clock goes high on main board - data latches */
        wait_ms(1);
        RS=HIGH;                /* clock goes low */
        SCOUNT++;        /* increment the bit counter */
        m=(m<<1);               /* shift bit mask up one position towards the MSB */
        printf("%d\n\r",m);
    } while (SCOUNT<16);

    wait_ms(1);
    RD=HIGH;                    /* Strobe the data  into the A6821*/
    wait_ms(1);
    RD=LOW;                     /* Strobe now goes LOW again  on the main board */
    wait_ms(1);
    SBUSON=LOW;                 /* remember to enable the outputs after intial set-up */
    wait_ms(1);
    
    //mutesw.fall(&mute_output);
    //volumein.fall(&volumecontrol);
    //inputsel.fall(&inputselect);
    //powersw.fall(&power_on_off);

}
/******************* volume control interrupt routine **********************/

void volumecontrol(void) {
    //int encoder =0;                     /* flush encoder to make sure its clean */
    long lrvol =0;       /* lrvol (i.e. left and right volume) is the concatenated left & right volume setting */
    int vol_shift_bit=0;

    wait_ms(3);        /* wait for contact bounces to subside */

    PortIn  encoder(Port2, 48);     /* extract data on mbed pins 21 and 22 - bit positions 8 and 16 on p2 */

    switch (encoder) {
        case 32:
            break;
        case 48:
            break;
        case 0: {              /* rotary encoder was turned clockwise */
            volume++;         /* increment the volume */
        }
        break;

        case 16: {
            volume--;
        }
        break;
    }
    printf("%d  ",volume);
    if (volume>127)
        volume=127;
    if (volume <1)
        volume=0;

    /******************* Write the data  out to the PGA2320 ************************/

    int volshift=0;
    int SCOUNT=0;
    int k = 1;

    volshift=volume;
    lrvol = (volume<<9);  /* volume value now occupies bits 7-14 with bits 0-6 filled with 0's */
    lrvol = (lrvol|volshift); /* now have a copy of volume in botton 7 LSB's  - so 14 bits of data in total*/

    printf("%d\n\r",lrvol);

    pga2320=LOW;             /* make sure the PGA2320 is de-selected */    
    RD=LOW;                 /* ASTROBE -  we do NOT  use STROBE when writing the volume to the PGA2320 */
    WR=LOW;                 /* MDATA */
    RS=HIGH;                 /* ACLK  - it is now LOW on the isolated side*/
    SBUSON=HIGH;            /* turn the SBUS on */
    wait_us(50);
    pga2320=HIGH;          /* chip select the  PGA2320-so it goes LOW on the analog board */
    do {
        vol_shift_bit = (lrvol&k);       /* ADATA = the LSB (k=1)  */
        WR=vol_shift_bit;                  /* put the 1st bit to be written on the WR pin */
        wait_ms(1);
        RS=LOW;              /* clock it out */
        wait_ms(1);
        RS=HIGH;
        SCOUNT=SCOUNT+1;     /* increment the bit counter */
        k=(k<<1);                /* shift bit mask up one position towards the MSB */
    } while (SCOUNT<16);    /* send all 16 bits  for L & R channel */
    
    wait_ms(1);
    //RD=HIGH;
    wait_ms(1);
    //RD=LOW;

    wait_us(100);
    pga2320=LOW;           /* deslect PGA2320 */
    wait_us(100);
    SBUSON=LOW;             /* remember to enable the outputs after intial set-up */
    wait_us(100);
}

/******************************* Input Selector Encoder Routine ****************************/
/* this function controls  which relays are energized  on the main analog board */
/* It calls the serialout  function to send the data */

void inputselect(void) {
    inputsel.fall(NULL);
    //mutesw.fall(NULL);
    //volumein.fall(NULL);
    //powersw.fall(NULL);
    wait_ms(10);                     /*wait for  contact bounce to subside */

    PortIn  select(Port2, 6);       /* extract data on mbed pins 24 and 25 - bit positions 2 and 4 on p2 */
    //wait_ms(3);
    switch (select) {
        case 0: {                   /* rotary encoder was turned clockwise */
            relay++;               /* increment the selection */
        }
        break;
        case 4: {} break;         /* fall through values - ignore */
        case 6: {} break;
        case 2: {
            relay--;
        }
        break;
    }

    if (relay>=6)                   /* here we make sure the select knob rotates from 1-8 to again */
        relay=1;
    if (relay<=0)
        relay=5;
    /* set the correct value in the serial bit position */
    switch (relay) {
        case 1:
            inputrelay=4;
            break;    /* this is Aux2*/
        case 2:
            inputrelay=8;
            break;      /*aux1 */
        case 3:
            inputrelay=16;
            break;     /*tuner */
        case 4:
            inputrelay=32;
            break;     /*CD*/
        case 5:
            inputrelay=64;
            break;      /*phono*/
    }

printf("hello");
    //mutesw.fall(&mute_output);
    //volumein.fall(&volumecontrol);
    //powersw.fall(&power_on_off);
    //wait_ms(1);
    //inputsel.fall(&inputselect);
    serialout();
    inputsel.fall(&inputselect);
    
}
/**************************** output mute via front panel push button********************************/

void mute_output(void) {    /* this is the interrupt handler routine  */

    //mutesw.fall(NULL);

    wait_ms(10);             /* debounce time - CRITICAL - MORE THAN 10 OR  LESS THAN 5 GIVES PROBLEMS! */
    if (mutebit==0) {       /* it must have been  off   */
        mutebit=1;              /* so turn it ON */
        volumein.fall(&volumecontrol);
        inputsel.fall(&inputselect);
        printf("\n\r %d Output ON",mutebit);
    }

    else if (mutebit==1) {    /* it currently ON  */
        mutebit=0;              /* so turn the output OFF */
        volumein.fall(NULL); /* while the output is muted,  you cannot change the volume or input selection */
        inputsel.fall(NULL);
        printf("\n\r %d Output OFF", mutebit);
    }
    //wait_ms(10);

    serialout();
    //mutesw.fall(&mute_output);

}
/**********************************initialize *****************************************/
/* this routine sets  up the pre-amp at initial power-up.  all relays on the main board */
/* are de-energized,  volume  is set to 0, display off,  system in standby */

void initialize(void) {

    int SCOUNT=0;
    int k=1;        /*this is the bit place holder that gets  shifted through the data to be sent */
    int flushit=0;  /* flushit explicitly declared since we may want a setting other than all OFF */
    int shift_flush =0;

    RD=LOW;                     /* ASTROBE */
    WR=LOW;                     /* MDATA */
    RS=LOW;                     /* ACLK */
    SBUSON=HIGH;                /* turn the SBUS on */

    do {
        shift_flush=(flushit&k);       /* ADATA = the LSB (k=1)  */
        WR=shift_flush;
        wait_ms(1);
        RS=HIGH;                /* clock it out */
        wait_ms(1);
        RS=LOW;
        SCOUNT=SCOUNT+1;        /* increment the bit counter */
        k=(k<<1);               /* shift bit mask up one position towards the MSB */
    } while (SCOUNT<8);         /* counting starts from 0! */

    wait_ms(1);
    RD=HIGH;                    /* Strobe the data  into the A6821*/
    wait_ms(1);
    RD=LOW;                     /* Strobe now goes HIGH again  on the main board */
    wait_ms(1);
    SBUSON=LOW;                 /* remember to enable the outputs after intial set-up */
    wait_ms(1);
    printf("\n\r");
}


/******************** ON-OFF Pushbutton input *******************************************/

void power_on_off(void) {

    wait_ms(10);           /* debounce time NOTE IF  ITS  MUCH LONGER THAN 10mS THERE ARE RE-ENTRY PROBLEMS*/
    if (power==0) {        /* it must have been  off so turn it ON */
        mutebit=0;              /* make sure output and headphone are muted */
        recloop1bit=0;
        loopgain=0;
        hpmutebit=0;
        trigger=0;
        power=64;      /* turn the main analog board on */
        serialout();
        wait(1);            /* let things settle a bit */
        NAOE=HIGH;          /* turn the A6821's on */
        hpmutebit=128;
        mutebit=1;
        serialout();
        bright=8;
        inputrelay=32;  /* always power up with CD selected */
        wait_ms(100);
        trigger=16;     /* go turn the power amp on */
        serialout();
        printf("\n\rPower ON\n");
        /* enable the interrupts again */
        mutesw.fall(&mute_output);
        volumein.fall(&volumecontrol);
        inputsel.fall(&inputselect);
        power=64;


    }

    else if (power==64) {                      /* it currently ON, so turn it OFF  */

        /*disable all the interrupts straight away */
        mutesw.fall(NULL);
        volumein.fall(NULL);
        inputsel.fall(NULL);
        trigger=0; /*turn the power amp off first */

        serialout();
        wait_ms(500);   /*let the amp diable the speaker */
        mutebit=0;
        hpmutebit=0;
        serialout();     /* now turn the pre-amp outputs OFF */
        wait_ms(100);
        NAOE=LOW;      /* disconnect all inputs */
        printf("\n\rPower OFF\n");
        power=0;
    }

}



/******************** Test Mode Pushbutton input *******************************************/
/* in this mode,  both volume and select pushbuttons   must be depressed for 5 seconds  */
/* the pre-amp then enters the test  mode where all the relays are cycled continuosly */
/* and the volume is ramped up and down. To exit, depress  any push button. */

/**********************************main program*****************************************/
int main () {
    NAOE=LOW;               /* make sure the A6821's are disabled */
    wait_ms(5);
    initialize();           /* call the SBUS  routine to clean things  up */
    wait(1);


    powersw.fall(&power_on_off);



    do {

    } while (1);
}