Example controlling galvanomirrors (X and Y) using the spi DAC MCP4922 and the new platform FRDM_K64F

Dependencies:   mbed

main.cpp

Committer:
mbedalvaro
Date:
2014-05-25
Revision:
1:4be6abc4ed43
Parent:
0:26b228dde70c
Child:
2:383b2acec6e4

File content as of revision 1:4be6abc4ed43:

#include "mbed.h"
#include "laserProjectorHardware.h"

DigitalOut myled(LED3);

void processSerial();

unsigned int X, Y; // position of the mirrors (note: in fact, azimuth and elevation)
bool newPositionReady=false;

int main() {

    // SETUP: --------------------------------------------------------------------------------------------
    IO.init(); // note: serial speed can be changed by checking in the hardwareIO.cpp initialization
  
    // Set displaying laser powers: 
    IO.setRedPower(1);//turn on the red (displaying) laser
    IO.setGreenPower(0);
    IO.setBluePower(0);

    wait_ms(100);

    Y = CENTER_AD_MIRROR_X;
    X = CENTER_AD_MIRROR_Y;
    IO.writeOutXY(X,Y);
    
    // MAIN LOOP: --------------------------------------------------------------------------------------------
    while(1) { 
    
   // myled=0;IO.setRedPower(1); 
   // wait_ms(1000); 
   //  myled=1;IO.setRedPower(0);
   //  wait_ms(1000); 
   
        processSerial();
        if (newPositionReady) {
            IO.writeOutXY(X,Y);
            newPositionReady=false;
        }
    }
}

// --------------------------------------------------------------------------------------------
// String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly: 
char stringData[24]; // note: an integer is two bytes long, represented with a maximum of 5 digits, but we may send floats or unsigned int...
int indexStringData=0;//position of the byte in the string

void processSerial() {

 while(pc.readable()>0) {
   
      
    char val =pc.getc();
  
  // Save ASCII numeric characters (ASCII 0 - 9) on stringData:
    if ((val >= '0') && (val <= '9')){ // this is 45 to 57 (included)
      stringData[indexStringData] = val;
      indexStringData++;
            
    }
    
  // X value?
  else if (val=='X') {
    stringData[indexStringData] = 0 ;
    X=atoi(stringData);
    indexStringData=0;
  }
   
  // Y value?
  else if (val=='Y') {
    stringData[indexStringData] = 0 ;
    Y=atoi(stringData);
    indexStringData=0;
    newPositionReady=true;
   }
   
    else if (val=='L') {     
    stringData[indexStringData] = 0 ;
    if(atoi(stringData)>0)  IO.setRedPower(1); else  IO.setRedPower(0);
    indexStringData=0;
  }
 
 }
}