⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demos.c

📁 freescale badge board 开发板测试 源程序
💻 C
字号:
/*!
 * \file    demos.c
 * \brief   Demos
 * \version $Revision: 1.1 $
 * \author  Anthony Huereca 
 */

#include "demos.h"
#include "jmbadgeboard.h"
/*******************************************************************/

/*******************************************************************/
/*!
 * Simple game to keep the ball from hitting the sides
 */ 
void SideBall() 
{
  int i=0;
  int count=0;
  byte leftPaddle=0x03;
  byte rightPaddle=0x03;
  byte hits=0;
  int speed=150;
  
  /* Initialize Ball */  
  ball.x=7;
  ball.y=2;
  ball.dirx=LEFT;
  ball.diry=UP;


  /* Wait for button to be depressed */
  while(KeyPressed()) {
  }
  
  /* Get speed selection */
  while(!KeyPressed()) 
  {
    
    LEDScrollTouch("Select Speed 1 - 8");
  }
  
  if(KeyPressed()) 
  {   
    speed=15+(LastKeyPressed()*20);
  }
  
  while(KeyPressed()) {
  }
     
  /* Turn on RTC Clock */
  SCGC2_RTC=1;
  
  /* Turn on RTC Interrupt */
  rtc_init(8);
 
  while(!ExitApp())
  {
    rtc_init(8);

    /* Display left paddle */
    PTAD=0x00;
    PTED=~(0x01);
    PTDD=~(0x00);
    PTAD=leftPaddle;
    asm (stop #0x2000);

    /* Display right paddle */
    PTAD=0x00;
    PTED=~(0x00);
    PTDD=~(0x80);
    PTAD=rightPaddle;
    asm (stop #0x2000);

         
    /* Display ball */
    if (ball.x < COLUMNS/2) 
    {
      PTAD = 0x00;
      PTED = ~(1 << ball.x);
      PTDD = ~(0x00);
      PTAD = 1 << ball.y; 
    } 
    else 
    {
      PTAD=0x00;
      PTED = ~(0x00);
      PTDD = ~(1 << (ball.x-(COLUMNS/2)));
      PTAD = 1 << ball.y;            
    }
    asm (stop #0x2000);       
                   
    /* Move Paddles */
    switch(LastKeyPressed()) 
    {
      case 1:
        leftPaddle=0x03;
        break;    
      case 2:
        leftPaddle=0x06;
        break;
      case 3:
        leftPaddle=0x0C;
        break;
      case 4:
        leftPaddle=0x018;
        break;
      case 5:
        rightPaddle=0x03;
        break;    
      case 6:
        rightPaddle=0x06;
        break;
      case 7:
        rightPaddle=0x0C;
        break;
      case 8:
        rightPaddle=0x18;
        break;
      default:
        break;
    }


      
    /* Determines how fast ball moves */  
    count++;
    if(count==speed) 
    {
      count=0;
    
      /* Check to see if ball hit paddle */
      
      /* Check left side */
      if(ball.x==1) 
      {
        if((1 << ball.y)&leftPaddle) 
        {
          hits++;
          ball.dirx=RIGHT;
        } 
        else
        {
          LEDScroll("LOST!");
          
          /* Turn back on RTC clock */
          SCGC2_RTC=1; 
          if(hits%2)
            ball.diry=UP;
          else
            ball.diry=DOWN;
          
          ball.y=ball.y; //keep same height that lost at
          ball.x=7;
          hits=0;
        }
      }
      
      /* Check right side */
      if(ball.x==14) 
      {
        if((1 << ball.y)&rightPaddle)
        {
          hits++;
          ball.dirx=LEFT;
        }
        else
        {
          LEDScroll("LOST!");
          
          /* Turn back on RTC clock */
          SCGC2_RTC=1;
          
          if(hits%2)
            ball.diry=UP;
          else
            ball.diry=DOWN;          
          
          ball.y=ball.y; //keep same height that lost at
          ball.x=7;
          hits=0;
        }
      }      

      /* Keep ball in LED Matrix */ 
      
      /* If ball at top of LED Matrix, move down */      
      if(ball.y==0)
        ball.diry=DOWN;
      
      /* If ball at bottom of LED Matrix, move up */
      if(ball.y==4)
        ball.diry=UP;
      
      if(ball.dirx==LEFT)
        ball.x--;
      else
        ball.x++;
      
      if(ball.diry==DOWN)
        ball.y++;
      else
        ball.y--;
    }       
  }
  SCGC2_RTC=0;
}


/*******************************************************************/
/*!
 * Keep the ball from hitting the floor
 */ 
void PaddleBall() 
{
  int i=0;
  int count=0;
  char paddle=8;
  byte hits=0;
  char p, p1, p2;
  int speed=150;

  char ready=1;

  /* Initialize Ball */
  ball.x=7;
  ball.y=2;
  ball.dirx=LEFT;
  ball.diry=UP;
  
  /* Wait for button to be depressed */
  while(KeyPressed()) {
  }
  
  /* Get speed selection */
  while(!KeyPressed()) 
  {
    
    LEDScrollTouch("Select Speed 1 - 8");
  }
  
  if(KeyPressed()) 
  {   
    speed=60+(LastKeyPressed()*45);
  }
  
  /* Wait until button depressed */
  while(KeyPressed()) {
  }


  
  /* Turn on RTC Clock */
  SCGC2_RTC=1;
  
  /* Turn on RTC Interrupt */
  rtc_init(8);

  /* So don't fail while loop is select Speed 1 */ 
  AddTouch(2);
 
  while(LastKeyPressed()!=1) 
  {
    rtc_init(8);
  
    /* Display Ball */
    if (ball.x < COLUMNS/2) 
    {
      PTAD = 0x00;
      PTED = ~(1 << ball.x);
      PTDD = ~(0x00);
      PTAD = 1 << ball.y; 
    } 
    else 
    {
      PTAD=0x00;
      PTED = ~(0x00);
      PTDD = ~(1 << (ball.x-(COLUMNS/2)));
      PTAD = 1 << ball.y;            
    }
       
                   
    // Enter stop mode and wait for RTC wakeup 
    //asm (stop #0x2000);
    for(i=0;i<150;i++)
      asm("nop");


    /* Display Paddle */
    
    /* If on left side of Matrix */ 
    if (paddle < COLUMNS/2) 
    {
      /* If paddle split between PTED and PTDD */
      if(paddle!=7) 
      {
        p1=1<<paddle;
        p2=1<<(paddle+1);
        p=p1|p2;
        PTAD=0x00;
        PTDD=~(0x00);
        PTED=~(p);
        PTAD=0x10;
      } 
      else 
      {
        PTAD=0x00;
        PTDD=~(0x01);
        PTED=~(0x80);
        PTAD=0x10;                
      }
    }
    /* If on right side of Matrix */ 
    else 
    {
      p1=1<<(paddle-8);
      p2=1<<(paddle-7);
      p=p1|p2;
      PTAD=0x00;
      PTED=~(0x00);
      PTDD=~(p);
      PTAD=0x10;         
    }

    // Enter stop mode and wait for RTC wakeup 
    //asm (stop #0x2000);
    for(i=0;i<150;i++)
      asm("nop");


    /* Ensure only move one spot per single key press */        
    if(!KeyPressed())
      ready=1;
    
    
    /* If key is pressed, move paddle once */    
    if(KeyPressed() && ready) 
    {
      ready=0;
      if(LastKeyPressed()==4) 
      {  
        if(paddle>0) 
        {
          paddle--;
        }
      }
      if(LastKeyPressed()==8) 
      {
        if(paddle<14) 
        {
          paddle++;
        }
      }
    }  

    /* Make sure ball stays on screen */
    if(ball.y==0)
      ball.diry=DOWN;
    if(ball.x==0)      
      ball.dirx=RIGHT;
    if(ball.x==15) 
      ball.dirx=LEFT;
    
    /* Determines how fast ball moves */
    count++;
    if(count==speed) 
    {
      count=0;
      /* Check to see if hit ball or not */    
      if(ball.y==3) 
      {
        if(ball.x==paddle) 
        {
          ball.dirx=LEFT;
          ball.diry=UP;
          hits++;
        } 
        else if(ball.x==paddle+1) 
        {
          ball.dirx=RIGHT;
          ball.diry=UP;
          hits++;
        } 
        else
        {
          LEDScroll("LOST!");
          SCGC2_RTC=1; 
          ball.diry=UP;
          if(hits%2)
            ball.dirx=LEFT;
          else
            ball.dirx=RIGHT;
          ball.y=3;
          
          /* Ball reset location depends on how many successful hits */
          ball.x=hits%15;
          
          /* Keep the paddle close to the ball */
          paddle=ball.x;
          hits=0; 
        }
        
        if(ball.x==0)      
          ball.dirx=RIGHT;
        if(ball.x==15) 
          ball.dirx=LEFT;          
      }
        
      /* Move ball */ 
      if(ball.dirx==LEFT)
        ball.x--;
      else
        ball.x++;
      if(ball.diry==DOWN)
        ball.y++;
      else
        ball.y--;
    }       
  }
  SCGC2_RTC=0;
}


/*******************************************************************/
/*!
 * The buzzer on the badge uses a lot of current (100mA) so 
 *  keep that in mind if planning on running off the battery
 * Currently being called from hid_mouse.c
 */ 
void Speaker() 
{
  /* Turn on PWM Module */
  SCGC1_TPM1=1;
  
  /* Set up for A note (440Hz) */
  TPM1MODH=0x03;
  TPM1MODL=0x54;
  TPM1C4VH=0x03;
  TPM1C4VL=0x54; 
  
  /* Toggle PWM Signal upon ouput compare */
  TPM1C4SC=0xD4;  

  /* Turn off PWM for now */
  TPM1SC=0x00;
 
  while(1) 
  {
    /* Check Charging Bit */
    if(!MC3467X_CHG) 
    {
      /* Charging */
      LED_GRN = OFF;
      LED_RED = ON;
    }
    else 
    {
      /* Charged */
      LED_GRN = ON;
      LED_RED = OFF;    
    }  
  
    /* Reset if USB unplugged */
    if (PTGD_PTGD1) 
    { 
      asm("halt");
    }
  
    /* Only make noise when button pressed */
    if(KeyPressed()) 
    {
    
      /* Display on LEDs */
      PTAD=0x1F;
      PTDD=~(0x00);
      
      
      switch(LastKeyPressed()) 
      {
        case 1:         //A 440Hz
          PTED=~(0x01);
          TPM1MODH=0x03;
          TPM1MODL=0x54;
          TPM1C4VH=0x03;
          TPM1C4VL=0x54; 
          break;
        case 2:         //B 494Hz
          PTED=~(0x02);
          TPM1MODH=0x02;
          TPM1MODL=0xF7;
          TPM1C4VH=0x02;
          TPM1C4VL=0xF7; 
          break;
        case 3:         //C 523Hz
          PTED=~(0x04);
          TPM1MODH=0x02;
          TPM1MODL=0xCD;
          TPM1C4VH=0x02;
          TPM1C4VL=0xCD; 
          break;          
        case 4:          //D 587Hz
          PTED=~(0x08);  
          TPM1MODH=0x02;
          TPM1MODL=0x7E;
          TPM1C4VH=0x02;
          TPM1C4VL=0x7E; 
          break;
        case 5:          //E 659Hz
          PTED=~(0x10);     
          TPM1MODH=0x02;
          TPM1MODL=0x39;
          TPM1C4VH=0x02;
          TPM1C4VL=0x39; 
          break;
        case 6:          //F 698Hz
          PTED=~(0x20); 
          TPM1MODH=0x02;
          TPM1MODL=0x19;
          TPM1C4VH=0x02;
          TPM1C4VL=0x19; 
          break;
        case 7:          //G 784Hz
          PTED=~(0x40);
          TPM1MODH=0x01;
          TPM1MODL=0xDE;
          TPM1C4VH=0x01;
          TPM1C4VL=0xDE; 
          break;
        case 8:          //A 880Hz
          PTED=~(0x80); 
          TPM1MODH=0x01;
          TPM1MODL=0xAA;
          TPM1C4VH=0x01;
          TPM1C4VL=0xAA; 
          break;          
        default:
          break;                                              
      }
     
      /* Turn on PWM interrupt 
         Use bus clock (12MHz) divided by 16 for 750Khz 
       */
       TPM1SC=0xCC; //for USB 
      
      /* Use when running off battery (750KHz clock), No need for pre-divider */
      //TPM1SC=0xC8; 
    } 
    else
    {
      /* If no key pressed, turn off speaker */
      PTAD=0x00;
      TPM1SC=0x00;
    }
  
  }
  
  SCGC1_TPM1=0;
}


/*******************************************************************/
/*!
 * Shake the badge and find out the answers to life's Yes or No questions
 */ 
void MagicBadge() 
{
  long vertical;
  long horizontal;
  char selection=0;
  char shakeDetected=0;
  

  
  while(KeyPressed()) {
  }

  /* Run until E8 is pressed */
  while(LastKeyPressed()!=8) 
  {
  
    /* Turn on Accelerometer using 2g Level */
    AccelOn(1);
  

    while(!shakeDetected)   
    {
      LEDScrollShake("The Magic Badge");
      horizontal=ReadAcceleration(0x05);   
      vertical=ReadAcceleration(0x06);
      if(vertical>0xb00 || horizontal>0xb00 || LastKeyPressed()==8)
        shakeDetected=1;
    }
  
    shakeDetected=0;
    
    /* Turn off LEDs */
    PTAD=0x00;
    PTDD=~(0x00);
    PTED=~(0x00);

    if(LastKeyPressed()==8) 
    {
      AccelOff();
      return;
    }
  
    /* Turn on RTC Clock */
    SCGC2_RTC=1;
 
    /* Delay for 1/10 second*/
    rtc_init(0xD);  
    asm("stop #0x2000");
    
    /* 
       Take a reading for quasi-randomness
       and that the user actually affects the outcome
       so it's not as impersonal as a RNGA
    */
    vertical=ReadAcceleration(0x06);   
    horizontal=ReadAcceleration(0x05);
    selection=(vertical+horizontal)%16;
    
    AccelOff();
    
    /* Delay for 1 second */
    rtc_init(0xF);
    asm("stop #0x2000");
    
    switch(selection) 
    {
          case 0: 
            LEDScrollTouch("Yes");
            break;
          case 1:
            LEDScrollTouch("No");
            break;    
          case 2:
            LEDScrollTouch("Better not tell you now");
            break;
          case 3:
            LEDScrollTouch("Concentrate and ask again");
            break;
          case 4:
            LEDScrollTouch("Don't count on it");
            break;
          case 5:
            LEDScrollTouch("It is decidedly so");
            break; 
          case 6:
            LEDScrollTouch("Most likely");
            break;  
          case 7:
            LEDScrollTouch("My sources say no");
            break;              
          case 8:
            LEDScrollTouch("Outlook good");
            break;                    
          case 9: 
            LEDScroll("Reply hazy, try again");
            break;
          case 10:
            LEDScrollTouch("Very doubtful");
            break;    
          case 11:
            LEDScrollTouch("Without a doubt");
            break;
          case 12:
            LEDScrollTouch("Yes - definitely");
            break;
          case 13:
            LEDScrollTouch("You may rely on it");
            break;
          case 14:
            LEDScrollTouch("Ask again later");
            break; 
          case 15:
            LEDScrollTouch("I would bet my chips on it");
            break;                    
          default:
            LEDScrollTouch("Try again");
            break;
    }
  }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -