coin code

 

/****************************************************************************

Coin header

        coin.h


Revision

        1.1


Description

        The coin module provides an interface for detecting and acting

        upon the coin sensor input


Pins

        M6 DI Coin sensor input


Notes

     

        

Todo

        Incorporate timing checks for two pennies

                           

History

When           WhoWhat/Why

-------------- -----------

11/11/07 14:46 DLL Created

11/12/07 23:53 DLL Updated comments

****************************************************************************/


/*------------------------------- Interface ------------------------------*/


//Provides information about whether a coin has entered the chute

unsigned char CheckCoinEvent(void);


//Initiates the game when the coin event has occurred

unsigned char HandleCoinEvent(unsigned char event, unsigned char *runScore, unsigned char *turretScore);


//Provides interface function to reset game

void ResetCoin(void);

/*------------------------------- SubFunctions------------------------------*/

/****************************************************************************

Coin source code

        coin.c


Revision

        1.0


Description

        The coin module provides an interface for detecting and acting

        upon the coin sensor input


Pins

        M6 DI Coin sensor input


Notes

        

Todo

        Incorporate timing checks for two pennies

                           

History

When           WhoWhat/Why

-------------- -----------

11/11/07 14:46 DLL Created

11/24/07 19:30 BDH massive overhaul. whoops.

****************************************************************************/


/*------------------------------- Constants------------------------------*/

#define COINABSENT 0    //No coin in sensor

#define NOEVENT 0      //Signals that user has not yet entered enough pennies

#define COINPRESENT 1   //Coin present in sensor

#define STARTGAME 2     //2 pennies have been inserted, ready to proceed

#define COINSTOPLAY 2   //Number of pennies in to play

#define COININSERTEDEVENT 1


/*-------------------------------Includes------------------------------*/

#include "coin.h"

#include <ME218_C32.h>

#include <stdio.h>


/*-------------------------------Variables------------------------------*/


static unsigned char pennyCount = 0;

static unsigned char CoinPresent = COINABSENT;

/*-------------------------------Interface ------------------------------*/

/*----------------------------------------------------------------------*/

//Checks for a coin insertion event.  Signal is M6, hi when coin is present

unsigned char CheckCoinEvent( void)

{



  //Make sure initial state is the same on each run 


  if(PTIAD == (PTIAD | BIT6HI))                  //checks if the coin is present

  {  

    if(CoinPresent == COINPRESENT)               //checks if the coin was already present

    {  

      return NOEVENT;     //tell main that this is an old coin, nothing new

    }

    else                 //if the coin wasn't already present, but is currently present

    {

      CoinPresent = COINPRESENT;             //reset the coin state

      return COININSERTEDEVENT;

    }

  }

  else //If there is no coin, reset the coin present variable

  {

    CoinPresent = COINABSENT;                    //Otherwise, report no coin present

    return NOEVENT;

  }

}


/*----------------------------------------------------------------------*/

//Handles a coin insertion event.  Controls the display and returns the

//number of pennies inserted since the last game

unsigned char HandleCoinEvent(unsigned char event, unsigned char *runScore, unsigned char *turretScore)

{

     if(event==COININSERTEDEVENT) //if a coin was inserted

     {

       printf("Penny detected\n\r");

       pennyCount = pennyCount + 1; //increase penny counting variable

       if(pennyCount == 1) //if 1 penny was inserted

       {

         *runScore = 0; //change the left display to a 0 (points to variable in main)

       }

       if(pennyCount == 2) //if 2 pennies have been inserted

       {

         *turretScore = 0; //change the right display to a 0 (points to variable in main)

       }    

     }

return pennyCount;

}


/*----------------------------------------------------------------------*/

//Provides interface function to reset game

void ResetCoin()

{

    pennyCount = 0;     //tells the game no coins have been inserted

    CoinPresent = 0;     //resets variable for coin event checker

}


/*-------------------------------SubFunctions------------------------------*/