Test for mike

Dependencies:   mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces

main.cpp

Committer:
kstokely
Date:
2019-02-04
Revision:
7:e649bb2b6187
Parent:
6:d1e569f370ac
Child:
8:abdd660056fb

File content as of revision 7:e649bb2b6187:

/** 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>

#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;

DigitalOut LDAC(p5);
DigitalOut CntPin(p6);



Serial pc(USBTX, USBRX);


//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);






int main(void) {
  
   // get the version of getVersion 
   getVersion gv;
   int addr;

  addr = 0;
   MCP4728  dac( i2cdev ,addr, Vdd);  // assuming the address  of the MCP4728 is set to 0 factory default 
   printf("\n\raddr %d MCP4728 :%s\n\r",addr,dac.getversioninfo());
   wait(1);

   int cnt=0;




pc.printf("\n\rEnter Voltage and Press Enter\n\r");
pc.printf("Agiltron Serial Control Program\n\r");
pc.printf("Left Square Bracket to control 4x4 Swtiching [\n\r");
pc.printf("Right Square Bracket to control 4x5 Swtiching ]\n\r");
pc.printf("1, 2, 3, 4 Control Shutters on 4x5 Module\n\r");
pc.printf("DAC control press Comma\n\r");

//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 );
    bool Shut1 = 0;
    bool Shut2 = 0;
    bool Shut3 = 0;
    bool Shut4 = 0;
    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 intial state
    m4x5TTL1 = 1;
    
    m4x4TTL1 = 1;
    float voltage = 0;
    string instr;

while(1) 
{
         char c = pc.getc();
        
        if( c == '1') //Toggle Shutter 1
        {
            Shut1 = !Shut1;
            SH.writeShutters(Shut1,Shut2,Shut3,Shut4);
        }
        
        else if( c == '2') //Toggle Shutter 2
        {
            Shut2 = !Shut2;
            SH.writeShutters(Shut1,Shut2,Shut3,Shut4);
        }
        
        else if( c == '3') //Toggle Shutter 3
        {
            Shut3 = !Shut3;
            SH.writeShutters(Shut1,Shut2,Shut3,Shut4);
        }
        
        else if( c == '4') //Toggle Shutter 4
        {
            Shut4 = !Shut4;
            SH.writeShutters(Shut1,Shut2,Shut3,Shut4);
        }
        
        
        else if( c == 91 ) //Left square bracket [
        {
            //Get 5 1's or 0's for binary number
            int count = 0;
            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++;
                }
            }
            
            for( int k = 0; k<5; k++) //Trigger clocking to change values
            {
                m4x4CLK = 1;
                wait(0.1); 
                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
        }
        
        
        
        else if( c == 93 ) //Right square bracket ]
        {
            //Get 5 1's or 0's for binary number
            int count = 0;
            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++;
                }
            }
            
            for( int k = 0; k<5; k++) //Trigger clocking to change values
            {
                m4x5CLK = 1;
                wait(0.1); 
                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
        }
        
else if( c == ',' )        
{
        pc.printf("4 Channel DAC Control Enter voltage and Press Enter\n\r");
        int ret = 0;
    while( ret == 0 )
    {
        char z = pc.getc();
        
        if( z == 13 )
        {
            voltage = atof( instr.c_str() );
            
            if( voltage == 0 || voltage > 5.0 )
            {
                pc.printf("ERROR\n\r");
            }
            else
            {
                for ( int cc =0 ; cc <4 ; cc++) { 
                    if ( dac.setVoltage(voltage,cc) )
                        printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
                    }
                //pc.printf("Voltage set to %3fV\n\r", voltage);
                 if(dac.update()) printf("\n\rfailed to readback channel info \n\r");
            else {
                for ( int cc =0 ; cc <4 ; cc++) { 
                 (void)dac.getVoltage(voltage,cc);//no need to test done with updat 
                  printf(" CH%d %f[V]",cc,voltage);
                }
                printf("\n\r");
                
            }
            }
            
            instr.clear();
            wait_ms(1200);
            ret = 1;
            
        }
        
        else if( z == ']')
        {
             for ( int cc =0 ; cc <4 ; cc++) { 
               if ( dac.setDACvalue(4095,cc) )
                   printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
                }
                ret = 1;
        }
        
        else
        {
            pc.printf("%c",z);
            instr.push_back(z);
        }
    }
    }

    
    // never reach this   
}

 return 1;
}