bool VistaJswJoystickDriver::DoSensorUpdate(VistaType::microtime nTs) { if(JSUpdate(&m_jsd) == JSGotEvent) { _sJoyMeasure nState; for(unsigned int n=0; n < 16; ++n) { if(JSIsAxisAllocated(&m_jsd, n)) { // ok, we have that axis js_axis_struct *axis_ptr = m_jsd.axis[n]; nState.m_nAxesTs[n] = axis_ptr->time; nState.m_nAxes[n] = axis_ptr->cur; } else { nState.m_nAxesTs[n] = 0; nState.m_nAxes[n] = 0; } } for(unsigned int b=0; b < 32; ++b) { if(JSIsButtonAllocated(&m_jsd, b)) { js_button_struct *button_ptr = m_jsd.button[b]; nState.m_nButtons[b] = (button_ptr->state ? true : false); nState.m_nButtonsTs[b] = button_ptr->time; } else { nState.m_nButtons[b] = false; nState.m_nButtonsTs[b] = 0; } } VistaDeviceSensor *pSensor = GetSensorByIndex(0); VistaSensorMeasure *pM = MeasureStart( 0, nTs, true ); std::memcpy( (*pM).getWrite<void>(), &nState, sizeof(_sJoyMeasure) ); MeasureStop( 0 ); pSensor->SetUpdateTimeStamp(nTs); } return true; }
/* * Updates the information in jsd, returns JSGotEvent if there * was some change or JSNoEvent if there was no change. * * jsd needs to be previously initialized by a call to * JSInit(). */ int JSUpdate(js_data_struct *jsd) { int n; int status = JSNoEvent; #if defined(__linux__) int keep_handling = 1; int bytes_read, cycles; struct js_event event; #elif defined(__FreeBSD__) struct joystick js; #endif #if defined(__linux__) || defined(__FreeBSD__) js_axis_struct **axis; js_button_struct **button; #endif /* __linux__ || __FreeBSD__ */ if(jsd == NULL) return(status); if(jsd->fd < 0) return(status); #if defined(__linux__) || defined(__FreeBSD__) /* Reset all button state change value on all buttons */ for(n = 0, button = jsd->button; n < jsd->total_buttons; n++, button++ ) { if(*button != NULL) (*button)->changed_state = JSButtonChangedStateNone; } /* Reset current and previous axis values */ for(n = 0, axis = jsd->axis; n < jsd->total_axises; n++, axis++ ) { if(*axis != NULL) (*axis)->prev = (*axis)->cur; } #endif /* __linux__ || __FreeBSD__ */ #if defined(__linux__) /* Linux joystick device fetching * * Handle up to 16 events from joystick driver */ cycles = 0; while(keep_handling && (cycles < 16) ) { /* Get event */ bytes_read = read( jsd->fd, &event, sizeof(struct js_event) ); /* No more events to be read? */ if(bytes_read != sizeof(struct js_event)) return(status); /* Handle by event type */ switch(event.type & ~JS_EVENT_INIT) { /* Axis event */ case JS_EVENT_AXIS: /* Get axis number */ n = event.number; /* Does axis exist? */ if(JSIsAxisAllocated(jsd, n)) SetAxisValue( jsd->axis[n], (int)event.value, (time_t)event.time ); jsd->events_received++; /* Increment events recv count */ status = JSGotEvent; /* Mark that we got event */ break; /* Button event */ case JS_EVENT_BUTTON: /* Get button number */ n = event.number; /* Does button exist? */ if(JSIsButtonAllocated(jsd, n)) SetButtonValue( jsd->button[n], (int)event.value, (time_t)event.time ); jsd->events_received++; /* Increment events recv count */ status = JSGotEvent; /* Mark that we got event */ break; /* Other event */ default: keep_handling = 0; /* Stop handling events */ break; } cycles++; } #elif defined(__FreeBSD__) /* FreeBSD joystick device fetching */ if(read(jsd->fd, &js, sizeof(struct joystick)) == sizeof(struct joystick)) { status = JSGotEvent; if(JSIsAxisAllocated(jsd, 0)) SetAxisValue(jsd->axis[0], js.x, time(NULL)); if(JSIsAxisAllocated(jsd, 1)) SetAxisValue(jsd->axis[1], js.y, time(NULL)); if(JSIsButtonAllocated(jsd, 0)) SetButtonValue(jsd->button[0], js.b1, time(NULL)); if(JSIsButtonAllocated(jsd, 1)) SetButtonValue(jsd->button[1], js.b2, time(NULL)); } #endif return(status); }
/*============================================================================*/ bool VistaJswJoystickDriver::Connect() { int status = 0; const char *device = JSDefaultDevice; const char *calib = JSDefaultCalibration; IVistaDriverProtocolAspect::_cVersionTag tag; if( m_pProtocol->GetProtocol(tag) ) { if(!tag.m_strProtocolName.empty()) device = tag.m_strProtocolName.c_str(); if(!tag.m_strProtocolRevision.empty()) calib = tag.m_strProtocolRevision.c_str(); } status = JSInit( &m_jsd, device, calib, JSFlagNonBlocking ); if(status != JSSuccess) { JSClose(&m_jsd); return false; } VistaPropertyList &oWrite = m_pInfo->GetInfoPropsWrite(); oWrite.SetValue("NAME", (m_jsd.name ? std::string(m_jsd.name) : "<none>")); oWrite.SetValue("DEVICENAME", (m_jsd.device_name ? std::string(m_jsd.device_name) : "<none>")); oWrite.SetValue("CALIBRATIONFILE", (m_jsd.calibration_file ? std::string(m_jsd.calibration_file) : "<none>")); for(unsigned int n=0; n < 16; ++n) { if(JSIsAxisAllocated(&m_jsd, n)) { // ok, we have that axis js_axis_struct *axis_ptr = m_jsd.axis[n]; float nMin[3], nMax[3]; nMin[0] = nMin[1] = nMin[2] = 0.0f; nMax[0] = nMax[1] = nMax[2] = 0.0f; nMin[0] = axis_ptr->min; nMax[0] = axis_ptr->max; m_pWorkspace->SetWorkspace("AXIS_"+VistaAspectsConversionStuff::ConvertToString(n), VistaBoundingBox(nMin, nMax)); nMin[0] = axis_ptr->dz_min; nMax[0] = axis_ptr->dz_max; m_pWorkspace->SetWorkspace("DEADZONE_"+VistaAspectsConversionStuff::ConvertToString(n), VistaBoundingBox(nMin, nMax)); nMin[0] = axis_ptr->corr_coeff_min1; nMax[0] = axis_ptr->corr_coeff_max1; m_pWorkspace->SetWorkspace("CORR_COEFF1_"+VistaAspectsConversionStuff::ConvertToString(n), VistaBoundingBox(nMin, nMax)); nMin[0] = axis_ptr->corr_coeff_min2; nMax[0] = axis_ptr->corr_coeff_max2; m_pWorkspace->SetWorkspace("CORR_COEFF2_"+VistaAspectsConversionStuff::ConvertToString(n), VistaBoundingBox(nMin, nMax)); } } m_bOpened = true; return true; }