Example code for 'Mechatronics Class'

Files at this revision

API Documentation at this revision

Comitter:
ykuroda
Date:
Mon Oct 14 02:35:03 2019 +0000
Parent:
1:49002ccc54b5
Commit message:
1st commit

Changed in this revision

counter.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/counter.h	Mon Oct 14 02:35:03 2019 +0000
@@ -0,0 +1,31 @@
+//
+//  Counter Class
+//
+//  2019.10.13 ... Originally written by Y.Kuroda
+//
+#ifndef _COUNTER_H
+#define _COUNTER_H
+
+class Counter {
+  public:
+    Counter(PinName pin) : _interrupt(pin), _count(0) {       // create the InterruptIn on the pin specified to Counter
+        _interrupt.rise(callback(this, &Counter::increment)); // attach increment function of this counter instance
+        _interrupt.fall(callback(this, &Counter::increment)); // attach increment function of this counter instance
+    }
+
+    void increment() { _count++; }
+    int read() { return _count; }
+    int set(int c) { return _count=c; }
+    int reset(){ return set(0); }
+
+    int operator=(int c) { return set(c); }
+    operator int() { return read(); }
+    Counter& operator=(Counter& c){ _count=c.read(); return *this; }
+
+  protected:
+    InterruptIn _interrupt;
+    volatile int _count;
+};
+
+#endif
+
--- a/main.cpp	Fri Jun 23 16:15:38 2017 -0500
+++ b/main.cpp	Mon Oct 14 02:35:03 2019 +0000
@@ -1,29 +1,18 @@
 #include "mbed.h"
-
-class Counter {
-public:
-    Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
-        _interrupt.rise(callback(this, &Counter::increment)); // attach increment function of this counter instance
-    }
+#include "counter.h"
 
-    void increment() {
-        _count++;
-    }
-
-    int read() {
-        return _count;
-    }
-
-private:
-    InterruptIn _interrupt;
-    volatile int _count;
-};
-
-Counter counter(SW2);
+PwmOut pwm(D10);    // wire to tie D10, D12, and D13(LED1)
+DigitalIn led(LED1);
 
 int main() {
+    Counter count(D12);
+    count = 0;
+
+    pwm.period(0.1);
+    pwm = 0.5;
+
     while(1) {
-        printf("Count so far: %d\n", counter.read());
-        wait(2);
+        printf("Count so far: %d\n", int(count));
+        wait(1);
     }
 }
\ No newline at end of file