From d215e204d4563b37ba33ceb437505751708119cc Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Tue, 30 Apr 2024 11:37:21 +0200 Subject: [PATCH] Rename files & re-factor to pretty up project --- prosjekt.X/command-handler.c | 7 +++- prosjekt.X/command-handler.h | 4 +-- prosjekt.X/{fan_speed.c => fan-speed.c} | 2 +- prosjekt.X/{fan_speed.h => fan-speed.h} | 4 +-- prosjekt.X/i2c.h | 1 - prosjekt.X/main.c | 44 +++++++++++++++++-------- prosjekt.X/nbproject/configurations.xml | 4 +-- prosjekt.X/themistor-temp.h | 7 ++-- prosjekt.X/thermistor-temp.c | 8 ++--- 9 files changed, 51 insertions(+), 30 deletions(-) rename prosjekt.X/{fan_speed.c => fan-speed.c} (95%) rename prosjekt.X/{fan_speed.h => fan-speed.h} (90%) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index eb97922..56df078 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -1,5 +1,5 @@ #include "command-handler.h" -#include "fan_speed.h" +#include "fan-speed.h" // Initialize empty, global command context volatile command_context_t context = {UNKNOWN_COMMAND, SRC_NONE, FAN_NONE, @@ -169,6 +169,10 @@ uint8_t route_command(int pos) { // Set the flag to store it in the EEPROM store_config = true; + return 0; + break; + case CNF_NONE: + return 0; break; } break; @@ -273,6 +277,7 @@ uint8_t route_command(int pos) { } else if (context.fan == FAN2) { memset(fan2_history, 0, sizeof(fan2_history)); } + return 0; break; case UNKNOWN_COMMAND: default: diff --git a/prosjekt.X/command-handler.h b/prosjekt.X/command-handler.h index 23ca9f8..8fbdd74 100644 --- a/prosjekt.X/command-handler.h +++ b/prosjekt.X/command-handler.h @@ -67,8 +67,8 @@ typedef struct { extern volatile uint16_t fan1_history[512]; extern volatile uint16_t fan2_history[512]; // Fan history index variable -volatile uint16_t fan1_history_index = 0; -volatile uint16_t fan2_history_index = 0; +extern volatile uint16_t fan1_history_index; +extern volatile uint16_t fan2_history_index; // Config extern volatile config_t config; diff --git a/prosjekt.X/fan_speed.c b/prosjekt.X/fan-speed.c similarity index 95% rename from prosjekt.X/fan_speed.c rename to prosjekt.X/fan-speed.c index 7b84c59..1cc0ed9 100644 --- a/prosjekt.X/fan_speed.c +++ b/prosjekt.X/fan-speed.c @@ -1,4 +1,4 @@ -#include "fan_speed.h" +#include "fan-speed.h" #include "uart.h" uint16_t timer_period_ms = 1; diff --git a/prosjekt.X/fan_speed.h b/prosjekt.X/fan-speed.h similarity index 90% rename from prosjekt.X/fan_speed.h rename to prosjekt.X/fan-speed.h index dfe47a8..e56e8a2 100644 --- a/prosjekt.X/fan_speed.h +++ b/prosjekt.X/fan-speed.h @@ -33,8 +33,8 @@ extern "C" { extern volatile uint16_t fan1_history[512]; extern volatile uint16_t fan2_history[512]; // Fan history index variable -volatile uint16_t fan1_history_index = 0; -volatile uint16_t fan2_history_index = 0; +extern volatile uint16_t fan1_history_index; +extern volatile uint16_t fan2_history_index; // INITALICE TIMER COUNTER void init_TCA0(); diff --git a/prosjekt.X/i2c.h b/prosjekt.X/i2c.h index 1272b11..859df15 100644 --- a/prosjekt.X/i2c.h +++ b/prosjekt.X/i2c.h @@ -14,7 +14,6 @@ extern "C" { // Include the IO for I2C #include "command-handler.h" -#include "uart.h" #include #include #include diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 25da254..75337b2 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -4,24 +4,32 @@ * * Created on March 6, 2024, 12:34 PM */ -#include "uart.h" -#include "voltage.h" -#include -#define RTC_PERIOD (511) -#define DELAY_TIME 1000 -#include "eeprom.h" +#define F_CPU 4E6 + +// Include AVR specific libs #include +#include +#include + +// Include standard C libraries +#include #include #include #include -#define F_CPU 4E6 +#include + +// Include custom source files +#include "eeprom.h" +#include "voltage.h" #include "command-handler.h" #include "i2c.h" #include "themistor-temp.h" +#include "fan-speed.h" + +// Only enable UART when required for debugging +#ifdef ENABLE_UART #include "uart.h" -#include -#include -#include +#endif // Fan history variables volatile uint16_t fan1_history[512] = {0}; @@ -36,17 +44,25 @@ volatile bool store_config = false; int main() { // Initialize functionality - init_uart((uint16_t)9600); ADC0_init(); - init_led(); + init_alarm_gpio(); init_i2c(); + + // Fanspeed + init_AC0(); + init_AC1(); + init_TCA0(); + + // Only enable UART when required for debugging +#ifdef ENABLE_UART + init_uart((uint16_t)9600); stdout = &USART_stream; +#endif // Read the stored config struct config = read_struct_from_EEPROM(); - PORTB.DIRSET = PIN3_bm; - + // Enable interrupts sei(); while (1) { diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 7d01886..aaaca26 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -10,7 +10,7 @@ command-handler.h eeprom.h i2c.h - fan_speed.h + fan-speed.h command-handler.c i2c.c eeprom.c - fan_speed.c + fan-speed.c Makefile diff --git a/prosjekt.X/themistor-temp.h b/prosjekt.X/themistor-temp.h index 4d85568..fe671b9 100644 --- a/prosjekt.X/themistor-temp.h +++ b/prosjekt.X/themistor-temp.h @@ -23,7 +23,8 @@ extern "C" { #define T_0 298.15 #define B 3950 #define R_1 1000 -#define ledpin PIN3_bm + +#define ALERT_PIN PIN3_bm // Takes inn messured value // Calculates the temperature in celcius @@ -36,8 +37,8 @@ bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp); void alert_voltage_threshold_exceeded(bool voltage_threshold_bool); -// Initialise led -void init_led(); +// Initialise alarm GPIO +void init_alarm_gpio(); #ifdef __cplusplus } diff --git a/prosjekt.X/thermistor-temp.c b/prosjekt.X/thermistor-temp.c index 71933a5..1b3bd2f 100644 --- a/prosjekt.X/thermistor-temp.c +++ b/prosjekt.X/thermistor-temp.c @@ -1,8 +1,8 @@ #include "themistor-temp.h" -void init_led(){ - PORTB.DIRSET = ledpin; +void init_alarm_gpio(){ + PORTB.DIRSET = ALERT_PIN; } float calculate_thermistor_temp(float thermistor_voltage){ @@ -42,8 +42,8 @@ bool voltage_threshold_bool(float thermistor_temp, uint8_t max_temp){ void alert_voltage_threshold_exceeded(bool voltage_threshold_bool){ if (voltage_threshold_bool){ printf("Error: maximum temperature exceeded"); - PORTB.OUTSET = ledpin; + PORTB.OUTSET = ALERT_PIN; } else{ - PORTB.OUTCLR = ledpin; + PORTB.OUTCLR = ALERT_PIN; } } \ No newline at end of file