summaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: fec1ba08efa5095ead4239faf9638ca33198e9d6 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* 
 * File:   main.cpp
 * Author: naopross
 *
 * Created on May 1, 2018, 6:18 PM
 */

#define DEBUG

// basic devices
#include "hal/confbits.hpp"
#include "hal/hwconfig.hpp"

// specific devices
#include "hal/uart.tpp"
#include "hal/pin.tpp"

// high level
#include "led.hpp"

// standard library 
#include <string>
#include <vector>
#include <algorithm>

// microchip libraries
extern "C" {
    #include <xc.h>
    // #include <proc/p32mx470f512h.h>
}

std::vector<std::string> split(const std::string& str, const char sep)
{
    std::vector<std::string> v;

    size_t last = 0; 
    size_t next = 0;

    while ((next = str.find(sep, last)) != std::string::npos) {
        v.push_back(str.substr(next, next +1));
        last = next + 1;
    }

    v.push_back(str.substr(last));

    return v;
}

void strip(std::string &str)
{
    const std::string strip_chars = "\n\r\b\t\f";

    for (const char &ch : strip_chars) {
        str.erase(std::remove_if(str.begin(), str.end(), [ch](const char &c){ return c == ch; }), str.end());
    }
}

int main(int argc, char** argv)
{
    osc::initialize();
    interrupts::initialize();

    // initialize uart and enable echo
    uart::initialize<1>();
    uart::echo<1>(true);

    // initialize pin as outputs
    io_pin<4> led1_pin(&LATEbits, &TRISEbits, &PORTEbits);
    io_pin<6> led2_pin(&LATEbits, &TRISEbits, &PORTEbits);
    io_pin<7> led3_pin(&LATEbits, &TRISEbits, &PORTEbits);

    // build leds
    led led1(static_cast<gpio *>(&led1_pin), led::color::RED);
    led led2(static_cast<gpio *>(&led2_pin), led::color::GREEN);
    led led3(static_cast<gpio *>(&led3_pin), led::color::YELLOW);

#ifdef DEBUG
    led1.set(1);
    led2.set(1);
    led3.set(1);

    uart::write<1>("started\n\r");
#endif
    
    while (true) {
        std::string input = "";
        
        // write prompt
        uart::write<1>("> ");

        while (uart::rx_buffer<1>().empty());
        do {
            input += uart::read_wait<1>();
        } while (uart::rx_buffer<1>().front() != '\r');
        // remove '\r' from the RX buffer
        uart::rx_buffer<1>().pop(); 

#ifdef DEBUG
        uart::write<1>("\n\rinput: "); 
        uart::print<1>(input);       
#endif

        // remove weird symbols
        strip(input);
        
        // split
        const std::vector<std::string> command = split(input, ' ');
        
#ifdef DEBUG
        for (const std::string &s : command) {
            uart::print<1>(s);
        }
#endif

        if (command.size() == 1) {
            if (command[0] == "help") {
                uart::print<1>("TODO: help text");
            }
        } else if (command.size() == 3) {
            if (command[0] == "show") {
                uart::print<1>(led1.to_string());
                uart::print<1>(led2.to_string());
                uart::print<1>(led3.to_string());
                continue;
            }

            if (command[0] == "set") {
                if (command[1] == "baudrate") {
                    // TODO
                }
            }
        } else if (command.size() == 4) {
            if (command[0] == "set") {
                if (command[1] == "led") {
                    if (command[2] == "red") {
                        led1.set((command[3] == "on"));
                    } else if (command[2] == "green") {
                        led2.set((command[3] == "on"));
                    } else if (command[2] == "yellow") {
                        led3.set((command[3] == "on"));
                    } else if (command[2] == "all") {
                        led1.set((command[3] == "on"));
                        led2.set((command[3] == "on"));
                        led3.set((command[3] == "on"));
                    }
                }
            }
        }
    }

    return 0;
}