<commit_before>#define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))

char read_buf[128];
int buf_index = 0;

const char* AI_REF_TBL[] = {
  "DEFAULT",
  "INTERNAL",
  "EXTERNAL"
};

const uint8_t AI_REF_VAL_TBL[] = {
  DEFAULT,
  INTERNAL,
  EXTERNAL
};

// 各々のコマンドに対して処理を行う関数の型
typedef String (*TASK_FUNC_PTR)(String str);

// コマンドの文字列の先頭の文字と処理する関数を保持する構造体
struct TASK_FUNC {
  String prefix;
  TASK_FUNC_PTR func;
};

// prefixとタスク関数のテーブルを使ってキメる.
const struct TASK_FUNC TASK_FUNC_TBL[] = {
  {String("ai/read/"), &aiReadTask},
  {String("ai/ref"), &aiRefSwitchTask},
  {String("ao/write/"), &aoWriteTask},
  {String("di/read/"), &diReadTask},
  {String("do/write/"), &doWriteTask},
  {String("system/close"), &closeSerial}
};

void setup() {
  memset(read_buf,0,128);
  Serial.begin(9600);
  Serial.println("READY");
}

void loop() {
  if(Serial.available() > 0){
    while(Serial.available() > 0){
      read_buf[buf_index] = Serial.read();
      if(read_buf[buf_index] == (char)10){
        String msg = String(read_buf);
        msg.trim();
        Serial.println(task(msg));
        buf_index = 0;
        memset(read_buf,0,128);
        break;<commit_msg>コメント充実!<commit_after>#define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))

char read_buf[128];
int buf_index = 0;

const char* AI_REF_TBL[] = {
  "DEFAULT",
  "INTERNAL",
  "EXTERNAL"
};

const uint8_t AI_REF_VAL_TBL[] = {
  DEFAULT,
  INTERNAL,
  EXTERNAL
};

// 各々のコマンドに対して処理を行う関数の型
typedef String (*TASK_FUNC_PTR)(String str);

// コマンドの文字列の先頭の文字と処理する関数を保持する構造体
struct TASK_FUNC {
  String prefix;
  TASK_FUNC_PTR func;
};

// prefixとタスク関数のテーブルを使ってキメる.
const struct TASK_FUNC TASK_FUNC_TBL[] = {
  /*
     Read AI port.
     format  => ai/read/{port}
     {port}  => port number.
     example => ai/read/0
   */
  {String("ai/read/"), &aiReadTask},

  /*
    Switch AI reference volt.
    format  => ai/ref?type={TYPE}
    {TYPE}  => DEFAULT | INTERNAL | EXTERNAL
    example => ai/ref?type=INTERNAL
   */
  {String("ai/ref"), &aiRefSwitchTask},

  /*
     Write AO port.
     format  => ao/write/{port}?val={val}
     {port}  => port number.
     {val}   => write value.
     example => ao/write/1?val=100
   */
  {String("ao/write/"), &aoWriteTask},

  /*
     Read DI port.
     format  => di/read/{port}
     {port}  => port number.
     example => di/read/2
   */
  {String("di/read/"), &diReadTask},


  /*
     Write DO port.
     format  => do/write/{port}?val={val}
     {port}  => port number.
     {val}   => write value.
     example => do/write/3?val=1
   */
  {String("do/write/"), &doWriteTask},



  /*
     Close serial port.
     format => system/close
   */
  {String("system/close"), &closeSerial}
};

void setup() {
  memset(read_buf,0,128);
  Serial.begin(9600);
  Serial.println("READY");
}

void loop() {
  if(Serial.available() > 0){
    while(Serial.available() > 0){
      read_buf[buf_index] = Serial.read();
      if(read_buf[buf_index] == (char)10){
        String msg = String(read_buf);
        msg.trim();
        Serial.println(task(msg));
        buf_index = 0;
        memset(read_buf,0,128);
        break;
{'fraction_non_alphanumeric': 0.1736622877282922, 'fraction_numerical': 0.01730214674783723, 'mean_word_length': 3.059817945383615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '5206152', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1267, 'n_words': 210}