/// <summary> /// Actually starts the demo /// </summary> /// <param name="Server">Reference to the Telnet Server</param> private static void Start(TelnetServer Server) { for (int f = 0; f < 16; ++f) { for (int b = 0; b < 8; ++b) { Server.Color((TelnetServer.Colors)f, (TelnetServer.Colors)b); Server.Print(" " + Tools.ZeroFill(f, 2) + "," + Tools.ZeroFill(b, 2) + " ", true, true); } Server.Color(TelnetServer.Colors.White, TelnetServer.Colors.Black); Server.Print(""); } }
/// <summary> /// Shows a specific help page /// </summary> /// <param name="Server">The telnet server object</param> /// <param name="Page">The page</param> /// <returns>True when the page exists</returns> private static bool DoHelp(TelnetServer Server, string Page) { switch (Page) { case "": Server.Print("NTPSYNC [HOSTNAME] Synchronizes with a timeserver"); return true; case "NTPSYNC": Server.Print("NTPSYNC [HOSTNAME] Synchronizes with a timeserver"); Server.Print("- [HOSTNAME] The hostname of an NTP-server (example: pool.ntp.org)"); return true; default: return false; } }
/// <summary> /// Actually starts the demo /// </summary> /// <param name="Server">Reference to the Telnet Server</param> private static void Start(TelnetServer Server) { NetworkInterface[] Ips = NetworkInterface.GetAllNetworkInterfaces(); for (int IpCnt = 0; IpCnt < Ips.Length; ++IpCnt) { Server.Print("Network interface " + IpCnt.ToString() + ":"); Server.Print("MAC Address: " + NetworkInfo.MacToString(Ips[IpCnt].PhysicalAddress)); Server.Print("- IP Address: " + Ips[IpCnt].IPAddress.ToString() + " (" + Ips[IpCnt].SubnetMask.ToString() + ")"); Server.Print("- Gateway: " + Ips[IpCnt].GatewayAddress.ToString()); for (int DnsCnt = 0; DnsCnt < Ips[IpCnt].DnsAddresses.Length; ++DnsCnt) Server.Print("- DNS-server " + DnsCnt.ToString() + ": " + Ips[IpCnt].DnsAddresses[DnsCnt].ToString()); } Server.Print("Connected to: " + Server.RemoteAddress); }
/// <summary> /// Shows a file entry (child of ListDir) /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Path">Path</param> private static void ShowFileEntry(TelnetServer Server, FileInfo Path) { Server.Print(Path.CreationTime.ToString() + Tools.ZeroFill(Path.Length.ToString(), 20, ' ') + " " + Path.Name); }
/// <summary> /// Shows a directory entry (child of ListDir) /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Path">Path</param> /// <param name="Name">If filled, this name will be shown instead (useful for "." and "..")</param> private static void ShowDirEntry(TelnetServer Server, DirectoryInfo Path, string Name = "") { if (Name == "") Name = Path.Name; Server.Print(Path.CreationTime.ToString() + " <DIR> " + Name); }
/// <summary> /// Displays a file /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Pathname">Filename</param> private static void DisplayFile(TelnetServer Server, string Pathname) { if (!File.Exists(Pathname)) { DisplayError(Server, "File not found"); return; } FileStream Stream = File.OpenRead(Pathname); while (true) { long Length = Stream.Length - Stream.Position; if (Length == 0) break; if (Length > 255) Length = 255; byte[] Buffer = new byte[(int)Length]; Stream.Read(Buffer, 0, Buffer.Length); Server.Print(new String(Tools.Bytes2Chars(Buffer)), true); } Stream.Close(); Server.Print(""); }
/// <summary> /// Actually starts the demo /// </summary> /// <param name="Server">Reference to the Telnet Server</param> public static void Start(TelnetServer Server) { // Configures the onboard button. If this fails, it's probably already in use by another app. InputPort Button = null; try { Button = new InputPort(ONBOARD_SW1, false, Port.ResistorMode.Disabled); } catch (Exception e) { Server.Color(TelnetServer.Colors.LightRed); Server.Print("Exception " + e.Message + " given. Were the onboard Button already configured?"); Server.Color(TelnetServer.Colors.White); return; } // Configures the onboard LED. If this fails, it's probably already in use by another app. OutputPort Led = null; try { Led = new OutputPort(ONBOARD_LED, false); } catch (Exception e) { Button.Dispose(); // The button is already defined Server.Color(TelnetServer.Colors.LightRed); Server.Print("Exception " + e.Message + " given. Were the onboard LED already configured?"); Server.Color(TelnetServer.Colors.White); return; } // Disables echoing of keypresses Server.EchoEnabled = false; // Clears the screen Server.ClearScreen(); // Draws a Netduino Plus in ANSI/ASCII art Server.Print("\xda\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xbf"); Server.Print("\xb3 \xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe \xfe\xfe\xfe\xfe\xfe\xfe\xfe\xfe\xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb \xdc \xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb NETDUINO \xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb PLUS \xb3"); Server.Print("\xb3 ..\xb3"); Server.Print("\xdb\xdb \xdb\xdb ::\xb3"); Server.Print("\xb3 \xdb\xdb\xdb\xdb\xdb\xdb \xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb \xdb\xdb\xdb\xdb\xdb\xdb \xb3"); Server.Print("\xdb\xdb\xdb\xdb\xdb \xdb\xdb\xdb\xdb\xdb\xdb \xfe\xfe\xfe\xfe\xfe\xfe \xfe\xfe\xfe\xfe\xfe\xfe\xb3"); Server.Print("\xc0\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xdb\xdb\xdb\xdb\xdb\xdb\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xd9"); Server.Print("", true); Server.Print("Push the Netduino Plus onboard button, or press a key to close this app"); // We only update the screen if the LastState is different from the current state bool LastState = false; while (Server.IsConnected && Server.Input(1, false) == "") { // We need to update if (Button.Read() == LastState) { // Lets record the last state LastState = !Button.Read(); Led.Write(LastState); // Draws the button if (LastState) Server.Color(TelnetServer.Colors.HighIntensityWhite); else Server.Color(TelnetServer.Colors.Gray); Server.Locate(7, 26, true); Server.Print("\xdb\xdb", true, true); // Draws the LED if (LastState) Server.Color(TelnetServer.Colors.LightBlue); else Server.Color(TelnetServer.Colors.Gray); Server.Locate(3, 29, true); Server.Print("\xdc", true); // Brings back the cursor to the last line Server.Locate(14, 1); } } // Releases the pins Button.Dispose(); Led.Dispose(); // Enables echo again and clears the screen if (Server.IsConnected) { Server.EchoEnabled = true; Server.ClearScreen(); } }
/// <summary> /// Synchronizes with an NTP-server /// </summary> /// <param name="Server">The telnet server object</param> /// <param name="NtpServer">Hostname of the NTP server</param> private static void Sync(TelnetServer Server, string NtpServer) { SNTP_Client Client = new SNTP_Client(new IntegratedSocket(NtpServer, 123)); Client.Synchronize(); Server.Print("Current time: " + DateTime.Now.ToString()); }
/// <summary> /// Edits a line /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Tempfile">The location of the temp file</param> /// <param name="LineNo">Line number to edit</param> private static void EditLine(TelnetServer Server, string Tempfile, int LineNo) { List(Server, Tempfile, LineNo, LineNo); Server.Print(Tools.ZeroFill(LineNo, 8, ' ') + ":*", true); string NewLine = Server.Input(); if (NewLine == "\x03") return; Edlin.UpdateLine(Tempfile, LineNo, NewLine); }
/// <summary> /// Starts editting a file /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Filename">File to edit</param> private static void EditFile(TelnetServer Server, string Filename) { if (!File.Exists(Filename)) throw new Exception("File does not exist: " + Filename); // Makes a working file string Tempfile = ReplaceExt(Filename, ".$$$"); string Bakfile = ReplaceExt(Filename, ".bak"); File.Copy(Filename, Tempfile, true); // Detecting line endings FileStream Stream = File.OpenRead(Tempfile); int Lines = 0; int Byte = 0; for (long Pos = 0; Pos < Stream.Length; ++Pos) { Byte = Stream.ReadByte(); if (Byte == 10) ++Lines; } Stream.Close(); // Appairently the file doesn't end with a line ending if (Byte != 10) ++Lines; // Okay, we know the amount of lines Server.Print(Lines.ToString() + " lines. Type ? for help."); // Lets start the main loop while (true) { // Shows a prompt and asks for a command Server.Print("*", true); string[] Arguments = SplitCommandline(Server.Input()); if (Arguments.Length == 0) continue; switch (Arguments[Arguments.Length - 1].Substring(0, 1).ToUpper()) { case "Q": // Quits, without saving File.Delete(Tempfile); return; case "E": // Quits, with saving if (File.Exists(Bakfile)) File.Delete(Bakfile); File.Move(Filename, Bakfile); File.Move(Tempfile, Filename); return; case "L": // Lists some lines if (Arguments.Length > 2) List(Server, Tempfile, int.Parse(Arguments[0]), int.Parse(Arguments[1])); else if (Arguments.Length > 1) List(Server, Tempfile, int.Parse(Arguments[0]), Lines); else List(Server, Tempfile, 1, Lines); break; case "?": // Shows help EdlinHelp(Server); break; default: // Is this a line number, or unknown entry? int LineNo = -1; try { LineNo = int.Parse(Arguments[0]); } catch { } if (LineNo == -1) Server.Print("Entry error"); else if (LineNo > Lines) Server.Print("Line out of range (1 to " + Lines.ToString() + ")"); else EditLine(Server, Tempfile, LineNo); break; } } }
/// <summary> /// Configures a pin /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Pin">Pin</param> /// <param name="Type">Type</param> private static void _ConfigPin(TelnetServer Server, string Pin, string Type) { int PinIndex = _PinToIndex(Pin); if (PinIndex == -1) { throw new ArgumentException("Invalid pin name: " + Pin); } switch (Type) { case "GPI": _Pins[PinIndex].ConfigureInput(); Server.Print("Pin " + Pin + " is now a digital input port"); break; case "GPO": _Pins[PinIndex].ConfigureOutput(); Server.Print("Pin " + Pin + " is now a digital output port"); break; case "PWM": _Pins[PinIndex].ConfigurePWM(); Server.Print("Pin " + Pin + " is now a PWM port"); break; case "ADC": _Pins[PinIndex].ConfigureAnalogIn(); Server.Print("Pin " + Pin + " is now an analog input port"); break; case "NONE": _Pins[PinIndex].ReleasePin(); Server.Print("Pin " + Pin + " is now released"); break; default: throw new ArgumentException("Type " + Type + " unknown"); } }
/// <summary> /// Reads a pin /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Pin">Pin</param> private static void _ReadPin(TelnetServer Server, string Pin) { int PinIndex = _PinToIndex(Pin); if (PinIndex == -1) { throw new ArgumentException("Invalid pin name: " + Pin); } Server.Print("Pin " + Pin + " has value: " + _Pins[PinIndex].Read().ToString()); }
/// <summary> /// Changes the pwm pulse /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Pin">Pin</param> /// <param name="Pulse">Pulse</param> /// <param name="Duration">Duration</param> private static void _SetPulse(TelnetServer Server, string Pin, string Pulse, string Duration) { int PinIndex = _PinToIndex(Pin); if (PinIndex == -1) { throw new ArgumentException("Invalid pin name: " + Pin); } int iPulse = int.Parse(Pulse); int iDuration = int.Parse(Duration); _Pins[PinIndex].Write((uint)iPulse, (uint)iDuration); Server.Print("Pulse for pin " + Pin + " set to " + Pulse + "," + Duration); }
/// <summary> /// Changes the dutycycle /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Pin">Pin</param> /// <param name="Dutycycle">Dutycycle</param> private static void _DutyCycle(TelnetServer Server, string Pin, string Dutycycle) { int PinIndex = _PinToIndex(Pin); if (PinIndex == -1) { throw new ArgumentException("Invalid pin name: " + Pin); } int Value = int.Parse(Dutycycle); _Pins[PinIndex].Write((uint)Value); Server.Print("Dutycycle for pin " + Pin + " set to " + Dutycycle); }
/// <summary> /// Shows a specific help page /// </summary> /// <param name="Server">The telnet server object</param> /// <param name="Page">The page</param> /// <returns>True when the page exists</returns> private static bool DoHelp(TelnetServer Server, string Page) { switch (Page) { case "": Server.Print("LISTPINS Shows a list of all pins"); Server.Print("CONFIG [PIN] [TYPE] Configures a pin"); Server.Print("READPIN [PIN] Reads a pin value"); Server.Print("GETPINTYPE [PIN] Reads a pin type"); Server.Print("WRITEPIN [PIN] [VALUE] Writes a pin value"); Server.Print("DUTYCYCLE [PIN] [DUTYCYCLE] Sets the PWM dutycycle"); Server.Print("SETPULSE [PIN] [PULSE] [DURATION] Sets the PWM pulse"); return true; case "LISTPINS": Server.Print("LISTPINS Shows a list of all pins"); return true; case "CONFIG": Server.Print("CONFIG [PIN] [TYPE] Configures a pin"); Server.Print("- [PIN] The name of the pin"); Server.Print("- [TYPE] Can be: GPI, GPO, PWM, ADC or None"); return true; case "READPIN": Server.Print("READPIN [PIN] Reads a pin value"); Server.Print("- [PIN] The name of the pin"); return true; case "GETPINTYPE": Server.Print("GETPINTYPE [PIN] Reads a pin type"); Server.Print("- [PIN] The name of the pin"); return true; case "WRITEPIN": Server.Print("WRITEPIN [PIN] [VALUE] Writes a pin value"); Server.Print("- [PIN] The name of the pin"); Server.Print("- [VALUE] 1 for high, 0 for low"); return true; case "DUTYCYCLE": Server.Print("DUTYCYCLE [PIN] [DUTYCYCLE] Sets the PWM dutycycle"); Server.Print("- [PIN] The name of the pin"); Server.Print("- [DUTYCYCLE] The PWM dutycycle (0 to 100)"); return true; case "SETPULSE": Server.Print("SETPULSE [PIN] [PULSE] [DURATION] Sets the PWM pulse"); Server.Print("- [PIN] The name of the pin"); Server.Print("- [PULSE] PWM pulse"); Server.Print("- [DURATION] PWM duration"); return true; default: return false; } }
/// <summary> /// Displays a line of text in red /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Text">Text to display</param> private static void DisplayError(TelnetServer Server, string Text) { Server.Color(TelnetServer.Colors.LightRed); Server.Print(Text); Server.Color(TelnetServer.Colors.White); }
/// <summary> /// Shows a specific help page /// </summary> /// <param name="Server">The telnet server object</param> /// <param name="Page">The page</param> /// <returns>True when the page exists</returns> private static bool DoHelp(TelnetServer Server, string Page) { switch (Page) { case "": Server.Print("DIR Reads out a listing of the current directory"); Server.Print("CD [PATH] Changes the current directory"); Server.Print("MKDIR [PATH] Creates a directory"); Server.Print("RMDIR [PATH] Removes a directory"); Server.Print("DEL [PATH] Removes a file"); Server.Print("TYPE [PATH] Prints out a file"); Server.Print("TOUCH [PATH] Creates an empty file"); return true; case "CD": Server.Print("CD [PATH] Changes the current directory"); Server.Print("- [PATH] The directory to go into"); return true; case "TYPE": Server.Print("TYPE [PATH] Prints out a file"); Server.Print("- [PATH] The file to display"); return true; case "DEL": Server.Print("DEL [PATH] Removes a file"); Server.Print("- [PATH] The file to remove"); return true; case "TOUCH": Server.Print("TOUCH [PATH] Creates an empty file"); Server.Print("- [PATH] The file to create"); return true; case "MKDIR": Server.Print("MKDIR [PATH] Creates a directory"); Server.Print("- [PATH] The directory to create"); return true; case "RMDIR": Server.Print("RMDIR [PATH] Removes a directory"); Server.Print("- [PATH] The directory to remove"); return true; case "DIR": Server.Print("DIR Reads out a listing of the current directory"); return true; default: return false; } }
/// <summary> /// Changes the current date /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Date">New date</param> private static void ChangeDate(TelnetServer Server, string Date) { // Splits up date parts string[] Parts = Date.Split(new char[] { '-' }); if (Parts.Length != 3) throw new ArgumentException("Invalid TIME format. See HELP DATE for more."); // Validates data int Years = int.Parse(Parts[0]); int Months = int.Parse(Parts[1]); int Days = int.Parse(Parts[2]); if (Years < 2000 || Years > 3000) throw new ArgumentException("Invalid DATE format. See HELP DATE for more."); if (Months > 12 || Months < 1) throw new ArgumentException("Invalid DATE format. See HELP DATE for more."); if (Days > 31 || Days < 1) throw new ArgumentException("Invalid DATE format. See HELP DATE for more."); DateTime NewTime = new DateTime( year: Years, month: Months, day: Days, hour: DateTime.Now.Hour, minute: DateTime.Now.Minute, second: DateTime.Now.Second ); Utility.SetLocalTime(NewTime); Server.Print("New date: " + DisplayDate()); }
/// <summary> /// Lists lines from a file /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Tempfile">The location of the temp file</param> /// <param name="Start">First line to show</param> /// <param name="End">Last line to show</param> private static void List(TelnetServer Server, string Tempfile, int Start, int End) { FileStream Stream = File.OpenRead(Tempfile); int LineNo = 1; string Buffer = ""; while (true) { // Defines the buffer size (max. 255 bytes) long Length = Stream.Length - Stream.Position; if (Length == 0) break; if (Length > 255) Length = 255; // Reads out the buffer byte[] ReadBuffer = new byte[(int)Length]; Stream.Read(ReadBuffer, 0, ReadBuffer.Length); Buffer += new string(Tools.Bytes2Chars(ReadBuffer)); // Is this a line? while (true) { int Pos = Buffer.IndexOf("\n"); if (Pos < 0) break; // New line found, lets split it string Line = Buffer.Substring(0, Pos); Buffer = Buffer.Substring(Pos + 1); // Do we need to print this line? if (LineNo >= Start && LineNo <= End) Server.Print(Tools.ZeroFill(LineNo, 8, ' ') + ": " + Line.TrimEnd()); ++LineNo; } } Stream.Close(); // If there's no line ending at the end of the file, we need to show this line if (Buffer != "" && LineNo >= Start && LineNo <= End) Server.Print(Tools.ZeroFill(LineNo, 8, ' ') + ": " + Buffer.TrimEnd()); }
/// <summary> /// Shows a specific help page /// </summary> /// <param name="Server">The telnet server object</param> /// <param name="Page">The page</param> /// <returns>True when the page exists</returns> private static bool DoHelp(TelnetServer Server, string Page) { switch (Page) { case "": Server.Print("TIME [TIME] Displays or changes the current time"); Server.Print("DATE [DATE] Displays or changes the current date"); return true; case "TIME": Server.Print("TIME [TIME] Displays or changes the current time"); Server.Print("- [TIME] If set, will change the time (Format: hh:mm:ss)"); return true; case "DATE": Server.Print("DATE [DATE] Displays or changes the current date"); Server.Print("- [DATE] If set, will change the date (Format: yyyy-mm-dd)"); return true; default: return false; } }
/// <summary> /// Shows the EDLIN help /// </summary> /// <param name="Server">Reference to the telnet server</param> private static void EdlinHelp(TelnetServer Server) { Server.Print("Edit line line#"); Server.Print("End (save file) E"); Server.Print("Quit (throw away changes) Q"); Server.Print("List [startline][,endline]L"); }
/// <summary> /// Changes the current time /// </summary> /// <param name="Server">Reference to the telnet server</param> /// <param name="Time">New time</param> private static void ChangeTime(TelnetServer Server, string Time) { // Splits up time parts string[] Parts = Time.Split(new char[] { ':' }); if (Parts.Length != 3) throw new ArgumentException("Invalid TIME format. See HELP TIME for more."); // Validates data int Hours = int.Parse(Parts[0]); int Minutes = int.Parse(Parts[1]); int Seconds = int.Parse(Parts[2]); if (Hours > 23 || Hours < 0) throw new ArgumentException("Invalid TIME format. See HELP TIME for more."); if (Minutes > 59 || Minutes < 0) throw new ArgumentException("Invalid TIME format. See HELP TIME for more."); if (Seconds > 59 || Seconds < 0) throw new ArgumentException("Invalid TIME format. See HELP TIME for more."); DateTime NewTime = new DateTime( year: DateTime.Now.Year, month: DateTime.Now.Month, day: DateTime.Now.Day, hour: Hours, minute: Minutes, second: Seconds ); Utility.SetLocalTime(NewTime); Server.Print("New time: " + DisplayTime()); }