Laser Sensing Display for UI interfaces in the real world
Fork of skinGames_forktest by
Diff: hardwareIO/hardwareIO.cpp
- Revision:
- 34:1244fa3f2559
- Parent:
- 31:5f039cbddee8
- Child:
- 35:35af5086ab4f
--- a/hardwareIO/hardwareIO.cpp Mon Nov 05 06:08:35 2012 +0000 +++ b/hardwareIO/hardwareIO.cpp Wed Nov 07 14:41:55 2012 +0000 @@ -9,27 +9,35 @@ SPI spiDAC(MOSI_PIN, MISO_PIN, SCK_PIN); // mosi, miso, sclk DigitalOut csDAC(CS_DAC_MIRRORS); - - AnalogIn ain(POT_ANALOG_INPUT); - + DigitalOut Laser_Red(LASER_RED_PIN); // NOTE: this is NOT the lock in sensing laser (actually, not used yet) DigitalOut Laser_Green(LASER_GREEN_PIN); DigitalOut Laser_Blue(LASER_BLUE_PIN); + + // Some manual controls over the hardware function: + InterruptIn switchOne(SWITCH_ONE); + DigitalOut ledSwitchOne(LED_SWITCH_ONE); + InterruptIn switchTwo(SWITCH_TWO); + //AnalogIn ain(POT_ANALOG_INPUT); void HardwareIO::init(void) { - Laser_Red = 0; // note: this is not the lockin-laser! - Laser_Green = 0; - Laser_Blue = 0; + + // Set laser powers down: + setRedPower(0);// TTL red laser (not used - actually not present) + setGreenPower(0); + setBluePower(0); //Serial Communication setup: pc.baud(115200);// // pc.baud(921600);//115200);// // Setup for lock-in amplifier and pwm references: + setLaserLockinPower(1);// actually this is the Red laser in the hardware lockin.init(); - // Setup the spi for 8 bit data, high steady state clock, - // second edge capture, with a 10MHz clock rate + // Setup for DAC control to move the mirrors: + // Set spi for 8 bit data, high steady state clock, + // second edge capture, with a 10MHz clock rate: csDAC = 1; spiDAC.format(16,0); spiDAC.frequency(16000000); @@ -40,6 +48,77 @@ // Load LUT table: setLUT(); + + // Set interrupts on RAISING edge for button-switch functions: + // Note: The pin input will be logic '0' for any voltage on the pin below 0.8v, and '1' for any voltage above 2.0v. + // By default, the InterruptIn is setup with an internal pull-down resistor, but for security and clarity I will do it explicitly here: + switchOne.mode(PullUp); // pull down seems not very good + switchTwo.mode(PullUp); + switchOne.fall(this, &HardwareIO::switchOneInterrupt); // attach the address of the flip function to the falling edge + switchTwo.fall(this, &HardwareIO::switchTwoInterrupt); // attach the address of the flip function to the falling edge + switchOneState=false; // false means fixed threshold + switchTwoState=false; + switchOneChange=false; + switchTwoChange=false; + ledSwitchOne=(switchOneState? 1 :0); + + // Read and update pot value: + // updatePotValue(); // the value will be ajusted in the range 0-255 +} + +// these ISR could do more (like debouncing). +// For the time being, I will debounce electrically with a small capacitor. +void HardwareIO::switchOneInterrupt() { + switchOneState=!switchOneState; + ledSwitchOne=(switchOneState? 1 :0); // this switch has a built-in led + switchOneChange=true; +} +void HardwareIO::switchTwoInterrupt() { + switchTwoState=!switchTwoState; + switchTwoChange=true; +} + +bool HardwareIO::switchOneCheck(bool& state) { + if (switchOneChange) { + switchOneChange=false; + state=switchOneState; + return(true); + } else + return(false); +} + +bool HardwareIO::switchTwoCheck(bool& state) { + if (switchTwoChange) { + switchTwoChange=false; + state=switchTwoState; + return(true); + } else return(false); +} + +unsigned char HardwareIO::updatePotValue() { // this will update the pot value, and return it too. + //The value will be ajusted in the range 0-255 + //The 0.0v to 3.3v range of the AnalogIn is represented in software as a normalised floating point number from 0.0 to 1.0. + //potValue=(unsigned char )(ain*255); + + lockin.setADC_forLockin(0); + + adc.setup(POT_ANALOG_INPUT,1); + wait(1); + + //Measure pin POT_ANALOG_INPUT + adc.select(POT_ANALOG_INPUT); + //Start ADC conversion + adc.start(); + //Wait for it to complete + while(!adc.done(POT_ANALOG_INPUT)); + potValue=adc.read(POT_ANALOG_INPUT); + + //Unset pin POT_ANALOG_INPUT + adc.setup(POT_ANALOG_INPUT,0); + + lockin.setADC_forLockin(1); + + return(potValue); } //write on the first DAC, output A (mirror X) @@ -68,7 +147,7 @@ csDAC = 1; } -void HardwareIO::setRedPower(int powerValue){ +void HardwareIO::setLaserLockinPower(int powerValue){ if(powerValue > 0){ lockin.setLaserPower(true); } @@ -76,6 +155,16 @@ lockin.setLaserPower(false); } } +// THE TTL controlled lasers: +// Note: the red one is not used here +void HardwareIO::setRedPower(int powerValue){ + if(powerValue > 0){ + Laser_Red = 1; + } + else{ + Laser_Red = 0; + } +} void HardwareIO::setGreenPower(int powerValue){ if(powerValue > 0){ Laser_Green = 1; @@ -94,7 +183,7 @@ } void HardwareIO::setRGBPower(unsigned char color) { - lockin.setLaserPower((color&0x04)>0? true : false); + lockin.setLaserPower((color&0x04)>0? true : false); // NOTE: here the "red" is the lockin laser, not the TTL one (not used yet) Laser_Green=(color&0x02)>>1; Laser_Blue =color&0x01; }