summaryrefslogtreecommitdiffstats
path: root/hal/pin.tpp
blob: 3954945d425b8093c58cea674b1446d4190831a0 (plain)
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
/* 
 * File:   pin.tpp
 * Author: naopross
 * 
 * Created on May 3, 2018, 8:02 PM
 */

#ifndef PIN_TPP
#define PIN_TPP
#include "pin.hpp"


template<unsigned bit>
template<typename latch_T, typename tris_T, typename port_T>
pin<bit>::pin(latch_T *latch, tris_T *tris, port_T *port) :
    _latch(reinterpret_cast<volatile uint8_t *>(latch)),
    _tris(reinterpret_cast<volatile uint8_t *>(tris)),
    _port(reinterpret_cast<volatile uint8_t *>(port))
{
    // default settings
    set_mode(pin<bit>::mode::OUTPUT);
    set(pin<bit>::state::OFF);
}


template<unsigned bit>
pin<bit>::~pin()
{
    
}


template<unsigned bit>
void pin<bit>::set_mode(unsigned m)
{
    if (m)
        *_tris |= 1<<bit; // input
    else
        *_tris &= ~(1<<bit); // output
}

template<unsigned bit>
void pin<bit>::set_mode(pin<bit>::mode m)
{
    set_mode(static_cast<unsigned>(m));
}

template<unsigned bit>
typename pin<bit>::state pin<bit>::read() const
{
    if (*_port & (1<<bit))
        return state::ON;
    else
        return state::OFF;
}

template<unsigned bit>
void pin<bit>::set(unsigned s)
{
    if (s > 0)
        *_latch |= 1<<bit; // on
    else
        *_latch &= ~(1<<bit); // off
}

template<unsigned bit>
void pin<bit>::set(pin<bit>::state s)
{
    set(static_cast<unsigned>(s));
}

template<unsigned bit>
void pin<bit>::toggle()
{
    *_latch ^= 1<<bit;
}

template<unsigned bit>
bool pin<bit>::operator==(const pin<bit> &other) const
{
    return (_latch == other._latch 
        && _tris == other._tris 
        && _port == other._port);
}

template<unsigned bit>
bool pin<bit>::operator!=(const pin<bit> &other) const
{
    return !(*this == other);
}

#endif