summaryrefslogtreecommitdiffstats
path: root/hal/pin.hpp
blob: 13e9a525ada88ff555657229f8b6989f14468b44 (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
/* 
 * File:   pin.hpp
 * Author: naopross
 *
 * Created on May 3, 2018, 8:02 PM
 */

#ifndef PIN_HPP
#define PIN_HPP

#include <cstdint>

extern "C" {
#include <xc.h>
}

class gpio_pin
{
public:
    virtual ~gpio_pin() {}

    // digital mode methods
    virtual void set_mode(unsigned m) = 0;

    // digital output methods
    virtual void set(unsigned s) = 0;
    virtual unsigned is_set() const = 0;
    virtual void toggle() = 0;

    // digital input methods
    virtual unsigned read() const = 0; 

    // TODO: analog mode methods

protected:
    gpio_pin() {}
};

template<unsigned bit>
class io_pin : public gpio_pin {
public:
    enum class mode : unsigned
    { INPUT = 1, OUTPUT = 0 };

    enum class state : unsigned
    { ON = 1, OFF = 0 };

    io_pin() = delete;

    template<typename latch_T, typename tris_T, typename port_T>
    io_pin(latch_T *latch, tris_T *tris, port_T *port);

    virtual ~io_pin();

    void set_mode(unsigned m);
    void set_mode(mode m);

    unsigned read() const;
    unsigned is_set() const;

    void set(unsigned s);
    void set(state s);

    void toggle();

    bool operator==(const io_pin<bit> &other) const;
    bool operator!=(const io_pin<bit> &other) const;

private:
    volatile uint8_t *_latch;
    volatile uint8_t *_tris;
    volatile uint8_t *_port;
};

#endif  /* PIN_HPP */