- Don't think anything was changed or updated.... simply viewed the class

Dependents:   Component_Test_Interface

Fork of BridgeDriver by Jason T

Revision:
1:c8e328389a98
Parent:
0:b1e3fa917367
Child:
2:4253000aacf2
diff -r b1e3fa917367 -r c8e328389a98 BridgeDriver.cpp
--- a/BridgeDriver.cpp	Thu Jul 03 17:09:29 2014 +0000
+++ b/BridgeDriver.cpp	Thu Jul 03 20:56:20 2014 +0000
@@ -1,15 +1,17 @@
 
 #include "BridgeDriver.h"
 #include "mbed.h"
+#include "TextLCD.h" // if using diagnostic
 
 #include "MCP23017.h"
 
 //TO-DO: check order of setting output and enabling/disabling channel
 
-BridgeDriver::BridgeDriver( I2C *i2c, const uint8_t enPwmA, const uint8_t enPwmB, const uint8_t enPwmC, const uint8_t enPwmD, const uint8_t enAddr, const uint8_t ledAddr) :
+BridgeDriver::BridgeDriver( I2C *i2c, uint8_t enPwmA, uint8_t enPwmB, uint8_t enPwmC, uint8_t enPwmD, uint8_t enAddr, uint8_t ledAddr) :
                             _i2c(i2c),
                             _enAddr(enAddr),
                             _ledAddr(ledAddr),
