Skip to content

Commit 846cbfb

Browse files
committed
Initial work on 0.1.0
1 parent 7799ffc commit 846cbfb

File tree

10 files changed

+1077
-2
lines changed

10 files changed

+1077
-2
lines changed

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,59 @@
1-
# spidriver-net
2-
An unofficial .NET SDK for the SPIDriver debugger
1+
2+
3+
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/alandoherty/spidriver-net/master/LICENSE)
4+
[![GitHub issues](https://img.shields.io/github/issues/alandoherty/spidriver-net.svg?style=flat-square)](https://github.com/alandoherty/spidriver-net/issues)
5+
[![GitHub stars](https://img.shields.io/github/stars/alandoherty/spidriver-net.svg?style=flat-square)](https://github.com/alandoherty/spidriver-net/stargazers)
6+
[![GitHub forks](https://img.shields.io/github/forks/alandoherty/spidriver-net.svg?style=flat-square)](https://github.com/alandoherty/spidriver-net/network)
7+
[![GitHub forks](https://img.shields.io/nuget/dt/SPIDriver.svg?style=flat-square)](https://www.nuget.org/packages/SPIDriver/)
8+
9+
</div>
10+
11+
# spidriver
12+
13+
An unofficial .NET library for [SPIDriver](https://spidriver.com/), allows for synchronous/asynchronous control of the device via the serial port. Open permissive MIT license with a dual-compliation for .NET Standard 2.0 and .NET Framework 4.6.1.
14+
15+
![SPIDriver PCB](img/spidriver.jpg)
16+
17+
## Getting Started
18+
19+
[![NuGet Status](https://img.shields.io/nuget/v/SPIDriver.svg?style=flat-square)](https://www.nuget.org/packages/SPIDriver/)
20+
21+
You can install the package using either the CLI:
22+
23+
```
24+
dotnet add package SPIDriver
25+
```
26+
27+
or from the NuGet package manager:
28+
29+
```
30+
Install-Package SPIDriver
31+
```
32+
33+
For assistance with the SPIDriver product itself you can view the [user guide](https://spidriver.com/spidriver.pdf) hosted on the official website.
34+
35+
### Example
36+
37+
You can find the example project `Example.CommandLine`, which demonstrates a CLI which implements all the features of the library.
38+
39+
```
40+
> connect
41+
Connected successfully
42+
> status
43+
Model: spidriver1
44+
Serial Number: DO01JHMO
45+
Uptime: 01:56:22
46+
Voltage: 5.203V
47+
Current: 0A
48+
Temperature: 32.6°C
49+
A: True
50+
B: True
51+
CS: True
52+
CRC: 0x0001
53+
> writef airline.mp4
54+
Wrote 1510004 bytes in 33.294s
55+
```
56+
57+
## Contributing
58+
59+
Any pull requests or bug reports are welcome, please try and keep to the existing style conventions and comment any additions.

SpiDriver.sln

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29009.5
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpiDriver", "src\SpiDriver\SpiDriver.csproj", "{DB31321A-1176-45BA-B346-3E935C461B62}"
7+
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7C86F419-D0FB-4251-AF6C-2C9194EA1C05}"
9+
ProjectSection(SolutionItems) = preProject
10+
README.md = README.md
11+
EndProjectSection
12+
EndProject
13+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{EFCCC756-6E2D-4608-865D-5CE6B204C6F8}"
14+
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.CommandLine", "samples\Example.CommandLine\Example.CommandLine.csproj", "{69010272-1995-4708-A354-CC8485F9109F}"
16+
EndProject
17+
Global
18+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
19+
Debug|Any CPU = Debug|Any CPU
20+
Release|Any CPU = Release|Any CPU
21+
EndGlobalSection
22+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
23+
{DB31321A-1176-45BA-B346-3E935C461B62}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{DB31321A-1176-45BA-B346-3E935C461B62}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{DB31321A-1176-45BA-B346-3E935C461B62}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{DB31321A-1176-45BA-B346-3E935C461B62}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{69010272-1995-4708-A354-CC8485F9109F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{69010272-1995-4708-A354-CC8485F9109F}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{69010272-1995-4708-A354-CC8485F9109F}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{69010272-1995-4708-A354-CC8485F9109F}.Release|Any CPU.Build.0 = Release|Any CPU
31+
EndGlobalSection
32+
GlobalSection(SolutionProperties) = preSolution
33+
HideSolutionNode = FALSE
34+
EndGlobalSection
35+
GlobalSection(NestedProjects) = preSolution
36+
{69010272-1995-4708-A354-CC8485F9109F} = {EFCCC756-6E2D-4608-865D-5CE6B204C6F8}
37+
EndGlobalSection
38+
GlobalSection(ExtensibilityGlobals) = postSolution
39+
SolutionGuid = {1AD50AD4-1933-4F12-BC76-14C291BA7587}
40+
EndGlobalSection
41+
EndGlobal

img/spidriver.jpg

38 KB
Loading
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.2</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\SpiDriver\SpiDriver.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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

Comments
 (0)