| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2009 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE freqmod. |
|---|
| 6 | |
|---|
| 7 | OSSIE freqmod is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | OSSIE freqmod is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU General Public License |
|---|
| 18 | along with OSSIE ChannelDemo; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | ****************************************************************************/ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #include <string> |
|---|
| 25 | #include <iostream> |
|---|
| 26 | #include "freqmod.h" |
|---|
| 27 | #include "math.h" |
|---|
| 28 | #include "ossie/debug.h" |
|---|
| 29 | |
|---|
| 30 | freqmod_i::freqmod_i(const char *uuid, omni_condition *condition) : |
|---|
| 31 | Resource_impl(uuid), component_running(condition) |
|---|
| 32 | { |
|---|
| 33 | dataIn_0 = new standardInterfaces_i::complexShort_p("real_in"); |
|---|
| 34 | dataOut_0 = new standardInterfaces_i::complexShort_u("complex_out"); |
|---|
| 35 | |
|---|
| 36 | //Create the thread for the writer's processing function |
|---|
| 37 | processing_thread = new omni_thread(Run, (void *) this); |
|---|
| 38 | |
|---|
| 39 | //Start the thread containing the writer's processing function |
|---|
| 40 | processing_thread->start(); |
|---|
| 41 | |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | freqmod_i::~freqmod_i(void) |
|---|
| 45 | { |
|---|
| 46 | delete dataIn_0; |
|---|
| 47 | delete dataOut_0; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | // Static function for omni thread |
|---|
| 51 | void freqmod_i::Run( void * data ) |
|---|
| 52 | { |
|---|
| 53 | ((freqmod_i*)data)->ProcessData(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | CORBA::Object_ptr freqmod_i::getPort( const char* portName ) throw ( |
|---|
| 57 | CORBA::SystemException, CF::PortSupplier::UnknownPort) |
|---|
| 58 | { |
|---|
| 59 | DEBUG(3, freqmod, "getPort() invoked with " << portName) |
|---|
| 60 | |
|---|
| 61 | CORBA::Object_var p; |
|---|
| 62 | |
|---|
| 63 | p = dataIn_0->getPort(portName); |
|---|
| 64 | |
|---|
| 65 | if (!CORBA::is_nil(p)) |
|---|
| 66 | return p._retn(); |
|---|
| 67 | |
|---|
| 68 | p = dataOut_0->getPort(portName); |
|---|
| 69 | |
|---|
| 70 | if (!CORBA::is_nil(p)) |
|---|
| 71 | return p._retn(); |
|---|
| 72 | |
|---|
| 73 | /*exception*/ |
|---|
| 74 | throw CF::PortSupplier::UnknownPort(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void freqmod_i::start() throw (CORBA::SystemException, |
|---|
| 78 | CF::Resource::StartError) |
|---|
| 79 | { |
|---|
| 80 | DEBUG(3, freqmod, "start() invoked") |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void freqmod_i::stop() throw (CORBA::SystemException, CF::Resource::StopError) |
|---|
| 84 | { |
|---|
| 85 | DEBUG(3, freqmod, "stop() invoked") |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | void freqmod_i::releaseObject() throw (CORBA::SystemException, |
|---|
| 89 | CF::LifeCycle::ReleaseError) |
|---|
| 90 | { |
|---|
| 91 | DEBUG(3, freqmod, "releaseObject() invoked") |
|---|
| 92 | |
|---|
| 93 | component_running->signal(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void freqmod_i::initialize() throw (CF::LifeCycle::InitializeError, |
|---|
| 97 | CORBA::SystemException) |
|---|
| 98 | { |
|---|
| 99 | DEBUG(3, freqmod, "initialize() invoked") |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | //OSSIE generated configure method |
|---|
| 103 | //void freqmod_i::configure(const CF::Properties& props) |
|---|
| 104 | //throw (CORBA::SystemException, |
|---|
| 105 | // CF::PropertySet::InvalidConfiguration, |
|---|
| 106 | // CF::PropertySet::PartialConfiguration) |
|---|
| 107 | //{ |
|---|
| 108 | // DEBUG(3, freqmod, "configure() invoked") |
|---|
| 109 | // |
|---|
| 110 | // std::cout << "props length : " << props.length() << std::endl; |
|---|
| 111 | // |
|---|
| 112 | // for (unsigned int i = 0; i <props.length(); i++) |
|---|
| 113 | // { |
|---|
| 114 | // std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 115 | // |
|---|
| 116 | // if (strcmp(props[i].id, "DCE:03ab4ef4-c56d-11de-a210-000c29451dde") == 0) |
|---|
| 117 | // { |
|---|
| 118 | // CORBA::Long simple_temp; |
|---|
| 119 | // props[i].value >>= simple_temp; |
|---|
| 120 | // freq_dev = simple_temp; |
|---|
| 121 | // |
|---|
| 122 | // } |
|---|
| 123 | // if (strcmp(props[i].id, "DCE:23f3457c-c56d-11de-a210-000c29451dde") == 0) |
|---|
| 124 | // { |
|---|
| 125 | // CORBA::Long simple_temp; |
|---|
| 126 | // props[i].value >>= simple_temp; |
|---|
| 127 | // samp_freq = simple_temp; |
|---|
| 128 | // |
|---|
| 129 | // } |
|---|
| 130 | // } |
|---|
| 131 | //} |
|---|
| 132 | void freqmod_i::query (CF::Properties & configProperties) |
|---|
| 133 | throw (CORBA::SystemException, CF::UnknownProperties) |
|---|
| 134 | { |
|---|
| 135 | if (configProperties.length () == 0) |
|---|
| 136 | { |
|---|
| 137 | configProperties.length (propertySet.length ()); |
|---|
| 138 | for (unsigned int i = 0; i < propertySet.length (); i++) |
|---|
| 139 | { |
|---|
| 140 | configProperties[i].id = CORBA::string_dup (propertySet[i].id); |
|---|
| 141 | configProperties[i].value = propertySet[i].value; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | return ; |
|---|
| 145 | } |
|---|
| 146 | else { |
|---|
| 147 | for (unsigned int i = 0; i < configProperties.length(); i++) { |
|---|
| 148 | for (unsigned int j=0; j < propertySet.length(); j++) { |
|---|
| 149 | if ( strcmp(configProperties[i].id, propertySet[i].id) == 0 ){ |
|---|
| 150 | configProperties[i].value = propertySet[i].value; |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | //Configure method written to work with WAVEDASH |
|---|
| 158 | void freqmod_i::configure(const CF::Properties& props) |
|---|
| 159 | throw (CORBA::SystemException, |
|---|
| 160 | CF::PropertySet::InvalidConfiguration, |
|---|
| 161 | CF::PropertySet::PartialConfiguration) |
|---|
| 162 | { |
|---|
| 163 | static int init = 0; |
|---|
| 164 | |
|---|
| 165 | DEBUG(3, freqmod, "configure() invoked") |
|---|
| 166 | |
|---|
| 167 | if (init == 0){ |
|---|
| 168 | if ( props.length() <= 0 ){ |
|---|
| 169 | std::cout << "freqmod: configure called with invalid props.length() - " << props.length() << std::endl; |
|---|
| 170 | return; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | propertySet.length(props.length()); |
|---|
| 174 | for (unsigned int i=0; i < props.length(); i++) { |
|---|
| 175 | propertySet[i].id = CORBA::string_dup(props[i].id); |
|---|
| 176 | propertySet[i].value = props[i].value; |
|---|
| 177 | } |
|---|
| 178 | init++; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | for (unsigned int i = 0; i <props.length(); i++) { |
|---|
| 182 | |
|---|
| 183 | if (strcmp(props[i].id, "DCE:03ab4ef4-c56d-11de-a210-000c29451dde") == 0) { |
|---|
| 184 | // Frequency Deviation |
|---|
| 185 | CORBA::Long simple_temp; |
|---|
| 186 | props[i].value >>= simple_temp; |
|---|
| 187 | // Test: setting propertySet[] to the given value. |
|---|
| 188 | freq_dev = simple_temp; |
|---|
| 189 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 190 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 191 | propertySet[i].value = props[i].value; |
|---|
| 192 | break; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | DEBUG(1, ChannelDemo, "Setting Frequency Deviation to " << freq_dev); |
|---|
| 196 | } else if (strcmp(props[i].id, "DCE:23f3457c-c56d-11de-a210-000c29451dde") == 0) { |
|---|
| 197 | // Sampling Frequency |
|---|
| 198 | CORBA::Long simple_temp; |
|---|
| 199 | props[i].value >>= simple_temp; |
|---|
| 200 | // Test: Setting propertySet[i] to the given value |
|---|
| 201 | propertySet[i].value = props[i].value; |
|---|
| 202 | samp_freq = simple_temp; |
|---|
| 203 | DEBUG(1, ChannelDemo, "Setting Sampling Frequency to " << samp_freq << " degrees"); |
|---|
| 204 | |
|---|
| 205 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 206 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 207 | propertySet[i].value = props[i].value; |
|---|
| 208 | break; |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | } else if (strcmp(props[i].id, "port_list") == 0) { |
|---|
| 213 | // Frequency Deviation |
|---|
| 214 | CORBA::Short simple_temp; |
|---|
| 215 | props[i].value >>= simple_temp; |
|---|
| 216 | // Test: setting propertySet[] to the given value. |
|---|
| 217 | for (unsigned int j=0; j < propertySet.length(); j++ ) { |
|---|
| 218 | if ( strcmp(propertySet[j].id, props[i].id) == 0 ) { |
|---|
| 219 | propertySet[i].value = props[i].value; |
|---|
| 220 | break; |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | DEBUG(1, ChannelDemo, "Setting Frequency Deviation to " << freq_dev); |
|---|
| 224 | } else { |
|---|
| 225 | // unknown property |
|---|
| 226 | std::cerr << "ERROR: freqmod::configure() unknown property \"" |
|---|
| 227 | << props[i].id << "\"" << std::endl; |
|---|
| 228 | throw CF::PropertySet::InvalidConfiguration(); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | void freqmod_i::ProcessData() |
|---|
| 235 | { |
|---|
| 236 | DEBUG(3, freqmod, "ProcessData() invoked") |
|---|
| 237 | |
|---|
| 238 | PortTypes::ShortSequence I_out_0, Q_out_0; |
|---|
| 239 | |
|---|
| 240 | //declare variables |
|---|
| 241 | float pi = 3.1415927; |
|---|
| 242 | double theta=0; //current output angle |
|---|
| 243 | double theta_prev=0; //previous output angle |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | PortTypes::ShortSequence *I_in_0(NULL), *Q_in_0(NULL); |
|---|
| 247 | CORBA::UShort I_in_0_length, Q_in_0_length; |
|---|
| 248 | |
|---|
| 249 | while(1) |
|---|
| 250 | { |
|---|
| 251 | dataIn_0->getData(I_in_0, Q_in_0); |
|---|
| 252 | |
|---|
| 253 | I_in_0_length = I_in_0->length(); |
|---|
| 254 | Q_in_0_length = Q_in_0->length(); |
|---|
| 255 | |
|---|
| 256 | I_out_0.length(I_in_0->length()); //must define length of output |
|---|
| 257 | Q_out_0.length(Q_in_0->length()); //must define length of output |
|---|
| 258 | |
|---|
| 259 | /*insert code here to do work*/ |
|---|
| 260 | //These variables may be refreshed at the beginning of each itiration |
|---|
| 261 | double sens; |
|---|
| 262 | int j=0; |
|---|
| 263 | |
|---|
| 264 | //Define the frequency sensitivity in terms of the desired frequency deviation and sampling rate |
|---|
| 265 | sens = (double)freq_dev/samp_freq; |
|---|
| 266 | |
|---|
| 267 | //generate a discrete approximation of an FM signal recursively |
|---|
| 268 | for (unsigned int k=0; k < I_in_0->length(); k++) |
|---|
| 269 | { |
|---|
| 270 | //compute the ouput angle |
|---|
| 271 | theta = theta_prev+(double)(*I_in_0)[k]/32768; |
|---|
| 272 | theta_prev = theta; |
|---|
| 273 | //compute the output |
|---|
| 274 | I_out_0[k] = (short int)(.25*32768)*(double)cos((double)(2*pi*sens*theta)); |
|---|
| 275 | Q_out_0[k] = (short int)(.25*32768)*(double)sin((double)(2*pi*sens*theta)); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | dataIn_0->bufferEmptied(); |
|---|
| 279 | dataOut_0->pushPacket(I_out_0, Q_out_0); |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | |
|---|