/****************************************************************************
 Function
    RunAdafruitAudioService

 Parameters
   ES_Event : the event to process

 Returns
   ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise

 Description
	Triggers an audio track to play by applying a low pulse to the relevant pin 
	on the Adafruit FX Sound Board. When this function is first called externally,
	the EventType will be PLAY_TRACK, EventParam will indicate the track number,
	and the function will begin the low pulse by setting the relevant pin Low. 
	After an internal timer times out, the EventType will be ES_TIMEOUT from an
	internal AUDIO_TIMER and this function will be called to end the low pulse 
	by setting the relevant pin back to High. 
	
 Notes
		In 'ES_Configure.h':
			#define TIMER14_RESP_FUNC RunAdafruitAudioService
			#define AUDIO_TIMER 14
****************************************************************************/
ES_Event RunAdafruitAudioService( ES_Event ThisEvent )
{
  ES_Event ReturnEvent;
  ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
  
	// set track line back to high to complete LowPulse
	if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == AUDIO_TIMER))
	{
		switch(track)
		{
			case 1:
				HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA + ALL_BITS)) |= AUDIO_TRACK01;
			break;
			
			case 2:
				HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA + ALL_BITS)) |= AUDIO_TRACK02;
			break;
			
			case 3:
				HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA + ALL_BITS)) |= AUDIO_TRACK03;
			break;
			
			default:
			break;
		}
	} else if (ThisEvent.EventType == PLAY_TRACK){ // play track by pulsing line low. 
					// Here, set line low, then set back to high after a timer expires 
					// (timer initialized in PulseLow)
		switch(ThisEvent.EventParam)
		{
			case 1: //track 01
				pulseLow(1);
				track = 1;
			break;
		
			case 2: //track 02
				pulseLow(2);
				track = 2;
			break;
			
			case 3: //track 03
				pulseLow(3);
				track = 3;
			break;
		
			default:
				track = 0;
			break;
	}
}
	
  return ReturnEvent;
}
inline uint8_t LCD_Low_Level::read8bits() {
	int data = 0;

	for (uint8_t i = 0; i < 8; i++) {
    pinMode(_data_pins[i], INPUT);
  }

  pulseHigh();

  for (uint8_t i = 0; i < 8; i++) {
    data |= (digitalRead(_data_pins[i]) & 0x01) << i;
  }

  pulseLow();
  
	return data;
}
void LCD_Low_Level::pulseEnable() {
	pulseHigh();
	pulseLow();
}