3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include <simgear/debug/logstream.hxx>
19 #include <simgear/io/iochannel.hxx>
24 #include <FDM/flight.hxx>
25 #include <Main/globals.hxx>
26 #include <Cockpit/radiostack.hxx>
30 FGOpenGC::FGOpenGC() {
33 FGOpenGC::~FGOpenGC() {
36 // open hailing frequencies
37 bool FGOpenGC::open() {
39 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
40 << "is already in use, ignoring" );
44 SGIOChannel *io = get_io_channel();
46 if ( ! io->open( get_direction() ) ) {
47 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
57 static void collect_data( const FGInterface *fdm, ogcFGData *data ) {
58 //static void collect_data( ogcFGData *data ) {
60 FGEngInterface *p_engine[4]; // four is enough unless you're a BUF
62 p_engine[0] = cur_fdm_state->get_engine(0);
64 data->latitude = fdm->get_Longitude_deg();
65 data->longitude = cur_fdm_state->get_Latitude_deg();
67 data->pitch = cur_fdm_state->get_Theta_deg();
68 data->bank = cur_fdm_state->get_Phi_deg();
69 data->heading = cur_fdm_state->get_Psi_deg();
70 data->altitude = cur_fdm_state->get_Altitude();
71 data->v_kcas = cur_fdm_state->get_V_calibrated_kts();
72 data->vvi = cur_fdm_state->get_Climb_Rate();
74 data->magvar = globals->get_mag()->get_magvar();
76 // engine data, for now set the 2nd engine equal to the first
78 data->rpm[0] = p_engine[0]->get_RPM();
79 data->rpm[1] = p_engine[0]->get_RPM();
81 data->epr[0] = p_engine[0]->get_Manifold_Pressure();
82 data->epr[1] = p_engine[0]->get_Manifold_Pressure();
84 data->egt[0] = p_engine[0]->get_EGT();
85 data->egt[1] = p_engine[0]->get_EGT();
87 data->oil_pressure[0] = p_engine[0]->get_Oil_Pressure();
88 data->oil_pressure[1] = p_engine[0]->get_Oil_Pressure();
92 // Once OPenGC develops a comparable navaids database some of this will not be required
94 //data->nav1_ident = current_radiostack->get_nav1_ident();
95 data->nav1_freq = current_radiostack->get_nav1_freq();
96 data->nav1_radial = current_radiostack->get_nav1_sel_radial();
97 data->nav1_course_dev = current_radiostack->get_nav1_heading_needle_deflection();
99 //data->nav1_ident = current_radiostack->get_nav1_ident();
100 data->nav2_freq = current_radiostack->get_nav2_freq();
101 data->nav2_radial = current_radiostack->get_nav2_sel_radial();
102 data->nav2_course_dev = current_radiostack->get_nav2_heading_needle_deflection();
106 static void distribute_data( const ogcFGData *data, FGInterface *chunk ) {
107 // just a place holder until the CDU is developed
111 // process work for this port
112 bool FGOpenGC::process() {
113 SGIOChannel *io = get_io_channel();
114 int length = sizeof(buf);
116 if ( get_direction() == SG_IO_OUT ) {
117 collect_data( cur_fdm_state, &buf );
118 //collect_data( &buf );
119 if ( ! io->write( (char *)(& buf), length ) ) {
120 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
123 } else if ( get_direction() == SG_IO_IN ) {
124 if ( io->get_type() == sgFileType ) {
125 if ( io->read( (char *)(& buf), length ) == length ) {
126 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
127 distribute_data( &buf, cur_fdm_state );
130 while ( io->read( (char *)(& buf), length ) == length ) {
131 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
132 distribute_data( &buf, cur_fdm_state );
142 bool FGOpenGC::close() {
143 SGIOChannel *io = get_io_channel();
145 set_enabled( false );
147 if ( ! io->close() ) {