![](/media/cache/profiles/68a15b5278e4f7c4c056df9d5f1d3b1f.jpg.50x50_q85.jpg)
Example controlling galvanomirrors (X and Y) using the spi DAC MCP4922 and the new platform FRDM_K64F
main.cpp@1:4be6abc4ed43, 2014-05-25 (annotated)
- Committer:
- mbedalvaro
- Date:
- Sun May 25 12:24:21 2014 +0000
- Revision:
- 1:4be6abc4ed43
- Parent:
- 0:26b228dde70c
- Child:
- 2:383b2acec6e4
this works fine, using the laserMouse_Processing controller; Note: for some reason I had to change spiDAC clock to 20MHz...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedalvaro | 0:26b228dde70c | 1 | #include "mbed.h" |
mbedalvaro | 0:26b228dde70c | 2 | #include "laserProjectorHardware.h" |
mbedalvaro | 0:26b228dde70c | 3 | |
mbedalvaro | 0:26b228dde70c | 4 | DigitalOut myled(LED3); |
mbedalvaro | 0:26b228dde70c | 5 | |
mbedalvaro | 0:26b228dde70c | 6 | void processSerial(); |
mbedalvaro | 0:26b228dde70c | 7 | |
mbedalvaro | 0:26b228dde70c | 8 | unsigned int X, Y; // position of the mirrors (note: in fact, azimuth and elevation) |
mbedalvaro | 0:26b228dde70c | 9 | bool newPositionReady=false; |
mbedalvaro | 0:26b228dde70c | 10 | |
mbedalvaro | 0:26b228dde70c | 11 | int main() { |
mbedalvaro | 0:26b228dde70c | 12 | |
mbedalvaro | 0:26b228dde70c | 13 | // SETUP: -------------------------------------------------------------------------------------------- |
mbedalvaro | 0:26b228dde70c | 14 | IO.init(); // note: serial speed can be changed by checking in the hardwareIO.cpp initialization |
mbedalvaro | 0:26b228dde70c | 15 | |
mbedalvaro | 0:26b228dde70c | 16 | // Set displaying laser powers: |
mbedalvaro | 0:26b228dde70c | 17 | IO.setRedPower(1);//turn on the red (displaying) laser |
mbedalvaro | 0:26b228dde70c | 18 | IO.setGreenPower(0); |
mbedalvaro | 0:26b228dde70c | 19 | IO.setBluePower(0); |
mbedalvaro | 0:26b228dde70c | 20 | |
mbedalvaro | 0:26b228dde70c | 21 | wait_ms(100); |
mbedalvaro | 0:26b228dde70c | 22 | |
mbedalvaro | 0:26b228dde70c | 23 | Y = CENTER_AD_MIRROR_X; |
mbedalvaro | 0:26b228dde70c | 24 | X = CENTER_AD_MIRROR_Y; |
mbedalvaro | 0:26b228dde70c | 25 | IO.writeOutXY(X,Y); |
mbedalvaro | 0:26b228dde70c | 26 | |
mbedalvaro | 0:26b228dde70c | 27 | // MAIN LOOP: -------------------------------------------------------------------------------------------- |
mbedalvaro | 1:4be6abc4ed43 | 28 | while(1) { |
mbedalvaro | 1:4be6abc4ed43 | 29 | |
mbedalvaro | 1:4be6abc4ed43 | 30 | // myled=0;IO.setRedPower(1); |
mbedalvaro | 1:4be6abc4ed43 | 31 | // wait_ms(1000); |
mbedalvaro | 1:4be6abc4ed43 | 32 | // myled=1;IO.setRedPower(0); |
mbedalvaro | 1:4be6abc4ed43 | 33 | // wait_ms(1000); |
mbedalvaro | 1:4be6abc4ed43 | 34 | |
mbedalvaro | 0:26b228dde70c | 35 | processSerial(); |
mbedalvaro | 0:26b228dde70c | 36 | if (newPositionReady) { |
mbedalvaro | 0:26b228dde70c | 37 | IO.writeOutXY(X,Y); |
mbedalvaro | 0:26b228dde70c | 38 | newPositionReady=false; |
mbedalvaro | 0:26b228dde70c | 39 | } |
mbedalvaro | 0:26b228dde70c | 40 | } |
mbedalvaro | 0:26b228dde70c | 41 | } |
mbedalvaro | 0:26b228dde70c | 42 | |
mbedalvaro | 0:26b228dde70c | 43 | // -------------------------------------------------------------------------------------------- |
mbedalvaro | 0:26b228dde70c | 44 | // String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly: |
mbedalvaro | 0:26b228dde70c | 45 | 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... |
mbedalvaro | 0:26b228dde70c | 46 | int indexStringData=0;//position of the byte in the string |
mbedalvaro | 0:26b228dde70c | 47 | |
mbedalvaro | 0:26b228dde70c | 48 | void processSerial() { |
mbedalvaro | 1:4be6abc4ed43 | 49 | |
mbedalvaro | 1:4be6abc4ed43 | 50 | while(pc.readable()>0) { |
mbedalvaro | 1:4be6abc4ed43 | 51 | |
mbedalvaro | 0:26b228dde70c | 52 | |
mbedalvaro | 0:26b228dde70c | 53 | char val =pc.getc(); |
mbedalvaro | 0:26b228dde70c | 54 | |
mbedalvaro | 0:26b228dde70c | 55 | // Save ASCII numeric characters (ASCII 0 - 9) on stringData: |
mbedalvaro | 0:26b228dde70c | 56 | if ((val >= '0') && (val <= '9')){ // this is 45 to 57 (included) |
mbedalvaro | 0:26b228dde70c | 57 | stringData[indexStringData] = val; |
mbedalvaro | 0:26b228dde70c | 58 | indexStringData++; |
mbedalvaro | 1:4be6abc4ed43 | 59 | |
mbedalvaro | 0:26b228dde70c | 60 | } |
mbedalvaro | 0:26b228dde70c | 61 | |
mbedalvaro | 0:26b228dde70c | 62 | // X value? |
mbedalvaro | 0:26b228dde70c | 63 | else if (val=='X') { |
mbedalvaro | 0:26b228dde70c | 64 | stringData[indexStringData] = 0 ; |
mbedalvaro | 0:26b228dde70c | 65 | X=atoi(stringData); |
mbedalvaro | 0:26b228dde70c | 66 | indexStringData=0; |
mbedalvaro | 0:26b228dde70c | 67 | } |
mbedalvaro | 0:26b228dde70c | 68 | |
mbedalvaro | 0:26b228dde70c | 69 | // Y value? |
mbedalvaro | 0:26b228dde70c | 70 | else if (val=='Y') { |
mbedalvaro | 0:26b228dde70c | 71 | stringData[indexStringData] = 0 ; |
mbedalvaro | 1:4be6abc4ed43 | 72 | Y=atoi(stringData); |
mbedalvaro | 0:26b228dde70c | 73 | indexStringData=0; |
mbedalvaro | 0:26b228dde70c | 74 | newPositionReady=true; |
mbedalvaro | 0:26b228dde70c | 75 | } |
mbedalvaro | 1:4be6abc4ed43 | 76 | |
mbedalvaro | 1:4be6abc4ed43 | 77 | else if (val=='L') { |
mbedalvaro | 1:4be6abc4ed43 | 78 | stringData[indexStringData] = 0 ; |
mbedalvaro | 1:4be6abc4ed43 | 79 | if(atoi(stringData)>0) IO.setRedPower(1); else IO.setRedPower(0); |
mbedalvaro | 1:4be6abc4ed43 | 80 | indexStringData=0; |
mbedalvaro | 1:4be6abc4ed43 | 81 | } |
mbedalvaro | 0:26b228dde70c | 82 | |
mbedalvaro | 0:26b228dde70c | 83 | } |
mbedalvaro | 0:26b228dde70c | 84 | } |