Exemplo n.º 1
0
Arquivo: main.cpp Projeto: ediril/BCI
void GenerateSample( void)
{
  // Increment sample no
  SampleNo++;

  // Fill in spikecount header, assume spike count itself has already been filled, and send it
  MDF_SPM_SPIKECOUNT *sc = (MDF_SPM_SPIKECOUNT*) SpikeCountMessage.GetDataPointer();
  sc->sample_header.DeltaTime = ((double)0.020);
  sc->sample_header.SerialNo = SampleNo;
  sc->sample_header.Flags = 0;
  sc->source_timestamp = LatestSourceTimestamp;
  sc->count_interval = RawCountInterval * ((double)RAW_COUNTS_PER_SAMPLE);
  dragonfly.SendMessage( &SpikeCountMessage);

  MDF_SAMPLE_GENERATED *sg = (MDF_SAMPLE_GENERATED*) SampleGeneratedMessage.GetDataPointer();
  //MDF_SAMPLE_GENERATED sgd;
  //MDF_SAMPLE_GENERATED *sg = &sgd;
  sg->sample_header.DeltaTime = ((double)0.020);
  sg->sample_header.SerialNo = SampleNo;
  sg->sample_header.Flags = 0;
  sg->source_timestamp = LatestSourceTimestamp;
  //SampleGeneratedMessage.SetData( sg, sizeof(MDF_SAMPLE_GENERATED));
  dragonfly.SendMessage( &SampleGeneratedMessage);


  // Update list of active sources
  UpdateActiveSources();

  // Reset count
  ResetCount();

  //PrintCount( SpikeCountData);
}
Exemplo n.º 2
0
void SendLFPs(int nsp_index,double ptime)
{
	//printf("~");
	int buffer_length=0;
	LFPData.GetAndReset(lfpdata, &buffer_length);

	// Fill in the Dragonfly message data struct
	MDF_RAW_CTSDATA sc;
	memset( &sc, 0, sizeof(sc));
	sc.source_index = nsp_index;
	sc.source_timestamp = ptime;

	for( int i = 0; i < ((MAX_SPIKE_CHANS_PER_SOURCE+MAX_ANALOG_CHANS)*LFPSAMPLES_PER_HEARTBEAT); i++)	
	{
		if ( i <= buffer_length)
		{
			sc.data[i] = lfpdata[i];
		}
		else
		{
			sc.data[i] = 0;
		}
	}

	// Send the message
	msg.Set( MT_RAW_CTSDATA, &sc, sizeof(sc));
	dragonfly.SendMessage( &msg);
}
Exemplo n.º 3
0
void SendDigEvents()
{
	Dig_Message_Number--;

	for (int i = 0; i<=Dig_Message_Number; i++)
	{
		// Send the message
		msg.Set( MT_DIGITAL_EVENT, de_msgs[i], sizeof(*de_msgs[i]));
		dragonfly.SendMessage( &msg);
		delete de_msgs[i];
	}

	Dig_Message_Number = 0;
}
Exemplo n.º 4
0
void SendStimSyncEvents()
{
	Sse_Message_Number--;

	for (int i = 0; i<=Sse_Message_Number; i++)
	{
		// Send the message
		msg.Set( MT_STIM_SYNC_EVENT, sse_msgs[i], sizeof(*sse_msgs[i]));
		dragonfly.SendMessage( &msg);
		delete sse_msgs[i];
	}

	Sse_Message_Number = 0;
}
Exemplo n.º 5
0
void SendCounts( double timestamp, unsigned char *count, int num_chans, int nsp_index)
{
	//printf("c");
	// Fill in the Dragonfly message data struct
	MDF_RAW_SPIKECOUNT sc;
	memset( &sc, 0, sizeof(sc));
	sc.source_index = nsp_index;
	sc.source_timestamp = timestamp;
	sc.count_interval = CEREBUS_HEARTBEAT_INTERVAL;
	if( num_chans > MAX_TOTAL_SPIKE_CHANS_PER_SOURCE) throw std::exception("SendCounts(): num_chans exceeds MAX_RAW_TOTAL_SPIKE_CHANS");
	for( int i = 0; i < num_chans; i++)	sc.counts[i] = count[i];

	// Send the message
	msg.Set( MT_RAW_SPIKECOUNT, &sc, sizeof(sc));
	dragonfly.SendMessage( &msg);
}
Exemplo n.º 6
0
Arquivo: main.cpp Projeto: ediril/BCI
//bool ProcessMessage(RTMA_Module *rtma, CMessage *M)
bool ProcessMessage(Dragonfly_Module *dragonfly, CMessage *M)
{
  bool keep_running = true;
  switch( M->msg_type) {
  case MT_RAW_SPIKECOUNT:
    {
      MDF_RAW_SPIKECOUNT *rc = (MDF_RAW_SPIKECOUNT*) M->GetDataPointer();
      ProcessRawCount( rc);
      //fprintf(stdout,".");
      break;
    }
  case MT_EXIT:
    {
        if ((M->dest_mod_id == 0) || (M->dest_mod_id == dragonfly->GetModuleID()))
        {
            fprintf(stdout,"got exit!\n");
            dragonfly->SendSignal(MT_EXIT_ACK);
            dragonfly->DisconnectFromMMM();
            keep_running = false;
        }
        break;
    }
  case MT_PING:
    {
      char MODULE_NAME[] = "SPM_Combiner";
      MDF_PING *pg = (MDF_PING *) M->GetDataPointer();
      if ( (strcmp(pg->module_name, MODULE_NAME) == 0) || 
           (strcmp(pg->module_name, "*") == 0) ||
           (M->dest_mod_id == dragonfly->GetModuleID()) )
      {
        MDF_PING_ACK *pa = (MDF_PING_ACK *) PingAckMessage.GetDataPointer();
        
        memset(pa,0,sizeof(MDF_PING_ACK));        
        for (int i = 0; i < strlen(MODULE_NAME); i++)
        {
          pa->module_name[i] = MODULE_NAME[i];
        }

        dragonfly->SendMessage( &PingAckMessage);
      }
      
      break;
    }
  }
  fflush(stdout);
  return keep_running;
}
Exemplo n.º 7
0
void SendSnippets()
{
	//printf("$");
	if (Overall_Spike_Count == 0)
		Snippet_Message_Number--;

	for (int i = 0; i<=Snippet_Message_Number; i++)
	{
		// Send the message
			msg.Set( MT_SPIKE_SNIPPET, ss_msgs[i], sizeof(*ss_msgs[i]));
			dragonfly.SendMessage( &msg);
			delete ss_msgs[i];
	}

	Overall_Spike_Count = 0;
	Snippet_Message_Number = 0;

}
Exemplo n.º 8
0
void SendRejectedSnippets()
{
	//printf("rs");
	if (Overall_Rejected_Count == 0)
		Rejected_Snippet_Message_Number--;

	for (int i = 0; i<=Rejected_Snippet_Message_Number; i++)
	{
		// Send the message
		//printf("rs");
		msg.Set( MT_REJECTED_SNIPPET, rs_msgs[i], sizeof(*rs_msgs[i]));
		dragonfly.SendMessage( &msg);
		delete rs_msgs[i];
	}

	Overall_Rejected_Count = 0;
	Rejected_Snippet_Message_Number = 0;

}
Exemplo n.º 9
0
void
mexFunction(
	int num_output_args,        // Number of left hand side (output) arguments
	mxArray *output_arg[],      // Array of left hand side arguments
	int num_input_args,         // Number of right hand side (input) arguments
	const mxArray *input_arg[]) // Array of right hand side arguments
{
	int opcode;
    
    MODULE_ID ModuleID, DestModID;
    HOST_ID DestHostID;
    short *HostAddress_raw;
    char HostAddress[MAX_HOST_ADDR_LENGTH];
    int HostAddressLength = 0;
    MSG_TYPE MessageType;

	double timeout;
    int status;
	unsigned int SnoozeTime;
    int TimerID;
    int logger_status;

    void *pData;
    int NumDataBytes;

    const mxArray *Template;
    mxArray *ReturnData;
	
	bool try_again;
	double begin_time, end_time, elapsed_time;

    try {
        if( num_input_args < 1) {
            Error( "MatlabDragonfly takes at least 1 argument!");
        }

        //
        // Get the "opcode" argument to determine what needs to be done
        //
        opcode = (int) mxGetScalar( input_arg[0]);


        switch( opcode) {

        case CONNECT_TO_MMM:
            
            if( num_input_args < 4) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                mexPrintf( "Disconnecting...\n");
                TheModule.DisconnectFromMMM();
                mexPrintf( "Re-connecting...\n");
            }
            ModuleID = (MODULE_ID) mxGetScalar( input_arg[1]);
            HostAddressLength = mxGetNumberOfElements( input_arg[2]);
            logger_status = (int) mxGetScalar( input_arg[3]);
            TheModule.InitVariables( ModuleID, 0);
            try{
                if( HostAddressLength > 0) {
                    // Convert unicode matlab string to regular character string
                    if( HostAddressLength > MAX_HOST_ADDR_LENGTH-1) Error( "Server name exceeds maximum allowed length");
                    HostAddress_raw = (short*) mxGetData( input_arg[2]);
                    int i;
                    for( i = 0; i < HostAddressLength; i++) HostAddress[i] = (char) HostAddress_raw[i];
                    HostAddress[i] = 0; // zero-terminate the string
                    status = TheModule.ConnectToMMM( HostAddress, logger_status);
                } else {
                    status = TheModule.ConnectToMMM( logger_status);
                }
            }catch(MyCException &E){
                MyCString err("Failed to connect to MM:");
                E.AppendTraceToString(err);
                Error(err.GetContent());
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            break;

        case DISCONNECT_FROM_MMM:

            if( num_input_args < 1) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                status = TheModule.DisconnectFromMMM( );
            } else {
                status = 0;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            break;

        case SUBSCRIBE:

            if( num_input_args < 2) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                MessageType = (MSG_TYPE) mxGetScalar( input_arg[1]);
                status = TheModule.Subscribe( MessageType);
            } else {
                status = 0;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            break;

        case UNSUBSCRIBE:

            if( num_input_args < 2) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                MessageType = (MSG_TYPE) mxGetScalar( input_arg[1]);
                status = TheModule.Unsubscribe( MessageType);
            } else {
                status = 0;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            break;

        case PAUSE_SUBSCRIPTION:

            if( num_input_args < 2) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                MessageType = (MSG_TYPE) mxGetScalar( input_arg[1]);
                status = TheModule.PauseSubscription( MessageType);
            } else {
                status = 0;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            break;

        case RESUME_SUBSCRIPTION:

            if( num_input_args < 2) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                MessageType = (MSG_TYPE) mxGetScalar( input_arg[1]);
                status = TheModule.ResumeSubscription( MessageType);
            } else {
                status = 0;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            break;

        case SEND_MESSAGE:

            if( num_input_args < 5) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                MessageType = (MSG_TYPE) mxGetScalar( input_arg[1]);
                pData = SerializeData( input_arg[2], &NumDataBytes);
                M.Set( MessageType, pData, NumDataBytes);
                DestModID = (MODULE_ID) mxGetScalar( input_arg[3]);
                DestHostID = (HOST_ID) mxGetScalar( input_arg[4]);
                status = TheModule.SendMessage( &M, DestModID, DestHostID);
                mxFree( pData);
            } else {
                status = 0;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            output_arg[1] = mxCreateDoubleScalar( M.send_time);
            output_arg[2] = mxCreateDoubleScalar( (double) M.msg_count);
            break;

        case SEND_SIGNAL:

            if( num_input_args < 4) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                MessageType = (MSG_TYPE) mxGetScalar( input_arg[1]);
                DestModID = (MODULE_ID) mxGetScalar( input_arg[2]);
                DestHostID = (HOST_ID) mxGetScalar( input_arg[3]);
				M.Set( MessageType);
                status = TheModule.SendMessage( &M, DestModID, DestHostID);
            } else {
                status = 0;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            output_arg[1] = mxCreateDoubleScalar( M.send_time);
            output_arg[2] = mxCreateDoubleScalar( (double) M.msg_count);
            break;

        case READ_MESSAGE_HDR:

            if( num_input_args < 3) Error( "incorrect number of arguments");
            if( !TheModule.IsConnected( )) {
                output_arg[0] = EmptyMatrix( );
                break;
            }

            timeout = mxGetScalar( input_arg[2]);

			begin_time = GetAbsTime();
			do {
			  try_again = false;
			  try {
				status = TheModule.ReadMessage( &M, timeout);
			  } catch( UPipeSignalException &e) {
				end_time = GetAbsTime();
				elapsed_time = end_time - begin_time;
				timeout = timeout - elapsed_time;
				if( timeout < 0) timeout = 0;
				//mexPrintf("\nSignal caught and ignored!\n");
				try_again = true;
			  }
			} while( try_again);

            if( status > 0) {
                Template = input_arg[1];
                ReturnData = C2Matlab( Template, &M, sizeof(DF_MSG_HEADER));
                output_arg[0] = ReturnData;
            } else {
                output_arg[0] = EmptyMatrix( );
            }
            break;

        case READ_MESSAGE_DATA:

            if( num_input_args < 2) Error( "incorrect number of arguments");
            Template = input_arg[1];
            ReturnData = C2Matlab( Template, M.GetDataPointer(), M.num_data_bytes);
            output_arg[0] = ReturnData;
            break;

        case SET_TIMER:

            if( num_input_args < 2) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                SnoozeTime = (unsigned int) mxGetScalar( input_arg[1]);
                TimerID = TheModule.SetTimer( SnoozeTime);
            } else {
                TimerID = -1;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) TimerID);
            break;

        case CANCEL_TIMER:
            if( num_input_args < 2) Error( "incorrect number of arguments");
            if( TheModule.IsConnected( )) {
                TimerID = (unsigned int) mxGetScalar( input_arg[1]);
                status = TheModule.CancelTimer( TimerID);
            } else {
                status = -1;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            break;

        case SEND_MODULE_READY:
            if( TheModule.IsConnected( )) {
                status = TheModule.SendModuleReady( );
            } else {
                status = 0;
            }
            output_arg[0] = mxCreateDoubleScalar( (double) status);
            break;
            
        case GET_TIME:
            output_arg[0] = mxCreateDoubleScalar( GetAbsTime());
            break;

        case GET_MODULE_ID:
            output_arg[0] = mxCreateDoubleScalar( TheModule.GetModuleID());
            break;
            
        default:
            Error( "Invalid opcode");
        }
    } catch( MyCException &e) {
        MyCString S;
        e.AppendTraceToString( S);
        char *s = S.GetContent( );
        Error( s);
    }
} // mexFunction
Exemplo n.º 10
0
Arquivo: main.cpp Projeto: ediril/BCI
void SendData(int message_type, void *data, unsigned data_size)
{
  static CMessage M;
  M.Set(message_type, data, data_size);
  dragonfly.SendMessage(&M);
}