-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoFunction.cs
More file actions
78 lines (73 loc) · 3.32 KB
/
ArduinoFunction.cs
File metadata and controls
78 lines (73 loc) · 3.32 KB
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
using System;
using System.Collections.Generic;
using System.Text;
namespace ArduinoDynamicDriver
{
public class ArduinoFunction
{
public readonly byte FunctionId;
public readonly IList<SupportedType> ArgumentTypes;
public readonly SupportedType ReturnType;
object[] Arguments;
public ArduinoFunction(byte functionId, IList<SupportedType> argumentTypes, SupportedType returnType, params object[] arguments)
{
FunctionId = functionId;
ArgumentTypes = argumentTypes ?? throw new ArgumentNullException(nameof(argumentTypes));
ReturnType = returnType;
Arguments = arguments;
}
public IList<Byte> GetDataBytes()
{
var result = new List<byte>();
for (int i = 0; i < Arguments.Length; i++)
{
var argument = Arguments[i];
var argumentType = ArgumentTypes[i];
switch (argumentType)
{
case SupportedType.BOOLEANT:
var boolean = (bool)argument;
result.Add(boolean ? (byte)1 : (byte)0);
break;
case SupportedType.CHART:
result.Add(BitConverter.GetBytes(Convert.ToSByte(argument))[0]);
break;
case SupportedType.UCHART:
result.Add(Convert.ToByte(argument));
break;
case SupportedType.BYTET:
result.Add(Convert.ToByte(argument));
break;
case SupportedType.INTT:
var integer = Convert.ToInt16(argument);
var integerReperesentation = BitConverter.GetBytes(integer);
result.AddRange(integerReperesentation);
break;
case SupportedType.UINTT:
var uinteger = Convert.ToUInt16(argument);
var uintegerReperesentation = BitConverter.GetBytes(uinteger);
result.AddRange(uintegerReperesentation);
break;
case SupportedType.LONG:
var lon = Convert.ToInt32(argument);
var lonRepresentation = BitConverter.GetBytes(lon);
result.AddRange(lonRepresentation);
break;
case SupportedType.ULONG:
var ulon = Convert.ToUInt32(argument);
var ulonRepresentation = BitConverter.GetBytes(ulon);
result.AddRange(ulonRepresentation);
break;
case SupportedType.SHORT:
var shor = Convert.ToInt16(argument);
var shorReprestation = BitConverter.GetBytes(shor);
result.AddRange(shorReprestation);
break;
case SupportedType.VOIDT:
throw new ArgumentException("An argument cant be VOIDT");
}
}
return result;
}
}
}