+                            _pwmCh(0x00),
                             _oldLedState(0x00)
                           {
 
@@ -194,7 +196,7 @@
     int bNum = static_cast<int>(bridge); //numeric bridge
     
     if(_pwm[bNum] == 0){
-        return -1;
+        return -20 - bNum;
     }
     
     if(speed == 0){
@@ -212,7 +214,7 @@
     int bNum = static_cast<uint8_t>(bridge); //numeric bridge
     
     if(_pwm[bNum] == 0){
-        return -1;
+        return -10 - bNum;
     }
     if(speed < -1 || speed > 1){        //maybe this should be bounding instead?
         return -1;
@@ -249,7 +251,7 @@
         if(_dir[bNum] != 1){
             setPwm(2*bNum, 1);
             setPwm(2*bNum + 1, 0);
-            _dir[bNum] = 0;
+            _dir[bNum] = 1;
         }
         setLed(2*bNum, 1);
         setLed(2*bNum + 1, 0);
@@ -259,10 +261,10 @@
         enableCh(2*bNum + 1, 1);
         return speed;
     }else if(dir == -1){
-        if(_dir[bNum] != 1){
+        if(_dir[bNum] != -1){
             setPwm(2*bNum, 0);
             setPwm(2*bNum + 1, 1);
-            _dir[bNum] = 0;
+            _dir[bNum] = -1;
         }
         setLed(2*bNum, 0);
         setLed(2*bNum + 1, 1);
@@ -272,7 +274,7 @@
         enableCh(2*bNum + 1, 1);
         return speed;
     } 
-    return -1;
+    return -2;
 }
 
 void BridgeDriver::enableCh(uint8_t ch, uint8_t en){
@@ -315,75 +317,89 @@
 void BridgeDriver::setPwm(uint8_t ch, uint8_t en){  
     enableCh(ch, 0);
     
-    if(_pwm[ch/2] && (                                  //if pwm is currently enabled
-                     ( ch%2 && _dir[ch/2] == -1) ||     //higher numbered channel (ch%2) and dir = -1
-                     ( !ch%2 && _dir[ch/2] == 1)        //lower numbered channel (!ch%2) and dir = 1
-        ) ){    
-        //pwn is currently enabled on this channel
+    
+    if(_pwmCh & (1 << ch)){        //pwm is currently enabled on this channel
         if(en == 0){
             delete _p[ch];
             switch(ch){
-            case 0:
-                _d[ch] = new DigitalOut(PIN_CH1);
-            break;
-            case 1:
-                _d[ch] = new DigitalOut(PIN_CH2);
-            break;
-            case 2:
-                _d[ch] = new DigitalOut(PIN_CH3);
-            break;
-            case 3:
-                _d[ch] = new DigitalOut(PIN_CH4);
-            break;
-            case 4:
-                _d[ch] = new DigitalOut(PIN_CH5);
-            break;
-            case 5:
-                _d[ch] = new DigitalOut(PIN_CH6);
-            break;
-            case 6:
-                _d[ch] = new DigitalOut(PIN_CH7);
-            break;
-            case 7:
-                _d[ch] = new DigitalOut(PIN_CH8);
-            break;
+                case 0:
+                    _d[ch] = new DigitalOut(PIN_CH1);
+                break;
+                case 1:
+                    _d[ch] = new DigitalOut(PIN_CH2);
+                break;
+                case 2:
+                    _d[ch] = new DigitalOut(PIN_CH3);
+                break;
+                case 3:
+                    _d[ch] = new DigitalOut(PIN_CH4);
+                break;
+                case 4:
+                    _d[ch] = new DigitalOut(PIN_CH5);
+                break;
+                case 5:
+                    _d[ch] = new DigitalOut(PIN_CH6);
+                break;
+                case 6:
+                    _d[ch] = new DigitalOut(PIN_CH7);
+                break;
+                case 7:
+                    _d[ch] = new DigitalOut(PIN_CH8);
+                break;
             }
+            _pwmCh = _pwmCh & ~(1 << ch);
+            
         }
     }else{ //pwm is currently disabled on this channel
         if(en == 1){
             delete _d[ch];
             switch(ch){
-            case 0:
-                _p[ch] = new PwmOut(PIN_CH1);
-            break;                     
-            case 1:                    
-                _p[ch] = new PwmOut(PIN_CH2);
-            break;                     
-            case 2:                    
-                _p[ch] = new PwmOut(PIN_CH3);
-            break;                     
-            case 3:                    
-                _p[ch] = new PwmOut(PIN_CH4);
-            break;                     
-            case 4:                    
-                _p[ch] = new PwmOut(PIN_CH5);
-            break;                     
-            case 5:                    
-                _p[ch] = new PwmOut(PIN_CH6);
-            break;                     
-            case 6:                    
-                _p[ch] = new PwmOut(PIN_CH7);
-            break;                     
-            case 7:                    
-                _p[ch] = new PwmOut(PIN_CH8);
-            break;
+                case 0:
+                    _p[ch] = new PwmOut(PIN_CH1);
+                break;                     
+                case 1:                    
+                    _p[ch] = new PwmOut(PIN_CH2);
+                break;                     
+                case 2:                    
+                    _p[ch] = new PwmOut(PIN_CH3);
+                break;                     
+                case 3:                    
+                    _p[ch] = new PwmOut(PIN_CH4);
+                break;                     
+                case 4:                    
+                    _p[ch] = new PwmOut(PIN_CH5);
+                break;                     
+                case 5:                    
+                    _p[ch] = new PwmOut(PIN_CH6);
+                break;                     
+                case 6:                    
+                    _p[ch] = new PwmOut(PIN_CH7);
+                break;                     
+                case 7:                    
+                    _p[ch] = new PwmOut(PIN_CH8);
+                break;
             }
+            _p[ch]->period(0.001);
+            _pwmCh = _pwmCh | (1 << ch);
         }
     }
     
 
 }
 
-
-
-
+void BridgeDriver::diagnostic(TextLCD_I2C *lcd){
+    lcd->setAddress(0,1);
+    lcd->printf("PWM:%d,%d,%d,%d    ", _pwm[0], _pwm[1], _pwm[2], _pwm[3]);
+    lcd->setAddress(0,2);
+    lcd->printf("pCH:%i%i%i%i%i%i%i%i", !!(_pwmCh & (1 << 0)),
+                                        !!(_pwmCh & (1 << 1)),
+                                        !!(_pwmCh & (1 << 2)),
+                                        !!(_pwmCh & (1 << 3)),
+                                        !!(_pwmCh & (1 << 4)),
+                                        !!(_pwmCh & (1 << 5)),
+                                        !!(_pwmCh & (1 << 6)),
+                                        !!(_pwmCh & (1 << 7))
+                                        );
+    lcd->setAddress(0,3);
+    lcd->printf("dir:%+d,%+d,%+d,%+d    ", _dir[0], _dir[1], _dir[2], _dir[3]);
+}
\ No newline at end of file