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
|
/*
* 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(last, next-last));
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>("\n\r> ");
do {
input += uart::read_wait<1>();
} while (input.back() != '\r' && input.back() != '\n');
#ifdef DEBUG
uart::write<1>("input: ");
uart::print<1>(input);
#endif
// remove weird symbols
strip(input);
// set to lowercase
std::transform(input.begin(), input.end(), input.begin(), ::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
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") {
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") {
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;
}
|