A simple Oscilloscope that log each pin's data change Please dump log and analyze using Excel x-y chart type!

/media/uploads/steeven/mbed_sco.jpg

Files at this revision

API Documentation at this revision

Comitter:
steeven
Date:
Sun May 10 13:16:26 2015 +0000
Commit message:
init

Changed in this revision

Scope.cpp Show annotated file Show diff for this revision Revisions of this file
Scope.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6c6d961dc8b3 Scope.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Scope.cpp	Sun May 10 13:16:26 2015 +0000
@@ -0,0 +1,70 @@
+#include "Scope.h"
+
+namespace steeven {
+
+ScopePin::ScopePin(PinName pin, char id, Scope *scope) :
+        _pin(pin), _id(id), _scope(scope) {
+    _pin.fall(this, &ScopePin::on_fall);
+    _pin.rise(this, &ScopePin::on_rise);
+}
+
+void ScopePin::on_fall() {
+    _scope->log(_id, 0);
+}
+void ScopePin::on_rise() {
+    _scope->log(_id, 1);
+}
+
+Scope::Scope(int buf_len) :
+        _buf_len(buf_len) {
+    _cnt = 0;
+    _buf_cnt = 0;
+    _buf = new ScopeData[buf_len];
+    _timer.start();
+}
+
+void Scope::add(PinName pin, const char *name) {
+    new ScopePin(pin, _cnt, this);
+    _names[_cnt] = name;
+//  printf("%s ", _names[_cnt]);
+    _cnt++;
+}
+
+void Scope::log(char id, int v) {
+    if (_buf_cnt >= _buf_len)
+        error("Scope buffer overflow!\n");
+    _buf[_buf_cnt].id = (char) (v << 7) + id;
+    _buf[_buf_cnt].time = _timer.read();
+    _buf_cnt++;
+}
+
+void Scope::dump(BufferedSerial *logger) {
+    int i, j, k;
+
+    logger->putc('\n');
+
+    // channel names;
+    logger->puts("time\t");
+    for (i = 0; i < _cnt; ++i) {
+        logger->puts(_names[i]);
+        logger->putc('\t');
+    }
+    logger->putc('\n');
+
+    // channel data
+    for (j = 0; j < _cnt; ++j) { // buy channel
+        for (i = 0; i < _buf_cnt; ++i) { // find in all data
+            if ((_buf[i].id & 0x7f) == j) { // if this pin
+                logger->printf("%f\t", _buf[i].time);
+                for (k = 0; k < j; ++k)
+                    logger->putc('\t');
+                logger->printf("%d", ((_buf[i].id & 0x80) > 0 ? 1 : 0) + j * 2); // add offset
+                logger->putc('\n');
+            }
+        }
+    }
+
+    //clear data
+    _buf_cnt = 0;
+}
+}
diff -r 000000000000 -r 6c6d961dc8b3 Scope.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Scope.h	Sun May 10 13:16:26 2015 +0000
@@ -0,0 +1,65 @@
+/*
+ * BreathLed.h
+ *
+ *  Created on: 2015/4/6
+ *      Author: steeven@gmail.com
+ */
+
+#ifndef SCOPE_H_
+#define SCOPE_H_
+
+#include "mbed.h"
+#include "BufferedSerial/BufferedSerial.h"
+
+#define SCOPE_PINS 16
+
+namespace steeven {
+
+/** A simple Oscilloscope that log each pin's data change
+ * Please dump log and analyze using excel x-y chart type!
+ */
+
+typedef struct {
+    char id; //bit 7: val, bit 0-6:id
+    float time;
+} ScopeData;
+
+class Scope {
+public:
+    Scope(int buf_len);
+
+    void add(PinName pin, const char *name);
+
+    void log(char id, int v);
+
+    void dump(  BufferedSerial *logger);
+
+protected:
+    const char *_names[SCOPE_PINS];
+    int _cnt;
+
+    Timer _timer;
+
+    ScopeData *_buf;
+    int _buf_len;
+    int _buf_cnt;
+};
+
+class ScopePin {
+
+public:
+    ScopePin(PinName pin, char id, Scope *scope);
+
+protected:
+    void on_fall();
+    void on_rise();
+
+    InterruptIn _pin;
+    char _id;
+    Scope *_scope;
+};
+
+}
+;
+
+#endif