forked from vitotai/BrewPiLess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensorArduinoPin.h
54 lines (42 loc) · 982 Bytes
/
SensorArduinoPin.h
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
/*
* File: SensorArduinoPin.h
* Author: mat
*
* Created on 22 August 2013, 19:36
*/
#pragma once
#ifdef ARDUINO
#include "Brewpi.h"
#include "FastDigitalPin.h"
#include "Pins.h"
/* A SwitchSensor whose state is provided by a hardware pin.
By using a template, the compiler can inline and optimize the call to digitalRead to a single instruction.
*/
template<uint8_t pin, bool invert, bool internalPullup>
class DigitalConstantPinSensor : public SwitchSensor
{
public:
DigitalConstantPinSensor() {
fastPinMode(pin, internalPullup ? INPUT_PULLUP : INPUT);
}
virtual bool sense() {
return fastDigitalRead(pin) ^ invert;
}
};
class DigitalPinSensor : public SwitchSensor
{
private:
bool invert;
uint8_t pin;
public:
DigitalPinSensor(uint8_t pin, bool invert)
{
pinMode(pin, USE_INTERNAL_PULL_UP_RESISTORS ? INPUT_PULLUP : INPUT);
this->invert = invert;
this->pin = pin;
}
virtual bool sense() {
return digitalRead(pin) ^ invert;
}
};
#endif