-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.cs
142 lines (126 loc) · 4.07 KB
/
Command.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using Windows.Storage.Streams;
namespace MoveHub
{
internal abstract class CommandBase
{
protected CommandBase(Port port)
{
Port = port;
}
protected Port Port { get; }
protected abstract byte Length { get; }
internal abstract void Write(DataWriter writer);
}
internal abstract class ControlCommandBase : CommandBase
{
protected ControlCommandBase(Port port) : base(port)
{
}
protected abstract byte Command { get; }
protected abstract void WriteBody(DataWriter writer);
internal override void Write(DataWriter writer)
{
writer.WriteBytes(new byte[] { Length, 0x00, 0x81, (byte)Port, 0x11, Command });
writer.ByteOrder = ByteOrder.LittleEndian;
WriteBody(writer);
}
}
internal class SubscribeCommand : CommandBase
{
internal SubscribeCommand(Port port, byte option) : base(port)
{
Option = option;
}
public byte Option { get; }
protected override byte Length => 0x0A;
internal override void Write(DataWriter write)
{
write.WriteBytes(new byte[] { Length, 0x00, 0x41, (byte)Port, Option, 0x01, 0x00, 0x00, 0x00, 0x01 });
}
}
internal class ColorCommand : ControlCommandBase
{
public ColorCommand(Color color) : base(Port.LED)
{
Color = color;
}
protected Color Color { get; }
protected override byte Length => 0x08;
protected override byte Command => 0x51;
protected override void WriteBody(DataWriter writer)
{
writer.WriteByte(0x00);
writer.WriteByte((byte)Color);
}
}
internal abstract class MotorCommandBase : ControlCommandBase
{
protected MotorCommandBase(Port port, sbyte power) : base(port)
{
Power = power;
}
protected sbyte Power { get; }
protected abstract void WriteValue(DataWriter writer);
protected override void WriteBody(DataWriter writer)
{
WriteValue(writer);
writer.WriteByte(unchecked((byte)Power));
if (this is IPowerB b)
writer.WriteByte(unchecked((byte)b.PowerB));
writer.WriteBytes(new byte[] { 0x64, 0x7F, 0x03 });
}
}
internal class MotorTimeCommand : MotorCommandBase
{
public MotorTimeCommand(Port port, ushort time, sbyte power) : base(port, power)
{
Time = time;
}
protected ushort Time { get; }
protected override byte Length => 0x0C;
protected override byte Command => 0x09;
protected override void WriteValue(DataWriter writer)
{
writer.WriteUInt16(Time);
}
}
internal class MotorTimeMoveCommand : MotorTimeCommand, IPowerB
{
public MotorTimeMoveCommand(ushort time, sbyte powerA, sbyte powerB) : base(Port.AB, time, powerA)
{
PowerB = powerB;
}
public sbyte PowerB { get; }
protected override byte Length => 0x0D;
protected override byte Command => 0x0A;
}
internal class MotorAngleCommand : MotorCommandBase
{
public MotorAngleCommand(Port port, uint angle, sbyte power) : base(port, power)
{
Angle = angle;
}
protected uint Angle { get; }
protected override byte Length => 0x0E;
protected override byte Command => 0x0B;
protected override void WriteValue(DataWriter writer)
{
writer.WriteUInt32(Angle);
}
}
internal class MotorAngleMoveCommand : MotorAngleCommand, IPowerB
{
public MotorAngleMoveCommand(uint angle, sbyte powerA, sbyte powerB) : base(Port.AB, angle, powerA)
{
PowerB = powerB;
}
public sbyte PowerB { get; }
protected override byte Length => 0x0F;
protected override byte Command => 0x0C;
}
internal interface IPowerB
{
sbyte PowerB { get; }
}
}