Dependents:   ShootingSystem

Files at this revision

API Documentation at this revision

Comitter:
inst
Date:
Wed Oct 14 03:52:41 2015 +0000
Parent:
0:49a747f09bd2
Commit message:

Changed in this revision

AmmoSupplier.cpp Show annotated file Show diff for this revision Revisions of this file
AmmoSupplier.h Show annotated file Show diff for this revision Revisions of this file
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;
 }
diff -r 49a747f09bd2 -r 2a43c09cc3be AmmoSupplier.h
--- a/AmmoSupplier.h	Fri Aug 21 04:52:23 2015 +0000
+++ b/AmmoSupplier.h	Wed Oct 14 03:52:41 2015 +0000
@@ -2,30 +2,30 @@
 #define INCLUDED_AMMO_SUPPLIER_H
 
 #include "mbed.h"
-class I2CMotor;
+#include "I2CDevice.h"
 
-class AmmoSupplier{
+class AmmoSupplier : public I2CDevice{
 public:
-    AmmoSupplier( I2CMotor** motor );
+    AmmoSupplier( char address );
     
-    void update();
-    
+    void supply(){
+        mSupplyCommand = true;
+    }
     bool isSupplying(){
         return mIsSupplying;
     }
-    void supply(){
-        mIsSupplying = true;
-        mTimer->start();
+    bool hasSupplied(){
+        return mHasSupplied;
+        //return true;
     }
+    
+    virtual int write();
+    virtual int read();
+    
 private:
-    static const float mTimeout_sec;
-    static const PinName mLazerSensorDinPinName;
-    static const float mDuty;
-    
-    I2CMotor** mSupplier;
-    DigitalIn* mAmmoLazerSensor;
-    Timer* mTimer;
+    bool mSupplyCommand;
     bool mIsSupplying;
+    bool mHasSupplied;
 };
 
 #endif