<commit_before>/*
 * capture.cpp - ADC data capture
 * Tiny Scope for Arduino project
 *
 * Copyright (C)2015 <NAME>
 *
 * This file may be redistributed under the terms of the MIT license.
 * A copy of this license has been included with this distribution in the file LICENSE.
 */
#include "capture.h"

Capture::Capture(ADCInput adc, byte samples, unsigned rangemV)
:adc(adc),
 samples(samples),
 rangemV(rangemV)
{
}

int Capture::init(){
    data = (unsigned*)calloc(samples+2, sizeof(unsigned)); // two extra reads: 0 and a non-zero
    if (!data){
        samples = 0;
    }
    return data != 0;
}

/*
 * ADC allowable float for zero-detection (because of unclean ground) [mV]
 */
#define ADC_JITTER 4
/*
 * Capture the requested number of samples.
 * on return, data contains the raw capture data.
 */
void Capture::capture(){
    unsigned long start;
    unsigned *dataCur;
    unsigned v;
    byte i = samples;

    dataCur = data;
    /*
     * Attempt to latch on a zero transition
     */
    for (byte j=255; j; j--){
        v = adc.read();
        if (v < ADC_JITTER){
            *dataCur = v;
            break;
        }
    }
    for (byte j=255; j; j--){
        v = adc.read();
        if (v >= ADC_JITTER){
            dataCur++;
            *dataCur++ = v;
            break;
        } else {
            *dataCur = v;
        }
    }
    start = micros();
    for (; i; i--){
        *dataCur++ = adc.read();
    }
    elapsedus = micros() - start;
}

/*
 * Convert captured data to mV and extract min/max values
 */
void Capture::tomV(){
    unsigned v;
    minmV = rangemV;
    maxmV = 0;
    for (int i=0; i<samples; i++){
        v = map(data[i], 0, (1L<<adc.bits)-1, 0, long(rangemV));
        data[i] = v;
        if (v < minmV){
            minmV = v;
        };
        if (v > maxmV){
            maxmV = v;<commit_msg>capture(): fix fake starting 0mV value.<commit_after>/*
 * capture.cpp - ADC data capture
 * Tiny Scope for Arduino project
 *
 * Copyright (C)2015 <NAME>
 *
 * This file may be redistributed under the terms of the MIT license.
 * A copy of this license has been included with this distribution in the file LICENSE.
 */
#include "capture.h"

Capture::Capture(ADCInput adc, byte samples, unsigned rangemV)
:adc(adc),
 samples(samples),
 rangemV(rangemV)
{
}

int Capture::init(){
    data = (unsigned*)calloc(samples+2, sizeof(unsigned)); // two extra reads: 0 and a non-zero
    if (!data){
        samples = 0;
    }
    return data != 0;
}

/*
 * ADC allowable float for zero-detection (because of unclean ground) [mV]
 */
#define ADC_JITTER 4
/*
 * Capture the requested number of samples.
 * on return, data contains the raw capture data.
 */
void Capture::capture(){
    unsigned long start;
    unsigned *dataCur;
    unsigned v;
    byte i = samples;

    dataCur = data;
    /*
     * Attempt to latch on a zero transition
     */
    for (byte j=255; j; j--){
        v = adc.read();
        if (v < ADC_JITTER){
            *dataCur++ = v;
            break;
        }
    }
    for (byte j=255; j; j--){
        v = adc.read();
        if (v >= ADC_JITTER){
            *dataCur++ = v;
            break;
        } else {
            *data = v; // overwrite a previous "0" at dataCur-1
        }
    }
    start = micros();
    for (; i; i--){
        *dataCur++ = adc.read();
    }
    elapsedus = micros() - start;
}

/*
 * Convert captured data to mV and extract min/max values
 */
void Capture::tomV(){
    unsigned v;
    minmV = rangemV;
    maxmV = 0;
    for (int i=0; i<samples; i++){
        v = map(data[i], 0, (1L<<adc.bits)-1, 0, long(rangemV));
        data[i] = v;
        if (v < minmV){
            minmV = v;
        };
        if (v > maxmV){
            maxmV = v;
{'fraction_non_alphanumeric': 0.13877768881772085, 'fraction_numerical': 0.01200960768614892, 'mean_word_length': 2.012861736334405, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6735543', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1239, 'n_words': 405}