Test for mike
Dependencies: mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces
main.cpp
- Committer:
- kstokely
- Date:
- 2019-05-20
- Revision:
- 10:bf195171d850
- Parent:
- 9:2d70aa45f16e
- Child:
- 11:4367b8eefa6c
File content as of revision 10:bf195171d850:
/** example program for the use of the MCP4728 class and DACInterface.h classes * * V 1.0 : tested on the KL05z but without the MCP4728 connected \ * (C) Wim Beaumont Universiteit Antwerpen 2016 */ #define MCP4728EXAMPLEVER "2.04" #define TARGET_MBED 1 #include "mbed.h" #include <string> #include <cstring> #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) PinName const SDA = PTE25; PinName const SCL = PTE24; #elif defined (TARGET_KL05Z) PinName const SDA = PTB4; PinName const SCL = PTB3; #elif defined (TARGET_K20D50M) PinName const SDA = PTB1; PinName const SCL = PTB0; #elif defined (TARGET_MBED) PinName const SDA = p28; PinName const SCL = p27; #else #error TARGET NOT DEFINED #endif #include "I2C.h" #include "I2CInterface.h" #include "MBEDI2CInterface.h" #include "DACInterface.h" #include "dev_interface_def.h" #include "MCP4728setaddr.h" #include "mcp4728.h" #include "shutter.h" MBEDI2CInterface mbedi2c( SDA, SCL); MBEDI2CInterface* mbedi2cp= &mbedi2c ; I2CInterface* i2cdev= mbedi2cp; const float Vdd = 4.97; Serial pc(USBTX, USBRX); DigitalOut CameraTrig(p29); //Pins for 4x4 Module DigitalOut m4x4TTL1(p21); DigitalOut m4x4TTL2(p22); DigitalOut m4x4TTL3(p23); DigitalOut m4x4TTL4(p24); DigitalOut m4x4TTL5(p25); DigitalOut m4x4CLK(p26); //CLk for triggering after new input //Pins for 4x5 Module DigitalOut m4x5TTL1(p7); DigitalOut m4x5TTL2(p8); DigitalOut m4x5TTL3(p9); DigitalOut m4x5TTL4(p10); DigitalOut m4x5TTL5(p11); //Shutter Pins DigitalOut SH1_1(p12); DigitalOut SH1_2(p13); DigitalOut SH2_1(p14); DigitalOut SH2_2(p15); DigitalOut SH3_1(p16); DigitalOut SH3_2(p17); DigitalOut SH4_1(p18); DigitalOut SH4_2(p19); DigitalOut m4x5CLK(p20); DigitalOut mRefShutters(p5); bool Shut1 = 0; bool Shut2 = 0; bool Shut3 = 0; bool Shut4 = 0; string pcstr; string seqstr; Timer t; void trigSH( Shutter SH, int nimg ) { for(int i = 0; i<nimg; i++) { SH.writeShutters(1,1,1,1);//Turn on Shutters wait_us(92);//Wait for shutter to go high wait_us(120);//Wait to trigger until middle of Shutter Opening CameraTrig = 1;//Trigger Camera to turn on wait_us(38);//Wait for rest of 250 us //wait_us(16417); //DEBUG: Added extra delay to slow down trigger //Turn off shutters and Camera Trigger SH.writeShutters(0,0,0,0); CameraTrig = 0; wait_us(16417); //Wait to run at 60 Hz //wait_us(16417); //DEBUG: Added extra delay to slow down trigger } } void trigNotEyeSafe( Shutter SH, int nimg ) { for(int i = 0; i<nimg; i++) { //SH.writeShutters(1,1,1,1);//Turn on Shutters wait_us(92);//Wait for shutter to go high wait_us(120);//Wait to trigger until middle of Shutter Opening CameraTrig = 1;//Trigger Camera to turn on wait_us(38);//Wait for rest of 250 us //wait_us(16417);//DEBUG: Added extra delay to slow down trigger //Turn off shutters and Camera Trigger //SH.writeShutters(0,0,0,0); CameraTrig = 0; wait_us(16417); //Wait to run at 60 Hz // wait_us(16417);//DEBUG: Added extra delay to slow down trigger } } int DACSetChannel( int chan, MCP4728* dac, int counts) { int ret = 0; int z = 0; int CHcnt; int voltCount = counts; string instr; if( voltCount > 4095 || voltCount < 0) { voltCount = 4095; } while(ret == 0 && z < 10) { //t.start(); dac->update(); //t.stop(); //t.reset(); //pc.printf("DAC update took %f seconds \n\r",t.read()); (void)dac->getDACvalue(CHcnt,chan); if(CHcnt == counts) { ret = 1; } else { //t.start(); dac->setDACvalue(voltCount,chan); //t.stop(); //pc.printf("DAC setting took %f seconds \n\r",t.read()); z++; } } /* if( z == 13 ) { voltCount = counts; if( voltCount > 4095 || voltCount < 0) { //////pc.printf("\n\r Voltage Entered Higher than Vdd Setting Voltage to Maximum\n\r"); dac->setDACvalue(4095,chan); } else { if ( dac->setDACvalue(voltCount,chan) ) { //printf("failed to set dac value %f for channel %d\n\r",voltCount,chan); } ////////pc.printf("Voltage set to %3fV\n\r", voltage); if( dac->update() ) { //printf("\n\rfailed to readback channel info \n\r"); } } } */ return 1; } void set4x4Piping(DigitalOut** Arr_4x4, char z1, char z2, char z3, char z4, char z5) { /* //Get 5 1's or 0's for binary number int count = 0; char c; ////pc.printf("4x4 Control: Enter 5 digit binary number\n\r"); while ( count < 5 ) { c = pc.getc(); if( c == '1' || c=='0' ) { *Arr_4x4[count] = atoi(&c); count++; } } */ *Arr_4x4[0] = atoi(&z1); *Arr_4x4[1] = atoi(&z2); *Arr_4x4[2] = atoi(&z3); *Arr_4x4[3] = atoi(&z4); *Arr_4x4[4] = atoi(&z5); for( int k = 0; k<2; k++) //Trigger clocking to change values { m4x4CLK = 1; wait_us(200); m4x4CLK = 0; } //pc.printf("Optical Path set to : %d%d%d%d%d\n\r", m4x4TTL1.read(), m4x4TTL2.read(), m4x4TTL3.read(), m4x4TTL4.read(), m4x4TTL5.read() ); //Confirm the correct output is set } void set4x5Piping(DigitalOut** Arr_4x5, char z1, char z2, char z3, char z4, char z5) { /* //Get 5 1's or 0's for binary number int count = 0; char c; ////pc.printf("4x5 Control: Enter 5 digit binary number\n\r"); while ( count < 5 ) { c = pc.getc(); if( c == '1' || c=='0' ) { *Arr_4x5[count] = atoi(&c); count++; } } */ *Arr_4x5[0] = atoi(&z1); *Arr_4x5[1] = atoi(&z2); *Arr_4x5[2] = atoi(&z3); *Arr_4x5[3] = atoi(&z4); *Arr_4x5[4] = atoi(&z5); for( int k = 0; k<2; k++) //Trigger clocking to change values { m4x5CLK = 1; wait_us(200); m4x5CLK = 0; } //pc.printf("Optical Path set to : %d%d%d%d%d\n\r", m4x5TTL1.read(), m4x5TTL2.read(), m4x5TTL3.read(), m4x5TTL4.read(), m4x5TTL5.read() ); //Confirm the correct output is set } void toggleShut1(Shutter SH) { Shut1 = !Shut1; SH.writeShutters(Shut1,Shut2,Shut3,Shut4); } void toggleShut2(Shutter SH) { Shut2 = !Shut2; SH.writeShutters(Shut1,Shut2,Shut3,Shut4); } void toggleShut3(Shutter SH) { Shut3 = !Shut3; SH.writeShutters(Shut1,Shut2,Shut3,Shut4); } void toggleShut4(Shutter SH) { Shut4 = !Shut4; SH.writeShutters(Shut1,Shut2,Shut3,Shut4); } void closeRef() { mRefShutters = 0; } void openRef() { mRefShutters = 1; } void openAllHP(Shutter SH) { Shut1 = 1; Shut2 = 1; Shut3 = 1; Shut4 = 1; SH.writeShutters(1,1,1,1); } void closeAllHP(Shutter SH) { Shut1 = 0; Shut2 = 0; Shut3 = 0; Shut4 = 0; SH.writeShutters(0,0,0,0); } void setAllShutters(Shutter SH,char z1, char z2, char z3, char z4) { /* //Writes to all 4x5 shutters a specific state int inarr[4]; for(int i=0; i<4; i++) { char z = pc.getc(); if( z == '1') { inarr[i] = 1; } else if( z == '0') { inarr[i] = 0; } else { inarr[i] = 0; } } */ Shut1 = atoi(&z1); Shut2 = atoi(&z2); Shut3 = atoi(&z3); Shut4 = atoi(&z4); SH.writeShutters(Shut1,Shut2,Shut3,Shut4); } void queryDAC(MCP4728* dac) { int CH1cnt; int CH2cnt; int CH3cnt; int CH4cnt; //Print State of DAC if( dac->update() == 0) { (void)dac->getDACvalue(CH1cnt,0); (void)dac->getDACvalue(CH2cnt,1); (void)dac->getDACvalue(CH3cnt,2); (void)dac->getDACvalue(CH4cnt,3); pc.printf("CH1:%4d CH2:%4d CH3:%4d CH4:%4d\n\r",CH1cnt,CH2cnt,CH3cnt,CH4cnt); } else { pc.printf("Error: No readback from DAC\n\r"); } } void queryShutters() { //Print State of Shutters pc.printf("SH1:%d SH2:%d SH3:%d SH4:%d SHRef:%d\n\r",Shut1,Shut2,Shut3,Shut4,mRefShutters.read()); } int main(void) { //get the version of getVersion getVersion gv; int addr; //Timer m; addr = 0; MCP4728 dac( i2cdev ,addr, Vdd); // assuming the address of the MCP4728 is set to 0 factory default wait(1); int cnt=0; pc.baud(921600); mRefShutters = 1; //Set up dac for external voltage dac.setRefExtern(); dac.update(); //Intialize shutters Shutter SH; SH.init( &SH1_1, &SH1_2, &SH2_1, &SH2_2, &SH3_1, &SH3_2, &SH4_1, &SH4_2 ); SH.writeShutters(Shut1,Shut2,Shut3,Shut4); //TTL pin arrays DigitalOut* Arr_4x4[5] = {&m4x4TTL1, &m4x4TTL2, &m4x4TTL3, &m4x4TTL4, &m4x4TTL5 }; DigitalOut* Arr_4x5[5] = {&m4x5TTL1, &m4x5TTL2, &m4x5TTL3, &m4x5TTL4, &m4x5TTL5 }; //Set straight pipes initial state m4x5TTL1 = 1; m4x4TTL1 = 1; float voltage = 0; int voltCount = 0; string instr; seqstr = "d010c010r010i010"; pcstr.resize(200); while(1) { //m.stop(); //pc.printf("Main loop took %f seconds \n\r",m.read()); pc.scanf("%s", pcstr.c_str() ); //m.reset(); //m.start(); //DEBUG //pc.printf( "String Entered: %s String Size:%d\n\r",pcstr.c_str(),pcstr.size() ); int indexArr[100]; int indexSize = 0; //Find all commas in string /* for(int i = 0; i < pcstr.size(); i++) { if( pcstr[i] == ',') { indexArr[indexSize] = i; indexSize++; } } */ //had to change as string does not resize int i =0; while( pcstr.c_str()[i] != 0 && pcstr.c_str()[i] != ' ' && pcstr.c_str()[i] != '\0') { if( pcstr[i] == ',') { indexArr[indexSize] = i; indexSize++; } i++; } //DEBUG //pc.printf( "Commas Found %d\n\r",indexSize ); //Interpret commands for(int i =0; i<indexSize; i++) { //DEBUG //pc.printf( "Loop #%d of %d\n\r",i,indexSize-1 ); char c; //command must begin with a comma c = pcstr.c_str()[ indexArr[i] +1 ]; //DEBUG //pc.printf( "Cmd is %c\n\r",c ); if( c == '1') //Toggle Shutter 1 { char z1 = pcstr[ indexArr[i] +2 ]; Shut1 = atoi(&z1); SH.writeShutters(Shut1,Shut2,Shut3,Shut4); } else if( c == '2') //Toggle Shutter 2 { char z1 = pcstr[ indexArr[i] +2 ]; Shut2 = atoi(&z1); SH.writeShutters(Shut1,Shut2,Shut3,Shut4); } else if( c == '3') //Toggle Shutter 3 { char z1 = pcstr[ indexArr[i] +2 ]; Shut3 = atoi(&z1); SH.writeShutters(Shut1,Shut2,Shut3,Shut4); } else if( c == '4') //Toggle Shutter 4 { char z1 = pcstr[ indexArr[i] +2 ]; Shut4 = atoi(&z1); SH.writeShutters(Shut1,Shut2,Shut3,Shut4); } else if( c == 'u') { closeRef(); } else if( c == 'y') { openRef(); } else if(c == 'o') //Open All Shutters { openAllHP(SH); } else if(c == 'r') //Close All Shutters { closeAllHP(SH); } else if( c == 'm') { //string sub = pcstr.substr( indexArr[i]+2 , 4 );//Used for DAC char z1 = pcstr[ indexArr[i] +2 ]; char z2 = pcstr[ indexArr[i] +3 ]; char z3 = pcstr[ indexArr[i] +4 ]; char z4 = pcstr[ indexArr[i] +5 ]; setAllShutters(SH,z1,z2,z3,z4); } else if( c == 91 ) //Left square bracket [ 4x4 Control { //get inputs for 4x4 char z1 = pcstr[ indexArr[i] +2 ]; char z2 = pcstr[ indexArr[i] +3 ]; char z3 = pcstr[ indexArr[i] +4 ]; char z4 = pcstr[ indexArr[i] +5 ]; char z5 = pcstr[ indexArr[i] +6 ]; set4x4Piping(Arr_4x4,z1,z2,z3,z4,z5); } else if( c == 93 ) //Right square bracket ] 4x5 Control { char z1 = pcstr[ indexArr[i] +2 ]; char z2 = pcstr[ indexArr[i] +3 ]; char z3 = pcstr[ indexArr[i] +4 ]; char z4 = pcstr[ indexArr[i] +5 ]; char z5 = pcstr[ indexArr[i] +6 ]; set4x5Piping(Arr_4x5,z1,z2,z3,z4,z5); } else if( c == 'd') { queryDAC(&dac); } else if( c == 'a') { queryShutters(); } else if( c == '.' ) //Single Channel DAC Control { ////pc.printf("Single Channel DAC Control Press Channel Number\n\r"); char achan = pcstr[ indexArr[i] + 2 ]; int ichan = atoi(&achan); char acount[5]; acount[0] = pcstr[ indexArr[i] + 3 ]; acount[1] = pcstr[ indexArr[i] + 4 ]; acount[2] = pcstr[ indexArr[i] + 5 ]; acount[3] = pcstr[ indexArr[i] + 6 ]; acount[4] = '\0'; int icount = atoi(acount);//Get 4 digit Count //icount = icount / 10; //DEBUG //pc.printf("DAC Channel: %d DAC Counts: %d Acount:\n\r",ichan,icount); if( achan == '1' || achan== '2' || achan == '3' || achan == '0' ) { //////////pc.printf("Channel %c Selected",z); DACSetChannel( ichan , &dac, icount); } else { ////pc.printf("ERROR"); } } else if( c == 't') { /* //Contours Hologran DACSetChannel( 0 , &dac, 0); DACSetChannel( 1 , &dac, 0); DACSetChannel( 2 , &dac, 0); DACSetChannel( 3 , &dac, 0); wait_ms(100); trigSH( SH, 1 ); wait_ms(100); //Illums DACSetChannel( 0 , &dac, 4095); DACSetChannel( 1 , &dac, 4095); DACSetChannel( 2 , &dac, 4095); DACSetChannel( 3 , &dac, 4095); SH.writeShutters(1,1,1,1); wait_ms(100); trigSH( SH, 10 ); wait_ms(100); //Reference DACSetChannel( 0 , &dac, 0); DACSetChannel( 1 , &dac, 0); DACSetChannel( 2 , &dac, 0); DACSetChannel( 3 , &dac, 0); SH.writeShutters(0,0,0,0); wait_ms(100); trigNotEyeSafe( SH, 10 ); wait_ms(100); //Darks SH.writeShutters(0,0,0,0); DACSetChannel( 0 , &dac, 4095); DACSetChannel( 1 , &dac, 4095); DACSetChannel( 2 , &dac, 4095); DACSetChannel( 3 , &dac, 4095); wait_ms(100); trigNotEyeSafe( SH, 10 ); wait_ms(100); */ int i; int nimgs = 0; for(i = 0; i<seqstr.size(); i++) { //Find characters and then grab next three digits as the number of times to trigger if( seqstr[i] == 'i') { nimgs = atoi( seqstr.substr(i+1, i+3).c_str() ); DACSetChannel( 0 , &dac, 4095); DACSetChannel( 1 , &dac, 4095); DACSetChannel( 2 , &dac, 4095); DACSetChannel( 3 , &dac, 4095); trigSH( SH, nimgs ); //pc.printf("Num Illums: %d\n",nimgs); } else if( seqstr[i] == 'd') { nimgs = atoi( seqstr.substr(i+1, i+3).c_str() ); SH.writeShutters(0,0,0,0); DACSetChannel( 0 , &dac, 4095); DACSetChannel( 1 , &dac, 4095); DACSetChannel( 2 , &dac, 4095); DACSetChannel( 3 , &dac, 4095); trigNotEyeSafe( SH, nimgs ); //pc.printf("Num Darks: %d\n",nimgs); } else if( seqstr[i] == 'c') { nimgs = atoi( seqstr.substr(i+1, i+3).c_str() ); DACSetChannel( 0 , &dac, 0); DACSetChannel( 1 , &dac, 0); DACSetChannel( 2 , &dac, 0); DACSetChannel( 3 , &dac, 0); trigSH( SH, nimgs ); //pc.printf("Num Contours: %d\n",nimgs); } else if( seqstr[i] == 'r') { nimgs = atoi( seqstr.substr(i+1, i+3).c_str() ); DACSetChannel( 0 , &dac, 0); DACSetChannel( 1 , &dac, 0); DACSetChannel( 2 , &dac, 0); DACSetChannel( 3 , &dac, 0); SH.writeShutters(0,0,0,0); trigNotEyeSafe( SH, nimgs ); //pc.printf("Num Refs: %d\n",nimgs); } } } else if( c == 's' ) { seqstr = pcstr.substr( indexArr[i] +2, indexArr[i+1] -1 ); pc.printf("String is %s \n",seqstr.c_str()); } else if( c == ';') { //Dumb states of everything out //All on one line //Shutters pc.printf("SH1:%d SH2:%d SH3:%d SH4:%d SHRef:%d",Shut1,Shut2,Shut3,Shut4,mRefShutters.read()); //DAC int CH1cnt; int CH2cnt; int CH3cnt; int CH4cnt; //Print State of DAC int sts = dac.update(); int updatecnt = 0; while( sts != 0 && updatecnt < 5) { if(sts != 0) { sts = dac.update(); updatecnt++; } } if( updatecnt >= 5) { pc.printf("CH1:xxxx CH2:xxxx CH3:xxxx CH4:xxxx"); } else { (void)dac.getDACvalue(CH1cnt,0); (void)dac.getDACvalue(CH2cnt,1); (void)dac.getDACvalue(CH3cnt,2); (void)dac.getDACvalue(CH4cnt,3); pc.printf("CH1:%4d CH2:%4d CH3:%4d CH4:%4d",CH1cnt,CH2cnt,CH3cnt,CH4cnt); } //Piping pc.printf("4x4:%d%d%d%d%d", m4x4TTL1.read(), m4x4TTL2.read(), m4x4TTL3.read(), m4x4TTL4.read(), m4x4TTL5.read() ); pc.printf("4x5:%d%d%d%d%d\n", m4x5TTL1.read(), m4x5TTL2.read(), m4x5TTL3.read(), m4x5TTL4.read(), m4x5TTL5.read() ); } else if( c == '+' ) { pc.printf(",[%d%d%d%d%d,]%d%d%d%d%d", m4x4TTL1.read(), m4x4TTL2.read(), m4x4TTL3.read(), m4x4TTL4.read(), m4x4TTL5.read(), m4x5TTL1.read(), m4x5TTL2.read(), m4x5TTL3.read(), m4x5TTL4.read(), m4x5TTL5.read() ); pc.printf(",m%d%d%d%d",Shut1,Shut2,Shut3,Shut4); int CH1cnt; int CH2cnt; int CH3cnt; int CH4cnt; int sts = dac.update(); int updatecnt = 0; while( sts != 0 && updatecnt < 5) { if(sts != 0) { sts = dac.update(); updatecnt++; } } if( updatecnt >= 5 && sts != 0) { pc.printf(",.0xxxx,.1xxxx,.2xxxx,.3xxxx\n"); } else { (void)dac.getDACvalue(CH1cnt,0); (void)dac.getDACvalue(CH2cnt,1); (void)dac.getDACvalue(CH3cnt,2); (void)dac.getDACvalue(CH4cnt,3); pc.printf(",.0%04d,.1%04d,.2%04d,.3%04d\n",CH1cnt,CH2cnt,CH3cnt,CH4cnt); } } } } //Never Reach This return 1; }