TFT

Dependencies:   mbed

Fork of Ovation_Controller_1 by Andrew R

Committer:
andrewcrussell
Date:
Mon Apr 04 05:32:25 2011 +0000
Revision:
1:ecf8078bf531
Parent:
0:3811b20051b1
Child:
2:67e16df2c89a

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewcrussell 1:ecf8078bf531 1 /*******************************************************************/
andrewcrussell 1:ecf8078bf531 2 /************* Dec 22 2010 ************************/
andrewcrussell 1:ecf8078bf531 3 /************* V.1a ************************/
andrewcrussell 1:ecf8078bf531 4 /************* Balanced Stereo Pre-amplifer Control Program *******/
andrewcrussell 1:ecf8078bf531 5 #include "mbed.h"
andrewcrussell 1:ecf8078bf531 6 #include "system_defines.h"
andrewcrussell 1:ecf8078bf531 7 #include "string.h"
andrewcrussell 1:ecf8078bf531 8 #include "stdlib.h"
andrewcrussell 1:ecf8078bf531 9 #include "Graphic.h"
andrewcrussell 1:ecf8078bf531 10 #include "font_1.h"
andrewcrussell 1:ecf8078bf531 11 #include "stdlib.h"
andrewcrussell 1:ecf8078bf531 12 #include "string.h"
andrewcrussell 1:ecf8078bf531 13 #include "math.h"
andrewcrussell 1:ecf8078bf531 14 //#include "font_new.h"
andrewcrussell 1:ecf8078bf531 15
andrewcrussell 1:ecf8078bf531 16 /* global defines */
andrewcrussell 1:ecf8078bf531 17 int volume;
andrewcrussell 1:ecf8078bf531 18 int balance_left =0;
andrewcrussell 1:ecf8078bf531 19 int balance_right=0;
andrewcrussell 1:ecf8078bf531 20 int bright=8; /* this is the backlight brightness */
andrewcrussell 1:ecf8078bf531 21 int relay=0; /* this is just a counter */
andrewcrussell 1:ecf8078bf531 22 unsigned int control = 0; /* for the control outputs on the CPU board. This is the intial setting */
andrewcrussell 1:ecf8078bf531 23 unsigned int controls = 0; /* this is temporary storage value for control above used in power up and power down */
andrewcrussell 1:ecf8078bf531 24 int unsigned inputrelay=1; /* inputrelay holds the bit position of the selected relay */
andrewcrussell 1:ecf8078bf531 25 int pwrupvol=1;
andrewcrussell 1:ecf8078bf531 26 int power=0; /* set to 64 to turn it ON */
andrewcrussell 1:ecf8078bf531 27 int loopgain=0; /*set to 32 to turn it ON */
andrewcrussell 1:ecf8078bf531 28 int trigger=0; /* set to 16 to turn it ON */
andrewcrussell 1:ecf8078bf531 29 int mutebit=0; /* set to 1 to turn it ON */
andrewcrussell 1:ecf8078bf531 30 int hpmutebit=0; /* set to 127 to turn it ON */
andrewcrussell 1:ecf8078bf531 31 int recloop1bit=0; /*set to 2 to turn it ON */
andrewcrussell 1:ecf8078bf531 32 char remcon[10], ch[10];
andrewcrussell 1:ecf8078bf531 33 int remcontoken;
andrewcrussell 1:ecf8078bf531 34 int remaction;
andrewcrussell 1:ecf8078bf531 35 int r=0, k=0; /* these are counters used in the remote control routine */
andrewcrussell 1:ecf8078bf531 36 int flag=0; /* this fetches the command control code - remote or via f/panel PB's */
andrewcrussell 1:ecf8078bf531 37
andrewcrussell 1:ecf8078bf531 38 /********************** system constants ********************/
andrewcrussell 1:ecf8078bf531 39 #define HIGH 1;
andrewcrussell 1:ecf8078bf531 40 #define LOW 0;
andrewcrussell 1:ecf8078bf531 41 //#define TRUE 1;
andrewcrussell 1:ecf8078bf531 42 #define FALSE 0;
andrewcrussell 1:ecf8078bf531 43 #define incdec 1;
andrewcrussell 1:ecf8078bf531 44 #define VOLA (p21);
andrewcrussell 1:ecf8078bf531 45 #define VOLB (p22);
andrewcrussell 1:ecf8078bf531 46 #define VOLPB (p23);
andrewcrussell 1:ecf8078bf531 47 #define IPSELA (p24);
andrewcrussell 1:ecf8078bf531 48 #define IPSELB (p25);
andrewcrussell 1:ecf8078bf531 49 #define IPSELPB (p26);
andrewcrussell 1:ecf8078bf531 50 #define SERIN (p27);
andrewcrussell 1:ecf8078bf531 51 /*********** these are the input select relay bit position assignments **********/
andrewcrussell 1:ecf8078bf531 52
andrewcrussell 1:ecf8078bf531 53 #define Aux2 4
andrewcrussell 1:ecf8078bf531 54 #define Aux1 8
andrewcrussell 1:ecf8078bf531 55 #define Recorder 16
andrewcrussell 1:ecf8078bf531 56 #define CD 32
andrewcrussell 1:ecf8078bf531 57 #define Phono 64
andrewcrussell 1:ecf8078bf531 58
andrewcrussell 1:ecf8078bf531 59 /********************** declare all interrupt input pins here**********************/
andrewcrussell 1:ecf8078bf531 60 InterruptIn volumein(p21); /* interuppt from the volume control encoder */
andrewcrussell 1:ecf8078bf531 61 InterruptIn inputsel(p24); /* for the input select encoder */
andrewcrussell 1:ecf8078bf531 62 InterruptIn mutesw(p26); /* this mutes the output - pb att to the sel encoder */
andrewcrussell 1:ecf8078bf531 63 InterruptIn powersw(p23); /* this turns the main power on-off. Att. to the vol control encoder */
andrewcrussell 1:ecf8078bf531 64 Timeout timeout; /* remote control timeout */
andrewcrussell 1:ecf8078bf531 65
andrewcrussell 1:ecf8078bf531 66 /********************** mbed HALT mode (from Igor Skochinsky) ****************************/
andrewcrussell 1:ecf8078bf531 67 /* note: this will not work if the usb is connected, if local files, */
andrewcrussell 1:ecf8078bf531 68 /* or printf statements are used. Disconnect USB to get this to work */
andrewcrussell 1:ecf8078bf531 69 void halt() {
andrewcrussell 1:ecf8078bf531 70 #if defined(TARGET_LPC1768)
andrewcrussell 1:ecf8078bf531 71 __wfi(); // (enter sleep mode and) wait for interrupt
andrewcrussell 1:ecf8078bf531 72 #else
andrewcrussell 1:ecf8078bf531 73 LPC_SC->PCON |= 1; // set PM0 = 1: enter idle mode (assuming PM1 and PM2 are 0)
andrewcrussell 1:ecf8078bf531 74 #endif
andrewcrussell 1:ecf8078bf531 75 }
andrewcrussell 1:ecf8078bf531 76 /********************** how to disable interrupts ****************************/
andrewcrussell 1:ecf8078bf531 77 //__disable_irq(); // Disable Interrupts
andrewcrussell 1:ecf8078bf531 78 //__enable_irq(); // Enable Interrupts
andrewcrussell 1:ecf8078bf531 79 /************************* read the light sensor ************************************/
andrewcrussell 1:ecf8078bf531 80 void lightsensor() {
andrewcrussell 1:ecf8078bf531 81 AnalogIn light(p20); /* fetch the light intensity */
andrewcrussell 1:ecf8078bf531 82 // printf("\n\rlight level= (%f)",light.read());
andrewcrussell 1:ecf8078bf531 83 }
andrewcrussell 1:ecf8078bf531 84 /********************* remote control IRQ *************************/
andrewcrussell 1:ecf8078bf531 85 void remotecontrol(void) {
andrewcrussell 1:ecf8078bf531 86
andrewcrussell 1:ecf8078bf531 87 int q=0;
andrewcrussell 1:ecf8078bf531 88 int z=0;
andrewcrussell 1:ecf8078bf531 89 //infra=1;
andrewcrussell 1:ecf8078bf531 90 myled=!myled; /* just to let us know we are looping through here */
andrewcrussell 1:ecf8078bf531 91 //infra(0);
andrewcrussell 1:ecf8078bf531 92 do {
andrewcrussell 1:ecf8078bf531 93 remcon[z]=getc(remote);
andrewcrussell 1:ecf8078bf531 94 z++;
andrewcrussell 1:ecf8078bf531 95 } while (z<6);
andrewcrussell 1:ecf8078bf531 96 remaction=atoi(remcon);
andrewcrussell 1:ecf8078bf531 97 //printf("%d %s",remaction,remcon);
andrewcrussell 1:ecf8078bf531 98 /* here we flush everything to make sure that any garbage entries do not remain */
andrewcrussell 1:ecf8078bf531 99 /* because what we got in from the serial link was not a valid string */
andrewcrussell 1:ecf8078bf531 100
andrewcrussell 1:ecf8078bf531 101 for (q=0;q<8;q++) {
andrewcrussell 1:ecf8078bf531 102 remcon[q]=(' '); /* clean it up for the next cycle */
andrewcrussell 1:ecf8078bf531 103 }
andrewcrussell 1:ecf8078bf531 104
andrewcrussell 1:ecf8078bf531 105 flag=remaction;
andrewcrussell 1:ecf8078bf531 106 remaction=0;
andrewcrussell 1:ecf8078bf531 107 remcontoken=1;
andrewcrussell 1:ecf8078bf531 108
andrewcrussell 1:ecf8078bf531 109 }
andrewcrussell 1:ecf8078bf531 110 /*********** Power ON/OFF IRQ **********/
andrewcrussell 1:ecf8078bf531 111 void power1() {
andrewcrussell 1:ecf8078bf531 112 wait_ms(10);
andrewcrussell 1:ecf8078bf531 113 flag=9261;
andrewcrussell 1:ecf8078bf531 114 remcontoken=0;
andrewcrussell 1:ecf8078bf531 115 }
andrewcrussell 1:ecf8078bf531 116 /*************** Mute IRQ **************/
andrewcrussell 1:ecf8078bf531 117 void mute1() {
andrewcrussell 1:ecf8078bf531 118 wait_ms(10);
andrewcrussell 1:ecf8078bf531 119 flag=9241;
andrewcrussell 1:ecf8078bf531 120 remcontoken=0;
andrewcrussell 1:ecf8078bf531 121 }
andrewcrussell 1:ecf8078bf531 122 /************ Input Select IRQ ********/
andrewcrussell 1:ecf8078bf531 123 void inputsel1() {
andrewcrussell 1:ecf8078bf531 124 wait_ms(10);
andrewcrussell 1:ecf8078bf531 125 flag=9231;
andrewcrussell 1:ecf8078bf531 126 remcontoken=0;
andrewcrussell 1:ecf8078bf531 127 }
andrewcrussell 1:ecf8078bf531 128 /*************** Volume IRQ ***********/
andrewcrussell 1:ecf8078bf531 129 void volume1() {
andrewcrussell 1:ecf8078bf531 130 wait_ms(5); /* wait for contact bounce to subside */
andrewcrussell 1:ecf8078bf531 131
andrewcrussell 1:ecf8078bf531 132 PortIn encoder(Port2, 48); /* extract data on mbed pins 21 and 22 - bit positions 8 and 16 on p2 */
andrewcrussell 1:ecf8078bf531 133
andrewcrussell 1:ecf8078bf531 134 switch (encoder) {
andrewcrussell 1:ecf8078bf531 135 case 32:
andrewcrussell 1:ecf8078bf531 136 break;
andrewcrussell 1:ecf8078bf531 137 case 48:
andrewcrussell 1:ecf8078bf531 138 break;
andrewcrussell 1:ecf8078bf531 139
andrewcrussell 1:ecf8078bf531 140 case 0: {
andrewcrussell 1:ecf8078bf531 141 volume--;
andrewcrussell 1:ecf8078bf531 142 flag=9211;
andrewcrussell 1:ecf8078bf531 143 }
andrewcrussell 1:ecf8078bf531 144 break; /* rotary encoder was turned clockwise */
andrewcrussell 1:ecf8078bf531 145 case 16: {
andrewcrussell 1:ecf8078bf531 146 volume++;
andrewcrussell 1:ecf8078bf531 147 flag=9221;
andrewcrussell 1:ecf8078bf531 148 }
andrewcrussell 1:ecf8078bf531 149 break;
andrewcrussell 1:ecf8078bf531 150 }
andrewcrussell 1:ecf8078bf531 151 remcontoken=0;
andrewcrussell 1:ecf8078bf531 152 }
andrewcrussell 1:ecf8078bf531 153 /******************* volume control write routine **********************/
andrewcrussell 1:ecf8078bf531 154 /* this routine writes the volume control data out to the PGA2320 */
andrewcrussell 1:ecf8078bf531 155 void volumecontrol(void) {
andrewcrussell 1:ecf8078bf531 156 char setting[10];
andrewcrussell 1:ecf8078bf531 157 int lrvol =0; /* lrvol (i.e. left and right volume) is the concatenated left & right volume setting */
andrewcrussell 1:ecf8078bf531 158 int gain;
andrewcrussell 1:ecf8078bf531 159 int vol_shift_bit=0;
andrewcrussell 1:ecf8078bf531 160 int volshift=0;
andrewcrussell 1:ecf8078bf531 161 int voltemp=0;
andrewcrussell 1:ecf8078bf531 162 int SCOUNT=1;
andrewcrussell 1:ecf8078bf531 163 int k = 32768; /* k is now located at bit position 15 */
andrewcrussell 1:ecf8078bf531 164 /********** ???????????????????????????????? check your remcontoken!!! ****/
andrewcrussell 1:ecf8078bf531 165 if (remcontoken==1) { /* command came in through the IRC */
andrewcrussell 1:ecf8078bf531 166 if (flag==9221) {
andrewcrussell 1:ecf8078bf531 167 volume=volume++; /* volume up and down is a bit quicker via the IRC */
andrewcrussell 1:ecf8078bf531 168 }
andrewcrussell 1:ecf8078bf531 169 if (flag==9211) {
andrewcrussell 1:ecf8078bf531 170 volume=volume--;
andrewcrussell 1:ecf8078bf531 171 }
andrewcrussell 1:ecf8078bf531 172 }
andrewcrussell 1:ecf8078bf531 173 /******* check here that it is within range 0 - +255 ********/
andrewcrussell 1:ecf8078bf531 174
andrewcrussell 1:ecf8078bf531 175 if (volume>=255)
andrewcrussell 1:ecf8078bf531 176 volume=255;
andrewcrussell 1:ecf8078bf531 177 if (volume <=1) // check this - can it not be 0?
andrewcrussell 1:ecf8078bf531 178 volume=0;
andrewcrussell 1:ecf8078bf531 179
andrewcrussell 1:ecf8078bf531 180 //setting = volume;
andrewcrussell 1:ecf8078bf531 181 volume_slider(volume);
andrewcrussell 1:ecf8078bf531 182 FontDraw_SetFont(Calibri72);
andrewcrussell 1:ecf8078bf531 183 LCDSetRect(190,370, 190,479,0,BLACK); //clear the volume area
andrewcrussell 1:ecf8078bf531 184 //FontDraw_printf(195,375,"%d dB",(127-volume));
andrewcrussell 1:ecf8078bf531 185 FontDraw_printf(195,375,"%d dB",volume);
andrewcrussell 1:ecf8078bf531 186 //FontDraw_printf( int x, int y, stting); //char *format, ... );
andrewcrussell 1:ecf8078bf531 187
andrewcrussell 1:ecf8078bf531 188 /************************ Write the data out to the PGA2320 ************************/
andrewcrussell 1:ecf8078bf531 189 /* for the PGA2320, data is clocked out MSB first, starting with the Right channel */
andrewcrussell 1:ecf8078bf531 190
andrewcrussell 1:ecf8078bf531 191 volshift=volume;
andrewcrussell 1:ecf8078bf531 192 voltemp=volume; /* save volume and leave it untouched */
andrewcrussell 1:ecf8078bf531 193 lrvol = (voltemp<<8); /* volume value now occupies bits 8-16 with bits 0-7 filled with 0's */
andrewcrussell 1:ecf8078bf531 194 printf("%d %d\n\r",lrvol, volshift);
andrewcrussell 1:ecf8078bf531 195 lrvol = (lrvol|volshift); /* now have a copy of volume in botton 8 LSB's - so 16 bits of data in total*/
andrewcrussell 1:ecf8078bf531 196 //printf("%d %d\n\r",lrvol, volshift);
andrewcrussell 1:ecf8078bf531 197 pga2320=LOW; /* make sure the PGA2320 is de-selected */
andrewcrussell 1:ecf8078bf531 198 RD=LOW; /* ASTROBE - we do NOT use STROBE when writing the volume to the PGA2320 */
andrewcrussell 1:ecf8078bf531 199 WR=LOW; /* MDATA in low state*/
andrewcrussell 1:ecf8078bf531 200 RS=HIGH; /* ACLK - it is now LOW on the isolated side*/
andrewcrussell 1:ecf8078bf531 201 SBUSON=HIGH; /* turn the SBUS on */
andrewcrussell 1:ecf8078bf531 202 wait_us(50);
andrewcrussell 1:ecf8078bf531 203 pga2320=HIGH; /* chip select the PGA2320-so it goes LOW on the analog board */
andrewcrussell 1:ecf8078bf531 204 do {
andrewcrussell 1:ecf8078bf531 205 vol_shift_bit = (lrvol&k); /* ADATA = the MSB (k=1) */
andrewcrussell 1:ecf8078bf531 206 WR=!vol_shift_bit; /* put the 1st bit to be written on the WR pin AFTER INVERTING IT */
andrewcrussell 1:ecf8078bf531 207 wait_us(100);
andrewcrussell 1:ecf8078bf531 208 RS=LOW; /* clock it out */
andrewcrussell 1:ecf8078bf531 209 wait_us(100);
andrewcrussell 1:ecf8078bf531 210 RS=HIGH;
andrewcrussell 1:ecf8078bf531 211 SCOUNT=SCOUNT+1; /* increment the bit counter */
andrewcrussell 1:ecf8078bf531 212 k=(k>>1); /* shift bit mask up one position towards the LSB */
andrewcrussell 1:ecf8078bf531 213 } while (SCOUNT<16); /* send all 16 bits for L & R channel */
andrewcrussell 1:ecf8078bf531 214 /* note that it is 15 bit positions! */
andrewcrussell 1:ecf8078bf531 215
andrewcrussell 1:ecf8078bf531 216 wait_us(10);
andrewcrussell 1:ecf8078bf531 217 pga2320=LOW; /* deselect PGA2320 */
andrewcrussell 1:ecf8078bf531 218 wait_us(10);
andrewcrussell 1:ecf8078bf531 219 SBUSON=LOW;
andrewcrussell 1:ecf8078bf531 220 flag=0; /* clear the flag since the command is now completed */
andrewcrussell 1:ecf8078bf531 221 remcontoken=0; /* we have executed the request, now clear the token */
andrewcrussell 1:ecf8078bf531 222 wait_us(20);
andrewcrussell 1:ecf8078bf531 223 gain= 31.5-(0.5*(255-volume));
andrewcrussell 1:ecf8078bf531 224 printf("%d\n\r",gain);
andrewcrussell 1:ecf8078bf531 225
andrewcrussell 1:ecf8078bf531 226 }
andrewcrussell 1:ecf8078bf531 227 /*************** Serial bus routine for control relays and digital I/O ***************/
andrewcrussell 1:ecf8078bf531 228 /* this routine takes the 8 bit relay data and the 8 bit control data and joins them */
andrewcrussell 1:ecf8078bf531 229 /* and writes 16 bits out on the serial bus */
andrewcrussell 1:ecf8078bf531 230 /* it is called by the relay function or the control function */
andrewcrussell 1:ecf8078bf531 231
andrewcrussell 1:ecf8078bf531 232 void serialout(void) {
andrewcrussell 1:ecf8078bf531 233 __disable_irq(); /* disable Interrupts - no interference while we send the data out */
andrewcrussell 1:ecf8078bf531 234
andrewcrussell 1:ecf8078bf531 235 unsigned int serialdata=0;/* the 16 bits of serial data to go out to the analog board - flush it so its clean */
andrewcrussell 1:ecf8078bf531 236 unsigned int shift_out_bit=0;
andrewcrussell 1:ecf8078bf531 237 long int stor_relay=0;
andrewcrussell 1:ecf8078bf531 238 int m=1;
andrewcrussell 1:ecf8078bf531 239 int SCOUNT=0;
andrewcrussell 1:ecf8078bf531 240
andrewcrussell 1:ecf8078bf531 241 stor_relay=(255-inputrelay); /* get the complement */
andrewcrussell 1:ecf8078bf531 242 serialdata = (control | stor_relay);
andrewcrussell 1:ecf8078bf531 243
andrewcrussell 1:ecf8078bf531 244 RD=LOW; /* ASTROBE - it must be LOW on the analog board intitially */
andrewcrussell 1:ecf8078bf531 245 WR=HIGH; /* MDATA */
andrewcrussell 1:ecf8078bf531 246 RS=HIGH; /* ACLK - so the clock line on the analog board is now LOW*/
andrewcrussell 1:ecf8078bf531 247 SBUSON=HIGH; /* turn the SBUS on */
andrewcrussell 1:ecf8078bf531 248 wait_us(100);
andrewcrussell 1:ecf8078bf531 249 do {
andrewcrussell 1:ecf8078bf531 250 shift_out_bit = (serialdata & m); /* ADATA = the LSB (k=1) */
andrewcrussell 1:ecf8078bf531 251 if (shift_out_bit!=0) { /* if it evaluates as TRUE, WR=HIGH */
andrewcrussell 1:ecf8078bf531 252 WR=1;
andrewcrussell 1:ecf8078bf531 253 } else { /* it must have evaluated as FALSE */
andrewcrussell 1:ecf8078bf531 254 WR=0;
andrewcrussell 1:ecf8078bf531 255 }
andrewcrussell 1:ecf8078bf531 256
andrewcrussell 1:ecf8078bf531 257 wait_us(100);
andrewcrussell 1:ecf8078bf531 258 RS=LOW; /* clock goes high on main board - data latches */
andrewcrussell 1:ecf8078bf531 259 wait_us(100);
andrewcrussell 1:ecf8078bf531 260 RS=HIGH; /* clock goes low */
andrewcrussell 1:ecf8078bf531 261 SCOUNT++; /* increment the bit counter */
andrewcrussell 1:ecf8078bf531 262 m=(m<<1); /* shift bit mask up one position towards the MSB */
andrewcrussell 1:ecf8078bf531 263
andrewcrussell 1:ecf8078bf531 264 } while (SCOUNT<16);
andrewcrussell 1:ecf8078bf531 265
andrewcrussell 1:ecf8078bf531 266 wait_us(100);
andrewcrussell 1:ecf8078bf531 267 RD=HIGH; /* Strobe the data into the A6821*/
andrewcrussell 1:ecf8078bf531 268 wait_us(100);
andrewcrussell 1:ecf8078bf531 269 RD=LOW; /* Strobe now goes LOW again on the main board */
andrewcrussell 1:ecf8078bf531 270 wait_us(100);
andrewcrussell 1:ecf8078bf531 271 SBUSON=LOW; /* remember to enable the outputs after intial set-up */
andrewcrussell 1:ecf8078bf531 272 wait_us(100);
andrewcrussell 1:ecf8078bf531 273 __enable_irq(); // Enable Interrupts
andrewcrussell 1:ecf8078bf531 274 }
andrewcrussell 1:ecf8078bf531 275
andrewcrussell 1:ecf8078bf531 276 /******************************* Play ****************************/
andrewcrussell 1:ecf8078bf531 277 void play(void) {
andrewcrussell 1:ecf8078bf531 278
andrewcrussell 1:ecf8078bf531 279 myled=!myled;
andrewcrussell 1:ecf8078bf531 280 flag=0;
andrewcrussell 1:ecf8078bf531 281 remcontoken=0;
andrewcrussell 1:ecf8078bf531 282 }
andrewcrussell 1:ecf8078bf531 283
andrewcrussell 1:ecf8078bf531 284 /******************************* Input Selector Encoder Routine ****************************/
andrewcrussell 1:ecf8078bf531 285 /* this function controls which relays are energized on the main analog board */
andrewcrussell 1:ecf8078bf531 286 /* It calls the serialout function to send the data */
andrewcrussell 1:ecf8078bf531 287
andrewcrussell 1:ecf8078bf531 288 void inputselect(void) {
andrewcrussell 1:ecf8078bf531 289
andrewcrussell 1:ecf8078bf531 290 int source=0; /* temporary storage */
andrewcrussell 1:ecf8078bf531 291 wait_ms(10); /*wait for contact bounce to subside */
andrewcrussell 1:ecf8078bf531 292 // __disable_irq(); // Disable Interrupts
andrewcrussell 1:ecf8078bf531 293
andrewcrussell 1:ecf8078bf531 294 if (remcontoken==0) { /* so it must have come in from the rotary encoder */
andrewcrussell 1:ecf8078bf531 295 PortIn select(Port2, 6); /* fetch data on mbed pins 24 and 25 - bit positions 2 and 4 on p2 */
andrewcrussell 1:ecf8078bf531 296 switch (select) {
andrewcrussell 1:ecf8078bf531 297 case 0:
andrewcrussell 1:ecf8078bf531 298 relay++;
andrewcrussell 1:ecf8078bf531 299 break; /* rotary encoder was turned clockwise */
andrewcrussell 1:ecf8078bf531 300 case 2:
andrewcrussell 1:ecf8078bf531 301 relay--;
andrewcrussell 1:ecf8078bf531 302 break; /* increment the selection */
andrewcrussell 1:ecf8078bf531 303
andrewcrussell 1:ecf8078bf531 304 case 4: {} break; /* fall through values - ignore */
andrewcrussell 1:ecf8078bf531 305 case 6: {} break;
andrewcrussell 1:ecf8078bf531 306 }
andrewcrussell 1:ecf8078bf531 307 } else {
andrewcrussell 1:ecf8078bf531 308 relay++; /* and here because it came in from the remote */
andrewcrussell 1:ecf8078bf531 309 } /* because it must have equaled 1 to have got here */
andrewcrussell 1:ecf8078bf531 310 /* because you can only go round select in one direction via the remote */
andrewcrussell 1:ecf8078bf531 311
andrewcrussell 1:ecf8078bf531 312 if (relay>=6) /* here we make sure the select knob rotates from 1-6 to again */
andrewcrussell 1:ecf8078bf531 313 relay=1;
andrewcrussell 1:ecf8078bf531 314 if (relay<=0)
andrewcrussell 1:ecf8078bf531 315 relay=5;
andrewcrussell 1:ecf8078bf531 316 /* set the correct value in the serial bit position */
andrewcrussell 1:ecf8078bf531 317 clear_input_select();
andrewcrussell 1:ecf8078bf531 318 switch (relay) {
andrewcrussell 1:ecf8078bf531 319 case 1:
andrewcrussell 1:ecf8078bf531 320 source=32;//~125;//~125;//~(2&127);
andrewcrussell 1:ecf8078bf531 321 AUX2(1);
andrewcrussell 1:ecf8078bf531 322 //printf("\n\rAux2");
andrewcrussell 1:ecf8078bf531 323 break; /* this is Aux2*/
andrewcrussell 1:ecf8078bf531 324 case 2:
andrewcrussell 1:ecf8078bf531 325 source=16;//~123;//~(4&127);
andrewcrussell 1:ecf8078bf531 326 AUX1(1);
andrewcrussell 1:ecf8078bf531 327 //printf("\n\raux1");
andrewcrussell 1:ecf8078bf531 328 break; /*aux1 */
andrewcrussell 1:ecf8078bf531 329 case 3:
andrewcrussell 1:ecf8078bf531 330 source=8;//~127;~(8&127);
andrewcrussell 1:ecf8078bf531 331 Recorder_d(1);
andrewcrussell 1:ecf8078bf531 332 //printf("\n\recorder");
andrewcrussell 1:ecf8078bf531 333
andrewcrussell 1:ecf8078bf531 334 break; /*recorder */
andrewcrussell 1:ecf8078bf531 335 case 4:
andrewcrussell 1:ecf8078bf531 336 source=4;//~111;//~(16&127);
andrewcrussell 1:ecf8078bf531 337 //printf("\n\rCD");
andrewcrussell 1:ecf8078bf531 338 //
andrewcrussell 1:ecf8078bf531 339 CD_d(1);
andrewcrussell 1:ecf8078bf531 340 break; /*CD*/
andrewcrussell 1:ecf8078bf531 341 case 5:
andrewcrussell 1:ecf8078bf531 342 source=2;;//~95;//~(32&127);
andrewcrussell 1:ecf8078bf531 343 //printf("\n\rphono");
andrewcrussell 1:ecf8078bf531 344 phono_d(1);
andrewcrussell 1:ecf8078bf531 345 break; /*phono*/
andrewcrussell 1:ecf8078bf531 346 }
andrewcrussell 1:ecf8078bf531 347
andrewcrussell 1:ecf8078bf531 348 inputrelay=(hpmutebit|mutebit|recloop1bit);/* save the non input select data */
andrewcrussell 1:ecf8078bf531 349
andrewcrussell 1:ecf8078bf531 350 inputrelay=(inputrelay&193); /*strip out the old input select, leaving HPMUTE, recloop1bit and mutebit intact */
andrewcrussell 1:ecf8078bf531 351
andrewcrussell 1:ecf8078bf531 352 inputrelay=(inputrelay|source); /* add in the new input selection back in*/
andrewcrussell 1:ecf8078bf531 353
andrewcrussell 1:ecf8078bf531 354 flag=0;
andrewcrussell 1:ecf8078bf531 355 remcontoken=0;
andrewcrussell 1:ecf8078bf531 356
andrewcrussell 1:ecf8078bf531 357 serialout(); /* send it out */
andrewcrussell 1:ecf8078bf531 358 //__enable_irq(); // Enable Interrupts
andrewcrussell 1:ecf8078bf531 359 }
andrewcrussell 1:ecf8078bf531 360 /**************************** output mute via front panel push button********************************/
andrewcrussell 1:ecf8078bf531 361
andrewcrussell 1:ecf8078bf531 362 void mute_output(void) { /* this is the interrupt handler routine */
andrewcrussell 1:ecf8078bf531 363
andrewcrussell 1:ecf8078bf531 364 wait_ms(50);
andrewcrussell 1:ecf8078bf531 365
andrewcrussell 1:ecf8078bf531 366 /* debounce time - CRITICAL - MORE THAN 10 OR LESS THAN 5 GIVES PROBLEMS! */
andrewcrussell 1:ecf8078bf531 367 if (mutebit==0) { /* it must have been off */
andrewcrussell 1:ecf8078bf531 368 mutebit=128; /* so turn it ON - this is a FLAG */
andrewcrussell 1:ecf8078bf531 369 inputrelay=(inputrelay|128);
andrewcrussell 1:ecf8078bf531 370 mute(0);
andrewcrussell 1:ecf8078bf531 371 }
andrewcrussell 1:ecf8078bf531 372
andrewcrussell 1:ecf8078bf531 373 else if (mutebit==128) { /* it currently ON */
andrewcrussell 1:ecf8078bf531 374 mutebit=0; /* so turn the output OFF - this is a FLAG */
andrewcrussell 1:ecf8078bf531 375 inputrelay=(inputrelay&127);
andrewcrussell 1:ecf8078bf531 376 mute(1);
andrewcrussell 1:ecf8078bf531 377 }
andrewcrussell 1:ecf8078bf531 378 flag=0;
andrewcrussell 1:ecf8078bf531 379 remcontoken=0;
andrewcrussell 1:ecf8078bf531 380 serialout();
andrewcrussell 1:ecf8078bf531 381
andrewcrussell 1:ecf8078bf531 382 }
andrewcrussell 1:ecf8078bf531 383 /**********************************initialize *****************************************/
andrewcrussell 1:ecf8078bf531 384 /* this routine sets up the pre-amp at initial power-up. all relays on the main board */
andrewcrussell 1:ecf8078bf531 385 /* are de-energized, volume is set to 0, display off, system in standby */
andrewcrussell 1:ecf8078bf531 386
andrewcrussell 1:ecf8078bf531 387 void initialize(void) {
andrewcrussell 1:ecf8078bf531 388
andrewcrussell 1:ecf8078bf531 389 int SCOUNT=0;
andrewcrussell 1:ecf8078bf531 390 int k=1; /*this is the bit place holder that gets shifted through the data to be sent */
andrewcrussell 1:ecf8078bf531 391 int flushit=0; /* flushit explicitly declared since we may want a setting other than all OFF */
andrewcrussell 1:ecf8078bf531 392 int shift_flush =0;
andrewcrussell 1:ecf8078bf531 393
andrewcrussell 1:ecf8078bf531 394 RD=LOW; /* ASTROBE */
andrewcrussell 1:ecf8078bf531 395 WR=LOW; /* MDATA */
andrewcrussell 1:ecf8078bf531 396 RS=LOW; /* ACLK */
andrewcrussell 1:ecf8078bf531 397 SBUSON=HIGH; /* turn the SBUS on */
andrewcrussell 1:ecf8078bf531 398 do {
andrewcrussell 1:ecf8078bf531 399 shift_flush=(flushit&k); /* ADATA = the LSB (k=1) */
andrewcrussell 1:ecf8078bf531 400 WR=shift_flush;
andrewcrussell 1:ecf8078bf531 401 wait_ms(1);
andrewcrussell 1:ecf8078bf531 402 RS=HIGH; /* clock it out */
andrewcrussell 1:ecf8078bf531 403 wait_ms(1);
andrewcrussell 1:ecf8078bf531 404 RS=LOW;
andrewcrussell 1:ecf8078bf531 405 SCOUNT=SCOUNT+1; /* increment the bit counter */
andrewcrussell 1:ecf8078bf531 406 k=(k<<1); /* shift bit mask up one position towards the MSB */
andrewcrussell 1:ecf8078bf531 407 } while (SCOUNT<8); /* counting starts from 0! */
andrewcrussell 1:ecf8078bf531 408 wait_ms(1);
andrewcrussell 1:ecf8078bf531 409 RD=HIGH; /* Strobe the data into the A6821*/
andrewcrussell 1:ecf8078bf531 410 wait_ms(1);
andrewcrussell 1:ecf8078bf531 411 RD=LOW; /* Strobe now goes HIGH again on the main board */
andrewcrussell 1:ecf8078bf531 412 wait_ms(1);
andrewcrussell 1:ecf8078bf531 413 SBUSON=LOW; /* remember to enable the outputs after intial set-up */
andrewcrussell 1:ecf8078bf531 414 power=0;
andrewcrussell 1:ecf8078bf531 415 wait_ms(100);
andrewcrussell 1:ecf8078bf531 416 }
andrewcrussell 1:ecf8078bf531 417
andrewcrussell 1:ecf8078bf531 418 /******************** ON-OFF Push button input *******************************************/
andrewcrussell 1:ecf8078bf531 419
andrewcrussell 1:ecf8078bf531 420 void power_on_off(void) {
andrewcrussell 1:ecf8078bf531 421 int inputstate = 0; /* where we temporarily store the input relay status */
andrewcrussell 1:ecf8078bf531 422 //__disable_irq(); // Disable Interrupts
andrewcrussell 1:ecf8078bf531 423 //__enable_irq(); // Enable Interrupts
andrewcrussell 1:ecf8078bf531 424 wait_ms(10); /* debounce time NOTE IF ITS MUCH LONGER THAN 10mS THERE ARE RE-ENTRY PROBLEMS*/
andrewcrussell 1:ecf8078bf531 425 if (power == 0) { /* it must have been off so turn it ON */
andrewcrussell 1:ecf8078bf531 426 //__disable_irq(); // Disable Interrupts
andrewcrussell 1:ecf8078bf531 427
andrewcrussell 1:ecf8078bf531 428 //Write_Command(DISON);
andrewcrussell 1:ecf8078bf531 429 //clear_to_color(BLACK); //clear the screen
andrewcrussell 1:ecf8078bf531 430 /* clean everything up first */
andrewcrussell 1:ecf8078bf531 431 inputrelay = 0; /* all input relays are DESELECTED */
andrewcrussell 1:ecf8078bf531 432 mute(1);
andrewcrussell 1:ecf8078bf531 433 control = 0;
andrewcrussell 1:ecf8078bf531 434 wait(1); /* initial wait period after power on - lets relays and electronics settle */
andrewcrussell 1:ecf8078bf531 435 serialout();
andrewcrussell 1:ecf8078bf531 436
andrewcrussell 1:ecf8078bf531 437 NAOE = HIGH; /* enable outputs of A6281's */
andrewcrussell 1:ecf8078bf531 438 wait(1);
andrewcrussell 1:ecf8078bf531 439 inputrelay = 4;//32; /* select CD input always on power up */
andrewcrussell 1:ecf8078bf531 440 CD_d(1);
andrewcrussell 1:ecf8078bf531 441 control = 33280; /* power to main board on and LCD brightness set to 50% */
andrewcrussell 1:ecf8078bf531 442 serialout();
andrewcrussell 1:ecf8078bf531 443
andrewcrussell 1:ecf8078bf531 444 wait(2);
andrewcrussell 1:ecf8078bf531 445 inputrelay = 5;// 161; /* unmute the headphone amp */
andrewcrussell 1:ecf8078bf531 446 control = 33280; /* stays the same power to main board on and LCD brightness set to 50% */
andrewcrussell 1:ecf8078bf531 447 serialout();
andrewcrussell 1:ecf8078bf531 448
andrewcrussell 1:ecf8078bf531 449 wait(2); /* let the power amp settle */
andrewcrussell 1:ecf8078bf531 450 control=35328; /* turn power amp on */
andrewcrussell 1:ecf8078bf531 451 inputrelay = 133; /* unmute the pre-amp output output*/
andrewcrussell 1:ecf8078bf531 452 mute(0);
andrewcrussell 1:ecf8078bf531 453 serialout();
andrewcrussell 1:ecf8078bf531 454
andrewcrussell 1:ecf8078bf531 455 mutebit = 128; /* set to 1 to turn it ON - THIS IS A FLAG!! */
andrewcrussell 1:ecf8078bf531 456 hpmutebit = 1; /* set to 127 to turn it ON - THIS IS THE FLAG!!*/
andrewcrussell 1:ecf8078bf531 457 //serialout();
andrewcrussell 1:ecf8078bf531 458 power = 64; /* this is the power ON/OFF FLAG */
andrewcrussell 1:ecf8078bf531 459 volume = 110; /* this sets tthe volume to 10/127 - so a very low level on power up */
andrewcrussell 1:ecf8078bf531 460 volumecontrol();
andrewcrussell 1:ecf8078bf531 461 /* re-enable the mute, volumecontrol and input selection interrupts */
andrewcrussell 1:ecf8078bf531 462 wait_ms(1);
andrewcrussell 1:ecf8078bf531 463
andrewcrussell 1:ecf8078bf531 464 mutesw.fall(&mute1);
andrewcrussell 1:ecf8078bf531 465 volumein.fall(&volume1);
andrewcrussell 1:ecf8078bf531 466 inputsel.fall(&inputsel1);
andrewcrussell 1:ecf8078bf531 467 // __enable_irq(); // Enable Interrupts
andrewcrussell 1:ecf8078bf531 468 }
andrewcrussell 1:ecf8078bf531 469
andrewcrussell 1:ecf8078bf531 470 else if (power == 64) { /* it currently ON, so turn it OFF */
andrewcrussell 1:ecf8078bf531 471 /* disable the mute, columecontrol and input selection interrupts - leave power ON/OFF enabled though */
andrewcrussell 1:ecf8078bf531 472 // __disable_irq(); // Disable Interrupts
andrewcrussell 1:ecf8078bf531 473 mutesw.fall(NULL);
andrewcrussell 1:ecf8078bf531 474 volumein.fall(NULL);
andrewcrussell 1:ecf8078bf531 475 inputsel.fall(NULL);
andrewcrussell 1:ecf8078bf531 476 control= (control & 62332); /*turn power amp off */
andrewcrussell 1:ecf8078bf531 477 serialout();
andrewcrussell 1:ecf8078bf531 478 wait(2); /* wait for the power-amp to power down and spkr relay to open */
andrewcrussell 1:ecf8078bf531 479 inputrelay = (inputrelay & 126); /* mute the pre-amp output */
andrewcrussell 1:ecf8078bf531 480 serialout(); /* note we did not change anything on the control side */
andrewcrussell 1:ecf8078bf531 481 wait(1);
andrewcrussell 1:ecf8078bf531 482 control=(control & 768); /* turn the display off and set the zero loop gain off */
andrewcrussell 1:ecf8078bf531 483 inputrelay = (inputrelay & 127); /* mute headphone output */
andrewcrussell 1:ecf8078bf531 484 serialout();
andrewcrussell 1:ecf8078bf531 485 wait(1);
andrewcrussell 1:ecf8078bf531 486 /*here we flush the A6821 registers */
andrewcrussell 1:ecf8078bf531 487 control = 0; /* turn everthing off */
andrewcrussell 1:ecf8078bf531 488 inputrelay = 0;
andrewcrussell 1:ecf8078bf531 489 serialout();
andrewcrussell 1:ecf8078bf531 490 wait(1);
andrewcrussell 1:ecf8078bf531 491 NAOE=LOW; /* disable the A6281's */
andrewcrussell 1:ecf8078bf531 492
andrewcrussell 1:ecf8078bf531 493 mutebit = 0; /* set to 1 to turn it ON - this is a FLAG */
andrewcrussell 1:ecf8078bf531 494 hpmutebit = 128; /* set to 127 to turn it ON - this is a FLAG */
andrewcrussell 1:ecf8078bf531 495 power = 0; /* this is the power FLAG */
andrewcrussell 1:ecf8078bf531 496 //Write_Command(DISOFF);
andrewcrussell 1:ecf8078bf531 497 //clear_to_color(BLACK); //clear the screen
andrewcrussell 1:ecf8078bf531 498
andrewcrussell 1:ecf8078bf531 499 //__enable_irq(); // Enable Interrupts again
andrewcrussell 1:ecf8078bf531 500 }
andrewcrussell 1:ecf8078bf531 501 flag = 0;
andrewcrussell 1:ecf8078bf531 502 remcontoken = 0;
andrewcrussell 1:ecf8078bf531 503 }
andrewcrussell 1:ecf8078bf531 504
andrewcrussell 1:ecf8078bf531 505 /******************** Test Mode Pushbutton input *******************************************/
andrewcrussell 1:ecf8078bf531 506 /* in this mode, both volume and select pushbuttons must be depressed for 5 seconds */
andrewcrussell 1:ecf8078bf531 507 /* the pre-amp then enters the test mode where all the relays are cycled continuosly */
andrewcrussell 1:ecf8078bf531 508 /* and the volume is ramped up and down. To exit, depress any push button. */
andrewcrussell 1:ecf8078bf531 509
andrewcrussell 1:ecf8078bf531 510 /**********************************main program*****************************************/
andrewcrussell 1:ecf8078bf531 511 int main () {
andrewcrussell 1:ecf8078bf531 512 int l=0;
andrewcrussell 1:ecf8078bf531 513
andrewcrussell 1:ecf8078bf531 514 __disable_irq(); // Disable Interrupts
andrewcrussell 1:ecf8078bf531 515 NAOE=LOW; // make sure the A6821's are disabled
andrewcrussell 1:ecf8078bf531 516 Init_SSD1963(); // set up he graphics controller
andrewcrussell 1:ecf8078bf531 517 Write_Command(DISOFF);
andrewcrussell 1:ecf8078bf531 518 clear_to_color(BLACK); //clear the screen
andrewcrussell 1:ecf8078bf531 519 wait_ms(5);
andrewcrussell 1:ecf8078bf531 520 initialize(); // call the SBUS routine to clean things up
andrewcrussell 1:ecf8078bf531 521 powersw.fall(&power1);
andrewcrussell 1:ecf8078bf531 522 Serial remote(NC, p27);
andrewcrussell 1:ecf8078bf531 523 remote.baud(1200);
andrewcrussell 1:ecf8078bf531 524 remote.format(8, Serial::None, 1);
andrewcrussell 1:ecf8078bf531 525 remote.attach(&remotecontrol); /* set up serial port via USB connector */
andrewcrussell 1:ecf8078bf531 526 __enable_irq(); // Enable Interrupts
andrewcrussell 1:ecf8078bf531 527 wait_us(10);
andrewcrussell 1:ecf8078bf531 528
andrewcrussell 1:ecf8078bf531 529 /**************************************************************************************/
andrewcrussell 1:ecf8078bf531 530 // graphics set up
andrewcrussell 1:ecf8078bf531 531 //clear_to_color(BLACK); //clear the screen
andrewcrussell 1:ecf8078bf531 532 // sett up graphics to write the data
andrewcrussell 1:ecf8078bf531 533 FontDrawInit();
andrewcrussell 1:ecf8078bf531 534 FontDraw_SetInterCharSpace( 0 );
andrewcrussell 1:ecf8078bf531 535 FontDraw_SetForegroundColor(WHITE);
andrewcrussell 1:ecf8078bf531 536 FontDraw_SetBackgroundColor(BLACK);
andrewcrussell 1:ecf8078bf531 537 FontDraw_SetFont(Calibri28);
andrewcrussell 1:ecf8078bf531 538 big_button();
andrewcrussell 1:ecf8078bf531 539 Control_buttons();
andrewcrussell 1:ecf8078bf531 540 input_buttons();
andrewcrussell 1:ecf8078bf531 541 slider_bar();
andrewcrussell 1:ecf8078bf531 542 Write_Command(DISON);
andrewcrussell 1:ecf8078bf531 543 //volume_slider(100); //bring the slider up, but set it to min vol
andrewcrussell 1:ecf8078bf531 544 //FontDrawString("-95dB",195,375,WHITE,BLACK,&Calibri72);
andrewcrussell 1:ecf8078bf531 545
andrewcrussell 1:ecf8078bf531 546 /************************ this is the main operating loop ************************/
andrewcrussell 1:ecf8078bf531 547
andrewcrussell 1:ecf8078bf531 548 LOOP:
andrewcrussell 1:ecf8078bf531 549
andrewcrussell 1:ecf8078bf531 550 if (flag!=0) {
andrewcrussell 1:ecf8078bf531 551 switch (flag) {
andrewcrussell 1:ecf8078bf531 552
andrewcrussell 1:ecf8078bf531 553 case 9211:
andrewcrussell 1:ecf8078bf531 554 volumecontrol();
andrewcrussell 1:ecf8078bf531 555 break; /* volume UP */
andrewcrussell 1:ecf8078bf531 556 case 9221:
andrewcrussell 1:ecf8078bf531 557 volumecontrol();
andrewcrussell 1:ecf8078bf531 558 break; /* volume DOWN */
andrewcrussell 1:ecf8078bf531 559 case 9231:
andrewcrussell 1:ecf8078bf531 560 inputselect();
andrewcrussell 1:ecf8078bf531 561 break; /* Input Select */
andrewcrussell 1:ecf8078bf531 562 case 9241:
andrewcrussell 1:ecf8078bf531 563 mute_output();
andrewcrussell 1:ecf8078bf531 564 break; /* Mute */
andrewcrussell 1:ecf8078bf531 565 case 9251:
andrewcrussell 1:ecf8078bf531 566 play();
andrewcrussell 1:ecf8078bf531 567 break; /* Play */
andrewcrussell 1:ecf8078bf531 568 case 9261:
andrewcrussell 1:ecf8078bf531 569 power_on_off();
andrewcrussell 1:ecf8078bf531 570 break; /* power ON/OFF */
andrewcrussell 1:ecf8078bf531 571 }
andrewcrussell 1:ecf8078bf531 572 }
andrewcrussell 1:ecf8078bf531 573 flag=0; /* flush the flag since we finished the task */
andrewcrussell 1:ecf8078bf531 574 wait_ms(10); /* we can only get an IRQ here */
andrewcrussell 1:ecf8078bf531 575
andrewcrussell 1:ecf8078bf531 576 //for (l=0;l<256;l++)
andrewcrussell 1:ecf8078bf531 577 //{volume=l;
andrewcrussell 1:ecf8078bf531 578 // volumecontrol();}
andrewcrussell 1:ecf8078bf531 579
andrewcrussell 1:ecf8078bf531 580 //halt(); /* shut precessor down and wait for interrupt */
andrewcrussell 1:ecf8078bf531 581 goto LOOP;
andrewcrussell 1:ecf8078bf531 582 }