| 203 | | Finally, the following figure shows where actually the user code should be written. We provide here an skeleton to easy the deployment of new |
| 204 | | |
| 205 | | #define DATA_OUT_SZ 128 |
| 206 | | |
| 207 | | #define FIR_COEF_SZ 12 |
| 208 | | |
| 209 | | /* frequency in/out rate */ |
| 210 | | |
| 211 | | #define DATA_INOUT_RATE 1 |
| 212 | | |
| 213 | | int data_in[DATA_IN_SZ],data_out[DATA_OUT_SZ]; |
| | 203 | Finally, the following figure shows where actually the user code should be written. We provide here an skeleton to easy the deployment of new |
| | 204 | |
| | 205 | '''Figure 6 – Object implementation (myobj_imp.c)''' |
| | 206 | |
| | 207 | {{{ |
| | 208 | /** ALOE headers |
| | 209 | */ |
| | 210 | #include <phal.h> |
| | 211 | #include <phal_sw_api.h> |
| | 212 | |
| | 213 | #include "input.h" /**< Definition of interface 'input' */ |
| | 214 | #include "control.h" /**< Definition of interface 'control' */ |
| | 215 | #include "output.h" /**< Definition of interface 'output' */ |
| | 216 | |
| | 217 | #define DATA_INOUT_RATE 2 /**< Output/input frecuencies rate */ |
| | 218 | |
| | 219 | struct input_itf input; |
| | 220 | struct output_itf output; |
| | 221 | struct control_itf control; |
| | 222 | |
| 238 | | |
| 239 | | fdi = !NewFlow(“input”, FLOW_READ_ONLY); |
| 240 | | |
| 241 | | if (fdi < 0) { |
| 242 | | |
| 243 | | !WriteLog(“Error creating flow\n”); |
| 244 | | |
| 245 | | return 0; |
| 246 | | |
| | 236 | int filter_type; |
| | 237 | |
| | 238 | fdi = NewFlow(“input”, FLOW_READ_ONLY); |
| | 239 | if (fdi < 0) { |
| | 240 | WriteLog(“Error creating flow\n”); |
| | 241 | return 0; |
| | 242 | } |
| | 243 | fdo = CreateFlow(“output”, FLOW_WRITE_ONLY); |
| | 244 | if (fdo < 0) { |
| | 245 | WriteLog(“Error creating flow\n”); |
| | 246 | return 0; |
| | 247 | } |
| | 248 | fdc = CreateFlow(“control”, FLOW_READ_ONLY); |
| | 249 | if (fdo < 0) { |
| | 250 | WriteLog(“Error creating flow\n”); |
| | 251 | return 0; |
| | 252 | } |
| | 253 | n = InitParamFile(); |
| | 254 | if (n < 0) { |
| | 255 | WriteLog (“Error initiating params file\n”); |
| | 256 | return 0; |
| | 257 | } |
| | 258 | n = GetParameter(“freq”, &freq, 1); |
| | 259 | if (n < 0) { |
| | 260 | WriteLog (“Error getting parameter\n”); |
| | 261 | return 0; |
| | 262 | } |
| | 263 | n = GetParameter(“filter_type”, &filter_type, 1); |
| | 264 | if (n < 0) { |
| | 265 | WriteLog (“Error getting parameter\n”); |
| | 266 | return 0; |
| | 267 | } |
| | 268 | calc_fir_coefs(fir_coef,filter_type); |
| | 269 | n = InitStatsFile(); |
| | 270 | if (n < 0) { |
| | 271 | WriteLog (“Error initiating stats\n”); |
| | 272 | return 0; |
| | 273 | } |
| | 274 | stat_outsignal = InitStat(“out_signal”); |
| | 275 | if (stat_outsignal < 0) { |
| | 276 | WriteLog (“Error initiating stat\n”); |
| | 277 | return 0; |
| | 278 | } |
| | 279 | rcv_len = 0; |
| | 280 | return 1; |
| 249 | | fdo = !CreateFlow(“output”, FLOW_WRITE_ONLY); |
| 250 | | |
| 251 | | if (fdo < 0) { |
| 252 | | |
| 253 | | !WriteLog(“Error creating flow\n”); |
| 254 | | |
| 255 | | return 0; |
| 256 | | |
| | 283 | /** Run function. |
| | 284 | * @return 1 if ok, 0 if error |
| | 285 | */ |
| | 286 | int Run() |
| | 287 | { |
| | 288 | |
| | 289 | /* Get number of samples to generate in current timeslot only if last block is already send*/ |
| | 290 | if (rcv_len == 0) { |
| | 291 | long_out_block = GetTempo(freq); |
| | 292 | long_in_block = long_out_block/DATA_INOUT_RATE; |
| | 293 | if (long_out_block > OUTPUT_MAX_DATA) { |
| | 294 | WriteLog(“Error requested tempo exceeds output buffer.\n”); |
| | 295 | return 0; |
| | 296 | } |
| | 297 | if (long_in_block > INPUT_MAX_DATA) { |
| | 298 | WriteLog(“Error requested tempo exceeds input buffer.\n”); |
| | 299 | return 0; |
| | 300 | } |
| | 301 | /* Read from control interface */ |
| | 302 | n = ReadFlow(fdc, &control, sizeof(struct control_itf)); |
| | 303 | if (n < 0) { |
| | 304 | WriteLog(“Error reading from control flow\n”); |
| | 305 | return 0; |
| | 306 | } else if (n>0) { |
| | 307 | do_my_control_reconfiguration(&control); |
| | 308 | } |
| | 309 | } |
| | 310 | /* receive a block of data */ |
| | 311 | do { |
| | 312 | n = ReadFlow(fdi, &input.data[rcv_len], long_in_block - rcv_len); |
| | 313 | if (n < 0) { |
| | 314 | WriteLog(“Error reading from flow\n”); |
| | 315 | return 0; |
| | 316 | } else if (!n) { |
| | 317 | break; |
| | 318 | } |
| | 319 | rcv_len += n; |
| | 320 | } while(n && rcv_len < long_in_block); |
| | 321 | |
| | 322 | /* when enough data is received, process and send it */ |
| | 323 | if (rcv_len == long_block) { |
| | 324 | my_process_function(input, output, fir_coef, long_in_block); |
| | 325 | n = WriteFlow(fdo, data_out.data, long_out_block); |
| | 326 | if (n < 0) { |
| | 327 | LogWrite(“Error writing to flow\n”); |
| | 328 | return 0; |
| | 329 | } else if (!n) { |
| | 330 | LogWrite(“Caution, missing packet due to full buffer\n”); |
| | 331 | } |
| | 332 | |
| | 333 | rcv_len=0; |
| | 334 | /* set stats variable */ |
| | 335 | SetStatsValue(stat_outsignal,output.data,long_out_block); |
| | 336 | } |
| 258 | | |
| 259 | | n = !InitParamFile(); |
| 260 | | |
| 261 | | if (n < 0) { |
| 262 | | |
| 263 | | !WriteLog (“Error initiating params file\n”); |
| 264 | | |
| 265 | | return 0; |
| 266 | | |
| 267 | | } |
| 268 | | |
| 269 | | n = !GetParameter(“freq”, &freq, 1); |
| 270 | | |
| 271 | | if (n < 0) { |
| 272 | | |
| 273 | | !WriteLog (“Error getting parameter\n”); |
| 274 | | |
| 275 | | return 0; |
| 276 | | |
| 277 | | } |
| 278 | | |
| 279 | | n = !GetParameter(“nof_coef”, &nof_coef, 1); |
| 280 | | |
| 281 | | if (n < 0) { |
| 282 | | |
| 283 | | !WriteLog (“Error getting parameter\n”); |
| 284 | | |
| 285 | | return 0; |
| 286 | | |
| 287 | | } |
| 288 | | |
| 289 | | if (nof_coef > FIR_COEF_SZ) { |
| 290 | | |
| 291 | | !WriteLog (“Error getting parameter, too long\n”); |
| 292 | | |
| 293 | | return 0; |
| 294 | | |
| 295 | | } |
| 296 | | |
| 297 | | memset(fir_coef, 0, FIR_COEF_SZ * 4); |
| 298 | | |
| 299 | | n = !GetParameter(“fir_coef”, fir_coef, nof_coef); |
| 300 | | |
| 301 | | if (n < 0) { |
| 302 | | |
| 303 | | !WriteLog (“Error getting parameter\n”); |
| 304 | | |
| 305 | | return 0; |
| 306 | | |
| 307 | | } |
| 308 | | |
| 309 | | n = !InitStatsFile(); |
| 310 | | |
| 311 | | if (n < 0) { |
| 312 | | |
| 313 | | !WriteLog (“Error initiating stats\n”); |
| 314 | | |
| 315 | | return 0; |
| 316 | | |
| 317 | | } |
| 318 | | |
| 319 | | stat_outsignal = !InitStat(“out_signal”); |
| 320 | | |
| 321 | | if (stat_outsignal < 0) { |
| 322 | | |
| 323 | | !WriteLog (“Error initiating stat\n”); |
| 324 | | |
| 325 | | return 0; |
| 326 | | |
| 327 | | } |
| 328 | | |
| 329 | | rcv_len = 0; |
| 330 | | |
| 331 | | return 1; |
| 332 | | |
| 333 | | } |
| 334 | | |
| 335 | | /** Run function. |
| 336 | | |
| 337 | | * @return 1 if ok, 0 if error |
| 338 | | |
| 339 | | */ |
| 340 | | |
| 341 | | int Run() |
| 342 | | |
| 343 | | { |
| 344 | | |
| 345 | | /* Get number of samples to generate in current timeslot only if last block is already send*/ |
| 346 | | |
| 347 | | if (rcv_len == 0) { |
| 348 | | |
| 349 | | long_block = !GetTempo(freq); |
| 350 | | |
| 351 | | if (long_block > DATA_OUT_SZ) { |
| 352 | | |
| 353 | | !WriteLog(“Error requested tempo exceeds output buffer.\n”); |
| 354 | | |
| 355 | | return 0; |
| 356 | | |
| 357 | | } |
| 358 | | |
| 359 | | if (long_block * DATA_INOUT_RATE > DATA_IN_SZ) { |
| 360 | | |
| 361 | | !WriteLog(“Error requested tempo exceeds input buffer.\n”); |
| 362 | | |
| 363 | | return 0; |
| 364 | | |
| 365 | | } |
| 366 | | |
| 367 | | } |
| 368 | | |
| 369 | | /* receive a block of data */ |
| 370 | | |
| 371 | | do { |
| 372 | | |
| 373 | | n = !ReadFlow(fdi, data_in, long_block / DATA_INOUT_RATE - rcv_len); |
| 374 | | |
| 375 | | if (n < 0) { |
| 376 | | |
| 377 | | !WriteLog(“Error reading from flow\n”); |
| 378 | | |
| 379 | | return 0; |
| 380 | | |
| 381 | | } else if (!n) { |
| 382 | | |
| 383 | | break; |
| 384 | | |
| 385 | | } |
| 386 | | |
| 387 | | rcv_len += n; |
| 388 | | |
| 389 | | } while(n && rcv_len < long_block / DATA_INOUT_RATE); |
| 390 | | |
| 391 | | /* when enough data is received, process and send it */ |
| 392 | | |
| 393 | | if (rcv_len * DATA_INOUT_RATE == long_block) { |
| 394 | | |
| 395 | | my_process_function(data_in, data_out, fir_coef, long_block); |
| 396 | | |
| 397 | | /* write output data to output flow */ |
| 398 | | |
| 399 | | n = !WriteFlow(fdo, data_out, long_block); |
| 400 | | |
| 401 | | if (n < 0) { |
| 402 | | |
| 403 | | !LogWrite(“Error writing to flow\n”); |
| 404 | | |
| 405 | | return 0; |
| 406 | | |
| 407 | | } else if (!n) { |
| 408 | | |
| 409 | | !LogWrite(“Caution, missing packet due to full buffer\n”); |
| 410 | | |
| 411 | | } |
| 412 | | |
| 413 | | /* reset rcv counter */ |
| 414 | | |
| 415 | | rcv_len=0; |
| 416 | | |
| 417 | | /* set stats variable */ |
| 418 | | |
| 419 | | !SetStatsValue(stat_outsignal,data_out,long_block); |
| 420 | | |
| 421 | | } |
| 422 | | |
| 423 | | } |
| 424 | | |
| | 338 | }}} |
| | 339 | == [[BR]][[BR]] == |