C ordening

This commit is contained in:
Rens Pastoor
2025-05-27 23:26:28 +02:00
parent 39269a71a7
commit 517087ccc1
207 changed files with 0 additions and 4278 deletions

32
C/C3 Watch/product/main.c Normal file
View File

@@ -0,0 +1,32 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "watch.h"
#include "watch_device_simulator.h"
// leave resource_detector.h as last include!
#include "resource_detector.h"
int main(int argc, char* argv[])
{
uint8_t hours = 11, minutes = 55, seconds = 42;
watch_set_time_hours(hours);
watch_set_time_minutes(minutes);
watch_set_time_seconds(seconds);
while (true)
{
watch_get_time(&hours, &minutes, &seconds);
watch_device_simulator_print_time(hours, minutes, seconds);
uint8_t number_seconds_update = 10;
watch_device_simulator_increase_time(number_seconds_update);
sleep(number_seconds_update);
}
return 0;
}

303
C/C3 Watch/product/watch.c Normal file
View File

@@ -0,0 +1,303 @@
#include "watch.h"
#include "watch_i2c.h"
#include "watch_registers.h"
// leave resource_detector.h as last include!
#include "resource_detector.h"
/*----------------------------------------------------------------------------*/
/* C O N F I G */
/*----------------------------------------------------------------------------*/
int watch_config_reset()
{
uint8_t config = 0;
if (watch_i2c_write_byte(ADDRESS_CONFIG, config) != 0)
{
return -1;
}
return 0;
}
int watch_config_toggle_pause()
{
uint8_t config;
if (watch_i2c_read_byte(ADDRESS_CONFIG, &config) != 0)
{
return -1;
}
watch_registers_toggle_config_is_paused(&config);
if (watch_i2c_write_byte(ADDRESS_CONFIG, config) != 0)
{
return -1;
}
return 0;
}
int watch_config_set_time_format(time_format format)
{
uint8_t config;
if (watch_i2c_read_byte(ADDRESS_CONFIG, &config) != 0)
{
return -1;
}
watch_registers_set_config_time_format(&config, format);
if (watch_i2c_write_byte(ADDRESS_CONFIG, config) != 0)
{
return -1;
}
return 0;
}
int watch_config_set_time_update_interval(time_update_interval interval)
{
uint8_t config;
if (watch_i2c_read_byte(ADDRESS_CONFIG, &config) != 0)
{
return -1;
}
watch_registers_set_config_time_update_interval(&config, interval);
if (watch_i2c_write_byte(ADDRESS_CONFIG, config) != 0)
{
return -1;
}
return 0;
}
int watch_config_get_settings(
bool* is_paused, time_format* format,
time_update_interval* interval)
{
uint8_t config;
if (is_paused == NULL || format == NULL || interval == NULL)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_CONFIG, &config) != 0)
{
return -1;
}
watch_registers_get_config_settings(config, is_paused, format, interval);
return 0;
}
/*----------------------------------------------------------------------------*/
/* T I M E */
/*----------------------------------------------------------------------------*/
int watch_set_time_hours(uint8_t hours)
{
uint8_t time_bits_low, time_bits_high;
if (watch_i2c_read_byte(ADDRESS_TIME_LOW, &time_bits_low) != 0)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_TIME_HIGH, &time_bits_high) != 0)
{
return -1;
}
watch_registers_set_time_hours(&time_bits_low, &time_bits_high, hours);
if (watch_i2c_write_byte(ADDRESS_TIME_LOW, time_bits_low) != 0)
{
return -1;
}
if (watch_i2c_write_byte(ADDRESS_TIME_HIGH, time_bits_high) != 0)
{
return -1;
}
return 0;
}
int watch_set_time_minutes(uint8_t minutes)
{
uint8_t time_bits_low, time_bits_high;
if (watch_i2c_read_byte(ADDRESS_TIME_LOW, &time_bits_low) != 0)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_TIME_HIGH, &time_bits_high) != 0)
{
return -1;
}
watch_registers_set_time_minutes(&time_bits_low, &time_bits_high, minutes);
if (watch_i2c_write_byte(ADDRESS_TIME_LOW, time_bits_low) != 0)
{
return -1;
}
if (watch_i2c_write_byte(ADDRESS_TIME_HIGH, time_bits_high) != 0)
{
return -1;
}
return 0;
}
int watch_set_time_seconds(uint8_t seconds)
{
uint8_t time_bits_low, time_bits_high;
if (watch_i2c_read_byte(ADDRESS_TIME_LOW, &time_bits_low) != 0)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_TIME_HIGH, &time_bits_high) != 0)
{
return -1;
}
watch_registers_set_time_seconds(&time_bits_low, &time_bits_high, seconds);
if (watch_i2c_write_byte(ADDRESS_TIME_LOW, time_bits_low) != 0)
{
return -1;
}
if (watch_i2c_write_byte(ADDRESS_TIME_HIGH, time_bits_high) != 0)
{
return -1;
}
return 0;
}
int watch_get_time(uint8_t* hours, uint8_t* minutes, uint8_t* seconds)
{
uint8_t time_bits_low, time_bits_high;
if (hours == NULL || minutes == NULL || seconds == NULL)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_TIME_LOW, &time_bits_low) != 0)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_TIME_HIGH, &time_bits_high) != 0)
{
return -1;
}
watch_registers_get_time(
time_bits_low, time_bits_high, hours, minutes, seconds);
return 0;
}
/*----------------------------------------------------------------------------*/
/* D A T E */
/*----------------------------------------------------------------------------*/
int watch_set_date_year(uint16_t year)
{
uint8_t date_bits_low, date_bits_high;
if (watch_i2c_read_byte(ADDRESS_DATE_LOW, &date_bits_low) != 0)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_DATE_HIGH, &date_bits_high) != 0)
{
return -1;
}
watch_registers_set_date_year(&date_bits_low, &date_bits_low, year);
if (watch_i2c_write_byte(ADDRESS_DATE_LOW, date_bits_low) != 0)
{
return -1;
}
if (watch_i2c_write_byte(ADDRESS_DATE_HIGH, date_bits_high) != 0)
{
return -1;
}
return 0;
}
int watch_set_date_month(uint8_t month)
{
uint8_t date_bits_low, date_bits_high;
if (watch_i2c_read_byte(ADDRESS_DATE_LOW, &date_bits_low) != 0)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_DATE_HIGH, &date_bits_high) != 0)
{
return -1;
}
watch_registers_set_date_month(&date_bits_low, &date_bits_low, month);
if (watch_i2c_write_byte(ADDRESS_DATE_LOW, date_bits_low) != 0)
{
return -1;
}
if (watch_i2c_write_byte(ADDRESS_DATE_HIGH, date_bits_high) != 0)
{
return -1;
}
return 0;
}
int watch_set_date_day_of_month(uint8_t day_of_month)
{
uint8_t date_bits_low, date_bits_high;
if (watch_i2c_read_byte(ADDRESS_DATE_LOW, &date_bits_low) != 0)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_DATE_HIGH, &date_bits_high) != 0)
{
return -1;
}
watch_registers_set_date_day_of_month(
&date_bits_low, &date_bits_low, day_of_month);
if (watch_i2c_write_byte(ADDRESS_DATE_LOW, date_bits_low) != 0)
{
return -1;
}
if (watch_i2c_write_byte(ADDRESS_DATE_HIGH, date_bits_high) != 0)
{
return -1;
}
return 0;
}
int watch_get_date(uint8_t* year, uint8_t* month, uint8_t* day_of_month)
{
uint8_t date_bits_low, date_bits_high;
if (year == NULL || month == NULL || day_of_month == NULL)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_DATE_LOW, &date_bits_low) != 0)
{
return -1;
}
if (watch_i2c_read_byte(ADDRESS_DATE_HIGH, &date_bits_high) != 0)
{
return -1;
}
watch_registers_get_date(
date_bits_low, date_bits_low, year, month, day_of_month);
return 0;
}

