<commit_before>#include "communication.h"

#define DEBUG_PRINT_ENABLE 0
#include <debug_print.h>
#include <safestring.h>

void parse_numbers(unsigned char where[], int length, int offset, int numbers[], const static int size) {
  int j = 0;
  int sign[size];

  for (int i = 0; i < size; i++) {
    numbers[i] = 0;
    sign[i] = 1;
  }

  for (int i = offset; i < length && j < size; i++) {
    if (where[i] == ',') {
      j++;
      continue;
    } else
    if (where[i] == '-') {
      sign[j] = -1;
      continue;
    }

    int digit = where[i] - '0';

    numbers[j] *= 10;
    numbers[j] += digit;
  }

  for (int i = 0; i < size; i++)
    numbers[i] *= sign[i];
}

[[combinable]]
void balancer_communication(balancer_client balancer, bluetooth_client bluetooth, balancer_sensors_client sensors) {
  unsigned char command[128];
  int command_length;

  balancer.balance();
  while (1) {
    select {
      case bluetooth.incoming():
        bluetooth.read(command, command_length);

        if (command_length == 0)
          break;

        if (safestrstr("S", command) == 0) {
          balancer.balance();
          bluetooth.send("OK\r", 3);
        } else
        if (safestrstr("X", command) == 0) {
          balancer.stop();
          bluetooth.send("OK\r", 3);
        } else
        if (safestrstr("V?", command) == 0)
          bluetooth.send_number(sensors.battery_voltage()); else
        if (safestrstr(command, "PID=") == 0) {
          int K[3];
          parse_numbers(command, command_length, 4, K, 3);

          balancer.set_pid(K);

          bluetooth.send("OK\r", 3);
        } else
        if (safestrstr("PID?", command) == 0) {
          int K[3];<commit_msg>commands proper order<commit_after>#include "communication.h"

#define DEBUG_PRINT_ENABLE 0
#include <debug_print.h>
#include <safestring.h>

void parse_numbers(unsigned char where[], int length, int offset, int numbers[], const static int size) {
  int j = 0;
  int sign[size];

  for (int i = 0; i < size; i++) {
    numbers[i] = 0;
    sign[i] = 1;
  }

  for (int i = offset; i < length && j < size; i++) {
    if (where[i] == ',') {
      j++;
      continue;
    } else
    if (where[i] == '-') {
      sign[j] = -1;
      continue;
    }

    int digit = where[i] - '0';

    numbers[j] *= 10;
    numbers[j] += digit;
  }

  for (int i = 0; i < size; i++)
    numbers[i] *= sign[i];
}

[[combinable]]
void balancer_communication(balancer_client balancer, bluetooth_client bluetooth, balancer_sensors_client sensors) {
  unsigned char command[128];
  int command_length;

  balancer.balance();
  while (1) {
    select {
      case bluetooth.incoming():
        bluetooth.read(command, command_length);

        if (command_length == 0)
          break;

        if (safestrstr(command, "S") == 0) {
          balancer.balance();
          bluetooth.send("OK\r", 3);
        } else
        if (safestrstr(command, "X") == 0) {
          balancer.stop();
          bluetooth.send("OK\r", 3);
        } else
        if (safestrstr(command, "V?") == 0)
          bluetooth.send_number(sensors.battery_voltage()); else
        if (safestrstr(command, "PID=") == 0) {
          int K[3];
          parse_numbers(command, command_length, 4, K, 3);

          balancer.set_pid(K);

          bluetooth.send("OK\r", 3);
        } else
        if (safestrstr(command, "PID?") == 0) {
          int K[3];
{'fraction_non_alphanumeric': 0.1614614024749558, 'fraction_numerical': 0.015910430170889805, 'mean_word_length': 2.3316977428851815, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, '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': '5428824', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1170, 'n_words': 287}