-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitalPin.cpp
93 lines (77 loc) · 1.19 KB
/
DigitalPin.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
/*
* Author: Klusjesman
*/
#include "DigitalPin.h"
void swap(DigitalPin& dst, const DigitalPin& src)
{
dst.port = src.port;
dst.pin = src.pin;
dst.iomode = src.iomode;
}
DigitalPin::DigitalPin()
{
}
DigitalPin& DigitalPin::operator=(const DigitalPin &c)
{
swap(*this,c);
return *this;
}
DigitalPin::DigitalPin(const DigitalPin &c)
{
swap(*this,c);
}
DigitalPin::DigitalPin(volatile uint8_t * port, uint8_t pin)
{
this->port = port;
this->pin = pin;
this->iomode = None;
}
DigitalPin::DigitalPin(volatile uint8_t * port, uint8_t pin, IOMode iomode)
{
this->port = port;
this->pin = pin;
//init port
switch (iomode)
{
case Input:
makeInput();
break;
case Output:
makeOutput();
break;
case None:
break;
}
}
DigitalPin::~DigitalPin()
{
}
void DigitalPin::makeOutput(void)
{
//set DDRx.pin to be output
*(port-1) |= (1 << pin);
iomode = Output;
}
void DigitalPin::makeInput(void)
{
//set DDRx.pin to be input
*(port-1) &= ~(1 << pin);
iomode = Input;
}
void DigitalPin::write(bool val)
{
if (val)
{
//DigitalPin on
*port |= (1<< pin);
}
else
{
//DigitalPin off
*port &= ~(1 << pin);
}
}
bool DigitalPin::read()
{
return (*(port-2) & (1 << pin));
}