diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..762072e --- /dev/null +++ b/.clang-format @@ -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 diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b773aa6..c01d509 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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: diff --git a/README.md b/README.md index c212d8a..cd0906d 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/examples/example_scope_avdweb_2channels/example_scope_avdweb_2channels.ino b/examples/example_scope_avdweb_2channels/example_scope_avdweb_2channels.ino index e7bf109..36b0b1f 100644 --- a/examples/example_scope_avdweb_2channels/example_scope_avdweb_2channels.ino +++ b/examples/example_scope_avdweb_2channels/example_scope_avdweb_2channels.ino @@ -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(); } diff --git a/library.properties b/library.properties index 1ee3645..2581ad9 100644 --- a/library.properties +++ b/library.properties @@ -1,12 +1,12 @@ name=CodeDebugScope -version=0.0.0 +version=0.0.1 author=Albert van Dalen license=GPL-3.0-or-later maintainer=Abhijit Bose (aka. Boseji) 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 diff --git a/src/CodeDebugScope.cpp b/src/CodeDebugScope.cpp index 671bcde..591702f 100644 --- a/src/CodeDebugScope.cpp +++ b/src/CodeDebugScope.cpp @@ -77,126 +77,136 @@ template // 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); + } } diff --git a/src/CodeDebugScope.h b/src/CodeDebugScope.h index edc94bf..ec30b23 100644 --- a/src/CodeDebugScope.h +++ b/src/CodeDebugScope.h @@ -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