summaryrefslogtreecommitdiffstats
path: root/hal/pin.tpp
blob: 527ba0ae5ee30665078557d0ad63ec6a356d37d0 (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
93
94
95
/* 
 * 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>
io_pin<bit>::io_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(io_pin<bit>::mode::OUTPUT);
    set(io_pin<bit>::state::OFF);
}


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


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

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

template<unsigned bit>
unsigned io_pin<bit>::read() const
{
    return (*_port & (1<<bit)) ? 1 : 0;
}

template<unsigned bit>
unsigned io_pin<bit>::is_set() const
{
    return (*_latch & (1<<bit)) ? 1 : 0;
}

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

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

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

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

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

#endif