/* * ********************************CONFIG******************************* */ // retrieves the commands from the config file public void initCommands(Port p) { // bring in the serial port serialComm = p; commands = new CommandsInfo(); // for reading form the file string line; string commandsFile = "config/commands.conf"; // read all setting from our config try { using ( StreamReader commandsConf = new StreamReader(commandsFile) ) { // revisit; think about using reflections and doing // this with a foreach loop (add string[] to commandsInfo) while ( (line = commandsConf.ReadLine()) != null ) { if ( line.Contains("command") ) commands.command = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("displayOn") ) commands.displayOn = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("displayOff") ) commands.displayOff = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("cursorHome") ) commands.cursorHome = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("underlineOn") ) commands.underlineOn = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("underlineOff") ) commands.underlineOff = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("cursorLeft") ) commands.cursorLeft = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("cursorRight") ) commands.cursorRight = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("blinkOn") ) commands.blinkOn = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("blinkOff") ) commands.blinkOff = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("backspace") ) commands.backspace = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("clear") ) commands.clear = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("contrast") ) commands.contrast = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("brightness") ) commands.brightness = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("customChar") ) commands.customChar = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("displayLeft") ) commands.displayLeft = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("displayRight") ) commands.displayRight = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("baudRate") ) commands.baudRate = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("i2dAddress") ) commands.i2cAddress = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("displayBaud") ) commands.displayBaud = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("displayI2c") ) commands.displayI2c = stringToByte(line.Substring(line.IndexOf("0x"), 4)); else if ( line.Contains("setCursor") ) commands.setCursor = stringToByte(line.Substring(line.IndexOf("0x"), 4)); } } } catch (Exception e) { Console.WriteLine("Could not read: {0}", commandsFile); Console.WriteLine(e.Message); } }
/* * *****************************MAIN************************************ */ static int Main(string[] args) { // set up serial serialComm = new Port(); serialComm.initSerial(); // set up commands commands = new Commands(); commands.initCommands(serialComm); // flags for options bool showHelp = false; bool printSentence = false; // get the options OptionSet options = new OptionSet() // short (common) .Add("b|backspace", "equivalent to backspace key", bs => commands.backspace()) .Add("c|clear", "clears the LCD screen", c => commands.clear()) .Add("displayleft", "shifts display x-1", dl => commands.displayLeft()) .Add("displayright", "shifts display x+1", dr => commands.displayRight()) .Add("home", "sets cursor position to 0,0", home => commands.cursorHome()) .Add("i|info", "dipslays baud and i2c address", i => { commands.displayBaud(); // wait for input to change Console.WriteLine("Press any key to show I2C address"); Console.ReadKey(); commands.displayI2c(); }) .Add("l|left", "sets cursor position x-1", l => commands.cursorLeft()) .Add("r|right", "sets cursor position x+1", r => commands.cursorRight()) // long only (not used as much) .Add("baud=", "change the baud rate; default 9600" , (byte baud) => commands.setBaud(baud)) .Add("blink=", "turns blinking cursor on(1)/off(0); default off (0)" , (int b) => commands.blink(b)) .Add("brightness=", "change brightness 1-8; default 5" , (byte br) => commands.display(br)) .Add("contrast=", "change contrast 1-50; default 40" , (byte w) => commands.setContrast(w)) .Add("display=", "turns LCD on(1)/off(0); default off (0)" , (int d) => commands.display(d)) .Add("i2c=", "change the I2C address; default 0x50" , (byte addr) => commands.setI2c(addr)) .Add("position=", "sets the cursor position x,y (top left" + "1,1)", (byte x, byte y) => commands.setCursor(x, y)) .Add("underline=", "turns underline on(1)/off(0); default off (0)" , (int u) => commands.underline(u)) // help and default .Add("?|h|help", "displays this help", h => showHelp = true) .Add("<>", words => printSentence = true); // parse options try { options.Parse(args); } catch (OptionException e) { Console.Write("Error: "); Console.WriteLine(e.Message); showHelp = true; } // shows help if flag is set // otherwise prints text if ( showHelp ) displayHelp(options); else if (printSentence) serialComm.commPort.Write(String.Join(" ", args)); // release comm serialComm.terminateSerial(); return 0; }