This commit is contained in:
Rens Pastoor
2025-06-12 11:20:08 +02:00
parent 37013ec1fc
commit 1086760c4a
21 changed files with 444 additions and 256 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -4,103 +4,55 @@
#include "resource_detector.h"
void watch_registers_toggle_config_is_paused(uint8_t* config){
if (config == NULL) return;
*config ^= 0x08; // Toggle the 4th bit 0x08 = 0b00001000
}
void watch_registers_set_config_time_format(uint8_t* config, time_format format){
uint8_t tf;
if (config == NULL) return;
if (format == TIME_HOUR_MINUTE) {
tf = 0x00;
} else if (format == TIME_HOUR_MINUTE_SECOND) {
tf = 0x01;
}
*config = (*config & 0x0E) | tf;
*config = (*config & 0x0E) | format;
}
void watch_registers_set_config_time_update_interval(uint8_t* config, time_update_interval interval){
uint8_t mask;
if (config == NULL) return;
if (interval == TIME_UPDATE_DISABLED){
mask = 0x00;
} else if (interval == TIME_EVERY_1_SECOND){
mask = 0x01;
} else if (interval == TIME_EVERY_30_SECONDS){
mask = 0x02;
} else if (interval == TIME_EVERY_MINUTE){
mask = 0x03;
}
*config = (*config & 0xF9) | (mask << 1); // Clear bits 2 and 3 and set them to the new value: 0x06 = 0b00000110
*config = (*config & 0xF9) | (interval << 1); // Clear bits 2 and 3 and set them to the new value: 0x06 = 0b00000110
}
void watch_registers_get_config_settings(uint8_t config, bool* is_paused, time_format* format, time_update_interval* interval){
if (format == NULL || is_paused == NULL || interval == NULL) return; //null check
if ((config & 0x08) != 0) {
*is_paused = true; // Check if the 4th bit is set
} else if ((config & 0x08) == 0) {
*is_paused = false;
}
*format = (time_format)(config & 0x01);
*interval = (time_update_interval)((config >> 1) & 0x03);
*is_paused = (config & 0x08);
*format = (config & 0x01);
*interval = ((config >> 1) & 0x03);
}
void watch_registers_set_time_hours(uint8_t* time_bits_low, uint8_t* time_bits_high, uint8_t hours){
if (time_bits_low == NULL || time_bits_high == NULL) return; //null check
if (hours > 11) {
hours = 11;
}else if (hours < 0) {
hours = 0;
}
if (hours > 11) hours = 11;
*time_bits_high = (*time_bits_high & 0x0F) | ((hours & 0x0F) << 4); // Set the upper nibble
}
void watch_registers_set_time_minutes(uint8_t* time_bits_low, uint8_t* time_bits_high, uint8_t minutes){
if (time_bits_low == NULL || time_bits_high == NULL) return; //null check
if (minutes > 59) {
minutes = 59;
} // if minutes < 0, isnt needed becouse uint8_t cannot be negative
if (minutes > 59) minutes = 59;
*time_bits_high = (*time_bits_high & 0xF0) | ((minutes >> 2) & 0x0F); // Set the lower nibble of MSB
*time_bits_low = (*time_bits_low & 0x3F) | ((minutes & 0x03) << 6); // Set the upper two bits of LSB
}
void watch_registers_set_time_seconds(uint8_t* time_bits_low, uint8_t* time_bits_high, uint8_t seconds){
if (time_bits_low == NULL || time_bits_high == NULL) return; //null check
if (seconds > 59) {
seconds = 59;
} // if seconds < 0, isnt needed becouse uint8_t cannot be negative
if (seconds > 59) seconds = 59;
*time_bits_low = (*time_bits_low & 0xC0) | (seconds & 0x3F); // Set the lower 6 bits of LSB
}
void watch_registers_get_time(uint8_t time_bits_low, uint8_t time_bits_high, uint8_t* hours, uint8_t* minutes, uint8_t* seconds){
if (hours == NULL || minutes == NULL || seconds == NULL) return; //null check
*hours = (time_bits_high >> 4) & 0x0F;
*minutes = ((time_bits_high & 0x0F) << 2) | ((time_bits_low >> 6) & 0x03);
*seconds = time_bits_low & 0x3F;
}
void watch_registers_set_date_year(uint8_t* date_bits_low, uint8_t* date_bits_high, uint8_t year){
if (date_bits_low == NULL || date_bits_high == NULL) return; //null check
if (year > 127) {
year = 127; // Maximum year value is 127 (0x7F) for 7 bits
} // if year < 0, isnt needed becouse uint8_t cannot be negative
if (year > 127) year = 127; // Maximum year value is 127 (0x7F) for 7 bits
*date_bits_low = (*date_bits_low & 0x80) | (year & 0x7F); // Set the lower 2 bits of LSB
}
void watch_registers_set_date_month(uint8_t* date_bits_low, uint8_t* date_bits_high, uint8_t month){
if (date_bits_low == NULL || date_bits_high == NULL) return; //null check
if (month > 12) {
month = 12; // Maximum month value is 12
} else if (month < 1) {
month = 1; // Minimum month value is 1
}
if (month > 12) month = 12; // Maximum month value is 12
else if (month < 1) month = 1; // Minimum month value is 1
*date_bits_high = (*date_bits_high & 0xF8) | ((month >> 1) & 0x07);
*date_bits_low = (*date_bits_low & 0x7F) | ((month & 0x01) << 7);
}
void watch_registers_set_date_day_of_month(uint8_t* date_bits_low, uint8_t* date_bits_high,uint8_t day_of_month){
if (date_bits_low == NULL || date_bits_high == NULL) return; //null check
if (day_of_month > 31) {
day_of_month = 31; // Maximum day of month value is 31
} else if (day_of_month < 1) {
day_of_month = 1; // Minimum day of month value is 1
}
if (day_of_month > 31) day_of_month = 31; // Maximum day of month value is 31
else if (day_of_month < 1) day_of_month = 1; // Minimum day of month value is 1
*date_bits_high = (*date_bits_high & 0x07) | ((day_of_month & 0x1F) << 3); // Set the upper 5 bits of MSB
}
void watch_registers_get_date(uint8_t date_bits_low, uint8_t date_bits_high, uint8_t* year, uint8_t* month, uint8_t* day_of_month){
if (year == NULL || month == NULL || day_of_month == NULL) return; //null check
*year = date_bits_low & 0x7F; // Get the lower 7 bits of LSB
*month = ((date_bits_high & 0x07) << 1) | ((date_bits_low >> 7) & 0x01); // Get the upper 3 bits of MSB and the lower bit of LSB
*day_of_month = (date_bits_high >> 3) & 0x1F; // Get the upper 5 bits of MSB

View File

@@ -26,15 +26,15 @@ void test_setting_set_config_time_format(void){
TEST_ASSERT_EQUAL(0x01, config); // 0b0001
}
void test_setting_set_config_time_update_interval(void){
uint8_t config = 0x00;
uint8_t config = 0xAA;
watch_registers_set_config_time_update_interval(&config, TIME_UPDATE_DISABLED);
TEST_ASSERT_EQUAL(0x00, config); // 0b0000
TEST_ASSERT_EQUAL(0xA8, config); // 0b0000
watch_registers_set_config_time_update_interval(&config, TIME_EVERY_1_SECOND);
TEST_ASSERT_EQUAL(0x02, config); // 0b0010
TEST_ASSERT_EQUAL(0xAA, config); // 0b0010
watch_registers_set_config_time_update_interval(&config, TIME_EVERY_30_SECONDS);
TEST_ASSERT_EQUAL(0x04, config); // 0b0100
TEST_ASSERT_EQUAL(0xAC, config); // 0b0100
watch_registers_set_config_time_update_interval(&config, TIME_EVERY_MINUTE);
TEST_ASSERT_EQUAL(0x06, config); // 0b0110
TEST_ASSERT_EQUAL(0xAE, config); // 0b0110
}
void test_setting_get_config_settings(void){
uint8_t config = 0x00; // 0b00000000 so time is updated, time format hour:minute, time refresh not updated
@@ -157,29 +157,7 @@ void test_date_get_date(void){
TEST_ASSERT_EQUAL(month, m);
TEST_ASSERT_EQUAL(day_of_month, d);
}
//NULL
void test_all_null(void){
watch_registers_toggle_config_is_paused(NULL);
watch_registers_set_config_time_format(NULL, TIME_HOUR_MINUTE);
watch_registers_set_config_time_update_interval(NULL, TIME_UPDATE_DISABLED);
watch_registers_get_config_settings(0x00, NULL, NULL, NULL);
uint8_t time_bits_low = 0;
uint8_t time_bits_high = 0;
watch_registers_set_time_hours(NULL, &time_bits_high, 0x04);
watch_registers_set_time_minutes(&time_bits_low, NULL, 0x30);
watch_registers_set_time_seconds(NULL, &time_bits_high, 0x45);
watch_registers_get_time(time_bits_low, time_bits_high, NULL, NULL, NULL);
uint8_t date_bits_low = 0;
uint8_t date_bits_high = 0;
watch_registers_set_date_year(NULL, &date_bits_high, 0x20);
watch_registers_set_date_month(&date_bits_low, NULL, 0x05);
watch_registers_set_date_day_of_month(NULL, &date_bits_high, 0x15);
watch_registers_get_date(date_bits_low, date_bits_high, NULL, NULL, NULL);
TEST_ASSERT_TRUE(true); // If we reach this point, the test passed
}
//
//full tests
void test_full_watch_configuration(void) {
uint8_t config = 0x00;
@@ -225,9 +203,9 @@ void test_full_datetime_overflow_setup(void) {
// Verify date
uint8_t year, month, day;
watch_registers_get_date(date_bits_low, date_bits_high, &year, &month, &day);
TEST_ASSERT_EQUAL(0x7F, year);
TEST_ASSERT_EQUAL(0x0C, month);
TEST_ASSERT_EQUAL(0x1F, day);
TEST_ASSERT_EQUAL(0x7F, year); // 127
TEST_ASSERT_EQUAL(0x0C, month); // 12
TEST_ASSERT_EQUAL(0x1F, day); // 31
}
//
void run_watch_tests()
@@ -248,8 +226,6 @@ void run_watch_tests()
MY_RUN_TEST(test_date_set_day_of_month);
MY_RUN_TEST(test_date_get_date);
MY_RUN_TEST(test_all_null);
MY_RUN_TEST(test_full_watch_configuration);
MY_RUN_TEST(test_full_datetime_overflow_setup);