forked from donovan6000/iMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan.cpp
executable file
·57 lines (41 loc) · 1.42 KB
/
fan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 5V 0.2A 25x25x7mm brushless axial DC fan with a PH2.0-2P connector
// Header files
extern "C" {
#include <asf.h>
}
#include "common.h"
#include "eeprom.h"
#include "fan.h"
#include "led.h"
// Definitions
#define FAN_ENABLE_PIN IOPORT_CREATE_PIN(PORTE, 1)
#define FAN_CHANNEL TC_CCB
#define FAN_CHANNEL_CAPTURE_COMPARE TC_CCBEN
// Supporting function implementation
void Fan::initialize() noexcept {
// Configure fan enable pin
ioport_set_pin_dir(FAN_ENABLE_PIN, IOPORT_DIR_OUTPUT);
// Configure fan timer
tc_enable(&FAN_TIMER);
tc_set_wgm(&FAN_TIMER, TC_WG_SS);
tc_write_period(&FAN_TIMER, FAN_TIMER_PERIOD);
tc_enable_cc_channels(&FAN_TIMER, static_cast<tc_cc_channel_mask_enable_t>(FAN_CHANNEL_CAPTURE_COMPARE | LED_CHANNEL_CAPTURE_COMPARE));
tc_write_clock_source(&FAN_TIMER, TC_CLKSEL_DIV64_gc);
// Reset
reset();
}
void Fan::setSpeed(uint8_t speed) noexcept {
// Get fan scale
float fanScale;
nvm_eeprom_read_buffer(EEPROM_FAN_SCALE_OFFSET, &fanScale, EEPROM_FAN_SCALE_LENGTH);
// Set speed
tc_write_cc(&FAN_TIMER, FAN_CHANNEL, speed <= FAN_MIN_SPEED ? 0 : (getValueInRange(speed, FAN_MIN_SPEED, FAN_MAX_SPEED) * fanScale + nvm_eeprom_read_byte(EEPROM_FAN_OFFSET_OFFSET)) * FAN_TIMER_PERIOD / FAN_MAX_SPEED);
}
void Fan::reset() noexcept {
// Set fan to min speed
setSpeed(FAN_MIN_SPEED);
}
bool Fan::isOn() noexcept {
// Return if fan isn't at its default speed
return tc_read_cc(&FAN_TIMER, FAN_CHANNEL);
}