using System.Diagnostics;
namespace TeensyFlashDetectionCLI
{
internal class Program
{
// Program Constants
const string ports_path = @""; // Paste your teensy_ports path here
const string ports_args = "-L";
const string bootloader_ident = "Bootloader";
const string not_ready_ident = "[no_device]";
// List of unique CLI returns
public static List<string> returnHistory = new List<string>();
static void Main(string[] args)
{
SetConsoleColorAndClear(ColorSet.IDLE);
// Monitor Ports
while(true)
{
// Check ports
Loop();
// Wait
Task.Delay(200).Wait();
}
}
static void Loop()
{
// Enumerate with basic teensy_ports output
string ret = ExecuteCommand(ports_path, ports_args);
// If unique, add to history
if (!returnHistory.Contains(ret))
{
returnHistory.Add(ret);
}
// Change terminal color and indicate between No Teensy Connected/Connected in Bootloader/Connected normal
if(String.IsNullOrWhiteSpace(ret) || ret.Contains(not_ready_ident))
{
// Not connected
SetConsoleColorAndClear(ColorSet.IDLE);
Console.WriteLine("Not Connected");
}
else if(ret.Contains(bootloader_ident))
{
// Connected, bootloader
SetConsoleColorAndClear(ColorSet.RED);
Console.WriteLine("Bootloader");
}
else
{
// Connected
SetConsoleColorAndClear(ColorSet.GREEN);
Console.WriteLine("Connected");
}
Console.WriteLine();
// Print historical results
Console.WriteLine("[History]");
foreach (string line in returnHistory)
{
Console.Write(" -> ");
if (String.IsNullOrWhiteSpace(line)) { Console.WriteLine("{empty}"); }
else { Console.WriteLine(line.Replace(Environment.NewLine, String.Empty)); }
}
// Print current output
Console.WriteLine();
Console.WriteLine("[Current]");
Console.WriteLine($"{ret}");
}
public enum ColorSet { IDLE, GREEN, RED};
static void SetConsoleColorAndClear(ColorSet color)
{
switch(color)
{
case ColorSet.GREEN:
Console.BackgroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.Black;
break;
case ColorSet.RED:
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
break;
default:
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
break;
}
Console.Clear();
}
public static string ExecuteCommand(string command, string arguments = "")
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(command)
{
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = new Process())
{
process.StartInfo = processStartInfo;
process.Start();
// Read the output of the command
string output = process.StandardOutput.ReadToEnd();
// Wait for the process to finish
process.WaitForExit();
return output;
}
}
}
}