Newer
Older
//=============================================================================
// TIMIQLib.cpp
//=============================================================================
// abstraction.......TimIQ Application Programming Interface
// class.............TIMIQLib
// original author...J. GOUNO - NEXEYA-FRANCE
//=============================================================================
// ============================================================================
// DEPENDENCIES
// ============================================================================
#include <timiq/TIMIQLib.h>
#include "TIMIQCurl.h"
namespace TIMIQLib_ns {
// ============================================================================
//- Check low level access to TIMIQ hardware pointer:
#define CHECK_TIMIQ_HW \
if (!m_timiq_hw) \
{ \
throw Exception(static_cast<const char*>("INTERNAL_ERROR"), \
static_cast<const char*>("Low level access to TIMIQ hardware pointer is null!"), \
static_cast<const char*>("CHECK_TIMIQ_HW()")); \
}
// ============================================================================
// ============================================================================
// TIMIQLib::TIMIQLib
// ============================================================================
TIMIQLib::TIMIQLib ()
{
m_timiq_hw = NULL;
m_ipAddress = "";
m_numPort = "";
m_internal_timiq_state = Undefined;
m_timiq_task = NULL;
}
// ============================================================================
// TIMIQLib::~TIMIQLib()
// ============================================================================
TIMIQLib::~TIMIQLib()
{
if (m_timiq_hw)
{
delete m_timiq_hw;
m_timiq_hw = NULL;
}
//-
if (m_timiq_task)
{
m_timiq_task->exit();
}
// ============================================================================
// TIMIQLib::init
// ============================================================================
void TIMIQLib::init(const std::string& ip_address, const short& num_port)
throw (Exception)
{
//- chek Ip Address/ num port
if (ip_address.empty() || num_port == 0)
throw Exception(static_cast<const char*>("INTERNAL_ERROR"),
static_cast<const char*>("Ip address or port number not properly defined!"),
static_cast<const char*>("TIMIQLib::init"));
//-
m_ipAddress = ip_address;
char buffer[20];
sprintf(buffer, "%d", num_port);
m_numPort = buffer;
// instantiate TIMIQCurl pointer
m_timiq_hw = new TIMIQCurl(m_ipAddress, m_numPort);
CHECK_TIMIQ_HW;
}
// ============================================================================
// TIMIQLib::set_data
// ============================================================================
void TIMIQLib::set_data(float data)
throw (Exception)
{
//- Threading configuration
timIQConfig l_ti_cfg;
//- set I value config
l_ti_cfg.id = TI_DATA;
l_ti_cfg.value = data;
//- force timiq state = Busy
m_internal_timiq_state = Busy;
m_timiq_task = new ThreadedAction(static_cast<yat::Thread::IOArg>(this), l_ti_cfg);
if (m_timiq_task == NULL)
{
std::string msg_err = "Set data error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::set_data"));
}
//- start the task to write data on TIMIQ equipment
m_timiq_task->start_undetached();
}
// ============================================================================
// TIMIQLib::data_end_task
// ============================================================================
void TIMIQLib::data_end_task()
throw (Exception)
{
// std::cout <<"data_end_task ..." <<std::endl;
//- Delete task
if (this->m_timiq_task)
{
if (m_timiq_task->m_ti_cfg.ti_err != timiq_NO_ERROR)
{
// std::cout <<"exit task done ..." <<std::endl;
m_timiq_task = NULL;
CHECK_TIMIQ_HW;
std::string msg_err = "Set data error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::data_end_task"));
}
//- destroy otherwise
m_timiq_task->exit();
delete m_timiq_task;
m_timiq_task = NULL;
}
//- update state of TIMIQ equipment
m_internal_timiq_state = OK;
}
// ============================================================================
// TIMIQLib::set_iValue
// ============================================================================
void TIMIQLib::set_iValue(float iValue)
throw (Exception)
{
//- Threading configuration
timIQConfig l_ti_cfg;
//- set I value config
l_ti_cfg.id = TI_IVAL;
l_ti_cfg.value = iValue;
if ( m_internal_timiq_state != Busy )
//- force timiq state = Busy
m_internal_timiq_state = Busy;
m_timiq_task = new ThreadedAction(static_cast<yat::Thread::IOArg>(this), l_ti_cfg);
if (m_timiq_task == NULL)
{
std::string msg_err = " Can't instanciate set I value task";
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::set_iValue"));
}
//- start the task
m_timiq_task->start_undetached();
}
}
// ============================================================================
// TIMIQLib::iVal_end_task
// ============================================================================
void TIMIQLib::iVal_end_task()
throw (Exception)
{
// std::cout <<"iVal_end_task ..." <<std::endl;
//- Delete task
if (this->m_timiq_task)
if (m_timiq_task->m_ti_cfg.ti_err != timiq_NO_ERROR)
m_timiq_task->exit();
m_timiq_task = NULL;
CHECK_TIMIQ_HW;
std::string msg_err = "Set I value error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::set_iValue"));
}
//- destroy otherwise
m_timiq_task->exit();
m_timiq_task = NULL;
//- update state of TIMIQ equipment
m_internal_timiq_state = OK;
}
// ============================================================================
// TIMIQLib::set_qValue
// ============================================================================
void TIMIQLib::set_qValue(float qValue)
throw (Exception)
{
//- Threading configuration
timIQConfig l_ti_cfg;
//- set Q value config
l_ti_cfg.id = TI_QVAL;
l_ti_cfg.value = qValue;
if ( m_internal_timiq_state != Busy )
//- force timiq state = Busy
m_internal_timiq_state = Busy;
m_timiq_task = new ThreadedAction(static_cast<yat::Thread::IOArg>(this), l_ti_cfg);
if (m_timiq_task == NULL)
{
std::string msg_err = "Set Q value error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::set_qValue"));
}
//- start the task to write "Q" tension value on TIMIQ equipment
m_timiq_task->start_undetached();
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// ============================================================================
// TIMIQLib::qVal_end_task
// ============================================================================
void TIMIQLib::qVal_end_task()
throw (Exception)
{
// std::cout <<"qVal_end_task ..." <<std::endl;
//- Delete task
if (this->m_timiq_task)
{
if (m_timiq_task->m_ti_cfg.ti_err != timiq_NO_ERROR)
{
m_timiq_task->exit();
m_timiq_task = NULL;
CHECK_TIMIQ_HW;
std::string msg_err = "Set Q value error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::qVal_end_task"));
}
//- destroy otherwise
m_timiq_task->exit();
m_timiq_task = NULL;
}
//- update state of TIMIQ equipment
m_internal_timiq_state = OK;
}
// ============================================================================
// TIMIQLib::set_boardTemperature
// ============================================================================
void TIMIQLib::set_boardTemperature(float boardTemperature)
throw (Exception)
{
//- Threading configuration
timIQConfig l_ti_cfg;
//- set board temperature value config
l_ti_cfg.id = TI_BTEMP;
l_ti_cfg.value = boardTemperature;
if ( m_internal_timiq_state != Busy )
//- force timiq state = Busy
m_internal_timiq_state = Busy;
m_timiq_task = new ThreadedAction(static_cast<yat::Thread::IOArg>(this), l_ti_cfg);
if (m_timiq_task == NULL)
{
std::string msg_err = "Set board Temperature error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::set_boardTemperature"));
}
//- start the task to set board temperature on TIMIQ equipment
m_timiq_task->start_undetached();
}
}
// ============================================================================
// TIMIQLib::boardT_end_task
// ============================================================================
void TIMIQLib::boardT_end_task()
throw (Exception)
{
// std::cout <<"boardT_end_task ..." <<std::endl;
//- Delete task
{
if (m_timiq_task->m_ti_cfg.ti_err != timiq_NO_ERROR)
{
//std::cout <<"exit task done ..." <<std::endl;
m_timiq_task->exit();
m_timiq_task = NULL;
CHECK_TIMIQ_HW;
std::string msg_err = "Set board Temperature error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::boardT_end_task"));
}
//- destroy otherwise
m_timiq_task->exit();
m_timiq_task = NULL;
//- update state of TIMIQ equipment
m_internal_timiq_state = OK;
}
// ============================================================================
// TIMIQLib::set_command
// ============================================================================
void TIMIQLib::set_command(E_timiq_cmd_t& cmd)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- write command on TIMIQ equipment
err = m_timiq_hw->write_command(cmd);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Set command error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::set_command()"));
}
//- update state of TIMIQ equipment
m_internal_timiq_state = OK;
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
}
// ============================================================================
// TIMIQLib::get_data
// ============================================================================
void TIMIQLib::get_data(float &data)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read data from TIMIQ equipment
err = m_timiq_hw->read_data(data);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get data error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::get_data()"));
}
}
// ============================================================================
// TIMIQLib::get_iValue
// ============================================================================
void TIMIQLib::get_iValue(float& iValue)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read "I" tension value from TIMIQ equipment
err = m_timiq_hw->read_iValue(iValue);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get I value error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::get_iValue()"));
}
}
// ============================================================================
// TIMIQLib::get_qValue
// ============================================================================
void TIMIQLib::get_qValue(float& qValue)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read "Q" tension value from TIMIQ equipment
err = m_timiq_hw->read_qValue(qValue);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get Q value error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::get_qValue()"));
}
}
// ============================================================================
// TIMIQLib::get_mixerCosOutput
// ============================================================================
void TIMIQLib::get_mixerCosOutput(float& mixerCosOutput)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read mixer cos output value from TIMIQ equipment
err = m_timiq_hw->read_mixerCosOutput(mixerCosOutput);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get mixer cosinus output value error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::get_mixerCosOutput()"));
}
}
// ============================================================================
// TIMIQLib::get_mixerSinOutput
// ============================================================================
void TIMIQLib::get_mixerSinOutput(float& mixerSinOutput)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read mixer sin output value from TIMIQ equipment
err = m_timiq_hw->read_mixerSinOutput(mixerSinOutput);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get mixer sinus output value error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::get_mixerSinOutput()"));
}
}
// ============================================================================
// TIMIQLib::get_boardTemperature
// ============================================================================
void TIMIQLib::get_boardTemperature(float& boardTemperature)
throw (Exception)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
//- read board temperature value from TIMIQ equipment
err = m_timiq_hw->read_boardTemperature(boardTemperature);
if (err != timiq_NO_ERROR)
{
std::string msg_err = "Get board temperature value error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::get_boardTemperature()"));
}
}
// ============================================================================
// TIMIQLib::get_state
// ============================================================================
E_timiq_code_t TIMIQLib::get_state(std::string& status)
throw (Exception)
{
E_timiq_errno_t err;
E_timiq_code_t retCode;
CHECK_TIMIQ_HW;
//- get internal timiq state
if (m_internal_timiq_state != Busy)
{
//- read status of TIMIQ equipment
err = m_timiq_hw->read_state_and_status(status, m_internal_timiq_state);
if (err != timiq_NO_ERROR)
{
m_internal_timiq_state = Undefined;
std::string msg_err = "Get status error -msg: " + m_timiq_hw->get_err_msg();
throw Exception(static_cast<const char*>("SOFTWARE_FAILURE"),
static_cast<const char*>(msg_err.c_str()),
static_cast<const char*>("TIMIQLib::get_status()"));
}
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
return m_internal_timiq_state;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
// Threaded actions for TimIQ equipment
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
// ============================================================================
// ThreadedAction::ThreadedAction
// ============================================================================
ThreadedAction::ThreadedAction ( yat::Thread::IOArg ioa, timIQConfig& cfg)
: yat::Thread(ioa),
m_goOn(true),
m_isActionDone(false),
m_ti_cfg(cfg)
{
//- noop ctor
}
// ============================================================================
// ThreadedAction::~ThreadedAction
// ============================================================================
ThreadedAction::~ThreadedAction (void)
{
//- noop dtor
}
// ============================================================================
// ThreadedAction::run_undetached
// ============================================================================
yat::Thread::IOArg ThreadedAction::run_undetached (yat::Thread::IOArg ioa)
{
//DEBUG_STREAM << "ThreadedAction::run_undetached() entering... " << std::endl;
m_isActionDone = false;
//- get the reference to parent task
TIMIQLib * l_task = reinterpret_cast<TIMIQLib *>(ioa);
l_task->ExecuteAction(m_ti_cfg);
//- Action is done
m_isActionDone = true;
return 0;
}
// ============================================================================
// ThreadedAction::exit
// ============================================================================
void ThreadedAction::exit (void)
{
this->m_goOn = false;
}
// ============================================================================
// TIMIQLib::ExecuteAction
// ============================================================================
void TIMIQLib::ExecuteAction(timIQConfig & cfg)
{
E_timiq_errno_t err;
CHECK_TIMIQ_HW;
// Actions to be threaded
switch(cfg.id)
{
//- write "I" value
case TI_IVAL:
cfg.ti_err = m_timiq_hw->write_iValue(cfg.value);
this->iVal_end_task();
break;
//- write "Q" value
case TI_QVAL:
cfg.ti_err = m_timiq_hw->write_qValue(cfg.value);
this->qVal_end_task();
break;
//- write "DATA" value
case TI_DATA:
cfg.ti_err = m_timiq_hw->write_data(cfg.value);
this->data_end_task();
break;
//- write "BOARD Temperature" value
case TI_BTEMP:
cfg.ti_err = m_timiq_hw->write_data(cfg.value);