int SwitchPanelCommandHandler(XPLMCommandRef inCommand, XPLMCommandPhase inPhase, void * inRefcon) { // XPLMDebugString("-> CP: SwitchPanelCommandHandler: start.\n"); // char Buffer[256]; // sprintf(Buffer,"Cmdh handler: 0x%08x, %d, 0x%08x\n", inCommand, inPhase, inRefcon); // XPLMDebugString(Buffer); int status = CMD_PASS_EVENT; switch ((int)(inRefcon)) { case SP_CMD_LANDING_GEAR_UP: case SP_CMD_LANDING_GEAR_DOWN: gSpGearRetract = (XPLMGetDatai(gSpGearRetractDataRef)); gSpOnGround = (XPLMGetDatai(gSpOnGroundDataRef)); XPLMGetDatavf(gSpLandingGearStatusDataRef, gSpLandingGearStatus, 0, 10); gSpGear1Fail = (XPLMGetDatai(gSpGear1FailDataRef)); gSpGear2Fail = (XPLMGetDatai(gSpGear2FailDataRef)); gSpGear3Fail = (XPLMGetDatai(gSpGear3FailDataRef)); break; default: break; } return status; }
FloatArrayDataRef::FloatArrayDataRef(QObject *parent, QString name, XPLMDataRef ref) : DataRef(parent, name, ref) { _typeString = "fa"; _type = xplmType_FloatArray; _length = XPLMGetDatavf(_ref, NULL, 0, 0); DEBUG << "Inited dataref with a length of =" << _length; _values.fill(-9999, _length); // Resize and initialize vector _valueArray = new float[_length]; }
FloatArrayDataRef::FloatArrayDataRef(QObject *parent, QString name, XPLMDataRef ref) : DataRef(parent, name, ref), _value(NULL) { _typeString = "fa"; _type = xplmType_FloatArray; _length = (float) XPLMGetDatavf(_ref, NULL, 0, 0); qDebug() << Q_FUNC_INFO << "Inited dataref with a length of =" << _length; _value = (float *)calloc(_length+1, sizeof(float)); _value[0] = (float) _length; }
void sp_update_datarefs() { gSpNumberOfBatteries = (XPLMGetDatai(gSpNumberOfBatteriesDataRef)); gSpNumberOfGenerators = (XPLMGetDatai(gSpNumberOfGeneratorsDataRef)); gSpNumberOfEngines = (XPLMGetDatai(gSpNumberOfEnginesDataRef)); gSpBatteryArrayOn = (XPLMGetDatai(gSpBatteryArrayOnDataRef)); gSpGearRetract = (XPLMGetDatai(gSpGearRetractDataRef)); gSpOnGround = (XPLMGetDatai(gSpOnGroundDataRef)); XPLMGetDatavf(gSpLandingGearStatusDataRef, gSpLandingGearStatus, 0, 10); gSpGear1Fail = (XPLMGetDatai(gSpGear1FailDataRef)); gSpGear2Fail = (XPLMGetDatai(gSpGear2FailDataRef)); gSpGear3Fail = (XPLMGetDatai(gSpGear3FailDataRef)); }
void FloatArrayDataRef::updateValue() { int valuesCopied = XPLMGetDatavf(_ref, _valueArray, 0, _length); Q_ASSERT(valuesCopied == _length); bool notequal = false; for(int i=0;i<_length;i++){ if(_values[i] != _valueArray[i]) { _values[i] = _valueArray[i]; notequal = true; } } if (notequal) emit changed(this); }
void FloatArrayDataRef::updateValue() { float newValue[_length]; long retval = XPLMGetDatavf(_ref, newValue, 0, _length); // qDebug() << Q_FUNC_INFO << "XPLMGetDatavf returns" << retval; bool notequal = false; for(long i=0;i<_length;i++){ if(_value[i+1] != newValue[i]) { _value[i+1] = newValue[i]; notequal = true; // break; } } if (notequal) emit changed(this); }
float XPlaneInterface::getSideStickRollRatio() { float values[100]; XPLMGetDatavf(findDataRefByCode(JOYSTICK_AXIS_VALUES), values, 0, 4); return values[2]; }
float sendRequestedDataCallback( float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter, void * inRefcon) { int i; int res; #if IBM char msg[80]; #endif struct RequestRecord rr; if( DEBUG ) { XPLMDebugString("XData: sendRequestedDataCallback called.\n"); } if (xdata_plugin_enabled && xdata_send_enabled && xdata_socket_open) { if( acf_packet_requested == 1 ) { acf_packet_requested = 0; sendAircraftPacket(); } // Tried a number of things, but absolute current time in millis just overflows and isn't printable via sprintf, it seems, as a long long. // So: initializing a start time, then evaluating time since that, which fits into an int. if( t_start.time == 0 ) { ftime(&t_start); XPLMDebugString("Initialising t_start.\n"); } struct timeb t_current; ftime(&t_current); int t_diff = (int) (1000.0 * (t_current.time - t_start.time) + (t_current.millitm - t_start.millitm)); // clear the buffer memset(response_data, 0, response_max_size); // sprintf(msg, "Time check: milliseconds since start = %d\n", t_diff); // XPLMDebugString(msg); strncpy(response_data, "REQD", 4); // first 4 bytes indicate packet type response_index = 8; number_of_points = 0; // work out which requests to send for( i=0; i<=max_requested_index; i++ ) { rr = request_records[i]; if( rr.enabled && request_records[i].dataref != NULL ) { //sprintf(msg, "%d - Checking %s which is scheduled for %d\n", t_diff, rr.dataref_name, rr.time_next_send); //XPLMDebugString(msg); if( rr.time_next_send == 0 || t_diff > rr.time_next_send ) { // let's work out the size of the data int size = 0; int nbrArrayValues = 0; int intValue = 0; float floatValue = 0.0f; double doubleValue = 0.0; int intArrayValues[20]; float floatArrayValues[20]; char data[900]; if( request_records[i].datatype == xplmType_Int ) { intValue = XPLMGetDatai(request_records[i].dataref); size = 4; } else if( request_records[i].datatype == xplmType_Float ) { floatValue = XPLMGetDataf(request_records[i].dataref); size = 4; } else if( request_records[i].datatype == xplmType_Double || request_records[i].datatype == 6 ) { // hack based on observed values doubleValue = XPLMGetDatad(request_records[i].dataref); size = 8; } else if( request_records[i].datatype == xplmType_FloatArray ) { nbrArrayValues = XPLMGetDatavf(request_records[i].dataref, floatArrayValues, 0, 20); size = (4 * nbrArrayValues); } else if( request_records[i].datatype == xplmType_IntArray ) { nbrArrayValues = XPLMGetDatavi(request_records[i].dataref, intArrayValues, 0, 20); size = (4 * nbrArrayValues); } else if( request_records[i].datatype == xplmType_Data ) { nbrArrayValues = XPLMGetDatab(request_records[i].dataref, data, 0, 900); size = nbrArrayValues; } // will it fit in the buffer int index_after_add = response_index + 4 + 4 + 4 + size; if( index_after_add >= response_max_size ) { // if not, send it // first update response with number of points int convertedNum = custom_htonl(number_of_points); memcpy(response_data+4, &convertedNum, 4); if( DEBUG ) { sprintf(msg, "Packet ready to go: number_of_points=%d response_index=%d\n", number_of_points, response_index); XPLMDebugString(msg); XPLMDebugString("Packet data: "); int p=0; for( p=0; p<response_index; p++ ) { sprintf(msg, "%d:%d ", p, (int)response_data[p]); XPLMDebugString(msg); } XPLMDebugString("\n"); } // ready to go for (i=0; i<NUM_DEST; i++) { if (dest_enable[i]) { res = sendto(sockfd, response_data, response_index, 0, (struct sockaddr *)&dest_sockaddr[i], sizeof(struct sockaddr)); #if IBM if ( res == SOCKET_ERROR ) { XPLMDebugString("XData: caught error while sending REQD packet! ("); sprintf(msg, "%d", WSAGetLastError()); XPLMDebugString(msg); XPLMDebugString(")\n"); } #else if ( res < 0 ) { XPLMDebugString("XData: caught error while sending REQD packet! ("); XPLMDebugString((char * const) strerror(GET_ERRNO)); XPLMDebugString(")\n"); } #endif } } XPLMDebugString("Sent packet\n"); response_index = 8; number_of_points = 0; } else { // otherwise add it to the buffer // TODO figure out how to either convert each of the above datatypes into char[] data, or // simply memcpy from the original dataref into the buffer. // ID|DATATYPE|LENGTH|DATA.... int convertedI = custom_htonl(i); memcpy(response_data+response_index, &convertedI, 4); response_index += 4; int convertedDatatype = custom_htonl(request_records[i].datatype); memcpy(response_data+response_index, &convertedDatatype, 4); response_index += 4; int convertedSize = custom_htonl(size); memcpy(response_data+response_index, &convertedSize, 4); response_index += 4; if( request_records[i].datatype == xplmType_Int ) { int convertedInt = custom_htonl(intValue); memcpy(response_data+response_index, &convertedInt, 4); response_index += 4; } else if( request_records[i].datatype == xplmType_Float ) { // skipping endianness conversion of float too, similar problem to double? //float convertedFloat = custom_htonf(floatValue); memcpy(response_data+response_index, &floatValue, 4); response_index += 4; if( DEBUG ) { sprintf(msg, "Converted a float: %f\n", floatValue); XPLMDebugString(msg); sprintf(msg, "%d %d %d %d\n", (int)response_data[response_index-4], (int)response_data[response_index-3], (int)response_data[response_index-2], (int)response_data[response_index-1]); XPLMDebugString(msg); } } else if( request_records[i].datatype == xplmType_Double || request_records[i].datatype == 6 ) { // hack based on observed values // Now this is weird. Converting the double breaks it somehow. Even reversing the byte order at the receiving // end doesn't work.. //double convertedDouble = custom_htond(doubleValue); memcpy(response_data+response_index, &doubleValue, 8); response_index += 8; } else if( request_records[i].datatype == xplmType_FloatArray ) { int d = 0; for( d = 0; d<nbrArrayValues; d++ ) { float convertedFloat = custom_htonf(floatArrayValues[d]); memcpy(response_data+response_index, &convertedFloat, 4); response_index += 4; } } else if( request_records[i].datatype == xplmType_IntArray ) { int d = 0; for( d = 0; d<nbrArrayValues; d++ ) { int convertedInt = custom_htonl(intArrayValues[d]); memcpy(response_data+response_index, &convertedInt, 4); response_index += 4; } } else if( request_records[i].datatype == xplmType_Data ) { memcpy(response_data+response_index, &data, size); response_index += size; } response_index = index_after_add; number_of_points++; if( DEBUG ) { sprintf(msg, "Packet accumulating: number_of_points=%d response_index=%d\n", number_of_points, response_index); XPLMDebugString(msg); } } // schedule next time request_records[i].time_next_send = t_diff + rr.every_millis; // sprintf(msg, "%d - Would send %s and rescheduled (every %d millis) for %d\n", t_diff, rr.dataref_name, rr.every_millis, rr.time_next_send); // XPLMDebugString(msg); } } } // does the buffer contain any data? if( number_of_points > 0 ) { // send it // first update response with number of points int convertedNum = custom_htonl(number_of_points); memcpy(response_data+4, &convertedNum, 4); if( DEBUG ) { sprintf(msg, "Packet ready to go: number_of_points=%d response_index=%d\n", number_of_points, response_index); XPLMDebugString(msg); XPLMDebugString("Packet data: "); int p=0; for( p=0; p<response_index; p++ ) { sprintf(msg, "%d:%d ", p, (int)response_data[p]); XPLMDebugString(msg); } XPLMDebugString("\n"); } // ready to go for (i=0; i<NUM_DEST; i++) { if (dest_enable[i]) { res = sendto(sockfd, response_data, response_index, 0, (struct sockaddr *)&dest_sockaddr[i], sizeof(struct sockaddr)); #if IBM if ( res == SOCKET_ERROR ) { XPLMDebugString("XData: caught error while sending REQD packet! ("); sprintf(msg, "%d", WSAGetLastError()); XPLMDebugString(msg); XPLMDebugString(")\n"); } #else if ( res < 0 ) { XPLMDebugString("XData: caught error while sending REQD packet! ("); XPLMDebugString((char * const) strerror(GET_ERRNO)); XPLMDebugString(")\n"); } #endif } } if( DEBUG ) { XPLMDebugString("Sent packet.\n"); } } /* long time_last_sent; long every_millis; long time_next_send; every 100 ms, create a buffer (well, reuse it for performance) and start populating it with eligible data. Keep going until the next ref would overflow the buffer. send the packet, then resume from the current ref. until there are no more eligible refs. send that last packet, if there's something in it. upon adding the data to the packet, update the struct with the calculated next send time. REQD|COUNT|ID|DATATYPE|LENGTH|DATA.....................................|(repeated ID, TYPE, LEN, DATA).. */ /* packet_size = createSituationPacket(); for (i=0; i<NUM_DEST; i++) { if (dest_enable[i]) { res = sendto(sockfd, (const char*)&sim_packet, packet_size, 0, (struct sockaddr *)&dest_sockaddr[i], sizeof(struct sockaddr)); #if IBM if ( res == SOCKET_ERROR ) { XPLMDebugString("XData: caught error while sending REQD packet! ("); sprintf(msg, "%d", WSAGetLastError()); XPLMDebugString(msg); XPLMDebugString(")\n"); } #else if ( res < 0 ) { XPLMDebugString("XData: caught error while sending REQD packet! ("); XPLMDebugString((char * const) strerror(GET_ERRNO)); XPLMDebugString(")\n"); } #endif } } */ return 0.1; } else { // print something to allow us to work out what happened. XPLMDebugString("XData: plugin enabled:"); if( xdata_plugin_enabled ) { XPLMDebugString("true\n"); } else { XPLMDebugString("false\n"); } XPLMDebugString("XData: send enabled:"); if( xdata_send_enabled ) { XPLMDebugString("true\n"); } else { XPLMDebugString("false\n"); } XPLMDebugString("XData: socket open:"); if( xdata_socket_open ) { XPLMDebugString("true\n"); } else { XPLMDebugString("false\n"); } return 0.1f; } }
inline void sp_led_update() { if (gSpGearRetract > 0) { if(gSpOnGround > 0) { gSpOnGroundCounter = 0; if(gSpGearSwitchUp == 1){ gearled = 0x07; } else { gearled = 0x38; } return; } else { gSpOnGroundCounter++; if (gSpOnGroundCounter < 10) { return; } } XPLMGetDatavf(gSpLandingGearStatusDataRef, gSpLandingGearStatus, 0, 10); // Gear is down and locked if (gSpLandingGearStatus[0] > 0.99) { gearled |= (1<<0); // * set bit 0 in gearled to 1 * gearled &= ~(1<<3); // * clear bit 3 in gearled to 0 * } if (gSpLandingGearStatus[1] > 0.99) { gearled |= (1<<1); // * set bit 1 in gearled to 1 * gearled &= ~(1<<4); // * clear bit 4 in gearled to 0 * } if (gSpLandingGearStatus[2] > 0.99) { gearled |= (1<<2); // * set bit 2 in gearled to 1 * gearled &= ~(1<<5); // * clear bit 5 in gearled to 0 * } // Gear is in motion if ((gSpLandingGearStatus[0] > 0.01) &&(gSpLandingGearStatus[0] < 0.99)) { gearled |= (1<<3); // * set bit 3 in gearled to 1 * gearled &= ~(1<<0); // * clear bit 0 in gearled to 0 * } if ((gSpLandingGearStatus[1] > 0.01) &&(gSpLandingGearStatus[1] < 0.99)) { gearled |= (1<<4); // * set bit 4 in gearled to 1 * gearled &= ~(1<<1); // * clear bit 1 in gearled to 0 * } if ((gSpLandingGearStatus[2] > 0.01) &&(gSpLandingGearStatus[2] < 0.99)) { gearled |= (1<<5); // * set bit 5 in gearled to 1 * gearled &= ~(1<<2); // * clear bit 2 in gearled to 0 * } // Gear is up if (gSpLandingGearStatus[0] < 0.01) { gearled &= ~(1<<0); // * clear bit 0 in gearled to 0 * gearled &= ~(1<<3); // * clear bit 3 in gearled to 0 * } if (gSpLandingGearStatus[1] < 0.01) { gearled &= ~(1<<1); // * clear bit 1 in gearled to 0 * gearled &= ~(1<<4); // * clear bit 4 in gearled to 0 * } if (gSpLandingGearStatus[2] < 0.01) { gearled &= ~(1<<2); // * clear bit 2 in gearled to 0 * gearled &= ~(1<<5); // * clear bit 5 in gearled to 0 * } if (gSpGearSwitchUp == 1) { // Nose gear had failed if (failed1dn == 0) { if (gSpGear1Fail == 6) { failed1up = 1; } if (gSpGear1Fail == 0) { failed1up = 0; } if (failed1up == 1) { gearled &= ~(1<<3); // * set bit 3 in gearled to 1 * gearled &= ~(1<<0); // * clear bit 1 in gearled to 0 * } } if (failed1dn == 1) { gearled |= (1<<3); // * set bit 3 in gearled to 1 * gearled &= ~(1<<0); // * clear bit 0 in gearled to 0 * } // Left gear has failed if (failed2dn == 0) { if (gSpGear2Fail == 6) { failed2up = 1; } if (gSpGear2Fail == 0) { failed2up = 0; } if (failed2up == 1) { gearled &= ~(1<<4); // * set bit 3 in gearled to 1 * gearled &= ~(1<<1); // * clear bit 1 in gearled to 0 * } } if (failed2dn == 1) { gearled |= (1<<4); // * set bit 3 in gearled to 1 * gearled &= ~(1<<1); // * clear bit 0 in gearled to 0 * } // Right gear has failed if (failed3dn == 0) { if (gSpGear3Fail == 6) { failed3up = 1; } if (gSpGear3Fail == 0) { failed3up = 0; } if (failed3up == 1) { gearled &= ~(1<<5); // * set bit 3 in gearled to 1 * gearled &= ~(1<<2); // * clear bit 1 in gearled to 0 * } } if (failed3dn == 1) { gearled |= (1<<5); // * set bit 3 in gearled to 1 * gearled &= ~(1<<2); // * clear bit 0 in gearled to 0 * } } if (gSpGearSwitchUp == 0) { // Nose gear has failed if (failed1up == 0) { if (gSpGear1Fail == 6) { failed1dn = 1; } if (gSpGear1Fail == 0) { failed1dn = 0; } if (failed1dn == 1) { gearled |= (1<<0); // * set bit 0 in gearled to 1 * gearled &= ~(1<<3); // * clear bit 3 in gearled to 0 * } } if (failed1up == 1) { gearled |= (1<<3); // * set bit 3 in gearled to 1 * gearled &= ~(1<<0); // * clear bit 0 in gearled to 0 * } // Left gear has failed if (failed2up == 0) { if (gSpGear2Fail == 6) { failed2dn = 1; } if (gSpGear2Fail == 0) { failed2dn = 0; } if (failed2dn == 1) { gearled |= (1<<1); // * set bit 0 in gearled to 1 * gearled &= ~(1<<4); // * clear bit 3 in gearled to 0 * } } if (failed2up == 1) { gearled |= (1<<4); // * set bit 3 in gearled to 1 * gearled &= ~(1<<1); // * clear bit 0 in gearled to 0 * } // Right gear has failed if (failed3up == 0) { if (gSpGear3Fail == 6) { failed3dn = 1; } if (gSpGear3Fail == 0) { failed3dn = 0; } if (failed3dn == 1) { gearled |= (1<<2); // * set bit 0 in gearled to 1 * gearled &= ~(1<<5); // * clear bit 3 in gearled to 0 * } } if (failed3up == 1) { gearled |= (1<<5); // * set bit 3 in gearled to 1 * gearled &= ~(1<<2); // * clear bit 0 in gearled to 0 * } } } else { gearled = 0x00; } }