-
Notifications
You must be signed in to change notification settings - Fork 586
/
ArduinoSpiDevice.cs
60 lines (51 loc) · 1.84 KB
/
ArduinoSpiDevice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Device.Spi;
using System.Text;
namespace Iot.Device.Arduino
{
internal sealed class ArduinoSpiDevice : SpiDevice
{
public ArduinoSpiDevice(ArduinoBoard board, SpiConnectionSettings connectionSettings)
{
Board = board;
ConnectionSettings = connectionSettings;
board.EnableSpi();
board.Firmata.ConfigureSpiDevice(connectionSettings);
}
public ArduinoBoard Board
{
get;
private set;
}
public override SpiConnectionSettings ConnectionSettings { get; }
public override void Read(Span<byte> buffer)
{
ReadOnlySpan<byte> dummy = stackalloc byte[buffer.Length];
Board.Firmata.SpiTransfer(ConnectionSettings.ChipSelectLine, dummy, buffer);
}
public override void Write(ReadOnlySpan<byte> buffer)
{
Board.Firmata.SpiWrite(ConnectionSettings.ChipSelectLine, buffer, !Board.StreamUsesHardwareFlowControl);
}
public override void TransferFullDuplex(ReadOnlySpan<byte> writeBuffer, Span<byte> readBuffer)
{
Board.Firmata.SpiTransfer(ConnectionSettings.ChipSelectLine, writeBuffer, readBuffer);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (Board != null)
{
Board.DisableSpi();
// To make sure this is called only once (and any further attempts to use this instance fail)
Board = null!;
}
}
base.Dispose(disposing);
}
}
}