YMotorDriverPusher.cpp

Committer:
inst
Date:
2015-08-23
Revision:
0:4b3f0e4681c9
Child:
1:9af02800ffac

File content as of revision 0:4b3f0e4681c9:

#include "YMotorDriverBase.h"
#include "YMotorDriverPusher.h"
#include "mbed.h"

const float YMotorDriverPusher::mDuty = 0.5f;
const PinName YMotorDriverPusher::mDinPinName[] = {
    dp2,
    dp4
};

YMotorDriverPusher::YMotorDriverPusher( char address ) : YMotorDriverBase( address ){
    mSwitchDin = new DigitalIn*[ 2 ];
    for ( int i = 0; i < 2; ++i ){
        mSwitchDin[ i ] = new DigitalIn( mDinPinName[ i ] );
    }
    mState = NO_OPERATION;
}

void YMotorDriverPusher::update(){
    switch ( mI2C->receive() ){
        case I2CSlave::ReadAddressed:{
            char buf = mState;
            mI2C->write( &buf, 1 );
            break;
        }
        case I2CSlave::WriteGeneral:
            break;
        
        case I2CSlave::WriteAddressed:{
            char buf;
            mI2C->read( &buf, 1 );
            mState = static_cast< State >( buf );
            break;
        }
        
        case I2CSlave::NoData:
            break;
    }
    
    updatePusher();
    write();
}

void YMotorDriverPusher::updatePusher(){
    for ( int i = 0; i < 2; ++i ){
        // もしリミットスイッチに入力があったら止める
        if ( mSwitchDin[ i ]->read() ){
            mState = NO_OPERATION;
            break;
        }
    }
    
    MotorAction action = BRAKE;
    if ( mState == DRAWING ){
        action = REVERSE;
    } else if ( mState == PUSHING ){
        action = FORWARD;
    }
    
    setMotorAction( action );
    setDuty( mDuty );
}