Академический Документы
Профессиональный Документы
Культура Документы
SoundSM
This state machine/service controls the adafruit using the shift register and
also performs low level shift register operations.
The other services would post to this function for sound effect with an
EventParam which will decide which sound pin to play.
Would also include the function to stop the sound
States:
*/
#include "BITDEFS.H"
#include "SoundSM.h"
// readability defines
//
********************************************************************************
*****************
//
********************************************************************************
******************************
*/
#define GET_MSB_IN_LSB(x) ((x & 0x80) >> 7)
#define ALL_BITS (0xff << 2)
#define ALL_HI (BIT0LO | BIT0HI)
//*********************************MODULE
DEFINES***********************************************
// an image of the last 8 bits written to the shift register
static uint8_t LocalRegisterImage = 0;
static uint8_t MyPriority;
//**********************Module
Functions***********************************************
static void SR_Sound_Write(uint8_t NewValue);
/****************************************************************************
Function
InitSoundSM
Parameters
uint8_t : the priorty of this service
Returns
true if an INIT event has been posted. Else False which is equivalent to an
error in posting the event
Description
A function to initialize the shift register
The data and sclk lines start from low and the RCLK starts from high
Notes
Author
****************************************************************************/
// set up the ports by enabling the peripheral clock, waiting for the
// peripheral to be ready and setting the direction
HWREG(SYSCTL_RCGCGPIO) |= SOUND_SR_DATA_PORT_HI; //Port enable for DATA
HWREG(SYSCTL_RCGCGPIO) |= SOUND_SR_SCLK_PORT_HI; //Port enable for SCLK
HWREG(SYSCTL_RCGCGPIO) |= SOUND_SR_RCLK_PORT_HI; //Port enable for RCLK
//Not required -- Sample the button port pin and use it to initialize
LastButtonState
//LastPinState = HWREG(LEAF_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &
LEAF_SENSOR_PIN_HI;
// start with the data & sclk lines low and the RCLK line high
HWREG(SOUND_SR_DATA_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &=
SOUND_SR_DATA_PIN_LOW;
HWREG(SOUND_SR_SCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &=
SOUND_SR_SCLK_PIN_LOW;
HWREG(SOUND_SR_RCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) |=
SOUND_SR_RCLK_PIN_HI;
ES_Event_t ThisEvent;
ThisEvent.EventType = ES_INIT;
/****************************************************************************
Function
PostSoundSM
Parameters
ES_Event_t ThisEvent ,the event to post to the queue
Returns
bool false if the Enqueue operation failed, true otherwise
Description
Posts an event to this state machine's queue
Notes
Author
****************************************************************************/
bool PostSoundSM(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunSoundSM
Parameters
ES_Event_t : the event from its service queue to process in this State
Machine.
The EventParam will carry the index of the sound to be played.
Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise
Description
Notes
Author
****************************************************************************/
ES_Event_t RunSoundSM(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
uint8_t NewValue;
if (ThisEvent.EventType == SOUND_STOP)
{
puts("\r \n Event received to stop the sound");
SR_Sound_Write(ALL_HI);
}
if (ThisEvent.EventType == SOUND_PLAY)
{
//puts("\r \n Event received by the SoundSM");
switch (ThisEvent.EventParam)
{
case 0:
{
puts("\r \n Received in Sound Case 0");
SR_Sound_Write( ALL_HI);
NewValue = BIT0LO;
SR_Sound_Write( NewValue);
}
break;
case 1:
{
puts("\r \n Received in Sound Case 1");
SR_Sound_Write( ALL_HI);
NewValue = BIT1LO;
SR_Sound_Write( NewValue);
}
break;
case 2:
{
SR_Sound_Write( ALL_HI);
NewValue = BIT2LO;
SR_Sound_Write( NewValue);
}
break;
case 3:
{
SR_Sound_Write( ALL_HI);
NewValue = BIT3LO;
SR_Sound_Write( NewValue);
}
break;
case 4:
{
SR_Sound_Write( ALL_HI);
NewValue = BIT4LO;
SR_Sound_Write( NewValue);
}
break;
}
}
return ReturnEvent;
}
/****************************************************************************
Function
SR_GetCurrentRegister
Parameters
Returns
uint8_t
Description
This function gives the last provided value to the shift register
Notes
Author
****************************************************************************/
uint8_t SR_Sound_GetCurrentRegister(void)
{
return LocalRegisterImage;
}
//******************************************HELPER
FUNCTIONS******************************************
// This function writes to the shift register given the uint8_t NewValue
static void SR_Sound_Write(uint8_t NewValue)
{
uint8_t BitCounter = 0;
LocalRegisterImage = NewValue; // save a local copy
//printf("\r \n NewValue %d", NewValue);
// lower the register clock
HWREG(SOUND_SR_RCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &=
SOUND_SR_RCLK_PIN_LOW;
//pulsing SCLK
HWREG(SOUND_SR_SCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) |=
SOUND_SR_SCLK_PIN_HI;
HWREG(SOUND_SR_SCLK_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &=
SOUND_SR_SCLK_PIN_LOW;