FRDM-K64 USB air mouse demo

Dependencies:   FXOS8700Q USBDevice mbed

Fork of FRDM-K64_USB by Augusto Panecatl

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBMouse.h"
00003 #include "FXOS8700Q.h"
00004 
00005 DigitalIn RightClick(PTC6);
00006 DigitalIn LeftClick(PTA4);
00007 DigitalIn Click(PTC11);
00008 DigitalOut BlueLed(LED3);
00009 DigitalOut GreenLed(LED2);
00010 Ticker flipper;
00011 USBMouse mouse;
00012 
00013 AnalogIn AnIn0(A0);
00014 AnalogIn AnIn1(A1);
00015 
00016 FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board
00017 
00018 Serial pc(USBTX, USBRX);
00019 
00020 void flip() {
00021     GreenLed = !GreenLed;
00022 }
00023 
00024 void LLW_IRQHandler(void){
00025     if (LLWU_F2 & LLWU_F2_WUF11_MASK) {
00026        LLWU_F2 |= LLWU_F2_WUF11_MASK;   // write one to clear the flag
00027     }
00028 }
00029 
00030 void LLW_Init(void){
00031     LLWU_F2 |= LLWU_F2_WUF11_MASK;
00032     LLWU_PE3 = 0x80;
00033     LLWU_ME = 0x01;
00034     LLWU_RST = 0x01;
00035 }
00036 
00037 void Enter_VLLS0(void){
00038     volatile unsigned int dummyread;
00039     int i;
00040     SMC_PMPROT = SMC_PMPROT_AVLLS_MASK;
00041     if ((SMC_PMSTAT & SMC_PMSTAT_PMSTAT_MASK)== 4){
00042         SMC_PMCTRL &= ~SMC_PMCTRL_RUNM_MASK;   // go back to RUN mode temporarily
00043          for (i=0;i<0xff;i++)
00044         {
00045             if ((SMC_PMSTAT & SMC_PMSTAT_PMSTAT_MASK)== 1)
00046                 break;
00047         }
00048     }
00049     /* Set the STOPM field to 0b100 for VLLS0 mode */
00050     SMC_PMCTRL &= ~SMC_PMCTRL_STOPM_MASK; 
00051     SMC_PMCTRL |=  SMC_PMCTRL_STOPM(0x4); 
00052     /* set VLLSM = 00 * and PORPO = 1 */
00053     SMC_VLLSCTRL &= ~SMC_VLLSCTRL_VLLSM_MASK;
00054     SMC_VLLSCTRL =  SMC_VLLSCTRL_VLLSM(0) | SMC_VLLSCTRL_PORPO_MASK;  
00055     /*wait for write to complete to SMC before stopping core */  
00056     dummyread = SMC_VLLSCTRL;
00057     dummyread++;
00058     /* Now execute the stop instruction to go into VLLS0 */
00059     /* Set the SLEEPDEEP bit to enable deep sleep mode (STOP) */
00060     //SCB->SCR |= SCB_SCR_SLEEPDEEP_MASK;
00061     SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
00062     __wfi();
00063 }
00064 
00065 int main() 
00066 {
00067     float faX, faY, faZ;
00068     float posx, posy; 
00069     float pastx, pasty;
00070     int16_t x = 0;
00071     int16_t y = 0;
00072     //Initializations
00073     acc.enable();
00074     LLW_Init();
00075     //Interrupts
00076     flipper.attach(&flip, 0.1);
00077     NVIC_EnableIRQ(LLW_IRQn);
00078     
00079     while (1) 
00080     {
00081         if(LeftClick) mouse.release(MOUSE_LEFT);
00082         else mouse.press(MOUSE_LEFT);
00083         
00084         if(RightClick) mouse.release(MOUSE_RIGHT);
00085         else mouse.press(MOUSE_RIGHT);
00086         
00087         BlueLed = Click;
00088         
00089         acc.getX(&faX);
00090         if(faX>0.5f) Enter_VLLS0();
00091         //pc.printf("X=%1.4f       \r",faX);
00092         
00093         posx=AnIn0.read();
00094         posy=AnIn1.read();
00095         x = -800*(posx-pastx);
00096         y = 800*(posy-pasty);
00097 
00098         mouse.move(x, y);
00099         
00100         pastx = posx;
00101         pasty = posy;
00102               
00103         wait(0.01);
00104     }
00105 }