summaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: de37652b60744f3ae260aa5da9d6415e364d5db2 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* 
 * 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;
    std::string sub;

    size_t last = 0; 
    size_t next = 0;

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

    return v;
}

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>("> ");
        
        do {
            input += uart::read_wait<1>();

            // DEL character
            if (input.back() == '\x7f') {
                input.pop_back();
                input.pop_back();
                uart::write<1>("\b \b");
            }
        } while (input.back() != '\r' && input.back() != '\n');

        uart::write<1>("\n\r");

#ifdef DEBUG
        uart::write<1>("input: "); 
        uart::print<1>(input);       
#endif
        
        // remove non printable symbols
        input.erase(std::remove_if(input.begin(), input.end(), std::iscntrl), input.end());

        // set to lowercase
        std::transform(input.begin(), input.end(), input.begin(), std::tolower);
        
        // split
        const std::vector<std::string> command = split(input, ' ');
        
#ifdef DEBUG
        uart::print<1>("parsed commmands:");
        for (int i = 0; i < command.size(); i++) {
            uart::print<1>(command[i]);
        }
#endif

        // parse command
        if (command.size() == 1) {
            if (command[0] == "help") {
                uart::print<1>("List of available commands:");
                uart::print<1>("help");
                uart::print<1>("set led <color> <on/off>");
                uart::print<1>("show led all <on/off>");
                uart::print<1>("set baudrate <baud>");

                uart::print<1>("\n\rList of available colors:");
                uart::print<1>("red, green, yellow");

                uart::print<1>("\n\rList of available baudrates:");
                uart::print<1>("9600, 4800");
            }
        } else if (command.size() == 3) {
            if (command[0] == "show") {
                if (command[1] == "led") {
                    if (command[2] == "all") {
                        uart::print<1>(led1.to_string());
                        uart::print<1>(led2.to_string());
                        uart::print<1>(led3.to_string());
                    }
                }
            }

            if (command[0] == "set") {
                if (command[1] == "baudrate") {
                    if (command[2] == "9600") {
                        // TODO: replace with uart::set_baudrate<dev>(baud);
                        U1BRG = 0x19;
                    } else if (command[2] == "4800") {
                        U1BRG = 0x33;
                    }
                }
            }
        } else if (command.size() == 4) {
            if (command[0] == "set") {
                if (command[1] == "led") {
                    if (command[2] == "red") {
                        if (command[3] == "on") {
                            led1.set(1);
                        } else if (command[3] == "off") {
                            led1.set(0);
                        }
                    } else if (command[2] == "green") {
                        if (command[3] == "on") {
                            led2.set(1);
                        } else if (command[3] == "off") {
                            led2.set(0);
                        }
                    } else if (command[2] == "yellow") {
                        if (command[3] == "on") {
                            led3.set(1);
                        } else if (command[3] == "off") {
                            led3.set(0);
                        }
                    } else if (command[2] == "all") {
                        if (command[3] == "on") {
                            led1.set(1);
                            led2.set(1);
                            led3.set(1);
                        } else if (command[3] == "off") {
                            led1.set(0);
                            led2.set(0);
                            led3.set(0);
                        }
                    }
                }
            }
        }
    }

    return 0;
}