Victor Hugo Silva / Mbed 2 deprecated Retrovisor

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
victorhlvsilva
Date:
Mon May 06 20:27:21 2019 +0000
Commit message:
Acionamento retrovisor;

Changed in this revision

DebouncedIn.cpp Show annotated file Show diff for this revision Revisions of this file
DebouncedIn.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.cpp	Mon May 06 20:27:21 2019 +0000
@@ -0,0 +1,91 @@
+#include "DebouncedIn.h"
+#include "mbed.h"
+
+/*
+ * Constructor
+ */
+DebouncedIn::DebouncedIn(PinName in) 
+    : _in(in) {    
+        
+    // reset all the flags and counters    
+    _samples = 0;
+    _output = 0;
+    _output_last = 0;
+    _rising_flag = 0;
+    _falling_flag = 0;
+    _state_counter = 0;
+    
+    // Attach ticker
+    _ticker.attach(this, &DebouncedIn::_sample, 0.005);     
+}
+  
+void DebouncedIn::_sample() {
+
+    // take a sample
+    _samples = _samples >> 1; // shift left
+      
+    if (_in) {
+        _samples |= 0x80;
+    }  
+      
+    // examine the sample window, look for steady state
+    if (_samples == 0x00) {
+        _output = 0;
+    } 
+    else if (_samples == 0xFF) {
+        _output = 1;
+    }
+
+
+    // Rising edge detection
+    if ((_output == 1) && (_output_last == 0)) {
+        _rising_flag++;
+        _state_counter = 0;
+    }
+
+    // Falling edge detection
+    else if ((_output == 0) && (_output_last == 1)) {
+        _falling_flag++;
+        _state_counter = 0;
+    }
+    
+    // steady state
+    else {
+        _state_counter++;
+    }
+    
+   // update the output
+    _output_last = _output;
+    
+}
+
+
+
+// return number of rising edges
+int DebouncedIn::rising(void) {
+    int return_value = _rising_flag; 
+    _rising_flag = 0;
+    return(return_value);
+}
+
+// return number of falling edges
+int DebouncedIn::falling(void) {
+    int return_value = _falling_flag; 
+    _falling_flag = 0;
+    return(return_value);
+}
+
+// return number of ticsk we've bene steady for
+int DebouncedIn::steady(void) {
+return(_state_counter);
+}
+
+// return the debounced status
+int DebouncedIn::read(void) {
+    return(_output);
+}
+
+// shorthand for read()
+DebouncedIn::operator int() {
+    return read();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.h	Mon May 06 20:27:21 2019 +0000
@@ -0,0 +1,31 @@
+#include "mbed.h"
+
+    class DebouncedIn {
+        public:      
+             DebouncedIn(PinName in);
+
+             int read (void);
+             operator int();
+              
+             int rising(void);
+             int falling(void);
+             int steady(void);
+              
+        private :    
+               // objects
+               DigitalIn _in;    
+               Ticker _ticker;
+
+               // function to take a sample, and update flags
+               void _sample(void);
+
+               // counters and flags
+               int _samples;
+               int _output;
+               int _output_last;
+               int _rising_flag;
+               int _falling_flag;
+               int _state_counter;
+
+    };
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon May 06 20:27:21 2019 +0000
@@ -0,0 +1,85 @@
+#include "mbed.h"
+
+Serial pc(USBTX, USBRX);
+
+AnalogIn Joy_X(A3);
+AnalogIn Joy_Y(A5);
+DigitalIn Joy_B(PC_13);
+
+DigitalOut saida_IN1a(D5);
+DigitalOut saida_IN1b(A0);
+DigitalOut saida_IN2a(D4);
+DigitalOut saida_IN2b(A1);
+DigitalOut saida_ENa(D2);
+DigitalOut saida_ENb(A4);
+
+float JX;
+float JY;
+bool JB;
+bool JBb = 0;
+
+
+int main() {
+    
+    saida_ENa = 0;
+    saida_ENb = 0;
+    
+    while(1){
+        
+        JX = Joy_X;
+        JY = Joy_Y;
+        JB = Joy_B;
+        
+        if (JB == 0){
+            saida_ENb = 1;
+            saida_ENa = 0;
+            saida_IN1b = 0;
+            saida_IN2b = 1;
+            wait_ms(500);
+            saida_IN2b = 0;
+            
+            while(JB == 0){
+                }
+                
+            saida_IN1b = 1;
+            saida_IN2b = 0;
+            wait_ms(500);
+            saida_IN1b = 0;
+             }
+        
+        if (JX >= 0.6f || JX <= 0.4f ){
+            saida_ENa = 1;
+            saida_ENb = 0;
+            
+            if (JX >= 0.6f){
+                saida_IN1a = 0;
+                saida_IN2a = 1;
+                }
+            
+            else if (JX <= 0.4f){
+                saida_IN1a = 1;
+                saida_IN2a = 0;
+                }
+            }
+            
+        else if (JY >= 0.6f || JY <= 0.4f ){
+            saida_ENb = 1;
+            saida_ENa = 0;
+            
+            if (JY >= 0.6f){
+                saida_IN1b = 0;
+                saida_IN2b = 1;
+                }
+                
+            else if (JY <= 0.4f){
+                saida_IN1b = 1;
+                saida_IN2b = 0;
+                }    
+            }
+            
+        else{
+            saida_ENa = 0;
+            saida_ENb = 0;
+            }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon May 06 20:27:21 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file