<commit_before>/* Driver for DS3231 high precision RTC module
 *
 * Part of esp-open-rtos
 * Copyright (C) 2015 <NAME> <<EMAIL>>
 * Copyright (C) 2016 <NAME> <<EMAIL>>
 * MIT Licensed as described in the file LICENSE
*/

#include "ds3231.h"
#include "espressif/esp_common.h"
#include "espressif/sdk_private.h"
#include "esp8266.h"

#include "i2c/i2c.h"

/* Convert normal decimal to binary coded decimal */
static inline uint8_t  decToBcd(uint8_t dec)
{
    return(((dec / 10) * 16) + (dec % 10));
}

/* Convert binary coded decimal to normal decimal */
static inline uint8_t  bcdToDec(uint8_t bcd)
{
    return(((bcd / 16) * 10) + (bcd % 16));
}

/* Send a number of bytes to the rtc over i2c
 * returns true to indicate success
 */
static inline int ds3231_send(uint8_t reg, uint8_t *data, uint8_t len)
{
    return i2c_slave_write(DS3231_ADDR, &reg, data, len);
}

/* Read a number of bytes from the rtc over i2c
 * returns true to indicate success
 */
static inline int ds3231_recv(uint8_t reg, uint8_t *data, uint8_t len)
{
    return i2c_slave_read(DS3231_ADDR, &reg, data, len);
}

int ds3231_setTime(struct tm *time)
{
    uint8_t data[7];

    /* time/date data */
    data[0] = decToBcd(time->tm_sec);
    data[1] = decToBcd(time->tm_min);
    data[2] = decToBcd(time->tm_hour);
    data[3] = decToBcd(time->tm_wday + 1);
    data[4] = decToBcd(time->tm_mday);
    data[5] = decToBcd(time->tm_mon + 1);
    data[6] = decToBcd(time->tm_year - 100);

    return ds3231_send(DS3231_ADDR_TIME, data, 7);
}

