Compare commits

...

4 Commits

Author SHA1 Message Date
Sebastian H. Gabrielli
b858d29ce8 Make read bulk fan speed read correctly from the array 2024-04-30 11:21:26 +02:00
Sebastian H. Gabrielli
1671476f41 Move index incrementation over storage
This avoids the problem where the current index has outdated data.
It does however mean the first piece of stored data is empty, but it is
a worthwile tradeoff.
2024-04-30 11:11:32 +02:00
Sebastian H. Gabrielli
bcd631001b Current fan speed now returns the last fan speed measured 2024-04-30 11:10:26 +02:00
Sebastian H. Gabrielli
dd53b4dbf2 Fan data is now saved in the array
Closes #9
2024-04-30 11:08:03 +02:00
4 changed files with 47 additions and 6 deletions

View File

@ -1,4 +1,5 @@
#include "command-handler.h"
#include "fan_speeeed.h"
// Initialize empty, global command context
volatile command_context_t context = {UNKNOWN_COMMAND, SRC_NONE, FAN_NONE,
@ -235,18 +236,32 @@ uint8_t route_command(int pos) {
break;
case READ_FAN_SPEED:
if (context.fan == FAN1) {
return fan1_history[0];
return fan1_history[fan1_history_index];
} else if (context.fan == FAN2) {
return fan2_history[0];
return fan2_history[fan2_history_index];
} else {
return 0;
}
break;
case READ_BULK_FAN_SPEED:
if (context.fan == FAN1) {
return fan1_history[pos];
// Validate that pos is valid, if not return 0
if (pos >= 512) { return 0; }
// Calculate the index position
int16_t index = fan1_history_index - pos;
if (index < 0) { index = 511-pos; }
return fan1_history[index];
} else if (context.fan == FAN2) {
return fan2_history[pos];
// Validate that pos is valid, if not return 0
if (pos >= 512) { return 0; }
// Calculate the index position
int16_t index = fan2_history_index - pos;
if (index < 0) { index = 511-pos; }
return fan2_history[index];
} else {
return 0;
}

View File

@ -66,6 +66,9 @@ typedef struct {
// Fan history variables
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;
// Config
extern volatile config_t config;

View File

@ -98,8 +98,25 @@ ISR(AC1_AC_vect){ // AC1 vec flag
// TIMER INTERUPT
ISR (TCA0_OVF_vect) {
cli();
RPM_calculation(fan1_edge_counter,timer_period_ms);
RPM_calculation(fan2_edge_counter,timer_period_ms);
// Increment the index, or reset if it is at the top
if (fan1_history_index < 512) {
fan1_history_index++;
} else {
fan1_history_index = 0;
}
if (fan2_history_index < 512) {
fan2_history_index++;
} else {
fan2_history_index = 0;
}
// Calculate the fanspeed
fan1_history[fan1_history_index] = RPM_calculation(fan1_edge_counter,timer_period_ms);
fan2_history[fan2_history_index] = RPM_calculation(fan2_edge_counter,timer_period_ms);
// Reset the edge counter
fan1_edge_counter = 0;
fan2_edge_counter = 0;
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm ;

View File

@ -29,6 +29,12 @@ extern "C" {
* and inspiration form practice 6 for TCA0 setup
*/
// Fan history variables
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;
// INITALICE TIMER COUNTER
void init_TCA0();