예제 #1
0
/* loop forever. doState() has a switch for the actions and events to be
   checked for 'port_state'. the actions and events may or may not change
   'port_state' by calling toState(), but once they are done we loop around
   again and perform the actions required for the new 'port_state'. */
void 
protocol(RunTimeOpts *rtOpts, PtpClock *ptpClock)
{
	DBG("event POWERUP\n");

	toState(PTP_INITIALIZING, rtOpts, ptpClock);

	DBG("Debug Initializing...\n");

	for (;;)
	{
		/* 20110701: this main loop was rewritten to be more clear */

		if (ptpClock->portState == PTP_INITIALIZING) {
			if (!doInit(rtOpts, ptpClock)) {
				return;
			}
		} else {
			doState(rtOpts, ptpClock);
		}

		
		if (ptpClock->message_activity)
			DBGV("activity\n");

		/* Perform the heavy signal processing synchronously */
		check_signals(rtOpts, ptpClock);
	}
}
예제 #2
0
/* forever after, call this function in a non-rtos system */
void protocol_loop(RunTimeOpts *rtOpts, PtpClock *ptpClock)
{
  if(ptpClock->port_state != PTP_INITIALIZING)
    doState(rtOpts, ptpClock);
  else if(!doInit(rtOpts, ptpClock))
    return;

  if(ptpClock->message_activity)
    DBGV("activity\n");
#if 0
  else
    DBGV("no activity\n");
#endif
}
bool GameStatsMenu::doInput(SDL_Event &event)
{
    if(event.type == SDL_MOUSEBUTTONUP) {
        if(currentState == State_Finished) {
            quit(0);
        } else {
            while(currentState != State_Finished) {
                doState(INT_MAX);
            }
        }
    }

    return MenuClass::doInput(event);
}
예제 #4
0
/* first time in a non-rtos system, call this function */
void protocol_first(RunTimeOpts *rtOpts, PtpClock *ptpClock)
{
  DBG("event POWERUP\n");

  toState(PTP_INITIALIZING, rtOpts, ptpClock);

  if(ptpClock->port_state != PTP_INITIALIZING)
    doState(rtOpts, ptpClock);
  else if(!doInit(rtOpts, ptpClock))
    return;

  if(ptpClock->message_activity)
    DBGV("activity\n");
#if 0
  else
    DBGV("no activity\n");
#endif
}
예제 #5
0
/* loop forever. doState() has a switch for the actions and events to be
   checked for 'port_state'. the actions and events may or may not change
   'port_state' by calling toState(), but once they are done we loop around
   again and perform the actions required for the new 'port_state'. */
void protocol(PtpClock *ptpClock)
{
  DBG("event POWERUP\n");
  
  toState(PTP_INITIALIZING, ptpClock);
  
  for(;;)
  {
    if(ptpClock->port_state != PTP_INITIALIZING)
      doState(ptpClock);
    else if(!doInit(ptpClock))
      return;
    
    if(ptpClock->message_activity)
      DBGV("activity\n");
    else
    {
      DBGV("no activity\n");
      timeNoActivity(ptpClock);
    }
  }
}
예제 #6
0
파일: LoopRecord.c 프로젝트: bobh/ILooper
int main(void)
{	    
	//printf("InfiniteLooper--based on patest_record.c\n"); //fflush(stdout);
	//Record for a few seconds. 
	data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; 
	data.frameIndex = 0;
	numSamples = totalFrames * NUM_CHANNELS;
	numBytes = numSamples * sizeof(SAMPLE);
	// From now on, recordedSamples is initialised.
	// 5 minutes of record buffer allocated 5% of free
	// on rPi zero. could record 90 minutes on rPi 0  
	//++++++++++++++++++++++++++++++++++++++++++++++++++++
	data.recordedSamples = (SAMPLE*) malloc( numBytes ); 
	//++++++++++++++++++++++++++++++++++++++++++++++++++++
	if( data.recordedSamples == NULL )
	{
		//printf("Could not allocate record array.\n");
		goto done;
	}
	else
	{
		//printf("allocated %d bytes\n", numBytes );
	}
	
	ringbufferWPTR = 0;
	ringbufferRPTR = 0;
    all_complete = FALSE;
    // ******************************************
    doState(S_INIT, data.recordedSamples);	
    // ******************************************
		
	// we do the recording set-up just once and cycle power
	// to do it again. This initialization should be moved to
	// ilooper_audio if multiple recordings are implemented
	
	err = Pa_Initialize();
	if( err != paNoError ) goto done;
	// default input device 
	inputParameters.device = Pa_GetDefaultInputDevice(); 
	if (inputParameters.device == paNoDevice) {
		fprintf(stderr,"Error: No default input device.\n");
		goto done;
	}
	inputParameters.channelCount = 2;                    //stereo input 
	inputParameters.sampleFormat = PA_SAMPLE_TYPE;
	inputParameters.suggestedLatency = 
	Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
	inputParameters.hostApiSpecificStreamInfo = NULL;
	
	//Record some audio. -------------------------------------------- 
	err = Pa_OpenStream
	(
	 &stream,
	 &inputParameters,
	 NULL,                  // &outputParameters, 
	 SAMPLE_RATE,           // ADC sample rate is a constant
	 FRAMES_PER_BUFFER,
	 //we won't output out of range samples so 
	 //don't bother clipping them 
	 //paClipOff, 
	 // ***wm6h for this app, let's clip
	 paNoFlag,
	 recordCallback,
	 &data 
	 );
	
	if( err != paNoError ) goto done;
	
	// the output sample rate is checked in playback()
	err = Pa_IsFormatSupported( &inputParameters, NULL, SAMPLE_RATE );
	if( err == paFormatIsSupported )
	{
		//printf("\n ADC samplerate is supported %d, FPB %d\n", \
		//         SAMPLE_RATE, FRAMES_PER_BUFFER);
	}
	else
	{
		//printf("\n ADC samplerate is NOT supported\n");
		goto done;
	}
	
	//printf(" %s\n", Pa_GetVersionText());	
	
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	
	//GPIO-0 access for Green LED and test
    //rPi J8-11 and gnd J8-6
    if(wiringPiSetup () < 0 )
    {
		fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno));
		return 1;
    }
    pinMode (LED_PIN, OUTPUT) ;
    digitalWrite (LED_PIN,  OFF) ;
    pinMode (BUTTON_PIN, INPUT) ;
    //pullUpDnControl(BUTTON_PIN, PUD_OFF);
    pullUpDnControl(BUTTON_PIN, PUD_UP);
    pinMode (DIRECTION_PIN, INPUT) ;
    pullUpDnControl(DIRECTION_PIN, PUD_UP); 
    pinMode (REBOOT_PIN, INPUT) ;
    pullUpDnControl(REBOOT_PIN, PUD_UP);
   
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	
	
	// underway--shift ship's colors--all ahead 8K sps
	// 
	err = Pa_StartStream( stream );
	if( err != paNoError ) goto done;

