<commit_before>
#include "split.h"

typedef void (* CommandFuncPtr)(String args); // typedef to the command

struct Command {
  char code[5]; // the code used to call the command
  String help; // the snippet help version
  CommandFuncPtr cmd; // point to the command to be called
};

#define _VERSION "0.2"
#define MAX_COMMANDS 10

String command;

Command com[MAX_COMMANDS];


void setup() {
  Serial.begin(9600);
  sysprintln("Send a command to start. Help if you have questions");
  command = "";
  com[0]=(Command){"HELP", "Prints this. Try HELP <CMD> for more", command_help};
  com[1]=(Command){"LSA", "Lists status of all analog pins", command_list_analog};
  com[2]=(Command){"ANR", "Reads a specific analog pin", command_read_analog};
  com[3]=(Command){"ANW", "Writes to an analog pin", command_analog_write};
  com[4]=(Command){"PINM", "Sets the mode of a digital pin [pin IN|OUT]", command_setmode};
  com[5]=(Command){"DIGW", "Writes to the digital pin", command_digital_write};
}

void loop() {
  while (Serial.available() > 0) {
     char ch = Serial.read();
     if (ch == 10) {
       // new line so now we can attempt to process the line
       process_command(&command);
       command = "";
     } else {
       command += String(ch);
     }
  }
}

void process_command(String* command) {
  // this method takes the command string and then breaks it down
  // looking for the relevant command and doing something with it or erroring.
  String argv[2]; // we have 2 args, the command and the param
  split(' ', *command, argv, 1); // so split only once
  
  int cmd_index = command_item(argv[0]);
  if (cmd_index >= 0) {
    com[cmd_index].cmd(argv[1]);
  } else {
    sysprintln("500 Couldn't find command");
  }
  
  return;
}

int command_item(String cmd_code) {
  // this method does all of the comparison stuff to determine the id of a command
  // which it then passes back
  int i=0;
  boolean arg_found = false;
  // look through the array of commands until you find it or else you exhaust the list.
  while (!arg_found && i<MAX_COMMANDS) {
     if (cmd_code.equalsIgnoreCase((String)com[i].code)) {
       arg_found = true;
      } else {
        i++;
      }
  }
  
  if (arg_found) {
    return (i);
  } else {
    return (-1);
  }
  
}

void command_help(String args) {
  // this command spits out the help messages
  
  int cmd_index;
  if (args.length() >=2) {
    // we attempt to see if there is a command we should spit out instead.
    cmd_index = command_item(args);
    if (cmd_index < 0) sysprintln("Syntax error please use a command");
  } else {
    cmd_index = -1;
  }
  
  if (cmd_index < 0) {
    sysprint("System Help - Version: ");
    sysprintln(_VERSION);
    sysprintln("Available commands");
    sysprintln("------------------");
    for (int i=0; i<MAX_COMMANDS; i++){
      if (com[i].help != ""){
        sysprint(com[i].code);
        sysprint(" ");
        sysprintln(com[i].help);
      }
    }
  } else {
    sysprintln("HELP");
    sysprint(com[cmd_index].code);
    sysprint(": ");
    sysprintln(com[cmd_index].help);
  }
}

void command_list_analog(String args) {
  // list the current status of all the analog pins.
  sysprintln("200 Analog Read all pins");
  for (int i=0; i<6; i++) {
    sysprint("211 A");
    sysprint(i);
    sysprint(" ");
    sysprintln(analogRead(i));
  }
}

void command_read_analog(String args) {
    // reads a specified analog pin
    // argument passed in should simply be a number and it's that one we read.<commit_msg>cleaned up naming<commit_after>
#include "split.h"

typedef void (* CommandFuncPtr)(String args); // typedef to the command

struct Command {
  char code[5]; // the code used to call the command
  String help; // the snippet help version
  CommandFuncPtr cmd; // point to the command to be called
};

#define _VERSION "0.2"
#define MAX_COMMANDS 10

String command;

Command com[MAX_COMMANDS];


void setup() {
  Serial.begin(9600);
  sysprintln("Send a command to start. Help if you have questions");
  command = "";
  com[0]=(Command){"HELP", "Prints this. Try HELP <CMD> for more", command_help};
  com[1]=(Command){"LSA", "Lists status of all analog pins", command_list_analog};
  com[2]=(Command){"ANR", "Reads a specific analog pin", command_analog_read};
  com[3]=(Command){"ANW", "Writes to an analog pin", command_analog_write};
  com[4]=(Command){"PINM", "Sets the mode of a digital pin [pin IN|OUT]", command_setmode};
  com[5]=(Command){"DIGW", "Writes to the digital pin", command_digital_write};
}

void loop() {
  while (Serial.available() > 0) {
     char ch = Serial.read();
     if (ch == 10) {
       // new line so now we can attempt to process the line
       process_command(&command);
       command = "";
     } else {
       command += String(ch);
     }
  }
}

void process_command(String* command) {
  // this method takes the command string and then breaks it down
  // looking for the relevant command and doing something with it or erroring.
  String argv[2]; // we have 2 args, the command and the param
  split(' ', *command, argv, 1); // so split only once
  
  int cmd_index = command_item(argv[0]);
  if (cmd_index >= 0) {
    com[cmd_index].cmd(argv[1]);
  } else {
    sysprintln("500 Couldn't find command");
  }
  
  return;
}

int command_item(String cmd_code) {
  // this method does all of the comparison stuff to determine the id of a command
  // which it then passes back
  int i=0;
  boolean arg_found = false;
  // look through the array of commands until you find it or else you exhaust the list.
  while (!arg_found && i<MAX_COMMANDS) {
     if (cmd_code.equalsIgnoreCase((String)com[i].code)) {
       arg_found = true;
      } else {
        i++;
      }
  }
  
  if (arg_found) {
    return (i);
  } else {
    return (-1);
  }
  
}

void command_help(String args) {
  // this command spits out the help messages
  
  int cmd_index;
  if (args.length() >=2) {
    // we attempt to see if there is a command we should spit out instead.
    cmd_index = command_item(args);
    if (cmd_index < 0) sysprintln("Syntax error please use a command");
  } else {
    cmd_index = -1;
  }
  
  if (cmd_index < 0) {
    sysprint("System Help - Version: ");
    sysprintln(_VERSION);
    sysprintln("Available commands");
    sysprintln("------------------");
    for (int i=0; i<MAX_COMMANDS; i++){
      if (com[i].help != ""){
        sysprint(com[i].code);
        sysprint(" ");
        sysprintln(com[i].help);
      }
    }
  } else {
    sysprintln("HELP");
    sysprint(com[cmd_index].code);
    sysprint(": ");
    sysprintln(com[cmd_index].help);
  }
}

void command_list_analog(String args) {
  // list the current status of all the analog pins.
  sysprintln("200 Analog Read all pins");
  for (int i=0; i<6; i++) {
    sysprint("211 A");
    sysprint(i);
    sysprint(" ");
    sysprintln(analogRead(i));
  }
}

void command_analog_read(String args) {
    // reads a specified analog pin
    // argument passed in should simply be a number and it's that one we read.
{'fraction_non_alphanumeric': 0.1388335704125178, 'fraction_numerical': 0.011948790896159318, 'mean_word_length': 3.033849684452094, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 11, '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': '2557370', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2274, 'n_words': 745}