浏览代码

ds3231: Use struct tm instead of self-defined struct

Helmut Pozimski 2 年之前
父节点
当前提交
6c3e5c05db
共有 3 个文件被更改,包括 22 次插入37 次删除
  1. 0 15
      main/datetime.h
  2. 20 19
      main/ds3231.c
  3. 2 3
      main/ds3231.h

+ 0 - 15
main/datetime.h

@@ -1,15 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
- *
- * SPDX-License-Identifier: GPL-2.0-only
- */
-
-typedef struct {
-	uint8_t year;
-	uint8_t month;
-	uint8_t day;
-	uint8_t dow;
-	uint8_t hours;
-	uint8_t minutes;
-	uint8_t seconds;
-} date_time;

+ 20 - 19
main/ds3231.c

@@ -4,14 +4,14 @@
  * SPDX-License-Identifier: GPL-2.0-only
  */
 
-#include "driver/i2c.h"
+#include <driver/i2c.h>
+#include <sys/time.h>
 
-#include "esp_err.h"
+#include <esp_err.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "datetime.h"
 #include "bcd.h"
 
 #define DS3231_ADDRESS 0x68
@@ -115,7 +115,7 @@ static esp_err_t read_month_value(uint8_t* month_value) {
 	return ret;
 }
 
-esp_err_t ds3231_read_date_time(date_time* date_time) {
+esp_err_t ds3231_read_date_time(struct tm* date_time) {
 	esp_err_t ret;
 	uint8_t seconds, minutes, hours, dow, day, month, year;
 	ret = read_value(SECONDS_REGISTER_ADDRESS, &seconds);
@@ -126,27 +126,28 @@ esp_err_t ds3231_read_date_time(date_time* date_time) {
 	ret &= read_month_value(&month);
 	ret &= read_value(YEAR_REGISTER_ADDRESS, &year);
 	
-	date_time->seconds = bcd2bin(seconds);
-	date_time->minutes = bcd2bin(minutes);
-	date_time->hours = hours;
-	date_time->dow = bcd2bin(dow);
-	date_time->day = bcd2bin(day);
-	date_time->month = month;
-	date_time->year = bcd2bin(year);
+	date_time->tm_sec = bcd2bin(seconds);
+	date_time->tm_min = bcd2bin(minutes);
+	date_time->tm_hour = hours;
+	date_time->tm_wday = bcd2bin(dow);
+	date_time->tm_mday = bcd2bin(day);
+	date_time->tm_mon = month;
+	date_time->tm_year = bcd2bin(year);
 	
 	return ret;
 
 }
 
-esp_err_t ds3231_write_date_time(date_time date_time) {
+esp_err_t ds3231_write_date_time(struct tm date_time) {
 	esp_err_t ret;
-	ret = write_value(SECONDS_REGISTER_ADDRESS, bin2bcd(date_time.seconds));
-	ret &= write_value(MINUTES_REGISTER_ADDRESS, bin2bcd(date_time.minutes));
-	ret &= write_value(HOURS_REGISTER_ADDRESS, bin2bcd(date_time.hours));
-	ret &= write_value(DOW_REGISTER_ADDRESS, bin2bcd(date_time.dow));
-	ret &= write_value(DAY_REGISTER_ADDRESS, bin2bcd(date_time.day));
-	ret &= write_value(MONTH_REGISTER_ADDRESS, bin2bcd(date_time.month) | 0x80);
-	ret &= write_value(YEAR_REGISTER_ADDRESS, bin2bcd(date_time.year));
+	ret = write_value(SECONDS_REGISTER_ADDRESS, bin2bcd(date_time.tm_sec));
+	ret &= write_value(MINUTES_REGISTER_ADDRESS, bin2bcd(date_time.tm_min));
+	ret &= write_value(HOURS_REGISTER_ADDRESS, bin2bcd(date_time.tm_hour));
+	ret &= write_value(DOW_REGISTER_ADDRESS, bin2bcd(date_time.tm_wday));
+	ret &= write_value(DAY_REGISTER_ADDRESS, bin2bcd(date_time.tm_mday));
+	ret &= write_value(MONTH_REGISTER_ADDRESS, bin2bcd(date_time.tm_mon) | 0x80);
+	ret &= write_value(YEAR_REGISTER_ADDRESS, bin2bcd(date_time.tm_year));
+	ret &= write_value(YEAR_REGISTER_ADDRESS, bin2bcd(date_time.tm_year));
 	
 	return ret;
 

+ 2 - 3
main/ds3231.h

@@ -1,6 +1,5 @@
 #include "esp_err.h"
-#include "datetime.h"
 
 esp_err_t ds3231_init(int sda_pin, int scl_pin);
-esp_err_t ds3231_read_date_time(date_time* date_time);
-esp_err_t ds3231_write_date_time(date_time date_time);
+esp_err_t ds3231_read_date_time(struct tm* date_time);
+esp_err_t ds3231_write_date_time(struct tm date_time);