Skip to content

Commit 87b62b5

Browse files
authored
Add files via upload
1 parent de8453a commit 87b62b5

2 files changed

Lines changed: 183 additions & 0 deletions

File tree

avdweb_scope.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
Scope.cpp
3+
4+
The library <Streaming.h> has to be installed too, download here: http://arduiniana.org/libraries/streaming/
5+
The library <Albert.h> has to be installed too, download here: http://www.avdweb.nl/arduino/albert-library.html
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+
Version 1-1-2016
14+
Version 14-1-2016 sample0_us
15+
Version 10-2-2017 1, 2, 3, 4 channels
16+
17+
start ___|____________________________________|________
18+
19+
sample |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_
20+
21+
trigger ____________|________________________________________
22+
___________________________
23+
triggered ____________| |___________
24+
25+
samples 0 0 1 2 3 . . .
26+
________
27+
show ________________________| |______________
28+
____________________ ___________
29+
samplingOn ___| |_______________|
30+
31+
*/
32+
33+
#include "avdweb_scope.h"
34+
#include <Albert.h> // this library has to be installed too
35+
#include <Streaming.h> // this library has to be installed too
36+
37+
void Scope::start(byte _channels, int _preSamples, unsigned int _recordLenght)
38+
{ ptr = samples = triggerPtr = preSamples = triggered = 0;
39+
stopPtr=32767;
40+
samplingOn=1;
41+
channels = _channels;
42+
recordLenght = maxBytes/(2*channels); // byte = 2 * int
43+
if(_recordLenght <= recordLenght) recordLenght = _recordLenght;
44+
if(abs(_preSamples) <= recordLenght) preSamples = _preSamples;
45+
}
46+
47+
void Scope::show()
48+
{ unsigned long stop_us = micros(); // to avoid delay, start with this
49+
unsigned long usPerDiv = samples > 1 ? (stop_us - sample0_us)/(samples-1) : 0;
50+
while(!SerialUSB); // wait on Serial Monitor
51+
SerialUSB << "\nusPerDiv " << usPerDiv;
52+
samplingOn=0;
53+
SerialUSB << "\nptr value ";
54+
ptr=stopPtr;
55+
do
56+
{ if(channels==1) SerialUSB << endl << ptr, ringBuffer.chA[ptr];
57+
if(channels==2) SerialUSB << endl << ptr, ringBuffer.chAB[ptr][0], ringBuffer.chAB[ptr][1];
58+
if(channels==3) SerialUSB << endl << ptr, ringBuffer.chABC[ptr][0], ringBuffer.chABC[ptr][1], ringBuffer.chABC[ptr][2];
59+
if(channels==4) SerialUSB << endl << ptr, ringBuffer.chABCD[ptr][0], ringBuffer.chABCD[ptr][1], ringBuffer.chABCD[ptr][2], ringBuffer.chABCD[ptr][3];
60+
if(ptr==triggerPtr) SerialUSB << " trigger";
61+
ptr = calcPtr(ptr+1);
62+
}
63+
while (ptr!=stopPtr);
64+
stopPtr = 32767;
65+
}
66+
67+
void Scope::sample(int valueA) // 1 channel
68+
{ if(samplingOn)
69+
{ ringBuffer.chA[ptr] = valueA;
70+
sampleControl();
71+
}
72+
}
73+
74+
void Scope::sample(int valueA, int valueB) // 2 channels
75+
{ if(samplingOn)
76+
{ ringBuffer.chAB[ptr][0] = valueA;
77+
ringBuffer.chAB[ptr][1] = valueB;
78+
sampleControl();
79+
}
80+
}
81+
82+
void Scope::sample(int valueA, int valueB, int valueC) // 3 channels
83+
{ if(samplingOn)
84+
{ ringBuffer.chABC[ptr][0] = valueA;
85+
ringBuffer.chABC[ptr][1] = valueB;
86+
ringBuffer.chABC[ptr][2] = valueC;
87+
sampleControl();
88+
}
89+
}
90+
91+
void Scope::sample(int valueA, int valueB, int valueC, int valueD) // 4 channels
92+
{ if(samplingOn)
93+
{ ringBuffer.chABCD[ptr][0] = valueA;
94+
ringBuffer.chABCD[ptr][1] = valueB;
95+
ringBuffer.chABCD[ptr][2] = valueC;
96+
ringBuffer.chABCD[ptr][3] = valueD;
97+
sampleControl();
98+
}
99+
}
100+
101+
void Scope::sampleControl()
102+
{ if(samples == 0) sample0_us = micros(); // doen werkt niet als er meerdere testen lopen
103+
samples++;
104+
ptr = calcPtr(ptr+1);
105+
if(ptr==stopPtr) show();
106+
}
107+
108+
unsigned int Scope::calcPtr(int _ptr)
109+
{ if(_ptr>=recordLenght) return (_ptr - recordLenght); // happens most frequent
110+
if(_ptr<0) return (_ptr + recordLenght);
111+
return _ptr;
112+
}
113+
114+
void Scope::trigger()
115+
{ if(!triggered)
116+
{ triggerPtr = ptr;
117+
stopPtr = calcPtr(triggerPtr-preSamples);
118+
triggered=1;
119+
}
120+
}
121+
122+
void Scope::testBuffer()
123+
{ start(1);
124+
for(int i=0; i<25000; i++)
125+
{ if(i==0) trigger();
126+
sample(i);
127+
}
128+
}
129+
130+
131+
132+
133+
134+

avdweb_scope.h

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

0 commit comments

Comments
 (0)