int ds3231_setAlarm(uint8_t alarms, struct tm *time1, uint8_t option1, struct tm *time2, uint8_t option2)
{
    int i = 0;
    uint8_t data[7];

    /* alarm 1 data */
    if (alarms != DS3231_ALARM_2)
    {
        data[i++] = (option1 >= DS3231_ALARM1_MATCH_SEC ? decToBcd(time1->tm_sec) : DS3231_ALARM_NOTSET);
        data[i++] = (option1 >= DS3231_ALARM1_MATCH_SECMIN ? decToBcd(time1->tm_min) : DS3231_ALARM_NOTSET);
        data[i++] = (option1 >= DS3231_ALARM1_MATCH_SECMINHOUR ? decToBcd(time1->tm_hour) : DS3231_ALARM_NOTSET);
        data[i++] = (option1 == DS3231_ALARM1_MATCH_SECMINHOURDAY ? (decToBcd(time1->tm_wday + 1) & DS3231_ALARM_WDAY) :
            (option1 == DS3231_ALARM1_MATCH_SECMINHOURDATE ? decToBcd(time1->tm_mday) : DS3231_ALARM_NOTSET));
    }

    /* alarm 2 data */
    if (alarms != DS3231_ALARM_1)
    {
        data[i++] = (option2 >= DS3231_ALARM2_MATCH_MIN ? decToBcd(time2->tm_min) : DS3231_ALARM_NOTSET);
        data[i++] = (option2 >= DS3231_ALARM2_MATCH_MINHOUR ? decToBcd(time2->tm_hour) : DS3231_ALARM_NOTSET);
        data[i++] = (option2 == DS3231_ALARM2_MATCH_MINHOURDAY ? (decToBcd(time2->tm_wday + 1) & DS3231_ALARM_WDAY) :
            (option2 == DS3231_ALARM2_MATCH_MINHOURDATE ? decToBcd(time2->tm_mday) : DS3231_ALARM_NOTSET));
    }<commit_msg>ds3231: minor code style fixes.<commit_after>/* Driver for DS3231 high precision RTC module
 *
 * Part of esp-open-rtos
 * Copyright (C) 2015 <NAME> <<EMAIL>>
 * Copyright (C) 2016 <NAME> <<EMAIL>>
 * MIT Licensed as described in the file LICENSE
*/

#include "ds3231.h"
#include "espressif/esp_common.h"
#include "espressif/sdk_private.h"
#include "esp8266.h"

#include "i2c/i2c.h"

/* Convert normal decimal to binary coded decimal */
static inline uint8_t  decToBcd(uint8_t dec)
{
    return (dec / 10) * 16 + dec % 10;
}

/* Convert binary coded decimal to normal decimal */
static inline uint8_t  bcdToDec(uint8_t bcd)
{
    return (bcd / 16) * 10 + bcd % 16;
}

/* Send a number of bytes to the rtc over i2c
 * returns true to indicate success
 */
static inline int ds3231_send(uint8_t reg, uint8_t *data, uint8_t len)
{
    return i2c_slave_write(DS3231_ADDR, &reg, data, len);
}

/* Read a number of bytes from the rtc over i2c
 * returns true to indicate success
 */
static inline int ds3231_recv(uint8_t reg, uint8_t *data, uint8_t len)
{
    return i2c_slave_read(DS3231_ADDR, &reg, data, len);
}

int ds3231_setTime(struct tm *time)
{
    uint8_t data[7];

    /* time/date data */
    data[0] = decToBcd(time->tm_sec);
    data[1] = decToBcd(time->tm_min);
    data[2] = decToBcd(time->tm_hour);
    /* The week data must be in the range 1 to 7, and to keep the start on the
     * same day as for tm_wday have it start at 1 on Sunday. */
    data[3] = decToBcd(time->tm_wday + 1);
    data[4] = decToBcd(time->tm_mday);
    data[5] = decToBcd(time->tm_mon + 1);
    data[6] = decToBcd(time->tm_year - 100);

    return ds3231_send(DS3231_ADDR_TIME, data, 7);
}

int ds3231_setAlarm(uint8_t alarms, struct tm *time1, uint8_t option1, struct tm *time2, uint8_t option2)
{
    int i = 0;
    uint8_t data[7];

    /* alarm 1 data */
    if (alarms != DS3231_ALARM_2)
    {
        data[i++] = (option1 >= DS3231_ALARM1_MATCH_SEC ? decToBcd(time1->tm_sec) : DS3231_ALARM_NOTSET);
        data[i++] = (option1 >= DS3231_ALARM1_MATCH_SECMIN ? decToBcd(time1->tm_min) : DS3231_ALARM_NOTSET);
        data[i++] = (option1 >= DS3231_ALARM1_MATCH_SECMINHOUR ? decToBcd(time1->tm_hour) : DS3231_ALARM_NOTSET);
        data[i++] = (option1 == DS3231_ALARM1_MATCH_SECMINHOURDAY ? (decToBcd(time1->tm_wday + 1) & DS3231_ALARM_WDAY) :
            (option1 == DS3231_ALARM1_MATCH_SECMINHOURDATE ? decToBcd(time1->tm_mday) : DS3231_ALARM_NOTSET));
    }

    /* alarm 2 data */
    if (alarms != DS3231_ALARM_1)
    {
        data[i++] = (option2 >= DS3231_ALARM2_MATCH_MIN ? decToBcd(time2->tm_min) : DS3231_ALARM_NOTSET);
        data[i++] = (option2 >= DS3231_ALARM2_MATCH_MINHOUR ? decToBcd(time2->tm_hour) : DS3231_ALARM_NOTSET);
        data[i++] = (option2 == DS3231_ALARM2_MATCH_MINHOURDAY ? (decToBcd(time2->tm_wday + 1) & DS3231_ALARM_WDAY) :
            (option2 == DS3231_ALARM2_MATCH_MINHOURDATE ? decToBcd(time2->tm_mday) : DS3231_ALARM_NOTSET));
    }
{'fraction_non_alphanumeric': 0.13246753246753246, 'fraction_numerical': 0.07670995670995671, 'mean_word_length': 3.801330008312552, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 57, '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': '6849778', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2361, 'n_words': 506}