Skip to content

Commit

Permalink
Merge pull request #5 from ardlib/master
Browse files Browse the repository at this point in the history
Update to Code Format
  • Loading branch information
boseji authored Aug 22, 2024
2 parents cd9e3ae + dc6bca6 commit b3037e2
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 135 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# We'll use defaults from the LLVM style, but with 4 columns indentation.
# Webkit is preference of Albert van Dalen
BasedOnStyle: WebKit
IndentWidth: 4
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v4
- uses: arduino/arduino-lint-action@v1
with:
library-manager: submit
library-manager: update
compliance: strict

format:
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ It is not always possible to debug Arduino programs by the serial monitor, as th
See the website:
http://www.avdweb.nl/arduino/libraries/oscilloscope.html

## Developer Note

Please run code formatting before committing the code.

```sh
clang-format -i src/*.* examples/**/*.ino
```

## License

`SPDX: GPL-3.0-or-later`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@

SWscope scope;

void setup(void) {
Serial.begin(115200);
scope.start(2, 10); // 2 integer channels , preSamples = 10, buffer is
// 100/4=25 integer values
// scope.testBuffer(); // print 100/2 = 50 integer values with only channel A
void setup(void)
{
Serial.begin(115200);
scope.start(2, 10); // 2 integer channels , preSamples = 10, buffer is
// 100/4=25 integer values
// scope.testBuffer(); // print 100/2 = 50 integer values with only channel A
}

void loop(void) {
static int internalValue;
internalValue += 2;
scope.probeAB(internalValue,
analogRead(A0)); // channel A = internalValue, channel B = ADC
if (internalValue > 10)
scope.trigger();
scope.showIfReady();
void loop(void)
{
static int internalValue;
internalValue += 2;
scope.probeAB(internalValue,
analogRead(A0)); // channel A = internalValue, channel B = ADC
if (internalValue > 10)
scope.trigger();
scope.showIfReady();
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name=CodeDebugScope
version=0.0.0
version=0.0.1
author=Albert van Dalen <http://www.avdweb.nl>
license=GPL-3.0-or-later
maintainer=Abhijit Bose (aka. Boseji) <boseji.com>
sentence=CodeDebugScope Library
paragraph=Easy to use Debug Library that works by capturing data in background and relaying it when needed over serial connection.
category=Data Processing
url=https://github.com/avandalen/avdweb_scope
url=https://github.com/avdwebLibraries/avdweb_CodeDebugScope
architectures=*
includes=CodeDebugScope.h
depends=Streaming
198 changes: 104 additions & 94 deletions src/CodeDebugScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,126 +77,136 @@ template <class T> // easy printing multiple variables with separator ','
}
*/
void SWscope::start(byte _channels, int _preSamples,
unsigned int _recordLenght) {
ptr = samples = triggerPtr = preSamples = triggered = 0;
stopPtr = 32767;
samplingOn = 1;
channels = _channels;
recordLenght = maxScopeBytes / (sizeof(short) * channels);
if (_recordLenght <= recordLenght)
recordLenght = _recordLenght;
if (abs(_preSamples) <= recordLenght)
preSamples = _preSamples;
unsigned int _recordLenght)
{
ptr = samples = triggerPtr = preSamples = triggered = 0;
stopPtr = 32767;
samplingOn = 1;
channels = _channels;
recordLenght = maxScopeBytes / (sizeof(short) * channels);
if (_recordLenght <= recordLenght)
recordLenght = _recordLenght;
if (abs(_preSamples) <= recordLenght)
preSamples = _preSamples;
}

void SWscope::showIfReady() {
if (!canShow)
return;
canShow = 0;
Serial.begin(115200); // HIER doen Serial part moet buiten de bibliotheek
// interrupts(); // hoeft niet
while (!Serial)
; // wait on Serial Monitor
Serial << "\nusPerDiv: " << usPerDiv << "\nptr, values ";
ptr = stopPtr;
do {
if (channels == 1)
Serial << endl << ptr, ringBuffer.chA[ptr];
if (channels == 2)
Serial << endl << ptr, ringBuffer.chAB[ptr][0], ringBuffer.chAB[ptr][1];
if (channels == 3)
Serial << endl
<< ptr,
ringBuffer.chABC[ptr][0], ringBuffer.chABC[ptr][1],
ringBuffer.chABC[ptr][2];
if (channels == 4)
Serial << endl
<< ptr,
ringBuffer.chABCD[ptr][0], ringBuffer.chABCD[ptr][1],
ringBuffer.chABCD[ptr][2], ringBuffer.chABCD[ptr][3];
if (ptr == triggerPtr)
Serial << " trigger";
ptr = calcPtr(ptr + 1);
} while (ptr != stopPtr);
stopPtr = 32767;
void SWscope::showIfReady()
{
if (!canShow)
return;
canShow = 0;
Serial.begin(115200); // HIER doen Serial part moet buiten de bibliotheek
// interrupts(); // hoeft niet
while (!Serial)
; // wait on Serial Monitor
Serial << "\nusPerDiv: " << usPerDiv << "\nptr, values ";
ptr = stopPtr;
do {
if (channels == 1)
Serial << endl
<< ptr,
ringBuffer.chA[ptr];
if (channels == 2)
Serial << endl
<< ptr,
ringBuffer.chAB[ptr][0], ringBuffer.chAB[ptr][1];
if (channels == 3)
Serial << endl
<< ptr,
ringBuffer.chABC[ptr][0], ringBuffer.chABC[ptr][1],
ringBuffer.chABC[ptr][2];
if (channels == 4)
Serial << endl
<< ptr,
ringBuffer.chABCD[ptr][0], ringBuffer.chABCD[ptr][1],
ringBuffer.chABCD[ptr][2], ringBuffer.chABCD[ptr][3];
if (ptr == triggerPtr)
Serial << " trigger";
ptr = calcPtr(ptr + 1);
} while (ptr != stopPtr);
stopPtr = 32767;
}

void SWscope::stop() { stopPtr = calcPtr(ptr + 1); }

void SWscope::probeA(short valueA) // 1 channel
{
if (samplingOn) {
ringBuffer.chA[ptr] = valueA;
sampleControl();
}
if (samplingOn) {
ringBuffer.chA[ptr] = valueA;
sampleControl();
}
}

void SWscope::probeAB(short valueA, short valueB) // 2 channels
{
if (samplingOn) {
ringBuffer.chAB[ptr][0] = valueA;
ringBuffer.chAB[ptr][1] = valueB;
sampleControl();
}
if (samplingOn) {
ringBuffer.chAB[ptr][0] = valueA;
ringBuffer.chAB[ptr][1] = valueB;
sampleControl();
}
}

void SWscope::probeABC(short valueA, short valueB, short valueC) // 3 channels
{
if (samplingOn) {
ringBuffer.chABC[ptr][0] = valueA;
ringBuffer.chABC[ptr][1] = valueB;
ringBuffer.chABC[ptr][2] = valueC;
sampleControl();
}
if (samplingOn) {
ringBuffer.chABC[ptr][0] = valueA;
ringBuffer.chABC[ptr][1] = valueB;
ringBuffer.chABC[ptr][2] = valueC;
sampleControl();
}
}

void SWscope::probeABCD(short valueA, short valueB, short valueC,
short valueD) // 4 channels
short valueD) // 4 channels
{
if (samplingOn) {
ringBuffer.chABCD[ptr][0] = valueA;
ringBuffer.chABCD[ptr][1] = valueB;
ringBuffer.chABCD[ptr][2] = valueC;
ringBuffer.chABCD[ptr][3] = valueD;
sampleControl();
}
if (samplingOn) {
ringBuffer.chABCD[ptr][0] = valueA;
ringBuffer.chABCD[ptr][1] = valueB;
ringBuffer.chABCD[ptr][2] = valueC;
ringBuffer.chABCD[ptr][3] = valueD;
sampleControl();
}
}

void SWscope::sampleControl() { // doen micros weggelaten if(samples == 0)
// sample0_us = micros();
samples++;
ptr = calcPtr(ptr + 1);
if (ptr == stopPtr) {
samplingOn = 0;
canShow = 1;
unsigned long stop_us; // doen = micros(); // to avoid delay, start with
// this
usPerDiv = samples > 1 ? (stop_us - sample0_us) / (samples - 1)
: 0; // is not exact?
}
void SWscope::sampleControl()
{ // doen micros weggelaten if(samples == 0)
// sample0_us = micros();
samples++;
ptr = calcPtr(ptr + 1);
if (ptr == stopPtr) {
samplingOn = 0;
canShow = 1;
unsigned long stop_us; // doen = micros(); // to avoid delay, start with
// this
usPerDiv = samples > 1 ? (stop_us - sample0_us) / (samples - 1)
: 0; // is not exact?
}
}

unsigned int SWscope::calcPtr(int _ptr) {
if (_ptr >= recordLenght)
return (_ptr - recordLenght); // happens most frequent
if (_ptr < 0)
return (_ptr + recordLenght);
return _ptr;
unsigned int SWscope::calcPtr(int _ptr)
{
if (_ptr >= recordLenght)
return (_ptr - recordLenght); // happens most frequent
if (_ptr < 0)
return (_ptr + recordLenght);
return _ptr;
}

void SWscope::trigger() {
if (!triggered) {
triggerPtr = ptr;
stopPtr = calcPtr(triggerPtr - preSamples);
triggered = 1;
}
void SWscope::trigger()
{
if (!triggered) {
triggerPtr = ptr;
stopPtr = calcPtr(triggerPtr - preSamples);
triggered = 1;
}
}

void SWscope::testBuffer() {
start(1);
for (int i = 0; i < 25000; i++) {
if (i == 0)
trigger();
probeA(i);
}
void SWscope::testBuffer()
{
start(1);
for (int i = 0; i < 25000; i++) {
if (i == 0)
trigger();
probeA(i);
}
}
49 changes: 24 additions & 25 deletions src/CodeDebugScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,41 @@
#if defined(__arm__)
const unsigned int maxScopeBytes = 7000; // SAMD21: max ~ 7000
#else
const int maxScopeBytes =
500; // ATTENTION SET HERE, AVR ATmega328: max ~ 780 ATmega168: max ~ 320
const int maxScopeBytes = 500; // ATTENTION SET HERE, AVR ATmega328: max ~ 780 ATmega168: max ~ 320
// const int maxScopeBytes = 100; // ATTENTION SET HERE, AVR ATmega328: max ~
// 780 ATmega168: max ~ 320
#endif

class SWscope {
public:
void start(byte _channels, int _preSamples = 0,
unsigned int _recordLenght = 65535);
void probeA(short valueA);
void probeAB(short valueA, short valueB);
void probeABC(short valueA, short valueB, short valueC);
void probeABCD(short valueA, short valueB, short valueC, short valueD);
void trigger();
void stop();
void showIfReady();
void testBuffer();
void start(byte _channels, int _preSamples = 0,
unsigned int _recordLenght = 65535);
void probeA(short valueA);
void probeAB(short valueA, short valueB);
void probeABC(short valueA, short valueB, short valueC);
void probeABCD(short valueA, short valueB, short valueC, short valueD);
void trigger();
void stop();
void showIfReady();
void testBuffer();

volatile bool canShow;
volatile bool canShow;

protected:
void sampleControl();
unsigned int calcPtr(int ptr);
void sampleControl();
unsigned int calcPtr(int ptr);

volatile union {
short chA[maxScopeBytes / 2];
short chAB[maxScopeBytes / 4][2];
short chABC[maxScopeBytes / 3][3];
short chABCD[maxScopeBytes / 8][4];
} ringBuffer;
volatile union {
short chA[maxScopeBytes / 2];
short chAB[maxScopeBytes / 4][2];
short chABC[maxScopeBytes / 3][3];
short chABCD[maxScopeBytes / 8][4];
} ringBuffer;

volatile unsigned long sample0_us, usPerDiv;
volatile bool triggered, samplingOn;
volatile byte channels; // 1, 2, 3, 4 channels
volatile int recordLenght, preSamples, ptr, samples, triggerPtr, stopPtr;
volatile unsigned long sample0_us, usPerDiv;
volatile bool triggered, samplingOn;
volatile byte channels; // 1, 2, 3, 4 channels
volatile int recordLenght, preSamples, ptr, samples, triggerPtr, stopPtr;
};

#endif

0 comments on commit b3037e2

Please sign in to comment.