|
| 1 | +using SpiDriver; |
| 2 | +using System; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Threading; |
| 6 | + |
| 7 | +namespace Example.CommandLine |
| 8 | +{ |
| 9 | + class Program |
| 10 | + { |
| 11 | + static void Main(string[] argv) { |
| 12 | + // the device |
| 13 | + Device device = null; |
| 14 | + |
| 15 | + // loop through accepting commands |
| 16 | + string line; |
| 17 | + |
| 18 | + while (true) { |
| 19 | + // read next line |
| 20 | + Console.Write("> "); |
| 21 | + line = Console.ReadLine().Trim(); |
| 22 | + |
| 23 | + // parse the line |
| 24 | + string[] components = line.Split(' '); |
| 25 | + string[] args = components.Length == 0 ? new string[0] : new string[components.Length - 1]; |
| 26 | + string cmd = components.Length == 0 ? null : components[0]; |
| 27 | + |
| 28 | + if (args.Length > 0) |
| 29 | + Array.Copy(components, 1, args, 0, args.Length); |
| 30 | + |
| 31 | + // ignore empty lines |
| 32 | + if (cmd == null) |
| 33 | + continue; |
| 34 | + |
| 35 | + // process command |
| 36 | + if (cmd.Equals("connect", StringComparison.CurrentCultureIgnoreCase)) { |
| 37 | + if (args.Length == 0) { |
| 38 | + Console.WriteLine("connect <port>"); |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + device = new Device(args[0]); |
| 44 | + device.Connect(); |
| 45 | + Console.WriteLine("Connected successfully"); |
| 46 | + } catch (Exception ex) { |
| 47 | + Console.Error.WriteLine(ex.Message); |
| 48 | + } |
| 49 | + } else if (cmd.Equals("a", StringComparison.CurrentCultureIgnoreCase) || cmd.Equals("b", StringComparison.CurrentCultureIgnoreCase) |
| 50 | + || cmd.Equals("cs", StringComparison.CurrentCultureIgnoreCase)) { |
| 51 | + // check device is connected |
| 52 | + if (device == null) { |
| 53 | + Console.WriteLine("Not connected to device"); |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + // determine output |
| 58 | + Output output = Output.A; |
| 59 | + |
| 60 | + if (cmd.Equals("b", StringComparison.CurrentCultureIgnoreCase)) |
| 61 | + output = Output.B; |
| 62 | + else if (cmd.Equals("cs", StringComparison.CurrentCultureIgnoreCase)) |
| 63 | + output = Output.CS; |
| 64 | + |
| 65 | + try { |
| 66 | + if (args.Length == 0) { |
| 67 | + bool value = device.GetOutput(output); |
| 68 | + device.SetOutput(output, !value); |
| 69 | + Console.WriteLine($"{output}: {!value}"); |
| 70 | + } else { |
| 71 | + if (args[0].Equals("yes", StringComparison.CurrentCultureIgnoreCase) || args[0].Equals("on", StringComparison.CurrentCultureIgnoreCase)) { |
| 72 | + device.SetOutput(output, true); |
| 73 | + } else { |
| 74 | + device.SetOutput(output, false); |
| 75 | + } |
| 76 | + } |
| 77 | + } catch (Exception ex) { |
| 78 | + Console.Error.WriteLine(ex.Message); |
| 79 | + } |
| 80 | + } else if (cmd.Equals("status", StringComparison.CurrentCultureIgnoreCase)) { |
| 81 | + if (device == null) { |
| 82 | + Console.WriteLine("Not connected to device"); |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + DeviceStatus status = device.GetStatus(); |
| 88 | + |
| 89 | + Console.WriteLine($"Model: {status.Model}"); |
| 90 | + Console.WriteLine($"Serial Number: {status.Serial}"); |
| 91 | + Console.WriteLine($"Uptime: {status.Uptime}"); |
| 92 | + Console.WriteLine($"Voltage: {status.Voltage}V"); |
| 93 | + Console.WriteLine($"Current: {status.Current}A"); |
| 94 | + Console.WriteLine($"Temperature: {status.Temperature}°C"); |
| 95 | + Console.WriteLine($"A: {status.A}"); |
| 96 | + Console.WriteLine($"B: {status.B}"); |
| 97 | + Console.WriteLine($"CS: {status.ChipSelect}"); |
| 98 | + Console.WriteLine($"CRC: 0x{status.Crc.ToString("x4")}"); |
| 99 | + } catch (Exception ex) { |
| 100 | + Console.Error.WriteLine(ex.Message); |
| 101 | + } |
| 102 | + } else if (cmd.Equals("writef", StringComparison.CurrentCultureIgnoreCase)) { |
| 103 | + string path = string.Join(' ', args); |
| 104 | + |
| 105 | + if (device == null) { |
| 106 | + Console.WriteLine("Not connected to device"); |
| 107 | + continue; |
| 108 | + } else if (args.Length == 0) { |
| 109 | + Console.WriteLine("No data in arguments"); |
| 110 | + continue; |
| 111 | + } else if (!File.Exists(path)) { |
| 112 | + Console.WriteLine("File does not exist"); |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + // create stopwatch to time transfer |
| 117 | + Stopwatch stopwatch = new Stopwatch(); |
| 118 | + |
| 119 | + try { |
| 120 | + byte[] fb = File.ReadAllBytes(path); |
| 121 | + |
| 122 | + stopwatch.Start(); |
| 123 | + device.Write(fb, 0, fb.Length); |
| 124 | + |
| 125 | + Console.WriteLine($"Wrote {fb.Length} bytes in {Math.Round(stopwatch.Elapsed.TotalSeconds, 3)}s"); |
| 126 | + } catch (Exception ex) { |
| 127 | + Console.Error.WriteLine(ex.Message); |
| 128 | + } |
| 129 | + } else if (cmd.Equals("read", StringComparison.CurrentCultureIgnoreCase)) { |
| 130 | + int count = 0; |
| 131 | + |
| 132 | + if (device == null) { |
| 133 | + Console.WriteLine("Not connected to device"); |
| 134 | + continue; |
| 135 | + } else if (args.Length == 0) { |
| 136 | + Console.WriteLine("No data in arguments"); |
| 137 | + continue; |
| 138 | + } else if (!int.TryParse(args[0], out count)) { |
| 139 | + Console.WriteLine("Invalid count to read"); |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + try { |
| 144 | + byte[] data = new byte[count]; |
| 145 | + device.Read(data, 0, data.Length); |
| 146 | + |
| 147 | + Console.WriteLine(BitConverter.ToString(data)); |
| 148 | + } catch (Exception ex) { |
| 149 | + Console.Error.WriteLine(ex.Message); |
| 150 | + } |
| 151 | + } else if (cmd.Equals("help", StringComparison.CurrentCultureIgnoreCase)) { |
| 152 | + Console.WriteLine("connect <port> - connect to serial port"); |
| 153 | + Console.WriteLine("a [on/off] - toggle or set A output"); |
| 154 | + Console.WriteLine("b [on/off] - toggle or set B output"); |
| 155 | + Console.WriteLine("cs [on/off] - toggle or set CS output"); |
| 156 | + Console.WriteLine("status - get device status"); |
| 157 | + Console.WriteLine("writef <path> - write file to SPI"); |
| 158 | + } else if (line != "quit" && line != "q" && line != "exit") { |
| 159 | + return; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments