/* * 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 #include #include // microchip libraries extern "C" { #include // #include } std::vector split(const std::string& str, const char sep) { std::vector 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(&led1_pin), led::color::RED); led led2(static_cast(&led2_pin), led::color::GREEN); led led3(static_cast(&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 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; }