gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/kbpd/src/pdbut.cbare sourcepermlink (0.04 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: 
   15: /*
   16:  *      pdbut.c
   17:  *
   18:  *       KB/PD device manager
   19:  *       Enableware function for PD button (state machine) processing
   20:  */
   21: 
   22: #include "kbpd.h"
   23: 
   24: #define pdMode          (kpMgrInfo.pd.pdMode)
   25: #define pdInStat        (kpMgrInfo.statMach.pdInStat)
   26: 
   27: /*
   28:  * generate PD move event
   29:  */
   30: LOCAL void makePdMoveEvent( InnerEvent *evt, PdInput *msg )
   31: {
   32:         evt->type |= IE_PDMOVE;
   33:         evt->i.pd.stat.o = msg->stat;
   34:         evt->i.pd.x = msg->xpos;
   35:         evt->i.pd.y = msg->ypos;
   36: }
   37: 
   38: /*
   39:  * generate PD button down event
   40:  */
   41: LOCAL void makePdButDownEvent( PdButState *pdBut, InnerEvent *evt )
   42: {
   43:         evt->type |= ( pdBut->button == BK_MAIN )? IE_MBUTDOWN: IE_SBUTDOWN;
   44:         evt->i.pd.stat = pdInStat;
   45: 
   46:         pdBut->butdown = TRUE;
   47: }
   48: 
   49: /*
   50:  * generate PD button up event
   51:  */
   52: LOCAL void makePdButUpEvent( PdButState *pdBut, InnerEvent *evt )
   53: {
   54:         evt->type |= ( pdBut->button == BK_MAIN )? IE_MBUTUP: IE_SBUTUP;
   55:         evt->i.pd.stat = pdInStat;
   56: 
   57:         pdBut->butdown = FALSE;
   58: }
   59: 
   60: /* ------------------------------------------------------------------------ */
   61: 
   62: /*
   63:  * if button press, then TRUE
   64:  */
   65: LOCAL BOOL buttonPress( PdButState *pdBut )
   66: {
   67:         return ( (pdInStat.u.button & pdBut->button) != 0 )? TRUE: FALSE;
   68: }
   69: 
   70: /*
   71:  * release button forcefully
   72:  */
   73: LOCAL void forceButtonRelease( PdButState *pdBut )
   74: {
   75:         pdInStat.u.button &= ~pdBut->button;
   76: }
   77: 
   78: /* ------------------------------------------------------------------------ */
   79: 
   80: /*
   81:  * PD button : release status
   82:  */
   83: LOCAL BState bsRelease( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
   84: {
   85:         if ( msg == NULL ) return BS_RELEASE;
   86: 
   87:         switch ( msg->head.cmd.cmd ) {
   88:           case INP_PD:
   89:                 if ( !buttonPress(pdBut) ) return BS_RELEASE;
   90:                 break;
   91: 
   92:           default:
   93:                 return BS_RELEASE;
   94:         }
   95: 
   96:         return BS_ONTIME;
   97: }
   98: 
   99: /*
  100:  * PD button: ON wait during effective time
  101:  */
  102: LOCAL BState bsOnTime( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
  103: {
  104:         ER     err;
  105: 
  106:         if ( msg == NULL ) {
  107:                 if ( pdMode.ontime == 0 ) goto butdown;
  108: 
  109:                 /* timeout setting for ON effective time */
  110:                 err = kpSetAlarm(&pdBut->alm, pdMode.ontime);
  111:                 if ( err != E_OK ) {
  112:                         DEBUG_PRINT(("bsOnTime, SetAlarm err = %d\n", err));
  113:                         return BS_ERROR;
  114:                 }
  115:                 return BS_ONTIME;
  116:         }
  117: 
  118:         switch ( msg->head.cmd.cmd ) {
  119:           case INP_PD:
  120:                 if ( !buttonPress(pdBut) ) return BS_RELEASE;
  121:                 return BS_ONTIME;
  122: 
  123:           case PIC_TIMEOUT:
  124:                 if ( !kpChkAlarm(&pdBut->alm, &msg->tmout) ) return BS_ONTIME;
  125:                 break;
  126: 
  127:           default:
  128:                 return BS_ONTIME;
  129:         }
  130: 
  131: butdown:
  132:         /* generate PD button down event */
  133:         makePdButDownEvent(pdBut, evt);
  134: 
  135:         return BS_PRESS;
  136: }
  137: 
  138: /*
  139:  * PD button : press status
  140:  */
  141: LOCAL BState bsPress( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
  142: {
  143:         ER     err;
  144: 
  145:         if ( msg == NULL ) {
  146: reset:
  147:                 /* A PD for which timeout is ineffective, the timeout setting is unnecessary */
  148:                 if ( pdInStat.u.tmout == 0 ) return BS_PRESS;
  149: 
  150:                 /* setting timeout */
  151:                 err = kpSetAlarm(&pdBut->alm, pdMode.timeout);
  152:                 if ( err != E_OK ) {
  153:                         DEBUG_PRINT(("bsPress, SetAlarm err = %d\n", err));
  154:                         return BS_ERROR;
  155:                 }
  156:                 return BS_PRESS;
  157:         }
  158: 
  159:         switch ( msg->head.cmd.cmd ) {
  160:           case INP_PD:
  161:                 if ( buttonPress(pdBut) ) goto reset;
  162:                 return BS_OFFTIME;
  163: 
  164:           case PIC_TIMEOUT:
  165:                 if ( pdInStat.u.tmout == 0
  166:                   || !kpChkAlarm(&pdBut->alm, &msg->tmout) ) return BS_PRESS;
  167: 
  168:                 /* button release due to timeout */
  169:                 forceButtonRelease(pdBut);
  170:                 break;
  171: 
  172:           default:
  173:                 return BS_PRESS;
  174:         }
  175: 
  176:         /* generate PD button up event */
  177:         makePdButUpEvent(pdBut, evt);
  178: 
  179:         return BS_INVTIME;
  180: }
  181: 
  182: /*
  183:  * PD button: OFF wait during effective time
  184:  */
  185: LOCAL BState bsOffTime( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
  186: {
  187:         ER     err;
  188: 
  189:         if ( msg == NULL ) {
  190:                 if ( pdMode.offtime == 0 ) goto butup;
  191: 
  192:                 /* time out setting for OFF effective time */
  193:                 err = kpSetAlarm(&pdBut->alm, pdMode.offtime);
  194:                 if ( err != E_OK ) {
  195:                         DEBUG_PRINT(("bsOffTime, SetAlarm err = %d\n", err));
  196:                         return BS_ERROR;
  197:                 }
  198:                 return BS_OFFTIME;
  199:         }
  200: 
  201:         switch ( msg->head.cmd.cmd ) {
  202:           case INP_PD:
  203:                 if ( buttonPress(pdBut) ) return BS_PRESS;
  204:                 return BS_OFFTIME;
  205: 
  206:           case PIC_TIMEOUT:
  207:                 if ( !kpChkAlarm(&pdBut->alm, &msg->tmout) ) return BS_OFFTIME;
  208:                 break;
  209: 
  210:           default:
  211:                 return BS_OFFTIME;
  212:         }
  213: 
  214: butup:
  215:         /* generate PD button up event */
  216:         makePdButUpEvent(pdBut, evt);
  217: 
  218:         return BS_INVTIME;
  219: }
  220: 
  221: /*
  222:  * PD button: wait during ineffective time period
  223:  */
  224: LOCAL BState bsInvTime( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
  225: {
  226:         ER     err;
  227: 
  228:         if ( msg == NULL ) {
  229:                 if ( pdMode.invtime == 0 ) goto timeout;
  230: 
  231:                 /* setting timeout for ineffective period */
  232:                 err = kpSetAlarm(&pdBut->alm, pdMode.invtime);
  233:                 if ( err != E_OK ) {
  234:                         DEBUG_PRINT(("bsInvTime, SetAlarm err = %d\n", err));
  235:                         return BS_ERROR;
  236:                 }
  237:                 return BS_INVTIME;
  238:         }
  239: 
  240:         switch ( msg->head.cmd.cmd ) {
  241:           case PIC_TIMEOUT:
  242:                 if ( !kpChkAlarm(&pdBut->alm, &msg->tmout) ) return BS_INVTIME;
  243:                 break;
  244: 
  245:           default:
  246:                 return BS_INVTIME;
  247:         }
  248: 
  249: timeout:
  250:         return ( buttonPress(pdBut) )? BS_ONTIME: BS_RELEASE;
  251: }
  252: 
  253: /*
  254:  * PD button: status reset
  255:  */
  256: LOCAL BState bsReset( PdButState *pdBut, InnerEvent *evt )
  257: {
  258:         if ( pdBut->butdown ) {
  259:                 /* generate button up event */
  260:                 makePdButUpEvent(pdBut, evt);
  261:         }
  262:         return BS_RELEASE;
  263: }
  264: 
  265: /*
  266:  * processing state for one PD button
  267:  */
  268: LOCAL void execPdButStateMachine( PdButState *pdBut,
  269:                                         InnerEvent *evt, ReceiveData *msg )
  270: {
  271:         BState prev;
  272: 
  273:         do {
  274:                 switch ( prev = pdBut->state ) {
  275:                   case BS_RELEASE:
  276:                         pdBut->state = bsRelease(pdBut, evt, msg);
  277:                         break;
  278:                   case BS_ONTIME:
  279:                         pdBut->state = bsOnTime(pdBut, evt, msg);
  280:                         break;
  281:                   case BS_PRESS:
  282:                         pdBut->state = bsPress(pdBut, evt, msg);
  283:                         break;
  284:                   case BS_OFFTIME:
  285:                         pdBut->state = bsOffTime(pdBut, evt, msg);
  286:                         break;
  287:                   case BS_INVTIME:
  288:                         pdBut->state = bsInvTime(pdBut, evt, msg);
  289:                         break;
  290: 
  291:                   default:
  292:                         DEBUG_PRINT(("unknown BState(%d)\n", pdBut->state));
  293:                         pdBut->state = bsReset(pdBut, evt);
  294:                         return;
  295:                 }
  296:                 msg = NULL;
  297:         } while ( pdBut->state != prev );
  298: }
  299: 
  300: /*
  301:  * state machine for a PD button
  302:  */
  303: EXPORT void kpExecPdButStateMachine( InnerEvent *evt, ReceiveData *msg, ButtonKind button )
  304: {
  305:         PdButState     *pdBut;
  306:         W              i;
  307: 
  308:         if ( msg->head.cmd.err != DEV_OK ) {
  309:                 /* reset button status */
  310:                 for ( i = 0; i < NumOfPdBut; ++i ) {
  311:                         if ( (button & (1 << i)) == 0 ) continue;
  312:                         pdBut = &kpMgrInfo.statMach.pdBut[i];
  313:                         pdBut->state = bsReset(pdBut, evt);
  314:                 }
  315:                 return;
  316:         }
  317: 
  318:         if ( msg->head.cmd.cmd == INP_PD ) {
  319: 
  320:                 /* save PD attribute data */
  321:                 pdInStat.o = msg->pd.stat;
  322: 
  323:                 if ( msg->pd.stat.inv == 0 ) {
  324:                         /* enable PD movement */
  325:                         makePdMoveEvent(evt, &msg->pd);
  326:                 }
  327:         }
  328: 
  329:         /* execute the state machine */
  330:         for ( i = 0; i < NumOfPdBut; ++i ) {
  331:                 if ( (button & (1 << i)) == 0 ) continue;
  332:                 pdBut = &kpMgrInfo.statMach.pdBut[i];
  333:                 execPdButStateMachine(pdBut, evt, msg);
  334:         }
  335: }