|
| 1 | +/* |
| 2 | + LET OP: Serial.begin(115200) IN DE scope LIB |
| 3 | +
|
| 4 | + The library <Streaming.h> has to be installed too, download here: http://arduiniana.org/libraries/streaming/ |
| 5 | + The library <avdweb.h> has to be installed too, download here: nog doen |
| 6 | +
|
| 7 | + Copyright (C) 2016 Albert van Dalen http://www.avdweb.nl |
| 8 | + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License |
| 9 | + as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 11 | + of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at http://www.gnu.org/licenses . |
| 12 | +
|
| 13 | + HISTORY: |
| 14 | + 1.0.0 1-1-2016 |
| 15 | + 1.0.1 14-1-2016 sample0_us |
| 16 | + 1.0.2 10-2-2017 1, 2, 3, 4 channels |
| 17 | + 1.0.3 10-2-2017 probeA sampleB probeABC probeABCD to avoid mistakes, all unsigned int -> int, added stop(), sizeof(int) |
| 18 | + SerialUSB -> Serial (incl. <Albert.h>), short |
| 19 | + 1.0.4 10-3-2017 for ISR measurements do show() in loop() |
| 20 | + 1.0.5 28-1-2018 renamed sample to probe, Scope to SWscope |
| 21 | +
|
| 22 | + start ___|____________________________________|________ |
| 23 | +
|
| 24 | + probe |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_ |
| 25 | +
|
| 26 | + trigger ____________|________________________________________ |
| 27 | + ___________________________ |
| 28 | + triggered ____________| |___________ |
| 29 | +
|
| 30 | + samples 0 0 1 2 3 . . . |
| 31 | + ________ |
| 32 | + show ________________________| |______________ |
| 33 | + ____________________ ___________ |
| 34 | + samplingOn ___| |_______________| |
| 35 | +
|
| 36 | + canShow ________________________|___________________________ |
| 37 | +
|
| 38 | +*/ |
| 39 | + |
| 40 | +#include "CodeDebugScope.h" |
| 41 | +#include <Streaming.h> |
| 42 | + |
| 43 | + |
| 44 | +template<class T> // easy printing multiple variables with separator ',' example: Serial << endl << a, b, c; |
| 45 | +inline Print &operator , (Print &stream, const T arg) |
| 46 | +{ stream.print(" "); |
| 47 | + stream.print(arg); |
| 48 | + return stream; |
| 49 | +} |
| 50 | + |
| 51 | +void SWscope::start(byte _channels, int _preSamples, unsigned int _recordLenght) |
| 52 | +{ ptr = samples = triggerPtr = preSamples = triggered = 0; |
| 53 | + stopPtr = 32767; |
| 54 | + samplingOn = 1; |
| 55 | + channels = _channels; |
| 56 | + recordLenght = maxScopeBytes / (sizeof(short) * channels); |
| 57 | + if (_recordLenght <= recordLenght) recordLenght = _recordLenght; |
| 58 | + if (abs(_preSamples) <= recordLenght) preSamples = _preSamples; |
| 59 | +} |
| 60 | + |
| 61 | +void SWscope::showIfReady() |
| 62 | +{ if (!canShow) return; |
| 63 | + canShow = 0; |
| 64 | + Serial.begin(115200); // HIER doen Serial part moet buiten de bibliotheek |
| 65 | + //interrupts(); // hoeft niet |
| 66 | + while (!Serial); // wait on Serial Monitor |
| 67 | + Serial << "\nusPerDiv: " << usPerDiv << "\nptr, values "; |
| 68 | + ptr = stopPtr; |
| 69 | + do |
| 70 | + { if (channels == 1) Serial << endl << ptr, ringBuffer.chA[ptr]; |
| 71 | + if (channels == 2) Serial << endl << ptr, ringBuffer.chAB[ptr][0], ringBuffer.chAB[ptr][1]; |
| 72 | + if (channels == 3) Serial << endl << ptr, ringBuffer.chABC[ptr][0], ringBuffer.chABC[ptr][1], ringBuffer.chABC[ptr][2]; |
| 73 | + if (channels == 4) Serial << endl << ptr, ringBuffer.chABCD[ptr][0], ringBuffer.chABCD[ptr][1], ringBuffer.chABCD[ptr][2], ringBuffer.chABCD[ptr][3]; |
| 74 | + if (ptr == triggerPtr) Serial << " trigger"; |
| 75 | + ptr = calcPtr(ptr + 1); |
| 76 | + } |
| 77 | + while (ptr != stopPtr); |
| 78 | + stopPtr = 32767; |
| 79 | +} |
| 80 | + |
| 81 | +void SWscope::stop() |
| 82 | +{ stopPtr = calcPtr(ptr + 1); |
| 83 | +} |
| 84 | + |
| 85 | +void SWscope::probeA(short valueA) // 1 channel |
| 86 | +{ if (samplingOn) |
| 87 | + { ringBuffer.chA[ptr] = valueA; |
| 88 | + sampleControl(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void SWscope::probeAB(short valueA, short valueB) // 2 channels |
| 93 | +{ if (samplingOn) |
| 94 | + { ringBuffer.chAB[ptr][0] = valueA; |
| 95 | + ringBuffer.chAB[ptr][1] = valueB; |
| 96 | + sampleControl(); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void SWscope::probeABC(short valueA, short valueB, short valueC) // 3 channels |
| 101 | +{ if (samplingOn) |
| 102 | + { ringBuffer.chABC[ptr][0] = valueA; |
| 103 | + ringBuffer.chABC[ptr][1] = valueB; |
| 104 | + ringBuffer.chABC[ptr][2] = valueC; |
| 105 | + sampleControl(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void SWscope::probeABCD(short valueA, short valueB, short valueC, short valueD) // 4 channels |
| 110 | +{ if (samplingOn) |
| 111 | + { ringBuffer.chABCD[ptr][0] = valueA; |
| 112 | + ringBuffer.chABCD[ptr][1] = valueB; |
| 113 | + ringBuffer.chABCD[ptr][2] = valueC; |
| 114 | + ringBuffer.chABCD[ptr][3] = valueD; |
| 115 | + sampleControl(); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void SWscope::sampleControl() |
| 120 | +{ //doen micros weggelaten if(samples == 0) sample0_us = micros(); |
| 121 | + samples++; |
| 122 | + ptr = calcPtr(ptr + 1); |
| 123 | + if (ptr == stopPtr) |
| 124 | + { samplingOn = 0; |
| 125 | + canShow = 1; |
| 126 | + unsigned long stop_us;//doen = micros(); // to avoid delay, start with this |
| 127 | + usPerDiv = samples > 1 ? (stop_us - sample0_us) / (samples - 1) : 0; // is not exact? |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +unsigned int SWscope::calcPtr(int _ptr) |
| 132 | +{ if (_ptr >= recordLenght) return (_ptr - recordLenght); // happens most frequent |
| 133 | + if (_ptr < 0) return (_ptr + recordLenght); |
| 134 | + return _ptr; |
| 135 | +} |
| 136 | + |
| 137 | +void SWscope::trigger() |
| 138 | +{ if (!triggered) |
| 139 | + { triggerPtr = ptr; |
| 140 | + stopPtr = calcPtr(triggerPtr - preSamples); |
| 141 | + triggered = 1; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +void SWscope::testBuffer() |
| 146 | +{ start(1); |
| 147 | + for (int i = 0; i < 25000; i++) |
| 148 | + { if (i == 0) trigger(); |
| 149 | + probeA(i); |
| 150 | + } |
| 151 | +} |
0 commit comments