// =============================================================================	
// =============================================================================	
// collecting samples phase
    // ******************************************
    doState(S_COLLECTING, data.recordedSamples);	
    // ******************************************
	
	// we are running the callbacks
	while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
	{
		Pa_Sleep(500);
		if(buttonPress == TRUE)
		{
			Pa_Sleep(100);
			all_complete = TRUE; //stop the sample interrupt, exit while loop
		}
		else
		{	
		    // no button press
		    // give the state machine a chance to run	 
        // ******************************************
			doState(currentSTATE, (void*) NULL);	
	    // ******************************************		
		}
		
	}
// =============================================================================	
// =============================================================================	
	
	if( err < 0 ) goto done;
	// all stop
	err = Pa_CloseStream( stream );
	if( err != paNoError ) goto done;
	
	buttonValue = digitalRead(BUTTON_PIN);
	//printf("button = %d\n", buttonValue); 
	
	if(buttonValue == 0)
	{ 
		//if button still pressed, it's a hold
		buttonHold = TRUE;        
		//printf("button hold\n"); //fflush(stdout);
	}
	else
	{  //else, it's a button press
		buttonPress = FALSE; //acknowledged, reset flag
		//printf("button pressed\n"); //fflush(stdout);					 			
	}
	
	
	// Measure maximum peak amplitude. 
	// we could indicate over-driving/clipping the audio
	// or AGC it
	max = 0;
	average = 0.0;
	for( i=0; i<numSamples; i++ )
	{
		val = data.recordedSamples[i];
		if( val < 0 ) val = -val; // ABS 
		if( val > max )
		{
			max = val;
		}
		average += val;
	}
	
	average = average / (double)numSamples;
	
	//printf("sample max amplitude = "PRINTF_S_FORMAT"\n", max );
	//printf("sample average = %lf\n", average );
	
	// Write recorded data to a file. 
#if WRITE_TO_FILE
	{   // the file size should be 5 minutes of 8K samples * 2
	    // the entire ring buffer is recorded without regard to the 
	    // read/write pointers. Make sense of the file by editing it in
	    // Audacity
	    // todo: write the file in correct time order using rd/wr pointers
		FILE  *fid;				
		fid = fopen("recorded.raw", "wb");
		if( fid == NULL )
		{
			//printf("Could not open file.");
		}
		else
		{
			fwrite( data.recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), 
				   totalFrames, fid );
			fclose( fid );
			//printf("Wrote data to 'recorded.raw'\n");
		}
	}
#endif
	

// initial state after recording
// all future state changes from within state machine
    if(buttonHold == FALSE)
    {
        // ******************************************
		doState(S_REWIND_10, (void*) NULL);
		// ******************************************	   
    }
    else
    {
        // ******************************************
		doState(S_REWIND_PLAYBACK, (void*) NULL);	
		// ******************************************    
    }
// |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||	
// |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||	
    while (1)
    {
        // advance state in the state machine
        // ******************************************
		doState(currentSTATE, (void*) NULL);    
		// ******************************************	
		Pa_Sleep(100);
		if(terminate != FALSE)
		 goto done;
		 
    }
// |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||	
// |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
	
	
done:
	Pa_Terminate();
	if( data.recordedSamples )       // Sure it is NULL or valid. 
		free( data.recordedSamples );
	if( err != paNoError )
	{
		fprintf( stderr, 
				"An error occured while using the portaudio stream\n" );
		fprintf( stderr, "Error number: %d\n", err );
		fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
		
		fatalError();
		
		err = 1;          // Always return 0 or 1, but no other return codes. 
	}
	return err;
}
void GameStatsMenu::DrawSpecificStuff()
{
    doState(SDL_GetTicks() - currentStateStartTime);
}