Skip to content

Commit 61c2010

Browse files
authored
Add files via upload
1 parent 98db321 commit 61c2010

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed

CodeDebugScope.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright (C) 2016 Albert van Dalen http://www.avdweb.nl
3+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
4+
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
6+
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at http://www.gnu.org/licenses .
7+
*/
8+
9+
#ifndef CodeDebugScope_H
10+
#define CodeDebugScope_H
11+
12+
#include <Arduino.h>
13+
14+
#if defined(__arm__)
15+
const unsigned int maxScopeBytes = 7000; // SAMD21: max ~ 7000
16+
#else
17+
const int maxScopeBytes = 500; // ATTENTION SET HERE, AVR ATmega328: max ~ 780 ATmega168: max ~ 320
18+
//const int maxScopeBytes = 100; // ATTENTION SET HERE, AVR ATmega328: max ~ 780 ATmega168: max ~ 320
19+
#endif
20+
21+
class SWscope
22+
{
23+
public:
24+
void start(byte _channels, int _preSamples=0, unsigned int _recordLenght=65535);
25+
void probeA(short valueA);
26+
void probeAB(short valueA, short valueB);
27+
void probeABC(short valueA, short valueB, short valueC);
28+
void probeABCD(short valueA, short valueB, short valueC, short valueD);
29+
void trigger();
30+
void stop();
31+
void showIfReady();
32+
void testBuffer();
33+
34+
volatile bool canShow;
35+
36+
protected:
37+
void sampleControl();
38+
unsigned int calcPtr(int ptr);
39+
40+
volatile union{short chA[maxScopeBytes/2];
41+
short chAB[maxScopeBytes/4][2];
42+
short chABC[maxScopeBytes/3][3];
43+
short chABCD[maxScopeBytes/8][4];} ringBuffer;
44+
45+
volatile unsigned long sample0_us, usPerDiv;
46+
volatile bool triggered, samplingOn;
47+
volatile byte channels; // 1, 2, 3, 4 channels
48+
volatile int recordLenght, preSamples, ptr, samples, triggerPtr, stopPtr;
49+
};
50+
51+
#endif

CodeDebugScope.ino

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

Comments
 (0)