Skip to content

Commit 1c46976

Browse files
authored
Add files via upload
1 parent 4e03153 commit 1c46976

2 files changed

Lines changed: 197 additions & 0 deletions

File tree

avdweb_SWscope.cpp

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

avdweb_SWscope.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 SWscope_H
10+
#define SWscope_H
11+
12+
#include <Arduino.h>
13+
14+
#if defined(__arm__)
15+
const unsigned int maxBytes = 7000; // SAMD21: max ~ 7000
16+
#else
17+
const int maxBytes = 700; // AVR: max ~ 780
18+
#endif
19+
20+
class SWscope
21+
{
22+
public:
23+
void start(byte _channels, int _preSamples=0, unsigned int _recordLenght=65535);
24+
void probeA(short valueA);
25+
void probeAB(short valueA, short valueB);
26+
void probeABC(short valueA, short valueB, short valueC);
27+
void probeABCD(short valueA, short valueB, short valueC, short valueD);
28+
void trigger();
29+
void stop();
30+
void showIfReady();
31+
void testBuffer();
32+
33+
bool canShow;
34+
35+
protected:
36+
void sampleControl();
37+
unsigned int calcPtr(int ptr);
38+
39+
union{short chA[maxBytes/2];
40+
short chAB[maxBytes/4][2];
41+
short chABC[maxBytes/3][3];
42+
short chABCD[maxBytes/8][4];} ringBuffer;
43+
44+
unsigned long sample0_us, usPerDiv;
45+
bool triggered, samplingOn;
46+
byte channels; // 1, 2, 3, 4 channels
47+
int recordLenght, preSamples, ptr, samples, triggerPtr, stopPtr;
48+
};
49+
50+
#endif
51+
52+

0 commit comments

Comments
 (0)