-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFanSpeed.cpp
executable file
·100 lines (84 loc) · 1.99 KB
/
FanSpeed.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
FanSpeed.cpp - Library for reading fan speed.
Created by Andrey Rublev, November 10, 2010.
*/
#include "WProgram.h"
#include "FanSpeed.h"
void FanSpeedBase::init(int pin, bool useInternalResistor = true)
{
pinMode(pin, INPUT);
if (useInternalResistor && pin != 13) {
digitalWrite(pin, HIGH);
}
_pin = pin;
counter = 0;
}
void FanSpeedBase::reset()
{
counter = 0;
}
FanSpeed::FanSpeed(int pin, bool useInternalResistor)
{
init(pin, useInternalResistor);
bit = _pin;
if (_pin > 7) {
bit -= 8;
}
bit = _BV(bit);
}
void FanSpeed::process()
{
register uint8_t pinn = PIND;
if (_pin > 7) {
pinn = PINB;
}
pulseState = pinn & bit;
if ((pulseState ^ prevPulseState) & pulseState) {
counter++;
}
prevPulseState = pulseState;
}
FanSpeedInt::FanSpeedInt(int pin, bool useInternalResistor)
{
init(pin, useInternalResistor);
}
void FanSpeedInt::process()
{
counter++;
}
FanSpeedMultiD::FanSpeedMultiD(uint8_t pins, uint8_t states, FANCB cb)
{
DDRD &= ~pins; // set selected pins to INPUT mode
PORTD |= states; // set pull-up resistors to selected pins
state = 0;
prevState = 0;
bits = pins;
callback = cb;
}
void FanSpeedMultiD::process()
{
state = PIND & bits;
register uint8_t stateMask = (state ^ prevState) & state;
if (stateMask) {
(*callback)(stateMask);
}
prevState = state;
}
FanSpeedMultiB::FanSpeedMultiB(uint8_t pins, uint8_t states, FANCB cb)
{
DDRB &= ~pins; // set selected pins to INPUT mode
PORTB |= states; // set pull-up resistors to selected pins
state = 0;
prevState = 0;
bits = pins;
callback = cb;
}
void FanSpeedMultiB::process()
{
state = PINB & bits;
register uint8_t stateMask = (state ^ prevState) & state;
if (stateMask) {
(*callback)(stateMask);
}
prevState = state;
}