A simple Oscilloscope that log each pin's data change Please dump log and analyze using Excel x-y chart type!
Scope.cpp@0:6c6d961dc8b3, 2015-05-10 (annotated)
- Committer:
- steeven
- Date:
- Sun May 10 13:16:26 2015 +0000
- Revision:
- 0:6c6d961dc8b3
init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
steeven | 0:6c6d961dc8b3 | 1 | #include "Scope.h" |
steeven | 0:6c6d961dc8b3 | 2 | |
steeven | 0:6c6d961dc8b3 | 3 | namespace steeven { |
steeven | 0:6c6d961dc8b3 | 4 | |
steeven | 0:6c6d961dc8b3 | 5 | ScopePin::ScopePin(PinName pin, char id, Scope *scope) : |
steeven | 0:6c6d961dc8b3 | 6 | _pin(pin), _id(id), _scope(scope) { |
steeven | 0:6c6d961dc8b3 | 7 | _pin.fall(this, &ScopePin::on_fall); |
steeven | 0:6c6d961dc8b3 | 8 | _pin.rise(this, &ScopePin::on_rise); |
steeven | 0:6c6d961dc8b3 | 9 | } |
steeven | 0:6c6d961dc8b3 | 10 | |
steeven | 0:6c6d961dc8b3 | 11 | void ScopePin::on_fall() { |
steeven | 0:6c6d961dc8b3 | 12 | _scope->log(_id, 0); |
steeven | 0:6c6d961dc8b3 | 13 | } |
steeven | 0:6c6d961dc8b3 | 14 | void ScopePin::on_rise() { |
steeven | 0:6c6d961dc8b3 | 15 | _scope->log(_id, 1); |
steeven | 0:6c6d961dc8b3 | 16 | } |
steeven | 0:6c6d961dc8b3 | 17 | |
steeven | 0:6c6d961dc8b3 | 18 | Scope::Scope(int buf_len) : |
steeven | 0:6c6d961dc8b3 | 19 | _buf_len(buf_len) { |
steeven | 0:6c6d961dc8b3 | 20 | _cnt = 0; |
steeven | 0:6c6d961dc8b3 | 21 | _buf_cnt = 0; |
steeven | 0:6c6d961dc8b3 | 22 | _buf = new ScopeData[buf_len]; |
steeven | 0:6c6d961dc8b3 | 23 | _timer.start(); |
steeven | 0:6c6d961dc8b3 | 24 | } |
steeven | 0:6c6d961dc8b3 | 25 | |
steeven | 0:6c6d961dc8b3 | 26 | void Scope::add(PinName pin, const char *name) { |
steeven | 0:6c6d961dc8b3 | 27 | new ScopePin(pin, _cnt, this); |
steeven | 0:6c6d961dc8b3 | 28 | _names[_cnt] = name; |
steeven | 0:6c6d961dc8b3 | 29 | // printf("%s ", _names[_cnt]); |
steeven | 0:6c6d961dc8b3 | 30 | _cnt++; |
steeven | 0:6c6d961dc8b3 | 31 | } |
steeven | 0:6c6d961dc8b3 | 32 | |
steeven | 0:6c6d961dc8b3 | 33 | void Scope::log(char id, int v) { |
steeven | 0:6c6d961dc8b3 | 34 | if (_buf_cnt >= _buf_len) |
steeven | 0:6c6d961dc8b3 | 35 | error("Scope buffer overflow!\n"); |
steeven | 0:6c6d961dc8b3 | 36 | _buf[_buf_cnt].id = (char) (v << 7) + id; |
steeven | 0:6c6d961dc8b3 | 37 | _buf[_buf_cnt].time = _timer.read(); |
steeven | 0:6c6d961dc8b3 | 38 | _buf_cnt++; |
steeven | 0:6c6d961dc8b3 | 39 | } |
steeven | 0:6c6d961dc8b3 | 40 | |
steeven | 0:6c6d961dc8b3 | 41 | void Scope::dump(BufferedSerial *logger) { |
steeven | 0:6c6d961dc8b3 | 42 | int i, j, k; |
steeven | 0:6c6d961dc8b3 | 43 | |
steeven | 0:6c6d961dc8b3 | 44 | logger->putc('\n'); |
steeven | 0:6c6d961dc8b3 | 45 | |
steeven | 0:6c6d961dc8b3 | 46 | // channel names; |
steeven | 0:6c6d961dc8b3 | 47 | logger->puts("time\t"); |
steeven | 0:6c6d961dc8b3 | 48 | for (i = 0; i < _cnt; ++i) { |
steeven | 0:6c6d961dc8b3 | 49 | logger->puts(_names[i]); |
steeven | 0:6c6d961dc8b3 | 50 | logger->putc('\t'); |
steeven | 0:6c6d961dc8b3 | 51 | } |
steeven | 0:6c6d961dc8b3 | 52 | logger->putc('\n'); |
steeven | 0:6c6d961dc8b3 | 53 | |
steeven | 0:6c6d961dc8b3 | 54 | // channel data |
steeven | 0:6c6d961dc8b3 | 55 | for (j = 0; j < _cnt; ++j) { // buy channel |
steeven | 0:6c6d961dc8b3 | 56 | for (i = 0; i < _buf_cnt; ++i) { // find in all data |
steeven | 0:6c6d961dc8b3 | 57 | if ((_buf[i].id & 0x7f) == j) { // if this pin |
steeven | 0:6c6d961dc8b3 | 58 | logger->printf("%f\t", _buf[i].time); |
steeven | 0:6c6d961dc8b3 | 59 | for (k = 0; k < j; ++k) |
steeven | 0:6c6d961dc8b3 | 60 | logger->putc('\t'); |
steeven | 0:6c6d961dc8b3 | 61 | logger->printf("%d", ((_buf[i].id & 0x80) > 0 ? 1 : 0) + j * 2); // add offset |
steeven | 0:6c6d961dc8b3 | 62 | logger->putc('\n'); |
steeven | 0:6c6d961dc8b3 | 63 | } |
steeven | 0:6c6d961dc8b3 | 64 | } |
steeven | 0:6c6d961dc8b3 | 65 | } |
steeven | 0:6c6d961dc8b3 | 66 | |
steeven | 0:6c6d961dc8b3 | 67 | //clear data |
steeven | 0:6c6d961dc8b3 | 68 | _buf_cnt = 0; |
steeven | 0:6c6d961dc8b3 | 69 | } |
steeven | 0:6c6d961dc8b3 | 70 | } |