/* * 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(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(&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>("\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 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 "); uart::print<1>("show led all "); uart::print<1>("set baudrate "); 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(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") { 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; }