<commit_before>/*
 * Name: <NAME>
 * Project: Half duplex protocol
 *
 * Source: src/communication.c
 */

#include "communication.h"

/******************** Sending data functions ********************/

void initialize(void) {
    /* Clears the data */
    data_clear(send_data, DATA_BYTES_RESERVED);
    data_clear(_settings, SETTINGS_BYTES_RESERVED);
    data_clear(_data_count, DATA_BYTES_COUNT_RESERVED);

    /* Sets the default values unless changed */
    bitrate = DEFAULT_BITRATE;
    half_duplex = DEFAULT_HALF_DUPLEX;
    debug = DEFAULT_DEBUG;
}

void transmit(void) {
    /* Setup the gpio & delay and pull gpio to low */
    platform_delay_setup();
    platform_gpio_setup();
    platform_gpio_set_low();

    /* Set the _data_count array to correct bits */
    unsigned char byte_count = _count_bytes_in_use(send_data, DATA_BYTES_RESERVED);
    data_set_byte(_data_count, DATA_BYTES_COUNT_RESERVED, 0, byte_count);

    /* Gets the delay per bit */
    int delay_per_bit = (int)(1000000.0 / (bitrate * 1));

    /*** Starts transmitting below ***/

    /* Starts the bitrate initialization */
    int i;
    int j;
    char bit;

    for (i = 0; i < BITRATE_BITS_RESERVED; i++) {
        if (i % 2 == 0) {
            _set_gpio(1, delay_per_bit);
        } else {
            _set_gpio(0, delay_per_bit);
        }
    }

    /* Send the settings as increased bits */
    for (i = 0; i < SETTINGS_BYTES_RESERVED; i++) {
        for (j = 7; j >= 0; j--) {
            bit = (_settings[i] >> j) & 1;
            _set_gpio_with_increased_bit(bit, delay_per_bit);
        }
    }

    /* Send the number as increased bits of bytes that will be sent in the next step */
    for (i = 0; i < DATA_BYTES_COUNT_RESERVED; i++) {
        for (j = 7; j >= 0; j--) {
            bit = (byte_count >> j) & 1;
            _set_gpio(bit, delay_per_bit);
        }
    }

    /* Send the data as increased bits */
    for (i = 0; i < byte_count; i++) {
        for (j = 7; j >= 0; j--) {
            bit = (send_data[i] >> j) & 1;
            _set_gpio_with_increased_bit(bit, delay_per_bit);
        }
    }

    /* Sets the gpio to low after transmit */
    platform_gpio_set_low();
}

/******************** Modifying data array ********************/

void data_set_bit(unsigned char *data, const int length, const int bit_position, const int bit) {
    int bit_pos = bit_position;
    int byte_pos = 0;

    /* "Klättra" ner till rätt position för bit respektive byte */<commit_msg>Fixed forgotten bit increasing.<commit_after>/*
 * Name: <NAME>
 * Project: Half duplex protocol
 *
 * Source: src/communication.c
 */

#include "communication.h"

/******************** Sending data functions ********************/

void initialize(void) {
    /* Clears the data */
    data_clear(send_data, DATA_BYTES_RESERVED);
    data_clear(_settings, SETTINGS_BYTES_RESERVED);
    data_clear(_data_count, DATA_BYTES_COUNT_RESERVED);

    /* Sets the default values unless changed */
    bitrate = DEFAULT_BITRATE;
    half_duplex = DEFAULT_HALF_DUPLEX;
    debug = DEFAULT_DEBUG;
}

void transmit(void) {
    /* Setup the gpio & delay and pull gpio to low */
    platform_delay_setup();
    platform_gpio_setup();
    platform_gpio_set_low();

    /* Set the _data_count array to correct bits */
    unsigned char byte_count = _count_bytes_in_use(send_data, DATA_BYTES_RESERVED);
    data_set_byte(_data_count, DATA_BYTES_COUNT_RESERVED, 0, byte_count);

    /* Gets the delay per bit */
    int delay_per_bit = (int)(1000000.0 / (bitrate * 1));

    /*** Starts transmitting below ***/

    /* Starts the bitrate initialization */
    int i;
    int j;
    char bit;

    for (i = 0; i < BITRATE_BITS_RESERVED; i++) {
        if (i % 2 == 0) {
            _set_gpio(1, delay_per_bit);
        } else {
            _set_gpio(0, delay_per_bit);
        }
    }

    /* Send the settings as increased bits */
    for (i = 0; i < SETTINGS_BYTES_RESERVED; i++) {
        for (j = 7; j >= 0; j--) {
            bit = (_settings[i] >> j) & 1;
            _set_gpio_with_increased_bit(bit, delay_per_bit);
        }
    }

    /* Send the number as increased bits of bytes that will be sent in the next step */
    for (i = 0; i < DATA_BYTES_COUNT_RESERVED; i++) {
        for (j = 7; j >= 0; j--) {
            bit = (byte_count >> j) & 1;
            _set_gpio_with_increased_bit(bit, delay_per_bit);
        }
    }

    /* Send the data as increased bits */
    for (i = 0; i < byte_count; i++) {
        for (j = 7; j >= 0; j--) {
            bit = (send_data[i] >> j) & 1;
            _set_gpio_with_increased_bit(bit, delay_per_bit);
        }
    }

    /* Sets the gpio to low after transmit */
    platform_gpio_set_low();
}

/******************** Modifying data array ********************/

void data_set_bit(unsigned char *data, const int length, const int bit_position, const int bit) {
    int bit_pos = bit_position;
    int byte_pos = 0;

    /* "Klättra" ner till rätt position för bit respektive byte */
{'fraction_non_alphanumeric': 0.14497100579884023, 'fraction_numerical': 0.011197760447910418, 'mean_word_length': 2.5652173913043477, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, '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': '6051084', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1662, 'n_words': 455}