Skip to content

Commit

Permalink
Add low voltage warnings (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuupola committed May 28, 2020
1 parent 3b53b98 commit 0ebcde2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## [0.5.0](https://github.com/tuupola/bm8563/compare/0.4.0...master) - unreleased

### Added
- Support for low voltage warnings ([#3](https://github.com/tuupola/bm8563/pull/3)).

## 0.4.0 - 2020-20-05

Initial release.
7 changes: 5 additions & 2 deletions bm8563.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ bm8563_err_t bm8563_read(const bm8563_t *bm, struct tm *time)
return status;
}

/* TODO: low voltage warning */

/* 0..59 */
bcd = buffer[0] & 0b01111111;
time->tm_sec = bcd2decimal(bcd);
Expand Down Expand Up @@ -111,6 +109,11 @@ bm8563_err_t bm8563_read(const bm8563_t *bm, struct tm *time)
/* Calculate tm_yday. */
mktime(time);

/* low voltage warning */
if (buffer[0] & 0b10000000) {
return BM8563_ERR_LOW_VOLTAGE;
}

return BM8563_OK;
}

Expand Down
3 changes: 2 additions & 1 deletion bm8563.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ extern "C" {
#define BM8563_TIME_SIZE (0x07)
#define BM8563_CENTURY_BIT (0b10000000)

#define BM8563_OK (0x00)
#define BM8563_OK (0x00)
#define BM8563_ERR_LOW_VOLTAGE (0x80)

/* These should be provided by the HAL. */
typedef struct {
Expand Down
6 changes: 6 additions & 0 deletions tests/mock_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ int32_t mock_i2c_write(void *handle, uint8_t address, uint8_t reg, const uint8_t
return BM8563_OK;
}

int32_t mock_i2c_low_voltage_read(void *handle, uint8_t address, uint8_t reg, uint8_t *buffer, uint16_t size) {
memcpy(buffer, memory, size);
buffer[0] |= 0b10000000;
return BM8563_OK;
}

int32_t mock_failing_i2c_read(void *handle, uint8_t address, uint8_t reg, uint8_t *buffer, uint16_t size) {
return 3;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/mock_i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ SPDX-License-Identifier: MIT
int32_t mock_i2c_read(void *handle, uint8_t address, uint8_t reg, uint8_t *buffer, uint16_t size);
int32_t mock_i2c_write(void *handle, uint8_t address, uint8_t reg, const uint8_t *buffer, uint16_t size);

int32_t mock_i2c_low_voltage_read(void *handle, uint8_t address, uint8_t reg, uint8_t *buffer, uint16_t size);

int32_t mock_failing_i2c_read(void *handle, uint8_t address, uint8_t reg, uint8_t *buffer, uint16_t size);
int32_t mock_failing_i2c_write(void *handle, uint8_t address, uint8_t reg, const uint8_t *buffer, uint16_t size);
16 changes: 14 additions & 2 deletions tests/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,20 @@ TEST should_fail_read_time(void) {
PASS();
}

TEST should_get_low_voltage_warning(void) {
struct tm datetime = {0};
bm8563_t bm;
bm.read = &mock_i2c_low_voltage_read;
bm.write = &mock_i2c_write;

ASSERT(BM8563_OK == bm8563_init(&bm));
ASSERT(BM8563_ERR_LOW_VOLTAGE == bm8563_read(&bm, &datetime));
PASS();
}

TEST should_read_and_write_time(void) {
struct tm datetime = {0};;
struct tm datetime2 = {0};;
struct tm datetime = {0};
struct tm datetime2 = {0};
char buffer[128];
bm8563_t bm;
bm.read = &mock_i2c_read;
Expand Down Expand Up @@ -129,6 +140,7 @@ int main(int argc, char **argv) {
RUN_TEST(should_fail_init);
RUN_TEST(should_init);
RUN_TEST(should_fail_read_time);
RUN_TEST(should_get_low_voltage_warning);
RUN_TEST(should_read_and_write_time);
RUN_TEST(should_handle_century);

Expand Down

0 comments on commit 0ebcde2

Please sign in to comment.