<commit_before>/*
 * Copyright (C) 2016 Inria
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v3. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @{
 *
 * @file
 * @brief       Sample application using the low-level I2C peripheral driver
 * with SAMR21-XPRO xplained pro extension.
 *
 * @author      <NAME> <<EMAIL>>
 *
 * @}
 */

#include <stdio.h>
#include <stdlib.h>

#include "xtimer.h"
#include "periph_conf.h"
#include "periph/i2c.h"

/* I2C interface number */
#define I2C_INTERFACE 0

/* I2C temperature address on sensor */
#define TEMP_ADDR  (0x48 | 0x07)

/* set interval to 5 seconds */
#define INTERVAL (5000000U)

static int read_temperature(void)
{
    uint16_t temperature;
    char buffer[2] = { 0 };
    
    i2c_read_bytes(I2C_INTERFACE, TEMP_ADDR, buffer, 2);
    uint16_t data = (buffer[0] << 8) | buffer[1];
    int8_t sign = 1;
    /* Check if negative and clear sign bit. */
    if (data & (1 << 15)) {
	sign *= -1;
	data &= ~(1 << 15);
    }
    /* Convert to temperature */
    data = (data >> 5);
    temperature = data * sign * 0.125;
    
    /* TODO convert to float */
    return (int)temperature;
}

int main(void)
{
    puts("Read temperature sensor on Atmel Samr21 IoXplained extension\n");

    /* Initialise the I2C serial interface as master */
    i2c_init_master(I2C_INTERFACE, I2C_SPEED_NORMAL);
    
    uint32_t last_wakeup = xtimer_now();
    for (;;) {
        xtimer_usleep_until(&last_wakeup, INTERVAL);
	printf("Temperature: %i°C\n", read_temperature());
	last_wakeup = xtimer_now();
    }

    return 0;
}<commit_msg>polishing i2c_temperature example code<commit_after>/*
 * Copyright (C) 2016 Inria
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v3. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @{
 *
 * @file
 * @brief       Sample application using the low-level I2C peripheral driver
 * with SAMR21-XPRO xplained pro extension.
 *
 * @author      <NAME> <<EMAIL>>
 *
 * @}
 */

#include <stdio.h>
#include <stdlib.h>

#include "xtimer.h"
#include "periph_conf.h"
#include "periph/i2c.h"

#define I2C_INTERFACE (0)           /* I2C interface number */
#define SENSOR_ADDR   (0x48 | 0x07) /* I2C temperature address on sensor */
#define INTERVAL      (5000000U)    /* set interval to 5 seconds */

static int read_temperature(void)
{
    uint16_t temperature;
    char buffer[2] = { 0 };
    
    i2c_read_bytes(I2C_INTERFACE, SENSOR_ADDR, buffer, 2);
    uint16_t data = (buffer[0] << 8) | buffer[1];
    int8_t sign = 1;
    /* Check if negative and clear sign bit. */
    if (data & (1 << 15)) {
	sign *= -1;
	data &= ~(1 << 15);
    }
    /* Convert to temperature */
    data = (data >> 5);
    temperature = data * sign * 0.125;
    
    /* TODO convert to float */
    return (int)temperature;
}

int main(void)
{
    puts("Read Atmel SAMR21 Io-Xplained-Pro temperature sensor on I2C bus\n");

    /* Initialise the I2C serial interface as master */
    i2c_init_master(I2C_INTERFACE, I2C_SPEED_NORMAL);
    
    uint32_t last_wakeup = xtimer_now();
    for (;;) {
        xtimer_usleep_until(&last_wakeup, INTERVAL);
	printf("Temperature: %i°C\n", read_temperature());
	last_wakeup = xtimer_now();
    }

    return 0;
}
{'fraction_non_alphanumeric': 0.12951357803640703, 'fraction_numerical': 0.03760071620411817, 'mean_word_length': 2.980997624703088, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 17, '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': '5249222', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1187, 'n_words': 342}