<commit_before>/**
 * @file Connection.cpp
 * @brief
 * @author <NAME>
 * @version 0.0.1
 * @date 2016-01-05
 */

#include "Connection.h"

Connection::Connection(Bluetooth &new_bt, int id) : bt(new_bt)
{
  this->id = id;
}

int Connection::write(int percent)
{
  int rc = 0;
  StaticJsonBuffer<200> buffer;
  JsonObject& object = buffer.createObject();

  if (!bt.connected()) {
    return -1;
  }

  object["id"] = this->id;
  object["throttle"] = percent;

  /* Don't pretty print, I deliminate on \n */
  rc = object.printTo(bt);
  bt.write('\n');

  return rc;
}

int Connection::read(int *out_percent)
{
  int rc = 0;
  char buff[200];
  StaticJsonBuffer<200> buffer;

  if (!bt.connected()) {
    return -1;
  }

  rc = bt.readBytesUntil('\n', buff, sizeof(buff));
  if (rc <= 0) {
    return -1;
  }

  /* Set last char to NULL */
  buff[rc] = '\0';

  JsonObject& object = buffer.parseObject(buff);

  if (!object.success()) {
    return -1;
  }

  if (object["id"] != id) {
    /* The ID doesn't match... We should ignore this message */
    return -1;
  }

  *out_percent = object["throttle"];
  return rc;
}
<commit_msg>Change status codes for debugging<commit_after>/**
 * @file Connection.cpp
 * @brief
 * @author <NAME>
 * @version 0.0.1
 * @date 2016-01-05
 */

#include "Connection.h"

Connection::Connection(Bluetooth &new_bt, int id) : bt(new_bt)
{
  this->id = id;
}

int Connection::write(int percent)
{
  int rc = 0;
  StaticJsonBuffer<200> buffer;
  JsonObject& object = buffer.createObject();

  if (!bt.connected()) {
    return -1;
  }

  object["id"] = this->id;
  object["throttle"] = percent;

  /* Don't pretty print, I deliminate on \n */
  rc = object.printTo(bt);
  bt.write('\n');

  return rc;
}

int Connection::read(int *out_percent)
{
  int rc = 0;
  char buff[200];
  StaticJsonBuffer<200> buffer;

  if (!bt.connected()) {
    return -1;
  }

  rc = bt.readBytesUntil('\n', buff, sizeof(buff));
  if (rc <= 0) {
    return -2;
  }

  /* Set last char to NULL */
  buff[rc] = '\0';

  JsonObject& object = buffer.parseObject(buff);

  if (!object.success()) {
    return -3;
  }

  if (object["id"] != id) {
    /* The ID doesn't match... We should ignore this message */
    return -4;
  }

  *out_percent = object["throttle"];
  return rc;
}

{'fraction_non_alphanumeric': 0.1796669588080631, 'fraction_numerical': 0.025416301489921123, 'mean_word_length': 2.9566724436741767, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '7410229', 'n_tokens_mistral': 898, 'n_tokens_neox': 836, 'n_words': 214}