Break offset calculation out into a separate function

This change will allow for a test program to verify that the offset
computation is valid.
pull/22/head
nirenjan 2020-06-09 14:43:59 -07:00
parent c9ffb415c8
commit 4d93df1d58
1 changed files with 44 additions and 52 deletions

View File

@ -209,27 +209,8 @@ static int _x52_write_date(libx52_device *x52, uint32_t bit)
return rc; return rc;
} }
static int _x52_write_time(libx52_device *x52, uint32_t bit) int _x52_calculate_clock_offset(libx52_device *x52, libx52_clock_id clock, uint16_t h24)
{ {
uint16_t value = 0;
uint16_t index;
libx52_clock_id clock;
switch (bit) {
case X52_BIT_MFD_TIME:
clock = LIBX52_CLOCK_1;
break;
case X52_BIT_MFD_OFFS1:
clock = LIBX52_CLOCK_2;
break;
case X52_BIT_MFD_OFFS2:
clock = LIBX52_CLOCK_3;
break;
}
uint16_t h24 = !!(x52->time_format[clock]);
if (clock != LIBX52_CLOCK_1) {
int offset; int offset;
int negative; int negative;
@ -264,26 +245,38 @@ static int _x52_write_time(libx52_device *x52, uint32_t bit)
offset = -offset; offset = -offset;
} }
value = h24 << 15 | return (h24 << 15 | negative << 10 | (offset & 0x3FF));
negative << 10 | }
(offset & 0x3FF);
static int _x52_write_time(libx52_device *x52, uint32_t bit)
{
uint16_t value = 0;
uint16_t index;
libx52_clock_id clock;
switch (bit) {
case X52_BIT_MFD_TIME:
clock = LIBX52_CLOCK_1;
index = X52_TIME_CLOCK1;
break;
case X52_BIT_MFD_OFFS1:
clock = LIBX52_CLOCK_2;
index = X52_OFFS_CLOCK2;
break;
case X52_BIT_MFD_OFFS2:
clock = LIBX52_CLOCK_3;
index = X52_OFFS_CLOCK3;
break;
} }
switch (clock) { uint16_t h24 = !!(x52->time_format[clock]);
case LIBX52_CLOCK_1:
index = X52_TIME_CLOCK1; if (clock != LIBX52_CLOCK_1) {
value = _x52_calculate_clock_offset(x52, clock, h24);
} else {
value = h24 << 15 | value = h24 << 15 |
(x52->time_hour & 0x7F) << 8 | (x52->time_hour & 0x7F) << 8 |
(x52->time_minute & 0xFF); (x52->time_minute & 0xFF);
break;
case LIBX52_CLOCK_2:
index = X52_OFFS_CLOCK2;
break;
case LIBX52_CLOCK_3:
index = X52_OFFS_CLOCK3;
break;
} }
return libx52_vendor_command(x52, index, value); return libx52_vendor_command(x52, index, value);
@ -329,7 +322,6 @@ int libx52_update(libx52_device *x52)
{ {
unsigned int i; unsigned int i;
uint32_t update_mask; uint32_t update_mask;
uint16_t value;
int rc = LIBX52_SUCCESS; int rc = LIBX52_SUCCESS;
x52_handler handler; x52_handler handler;