<commit_before>#include "mhs5200.hpp"
#include <malloc.h>
#include <iostream>
#include <iomanip>
#include <functional>
#include <map>
#include <vector>
#include <string>
#include <string.h>
#include <stdlib.h>

using namespace std;

bool parseInt(const char *str, int &value) {
    char *p = nullptr;
    int i = (int) strtol(str, &p, 10);
    if ( p == nullptr || *p != 0 || p == str) {
        return false;
    }
    value = i;
    return true;
}

bool parseDouble(const char *str, double &value) {
    char *p = nullptr;
    double d = strtod(str, &p);
    if ( p == nullptr || *p != 0 || p == str) {
        return false;
    }
    value = d;
    return true;
};

void raise_expected_more_argments( const char *command ) {
    stringstream ss;
    ss << "Error: ";
    ss << command;
    ss << " Expected more arguments.";
    throw ss.str();
}

void raise_expected_argument( const char *command, const char *argnickname, const char *suggestedValues, const char *invalidValue ) {
    stringstream ss;
    ss << "Error: Invalid argument for " << command << ". Expected " << argnickname << " with value of " << suggestedValues << ". " << invalidValue << " not accepted.";
    throw ss.str();
}

int main( int argc, const char *argv[] ) 
{
    const char *deviceName;
    MHS5200Driver signalGenerator;
    int currentChannel = -1;
    int argp = 1;
    vector< function<void()> > commandChain;
    map<string, function<void(int argc, const char *argv[])> > commandParser;
    
    try {
        commandParser["-?"] = [](int argc, const char *argv[])->void {
            printf("Usage: %s <tty device> <command list>\n\n", argv[0]);
            printf(" Command List:\n");
            printf("\t-?, --help\t\tShows this information and terminate.\n");
            printf("\tchannel 1/2\t\tSet channel to use for subsequent commands.\n");
            printf("\tdebug\t\t\tOutput debug trace information.\n");
            printf("\tstatus\t\t\tShows this command information.\n");
            printf("\tfreq, frequency <hz>\tSet frequency in hz.\n");
            printf("\tduty <percent>\t\tSet duty cycle percent.\n");
            printf("\tamplitude <volts>\t\tSet peek to peek amplitude of the wave.\n");
            printf("\toffset <+/-120>\t\tSets the voltage offset from between -120%% and +120%%.\n");
            printf("\tphase <0-359>\t\tSets the phase angle offset.\n");
            printf("\ton/off\t\t\tTurn the channel on or off (will change the displayed channel).\n");
            printf("\tactive\t\t\tSet the device to display channel.\n");
            printf("\tinverse\t\t\tSet the previously selected wave form to be inverted.\n");
            printf("\tstore <0-9>\t\tSave channel settings to memory slot 0-9.\n");
            printf("\tload <0-9>\t\tLoad channel settings from memory slot 0-9.\n");
            
            printf("\n\tWave form selection command:\n");
            printf("\t\tsine\t\tSine wave output.\n");
            printf("\t\tsquare\t\tSquare wave output.\n");
            printf("\t\ttriangle\t\tTriangle wave output.\n");
            printf("\t\tsaw\t\tSawtooth / upward slope wave output.\n");
            printf("\t\treversesaw\tReverse sawtooth / downward ramp waveform.\n");
            printf("\t\tarb <0-15>\tArbitrary waveform 0-15.\n");
            printf("\n\tUsing a wave form command turns off inverse.\n");
            
            
            printf("\n\nMost commands are executed in the order given so commands like channel will affect certain subsequent commands.\n\nExample: %s /dev/ttyUSB0 channel 1 off square inverse freq 12345678.12 on\n", argv[0]);
            printf("The above example turns off channel 1, sets waveform to inverted sine wave of a given frequency\n");
            printf("then turns the channel back on.\n");
            printf("\nYou can combine commands in any order, switching channels at will. All commands will be executed in the specified order\nonly after validating that the command line does not contain errors.\n");
            exit(0);
        };
        
        commandParser["--help"] = commandParser["-?"]; 
        
        commandParser["debug"] = [&](int argc, const char *argv[])->void {
            argp++;
            signalGenerator.setDebugOutput(true);
        };
        
        commandParser["channel"] = [&](int argc, const char *argv[])->void {
            int cmdarg = argp++;
            if ( argp < argc ) {
                const char *arg = argv[argp++];
                int channel;
                if ( !parseInt(arg, channel) || channel < 1 || channel > 2 ) {
                    raise_expected_argument(argv[cmdarg], "<channel number>", "1/2", arg);
                }
                commandChain.push_back([&currentChannel,channel](){
                    currentChannel = channel;
                });
            } else {
                raise_expected_more_argments(argv[cmdarg]);
            }
        };
        
        commandParser["status"] = [&](int argc, const char *argv[])->void {
            argp++;
            
            commandChain.push_back([&](){
                for ( int channel = 1; channel <= 2; channel++ ) {
                    printf("%d: %2.3fV %8.2fHz %2.1f%% Duty\n", channel, signalGenerator.getAmplitude(channel), signalGenerator.getFrequency(channel), signalGenerator.getDutyCycle(channel) );
                }
            });
        };
        
        commandParser["on"] = [&](int argc, const char *argv[])->void {
            argp++;
            commandChain.push_back([&]() {
                int activeChannel = signalGenerator.getCurrentChannel();
                if ( currentChannel != activeChannel )
                    signalGenerator.setCurrentChannel(currentChannel);
                signalGenerator.setCurrentChannelStatus(true);
                if ( currentChannel != activeChannel )    
                    signalGenerator.setCurrentChannel(activeChannel);
            });
        };
        
        commandParser["off"] = [&](int argc, const char *argv[])->void {
            argp++;
            commandChain.push_back([&]() {
                int activeChannel = signalGenerator.getCurrentChannel();
                if ( currentChannel != activeChannel )
                    signalGenerator.setCurrentChannel(currentChannel);
                signalGenerator.setCurrentChannelStatus(false);
                if ( currentChannel != activeChannel )    <commit_msg>Updated status command.<commit_after>#include "mhs5200.hpp"
#include <malloc.h>
#include <iostream>
#include <iomanip>
#include <functional>
#include <map>
#include <vector>
#include <string>
#include <string.h>
#include <stdlib.h>

using namespace std;

const char *waveToString( MHS5200Driver::WaveType wave ) {
    switch ( wave ) {
        case MHS5200Driver::WaveType::Sine: return "Sine";
        case MHS5200Driver::WaveType::Square: return "Square";
        case MHS5200Driver::WaveType::Triangle: return "Tri";
        case MHS5200Driver::WaveType::Sawtooth: return "Saw";
        case MHS5200Driver::WaveType::SawtoothReverse: return "SawRev";
        case MHS5200Driver::WaveType::Arbitrary0: return "Arb0";
        case MHS5200Driver::WaveType::Arbitrary1: return "Arb1";
        case MHS5200Driver::WaveType::Arbitrary2: return "Arb2";
        case MHS5200Driver::WaveType::Arbitrary3: return "Arb3";
        case MHS5200Driver::WaveType::Arbitrary4: return "Arb4";
        case MHS5200Driver::WaveType::Arbitrary5: return "Arb5";
        case MHS5200Driver::WaveType::Arbitrary6: return "Arb6";
        case MHS5200Driver::WaveType::Arbitrary7: return "Arb7";
        case MHS5200Driver::WaveType::Arbitrary8: return "Arb8";
        case MHS5200Driver::WaveType::Arbitrary9: return "Arb9";
        case MHS5200Driver::WaveType::Arbitrary10: return "Arb10";
        case MHS5200Driver::WaveType::Arbitrary11: return "Arb11";
        case MHS5200Driver::WaveType::Arbitrary12: return "Arb12";
        case MHS5200Driver::WaveType::Arbitrary13: return "Arb13";
        case MHS5200Driver::WaveType::Arbitrary14: return "Arb14";
        case MHS5200Driver::WaveType::Arbitrary15: return "Arb15";
    }
    return nullptr;
}

bool parseInt(const char *str, int &value) {
    char *p = nullptr;
    int i = (int) strtol(str, &p, 10);
    if ( p == nullptr || *p != 0 || p == str) {
        return false;
    }
    value = i;
    return true;
}

bool parseDouble(const char *str, double &value) {
    char *p = nullptr;
    double d = strtod(str, &p);
    if ( p == nullptr || *p != 0 || p == str) {
        return false;
    }
    value = d;
    return true;
};

void raise_expected_more_argments( const char *command ) {
    stringstream ss;
    ss << "Error: ";
    ss << command;
    ss << " Expected more arguments.";
    throw ss.str();
}

void raise_expected_argument( const char *command, const char *argnickname, const char *suggestedValues, const char *invalidValue ) {
    stringstream ss;
    ss << "Error: Invalid argument for " << command << ". Expected " << argnickname << " with value of " << suggestedValues << ". " << invalidValue << " not accepted.";
    throw ss.str();
}

int main( int argc, const char *argv[] ) 
{
    const char *deviceName;
    MHS5200Driver signalGenerator;
    int currentChannel = -1;
    int argp = 1;
    vector< function<void()> > commandChain;
    map<string, function<void(int argc, const char *argv[])> > commandParser;
    
    try {
        commandParser["-?"] = [](int argc, const char *argv[])->void {
            printf("Usage: %s <tty device> <command list>\n\n", argv[0]);
            printf(" Command List:\n");
            printf("\t-?, --help\t\tShows this information and terminate.\n");
            printf("\tchannel 1/2\t\tSet channel to use for subsequent commands.\n");
            printf("\tdebug\t\t\tOutput debug trace information.\n");
            printf("\tstatus\t\t\tShows this command information.\n");
            printf("\tfreq, frequency <hz>\tSet frequency in hz.\n");
            printf("\tduty <percent>\t\tSet duty cycle percent.\n");
            printf("\tamplitude <volts>\t\tSet peek to peek amplitude of the wave.\n");
            printf("\toffset <+/-120>\t\tSets the voltage offset from between -120%% and +120%%.\n");
            printf("\tphase <0-359>\t\tSets the phase angle offset.\n");
            printf("\ton/off\t\t\tTurn the channel on or off (will change the displayed channel).\n");
            printf("\tactive\t\t\tSet the device to display channel.\n");
            printf("\tinverse\t\t\tSet the previously selected wave form to be inverted.\n");
            printf("\tstore <0-9>\t\tSave channel settings to memory slot 0-9.\n");
            printf("\tload <0-9>\t\tLoad channel settings from memory slot 0-9.\n");
            
            printf("\n\tWave form selection command:\n");
            printf("\t\tsine\t\tSine wave output.\n");
            printf("\t\tsquare\t\tSquare wave output.\n");
            printf("\t\ttriangle\t\tTriangle wave output.\n");
            printf("\t\tsaw\t\tSawtooth / upward slope wave output.\n");
            printf("\t\treversesaw\tReverse sawtooth / downward ramp waveform.\n");
            printf("\t\tarb <0-15>\tArbitrary waveform 0-15.\n");
            printf("\n\tUsing a wave form command turns off inverse.\n");
            
            
            printf("\n\nMost commands are executed in the order given so commands like channel will affect certain subsequent commands.\n\nExample: %s /dev/ttyUSB0 channel 1 off square inverse freq 12345678.12 on\n", argv[0]);
            printf("The above example turns off channel 1, sets waveform to inverted sine wave of a given frequency\n");
            printf("then turns the channel back on.\n");
            printf("\nYou can combine commands in any order, switching channels at will. All commands will be executed in the specified order\nonly after validating that the command line does not contain errors.\n");
            exit(0);
        };
        
        commandParser["--help"] = commandParser["-?"]; 
        
        commandParser["debug"] = [&](int argc, const char *argv[])->void {
            argp++;
            signalGenerator.setDebugOutput(true);
        };
        
        commandParser["channel"] = [&](int argc, const char *argv[])->void {
            int cmdarg = argp++;
            if ( argp < argc ) {
                const char *arg = argv[argp++];
                int channel;
                if ( !parseInt(arg, channel) || channel < 1 || channel > 2 ) {
                    raise_expected_argument(argv[cmdarg], "<channel number>", "1/2", arg);
                }
                commandChain.push_back([&currentChannel,channel](){
                    currentChannel = channel;
                });
            } else {
                raise_expected_more_argments(argv[cmdarg]);
            }
        };
        
        commandParser["status"] = [&](int argc, const char *argv[])->void {
            argp++;
            
            commandChain.push_back([&](){
                for ( int channel = 1; channel <= 2; channel++ ) {
                    printf("%d: %c%-6s %6.3fV  %11.2fHz  %4.1f%% Duty  %3d\u00B0 Phase  %4d%% Offset\n", channel, signalGenerator.getInverted(channel) ? '~' : ' ', waveToString(signalGenerator.getWaveType(channel)), signalGenerator.getAmplitude(channel), signalGenerator.getFrequency(channel), signalGenerator.getDutyCycle(channel), signalGenerator.getPhaseOffset(channel), signalGenerator.getOffset(channel) );
                }
            });
        };
        
        commandParser["on"] = [&](int argc, const char *argv[])->void {
            argp++;
            commandChain.push_back([&]() {
                int activeChannel = signalGenerator.getCurrentChannel();
                if ( currentChannel != activeChannel )
                    signalGenerator.setCurrentChannel(currentChannel);
                signalGenerator.setCurrentChannelStatus(true);
                if ( currentChannel != activeChannel )    
                    signalGenerator.setCurrentChannel(activeChannel);
            });
        };
        
        commandParser["off"] = [&](int argc, const char *argv[])->void {
            argp++;
            commandChain.push_back([&]() {
                int activeChannel = signalGenerator.getCurrentChannel();
                if ( currentChannel != activeChannel )
                    signalGenerator.setCurrentChannel(currentChannel);
                signalGenerator.setCurrentChannelStatus(false);
                if ( currentChannel != activeChannel )    
{'fraction_non_alphanumeric': 0.1362763915547025, 'fraction_numerical': 0.01926240745818481, 'mean_word_length': 2.280638632786148, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 65, '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': '6089183', 'n_tokens_mistral': 4333, 'n_tokens_neox': 4092, 'n_words': 1098}