View File

@@ -0,0 +1,31 @@
#ifndef WATCH_H
#define WATCH_H
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include "watch_registers.h"
int watch_config_reset();
int watch_config_toggle_pause();
int watch_config_set_time_format(time_format format);
int watch_config_set_time_update_interval(time_update_interval interval);
int watch_config_get_settings(
bool *is_paused,
time_format* format,
time_update_interval *interval);
int watch_set_time_hours(uint8_t hours);
int watch_set_time_minutes(uint8_t minutes);
int watch_set_time_seconds(uint8_t seconds);
int watch_get_time(uint8_t* hours, uint8_t* minutes, uint8_t* seconds);
int watch_set_date_year(uint16_t year);
int watch_set_date_month(uint8_t month);
int watch_set_date_day_of_month(uint8_t day_of_month);
int watch_get_date(uint8_t *year,
uint8_t* month,
uint8_t* day_of_month);
#endif

View File

@@ -0,0 +1,107 @@
#include <stdint.h>
#include <stdio.h>
#include "watch_device_simulator.h"
#include "watch_i2c.h"
#include "watch_registers.h"
// leave resource_detector.h as last include!
#include "resource_detector.h"
uint8_t register_values[5];
static int watch_device_simulator_address_to_array_index(
uint8_t address, uint8_t* register_value_index)
{
if (register_value_index == NULL)
{
return -1;
}
if ((address < ADDRESS_CONFIG) || (address > ADDRESS_DATE_HIGH))
{
return -1;
}
*register_value_index = address - ADDRESS_CONFIG;
return 0;
}
int watch_device_simulator_write_byte(uint8_t address, uint8_t value)
{
uint8_t index = 0;
if (watch_device_simulator_address_to_array_index(address, &index) != 0)
{
return -1;
}
register_values[index] = value;
return 0;
}
int watch_device_simulator_read_byte(uint8_t address, uint8_t* value)
{
uint8_t index = 0;
if (watch_device_simulator_address_to_array_index(address, &index) != 0)
{
return -1;
}
*value = register_values[index];
return 0;
}
static void watch_device_simulator_add_one_second(
uint8_t* hours, uint8_t* minutes, uint8_t* seconds)
{
*seconds += 1;
if (*seconds == 60)
{
*seconds = 0;
*minutes += 1;
if (*minutes == 60)
{
*minutes = 0;
*hours += 1;
if (*hours == 12)
{
*hours = 0;
}
}
}
}
void watch_device_simulator_print_time(
uint8_t hours, uint8_t minutes, uint8_t seconds)
{
printf("Time: %02d:%02d:%02d\n", hours, minutes, seconds);
}
int watch_device_simulator_increase_time(uint8_t number_of_seconds)
{
uint8_t time_bits_low, time_bits_high = 0;
watch_device_simulator_read_byte(ADDRESS_TIME_LOW, &time_bits_low);
watch_device_simulator_read_byte(ADDRESS_TIME_HIGH, &time_bits_high);
uint8_t seconds = 0, minutes = 0, hours = 0;
watch_registers_get_time(
time_bits_low, time_bits_high, &hours, &minutes, &seconds);
for (uint8_t i = 0; i < number_of_seconds; i++)
{
watch_device_simulator_add_one_second(&hours, &minutes, &seconds);
}
watch_registers_set_time_hours(&time_bits_low, &time_bits_high, hours);
watch_registers_set_time_minutes(&time_bits_low, &time_bits_high, minutes);
watch_registers_set_time_seconds(&time_bits_low, &time_bits_high, seconds);
watch_device_simulator_write_byte(ADDRESS_TIME_LOW, time_bits_low);
watch_device_simulator_write_byte(ADDRESS_TIME_HIGH, time_bits_high);
return 0;
}

View File

@@ -0,0 +1,13 @@
#ifndef WATCH_DEVICE_SIMULATOR_H
#define WATCH_DEVICE_SIMULATOR_H
#include <stdint.h>
int watch_device_simulator_write_byte(uint8_t address, uint8_t value);
int watch_device_simulator_read_byte(uint8_t address, uint8_t* value);
int watch_device_simulator_increase_time(uint8_t number_of_seconds);
void watch_device_simulator_print_time(
uint8_t hours, uint8_t minutes, uint8_t seconds);
#endif

View File

@@ -0,0 +1,15 @@
#include "watch_i2c.h"
#include "watch_device_simulator.h"
// leave resource_detector.h as last include!
#include "resource_detector.h"
int watch_i2c_write_byte(uint8_t address, uint8_t value)
{
return watch_device_simulator_write_byte(address, value);
}
int watch_i2c_read_byte(uint8_t address, uint8_t* value)
{
return watch_device_simulator_read_byte(address, value);
}

View File

@@ -0,0 +1,11 @@
#ifndef WATCH_I2C_H
#define WATCH_I2C_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
int watch_i2c_write_byte(uint8_t address, uint8_t value);
int watch_i2c_read_byte(uint8_t address, uint8_t* value);
#endif