<commit_before>package io.treehopper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import io.treehopper.interfaces.I2c;

/**
 * Created by jay on 12/4/2016.
 */

public class HardwareI2c implements I2c {

    static final Logger logger = LogManager.getLogger("HardwareI2c");

    boolean enabled;
    double speed = 100.0;
    TreehopperUsb board;

    HardwareI2c(TreehopperUsb board)
    {
        this.board = board;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
        SendConfig();
    }

    @Override
    public double getSpeed() {
        return speed;
    }

    @Override
    public void setSpeed(double speed) {
        this.speed = speed;
        SendConfig();
    }

    private void SendConfig()
    {
        double TH0 = 256.0 - 4000.0 / (3.0 * speed);
        if(TH0 < 0 || TH0 > 255)
        {
            if(TreehopperUsb.Settings.shouldThrowExceptions())
            {
                throw new IllegalArgumentException("Speed out of limits. Valid speeds are 62.5 kHz to 16000 kHz");
            }

            logger.error("Speed out of limits. Valid rate is 62.5 kHz - 16000 kHz (16 MHz)");
        }
        byte[] dataToSend = new byte[3];
        dataToSend[0] = (byte) DeviceCommands.I2cConfig.ordinal();
        dataToSend[1] = (byte)(enabled ? 0x01 : 0x00);
        dataToSend[2] = (byte)Math.round(TH0);
        board.sendPeripheralConfigPacket(dataToSend);
    }

    @Override
    public byte[] sendReceive(byte address, byte[] dataToWrite, int numBytesToRead)
    {
        if(!enabled)
        {
            String message = "I2c.SendReceive() called before enabling the peripheral. This call will be ignored.";
            logger.error(message);
            if(TreehopperUsb.Settings.shouldThrowExceptions())<commit_msg>Don't resend I2c speed if it hasn't changed<commit_after>package io.treehopper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import io.treehopper.interfaces.I2c;

/**
 * Created by jay on 12/4/2016.
 */

public class HardwareI2c implements I2c {

    static final Logger logger = LogManager.getLogger("HardwareI2c");

    boolean enabled;
    double speed = 100.0;
    TreehopperUsb board;

    HardwareI2c(TreehopperUsb board)
    {
        this.board = board;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public void setEnabled(boolean enabled) {
        if(this.enabled == enabled) return;

        this.enabled = enabled;
        SendConfig();
    }

    @Override
    public double getSpeed() {
        return speed;
    }

    @Override
    public void setSpeed(double speed) {
        if(Utilities.CloseTo(speed, this.speed)) return; // don't update if we're already there

        this.speed = speed;
        SendConfig();
    }

    private void SendConfig()
    {
        double TH0 = 256.0 - 4000.0 / (3.0 * speed);
        if(TH0 < 0 || TH0 > 255)
        {
            if(TreehopperUsb.Settings.shouldThrowExceptions())
            {
                throw new IllegalArgumentException("Speed out of limits. Valid speeds are 62.5 kHz to 16000 kHz");
            }

            logger.error("Speed out of limits. Valid rate is 62.5 kHz - 16000 kHz (16 MHz)");
        }
        byte[] dataToSend = new byte[3];
        dataToSend[0] = (byte) DeviceCommands.I2cConfig.ordinal();
        dataToSend[1] = (byte)(enabled ? 0x01 : 0x00);
        dataToSend[2] = (byte)Math.round(TH0);
        board.sendPeripheralConfigPacket(dataToSend);
    }

    @Override
    public byte[] sendReceive(byte address, byte[] dataToWrite, int numBytesToRead)
    {
        if(!enabled)
        {
            String message = "I2c.SendReceive() called before enabling the peripheral. This call will be ignored.";
            logger.error(message);
            if(TreehopperUsb.Settings.shouldThrowExceptions())
{'fraction_non_alphanumeric': 0.10531544957774466, 'fraction_numerical': 0.03353204172876304, 'mean_word_length': 2.298116298116298, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3749877', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1201, 'n_words': 302}