/* * File: main.cpp * Author: naopross * * Created on May 1, 2018, 6:18 PM */ // Note: there is some debug code in uart.tpp that controls the RGB led // #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; 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 led_red(static_cast(&led1_pin), led::color::RED); led led_green(static_cast(&led2_pin), led::color::GREEN); led led_yellow(static_cast(&led3_pin), led::color::YELLOW); #ifdef DEBUG led_red.set(1); led_green.set(1); led_yellow.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>(); // Note: does not work with teraterm on windows // DEL character if (input.back() == '\x7f') { input.pop_back(); if (input.size() > 1) { 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 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>(led_red.to_string()); uart::print<1>(led_green.to_string()); uart::print<1>(led_yellow.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") { if (command[3] == "on") { led_red.set(1); } else if (command[3] == "off") { led_red.set(0); } } else if (command[2] == "green") { if (command[3] == "on") { led_green.set(1); } else if (command[3] == "off") { led_green.set(0); } } else if (command[2] == "yellow") { if (command[3] == "on") { led_yellow.set(1); } else if (command[3] == "off") { led_yellow.set(0); } } else if (command[2] == "all") { if (command[3] == "on") { led_red.set(1); led_green.set(1); led_yellow.set(1); } else if (command[3] == "off") { led_red.set(0); led_green.set(0); led_yellow.set(0); } } } } } } return 0; }