Dependents:   ShootingSystem

Revision:
1:2a43c09cc3be
Parent:
0:49a747f09bd2
diff -r 49a747f09bd2 -r 2a43c09cc3be AmmoSupplier.cpp
--- a/AmmoSupplier.cpp	Fri Aug 21 04:52:23 2015 +0000
+++ b/AmmoSupplier.cpp	Wed Oct 14 03:52:41 2015 +0000
@@ -1,35 +1,40 @@
 #include "mbed.h"
 #include "AmmoSupplier.h"
 #include "I2CMotor.h"
-
-const float AmmoSupplier::mTimeout_sec              = 3.0f;
-const float AmmoSupplier::mDuty                     = 0.5f;
-const PinName AmmoSupplier::mLazerSensorDinPinName  = D5;
+#include "ShootingSystem.h"
 
-AmmoSupplier::AmmoSupplier( I2CMotor** motor ){
-    mSupplier        = motor;
-    mAmmoLazerSensor = new DigitalIn( mLazerSensorDinPinName );
-    mTimer           = new Timer;
-    mIsSupplying     = false;
+AmmoSupplier::AmmoSupplier( char address ) : I2CDevice( address ){
+    mSupplyCommand  = false;
+    mIsSupplying    = false;
+    mHasSupplied    = false;
 }
 
-void AmmoSupplier::update(){
-    I2CMotor::ActionType action = I2CMotor::BRAKE;
+int AmmoSupplier::write(){
+    int val = 0;
     
-    if ( mIsSupplying ){
-        if ( mTimer->read() > mTimeout_sec ){
-            mIsSupplying = false;
-            mTimer->stop();
-            mTimer->reset();
-        } else if ( mAmmoLazerSensor->read() ){
-            mIsSupplying = false;
-        } else {
-            action = I2CMotor::FORWARD;
-        }
+    if ( mSupplyCommand ){
+        char buf = 1;
+        val = I2CDevice::write( &buf, 1 );
+        mSupplyCommand = false;
     }
     
-    for ( int i = 0; i < 2; ++i ){
-        mSupplier[ i ]->setActionType( action );
-        mSupplier[ i ]->setDuty( mDuty );
+    return val;
+}
+
+int AmmoSupplier::read(){
+    char buf;
+    int val = I2CDevice::read( &buf, 1 );
+    
+    mHasSupplied = ( mIsSupplying && !buf );
+    mIsSupplying = buf;
+    
+    //printf( "%d\n", buf );
+    
+    /*
+    if ( mHasSupplied ){
+        wait( 1.0f );
     }
+    */
+    
+    return val;
 }