6363qChannel_t channels = NULL_CHANNELS_INITIALIZATION ;
6464qDigitalChannel_t digitalChannels = NULL_DIGITAL_CHANNELS_INITIALIZATION ;
6565qAnalogChannel_t analogChannels = NULL_ANALOG_CHANNELS_INITIALIZATION ;
66- // qWatcher_t watcher ;//= NULL_WATCHER_INITIALIZATION;
66+ qWatcher_t watcher ;// = NULL_WATCHER_INITIALIZATION;
6767//----------------------------------------------------
6868static void analogXCallback ( qChannel_t * c ) {
6969
@@ -107,6 +107,8 @@ static void analogUpdateReading( qBool_t act ) {
107107
108108static void analogEvaluateState () {
109109 //channelState( *this );
110+ //TODO: is this good?
111+ analogChannels .channelStateFcn (& analogChannels );
110112}
111113
112114static qBool_t analogIsValidConfig () {
@@ -389,6 +391,8 @@ static void digitalUpdateReading( qBool_t act ) {
389391
390392static void digitalEvaluateState () {
391393 //channelStateFcn =
394+ //TODO: is this good?
395+ digitalChannels .channelStateFcn (& digitalChannels );
392396}
393397
394398static qBool_t digitalIsValidConfig () {
@@ -571,31 +575,36 @@ static void digitalSteadyInLowState( struct _qDigitalChannel_t * c ) {
571575
572576
573577static qBool_t addChannel ( qChannel_t * c ) {
574- // qBool_t retValue;
575- //
576- // if ( DIGITAL_CHANNEL == c->getType() ) {
577- // (void)c->setDigitalReader( watcher.digitalReader );
578- // c->setInitalState();
579- // retValue = watcher.digitalChannels. insert( &c ); TODO
580- // }
581- // else {
582- // (void)c->setAnalogReader( watcher.analogReader );
583- // (void)c->setInitalState();
584- // qAnalogChannel_t *chan = (qAnalogChannel_t*)c;
585- // /* check if channel is shared( same channel number)*/
586- // for ( auto i = analogChannels.begin(); i.untilEnd() ; i++ ) {
587- // //input::analogChannel& channelInWatcher = *i.get<input::analogChannel*>(); TODO
588- //
589- // if ( chan->channel.number == channelInWatcher.number ) {
590- // chan->ptrValue = &channelInWatcher.value;
591- // break;
592- // }
593- // }
594- // retValue = watcher.analogChannels. insert( &c ); TODO
595- // }
596- // c->tChange = qGetTick();
597-
598- // return retValue;
578+ qBool_t retValue ;
579+ qList_Iterator_t i ;
580+
581+ if ( DIGITAL_CHANNEL == c -> getType () ) {
582+ (void )c -> setDigitalReader ( watcher .digitalReader );
583+ c -> setInitalState ();
584+ retValue = qList_Insert ( & watcher .digitalChannels , c , QLIST_AT_BACK );
585+ }
586+ else {
587+ (void )c -> setAnalogReader ( watcher .analogReader );
588+ (void )c -> setInitalState ();
589+ qAnalogChannel_t * chan = (qAnalogChannel_t * )c ;
590+ /* check if channel is shared( same channel number)*/
591+ for ( i = qList_Begin ( & watcher .analogChannels ) ;
592+ qListIterator_Until ( & i , NULL ) ;
593+ qListIterator_Forward ( & i ) ) {
594+
595+ qAnalogChannel_t * channelInWatcher =
596+ (qAnalogChannel_t * )qListIterator_Get ( & i );
597+
598+ if ( chan -> channel .number == channelInWatcher -> channel .number ) {
599+ chan -> ptrValue = & channelInWatcher -> value ;
600+ break ;
601+ }
602+ }
603+ retValue = qList_Insert ( & watcher .analogChannels , c , QLIST_AT_BACK );
604+ }
605+ c -> tChange = qClock_GetTick ();
606+
607+ return retValue ;
599608}
600609
601610static qBool_t addCallback ( qChannel_t * c , qEventCallback_t cb ) {
@@ -615,37 +624,89 @@ static qBool_t addCallbackDigital( qChannel_t *c, qDigitalReaderFcn_t fcn, qEven
615624static qBool_t addCallbackAnalog ( qChannel_t * c , qAnalogReaderFcn_t fcn , qEventCallback_t cb ) {
616625 qBool_t retValue ;
617626 (void )c -> setCallback ( cb );
618- retValue = add ( c );
627+ retValue = addChannel ( c );
619628 (void )c -> setAnalogReader ( fcn );
620629 return retValue ;
621630}
622631
623632static qBool_t remove ( qChannel_t * c ) {
624- //list* const channelContainer = c.getContainer();
625- //const bool retValue = channelContainer->remove( &c );
626- //(void)c.unShare();
627- //
628- ///*cstat -MISRAC++2008-5-14-1*/
629- //if ( ( input::type::ANALOG_CHANNEL == c.getType() ) && !c.isShared() ) { // no side-effects here
630- ///*cstat +MISRAC++2008-5-14-1*/
631- // analogValue_t *newPtrVal = nullptr;
632- // /*find the next shared channel*/
633- // for ( auto i = analogChannels.begin(); i.untilEnd() ; i++ ) {
634- // input::analogChannel& channelInList = *i.get<input::analogChannel*>();
635- //
636- // if ( channelInList.number == c.number ) {
637- // if ( nullptr == newPtrVal ) { /*first shared channel*/
638- // newPtrVal = &channelInList.value;
639- // channelInList.ptrValue = &channelInList.value;
640- // }
641- // else {
642- // channelInList.ptrValue = newPtrVal;
643- // }
644- // }
645- // }
646- //}
647-
648- //return retValue;
633+ qList_Iterator_t i ;
634+ qList_t * const channelContainer = c -> node .container ;
635+ const qBool_t retValue = //channelContainer->remove( &c );
636+ qList_Remove (channelContainer , c , QLIST_AT_BACK ) == NULL ? qFalse :qTrue ;
637+
638+ (void )c -> unShare ();
639+
640+ if ( (ANALOG_CHANNEL == c -> getType () ) && !c -> isShared () ) { // no side-effects here
641+ qAnalogValue_t * newPtrVal = NULL ;
642+ /*find the next shared channel*/
643+ for ( i = qList_Begin ( & watcher .analogChannels ) ;
644+ qListIterator_Until ( & i , NULL ) ;
645+ qListIterator_Forward ( & i ) ) {
646+
647+ qAnalogChannel_t * channelInList =
648+ (qAnalogChannel_t * )qListIterator_Get ( & i );
649+
650+ if ( channelInList -> channel .number == c -> number ) {
651+ if ( NULL == newPtrVal ) { /*first shared channel*/
652+ newPtrVal = & channelInList -> value ;
653+ channelInList -> ptrValue = & channelInList -> value ;
654+ }
655+ else {
656+ channelInList -> ptrValue = newPtrVal ;
657+ }
658+ }
659+ }
660+ }
661+
662+ return retValue ;
663+ }
664+
665+ /*============================================================================*/
666+ static qBool_t watch ( void )
667+ {
668+ qList_Iterator_t i ;
669+ const qBool_t act = qSTimer_FreeRun (
670+ & watcher .waitDebounce , watcher .debounceTime );
671+
672+ if ( ( qList_Length (& watcher .digitalChannels ) > 0U ) && act ) {
673+
674+ for ( i = qList_Begin ( & watcher .digitalChannels ) ;
675+ qListIterator_Until ( & i , NULL ) ;
676+ qListIterator_Forward ( & i ) ) {
677+
678+ qChannel_t * c = (qChannel_t * )qListIterator_Get ( & i );
679+ if ( NULL != c -> callback ) {
680+ if ( NULL != watcher .digitalReader ) {
681+ c -> updateReading ( qTrue );
682+ c -> evaluateState ();
683+ }
684+ else {
685+ c -> dispatchEvent ( EXCEPTION );
686+ }
687+ }
688+ }
689+ }
690+
691+ if ( ( qList_Length (& watcher .analogChannels ) > 0U ) > 0U ) {
692+ for ( i = qList_Begin ( & watcher .analogChannels ) ;
693+ qListIterator_Until ( & i , NULL ) ;
694+ qListIterator_Forward ( & i ) ) {
695+
696+ qChannel_t * c = (qChannel_t * )qListIterator_Get ( & i );
697+ if ( NULL != c -> callback ) {
698+ if ( ( NULL != watcher .analogReader ) && c -> isValidConfig () ) { // no side-effects here
699+ c -> updateReading ( act );
700+ c -> evaluateState ();
701+ }
702+ else {
703+ c -> dispatchEvent ( EXCEPTION );
704+ }
705+ }
706+ }
707+ }
708+
709+ return qTrue ;
649710}
650711
651712/**
@@ -661,7 +722,9 @@ void qInputInitialize() {
661722 const qWatcher_t _watcher ;
662723 digitalChannels = _digitalChannels ;
663724 analogChannels = _analogChannels ;
664- // watcher = _watcher;
725+ watcher = _watcher ;
726+ (void )qList_Initialize ( & watcher .digitalChannels );
727+ (void )qList_Initialize ( & watcher .analogChannels );
665728
666729}
667730
0 commit comments