Datum ComponentRegistry::getNumber(std::string expression, GenericDataType type){

  Datum value;
    switch(type){
      case M_FLOAT:
      case M_INTEGER:
      case M_BOOLEAN:
      case M_STRING:
          value = getValue(expression, type);
          if (value.getDataType() == type) {
              return value;
          }
          break;
      default:
          throw FatalParserException("Attempt to cast a number of invalid type");
    }
	
	switch (type){
	
		case M_FLOAT:
			return Datum(value.getFloat());
		case M_INTEGER:
			return Datum(value.getInteger());
		case M_STRING:
			return Datum(value.getString());
		case M_BOOLEAN:
			return Datum(value.getBool());
		default:
			return value;			
	}
}
void StimulusDisplay::stateSystemCallback(const Datum &data, MWorksTime time) {
    unique_lock lock(display_lock);
    
    int newState = data.getInteger();
    
    if ((IDLE == newState) && displayLinksRunning) {
        
        // If another thread is waiting for a display refresh, allow it to complete before stopping
        // the display link
        while (waitingForRefresh) {
            refreshCond.wait(lock);
        }
        
        displayLinksRunning = false;
        currentOutputTimeUS = -1;
        
        // We need to release the lock before calling CVDisplayLinkStop, because
        // StimulusDisplay::displayLinkCallback could be blocked waiting for the lock, and
        // CVDisplayLinkStop won't return until displayLinkCallback exits, leading to deadlock.
        lock.unlock();
        
        // NOTE: As of OS X 10.11, stopping the display links from a non-main thread causes issues
        dispatch_sync(dispatch_get_main_queue(), ^{
            for (auto dl : displayLinks) {
                if (kCVReturnSuccess != CVDisplayLinkStop(dl)) {
                    merror(M_DISPLAY_MESSAGE_DOMAIN,
                           "Unable to stop updates on display %d",
                           CVDisplayLinkGetCurrentCGDisplay(dl));
                }
            }
        });
        
        mprintf(M_DISPLAY_MESSAGE_DOMAIN, "Display updates stopped");
        
    } else if ((RUNNING == newState) && !displayLinksRunning) {
void IODeviceVariableNotification::notify(const Datum& data, MWTime timeUS) {
	if(data.getInteger() == IDLE) {
		shared_ptr<IODevice> io_dev_shared = io_device.lock();
        if(io_dev_shared != NULL){ 
            io_dev_shared->stopDeviceIO();
        }
	}
}
Exemple #4
0
boost::python::object convert_datum_to_python(const Datum &datum) {
    ScopedRecursionGuard srg(" while converting MWorks datum to Python object");
    
    if (datum.isUndefined()) {
        return boost::python::object();
    }
    
    switch (datum.getDataType()) {
        case M_INTEGER:
            return convert_longlong_to_python(datum.getInteger());
            
        case M_FLOAT:
            return manageNewRef( PyFloat_FromDouble(datum.getFloat()) );
            
        case M_BOOLEAN:
            return manageNewRef( PyBool_FromLong(datum.getBool()) );
            
        case M_STRING: {
            auto &str = datum.getString();
            return manageNewRef( PyString_FromStringAndSize(str.c_str(), str.size()) );
        }
            
        case M_LIST: {
            auto &listValue = datum.getList();
            boost::python::object list = manageNewRef( PyList_New(listValue.size()) );
            
            for (Py_ssize_t i = 0; i < listValue.size(); i++) {
                boost::python::object item = convert_datum_to_python(listValue.at(i));
                // PyList_SetItem "steals" the item reference, so we need to INCREF it
                Py_INCREF(item.ptr());
                if (PyList_SetItem(list.ptr(), i, item.ptr()))
                    throw_error_already_set();
            }
            
            return list;
        }
            
        case M_DICTIONARY: {
            boost::python::object dict = manageNewRef( PyDict_New() );
            
            for (auto &item : datum.getDict()) {
                if (PyDict_SetItem(dict.ptr(),
                                   convert_datum_to_python(item.first).ptr(),
                                   convert_datum_to_python(item.second).ptr()))
                {
                    throw_error_already_set();
                }
            }
            
            return dict;
        }
            
        default:
            PyErr_Format(PyExc_TypeError, "Cannot convert Datum of unknown type (%d)", datum.getDataType());
            throw_error_already_set();
            return boost::python::object();  // Never reached
    }
}
void StimulusDisplay::stateSystemCallback(const Datum &data, MWorksTime time) {
    unique_lock lock(display_lock);

    int newState = data.getInteger();

    if (IDLE == newState) {
        
        if (CVDisplayLinkIsRunning(displayLink)) {
            // If another thread is waiting for a display refresh, allow it to complete before stopping
            // the display link
            while (waitingForRefresh) {
                refreshCond.wait(lock);
            }

            // We need to release the lock before calling CVDisplayLinkStop, because
            // StimulusDisplay::displayLinkCallback could be blocked waiting for the lock, and
            // CVDisplayLinkStop won't return until displayLinkCallback exits, leading to deadlock.
            lock.unlock();
            
            if (kCVReturnSuccess != CVDisplayLinkStop(displayLink)) {
                merror(M_DISPLAY_MESSAGE_DOMAIN, "Unable to stop display updates");
            } else {
                currentOutputTimeUS = -1;
                mprintf(M_DISPLAY_MESSAGE_DOMAIN, "Display updates stopped");
            }
        }
        
    } else if (RUNNING == newState) {

        if (!CVDisplayLinkIsRunning(displayLink)) {

            lastFrameTime = 0;

            if (kCVReturnSuccess != CVDisplayLinkStart(displayLink)) {
                merror(M_DISPLAY_MESSAGE_DOMAIN, "Unable to start display updates");
            } else {
                // Wait for a refresh to complete, so we know that getCurrentOutputTimeUS() will return a valid time
                ensureRefresh(lock);
                
                mprintf(M_DISPLAY_MESSAGE_DOMAIN,
                        "Display updates started (main = %d, current = %d)",
                        CGMainDisplayID(),
                        CVDisplayLinkGetCurrentCGDisplay(displayLink));
            }

        }
        
    }
}
void DynamicStimulusDriver::stateSystemCallback(const Datum &data, MWorksTime time) {
    switch (data.getInteger()) {
        case IDLE:
            stop();
            break;
            
        case RUNNING:
            unpause();
            break;
            
        case PAUSED:
            pause();
            break;
    }
}