summaryrefslogtreecommitdiffstats
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp114
1 files changed, 104 insertions, 10 deletions
diff --git a/main.cpp b/main.cpp
index 860460c..fec1ba0 100644
--- a/main.cpp
+++ b/main.cpp
@@ -15,8 +15,13 @@
#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" {
@@ -24,34 +29,123 @@ extern "C" {
// #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);
- pin<4> led1(&LATEbits, &TRISEbits, &PORTEbits);
- pin<6> led2(&LATEbits, &TRISEbits, &PORTEbits);
- pin<7> led3(&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());
- while (uart::rx_buffer<1>().size() > 0) {
- char c = uart::read<1>();
-
- uart::write<1>(c);
- uart::write<1>("\n\r");
- }
+ 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, ' ');
- led1.toggle();
+#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;