-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cs
More file actions
230 lines (198 loc) · 8.86 KB
/
Controller.cs
File metadata and controls
230 lines (198 loc) · 8.86 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Threading;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Pwm;
using GHIElectronics.TinyCLR.Pins;
namespace BrainPad {
public static class Controller {
public const int P0 = 0;
public const int P1 = 1;
public const int P2 = 2;
public const int P3 = 3;
public const int P4 = 4;
public const int P5 = 5;
public const int P6 = 6;
public const int P7 = 7;
public const int P8 = 8;
public const int P9 = 9;
public const int P10 = 10;
public const int P11 = 11;
public const int P12 = 12;
public const int P13 = 13;
public const int P14 = 14;
public const int P15 = 15;
public const int P16 = 16;
public const int P19 = 19;
public const int P20 = 20;
public const int LED = 21;
public const int Led = 21;
public const int Buzzer = 22;
public const int ButtonA = 23;
public const int ButtonB = 24;
public const int AccelX = 25;
public const int AccelY = 26;
public const int AccelZ = 27;
public const uint Celsius = 28;
public const uint Fahrenheit = 29;
public const uint Red = 0xFF0000;
public const uint Green = 0x00FF00;
public const uint Blue = 0x0000FF;
public const uint White = 0xFFFFFF;
public const uint Black = 0x000000;
public const uint Yellow = 0xFFFF00;
public const uint Magenta = 0xFF00FF;
public const uint Cyan = 0x00FFFF;
public static readonly int[] PinMapPulse = {
SC13048.GpioPin.PA5, //P0
SC13048.GpioPin.PA3, //P1
SC13048.GpioPin.PA2, //P2
SC13048.GpioPin.PA1, //P3
SC13048.GpioPin.PA0, //P4
SC13048.GpioPin.PA7, //P5
SC13048.GpioPin.PA4, //P6
SC13048.GpioPin.PB0, //P7
SC13048.GpioPin.PA9, //P8
SC13048.GpioPin.PB1, //P9
SC13048.GpioPin.PA6, //P10
SC13048.GpioPin.PB6, //P11
SC13048.GpioPin.PA10, //P12
SC13048.GpioPin.PB3, //P13
SC13048.GpioPin.PB4, //P14
SC13048.GpioPin.PB5, //P15
SC13048.GpioPin.PB12, //P16
-1 , //P17
-1 , //P18
SC13048.GpioPin.PB10, //P19
SC13048.GpioPin.PB11, //P20
SC13048.GpioPin.PA8, //LED
SC13048.GpioPin.PB8, //BUZZER
SC13048.GpioPin.PC13, //A
SC13048.GpioPin.PB7, //B
};
public static readonly int[] PinMapTick = {
SC13048.GpioPin.PA5, //P0
SC13048.GpioPin.PA3, //P1
SC13048.GpioPin.PA2, //P2
-1 , //P3
-1 , //P4
-1 , //P5
-1 , //P6
-1 , //P7
-1 , //P8
-1 , //P9
-1 , //P10
-1 , //P11
SC13048.GpioPin.PA5, //P12
SC13048.GpioPin.PB3, //P13
SC13048.GpioPin.PB4, //P14
SC13048.GpioPin.PB5, //P15
SC13048.GpioPin.PA3, //P16
-1 , //P17
-1 , //P18
SC13048.GpioPin.PB10, //P19
SC13048.GpioPin.PB11, //P20
SC13048.GpioPin.PA8, //LED
SC13048.GpioPin.PB8, //BUZZER
SC13048.GpioPin.PC13, //A
SC13048.GpioPin.PB7, //B
};
internal static GpioController Gpio = GpioController.GetDefault();
internal static PwmController PwmSoftware = PwmController.FromName(SC13048.Timer.Pwm.Software.Id);
private static bool isPulse;
static Controller() {
using (var pb15 = GpioController.GetDefault().OpenPin(SC13048.GpioPin.PB15)) {
pb15.SetDriveMode(GpioPinDriveMode.InputPullDown);
isPulse = pb15.Read() == GpioPinValue.High;
}
}
public static bool IsPulse => isPulse;
public static bool IsTick => !isPulse;
public static int GetPwmChannelFromPin(int pin) {
switch (pin) {
case SC13048.GpioPin.PA8: return SC13048.Timer.Pwm.Controller1.PA8;
case SC13048.GpioPin.PA9: return SC13048.Timer.Pwm.Controller1.PA9;
case SC13048.GpioPin.PA10: return SC13048.Timer.Pwm.Controller1.PA10;
case SC13048.GpioPin.PA5: return SC13048.Timer.Pwm.Controller2.PA5;
case SC13048.GpioPin.PA1: return SC13048.Timer.Pwm.Controller2.PA1;
case SC13048.GpioPin.PB10: return SC13048.Timer.Pwm.Controller2.PB10;
case SC13048.GpioPin.PB11: return SC13048.Timer.Pwm.Controller2.PB11;
case SC13048.GpioPin.PA2: return SC13048.Timer.Pwm.Controller15.PA2;
case SC13048.GpioPin.PA3: return SC13048.Timer.Pwm.Controller15.PA3;
case SC13048.GpioPin.PB8: return SC13048.Timer.Pwm.Controller16.PB8;
}
return -1;
}
public static string GetPwmTimerFromPin(int pin) {
switch (pin) {
case SC13048.GpioPin.PA8:
case SC13048.GpioPin.PA9:
case SC13048.GpioPin.PA10:
return SC13048.Timer.Pwm.Controller1.Id;
case SC13048.GpioPin.PA5:
case SC13048.GpioPin.PA1:
case SC13048.GpioPin.PB10:
case SC13048.GpioPin.PB11:
return SC13048.Timer.Pwm.Controller2.Id;
case SC13048.GpioPin.PA2:
case SC13048.GpioPin.PA3:
return SC13048.Timer.Pwm.Controller15.Id;
case SC13048.GpioPin.PB8:
return SC13048.Timer.Pwm.Controller16.Id;
}
return null;
}
public static bool IsPwmFromPin(double pin) {
if (IsPulse) {
switch (pin) {
case P3:
case P0:
case P1:
case P8:
case P12:
case P2:
case Buzzer:
return true;
}
}
else {
switch (pin) {
case P0:
case P2:
case P1:
case P15:
case P16:
case Buzzer:
return true;
}
}
return false;
}
public static int GetGpioFromPin(double pin) => Controller.IsPulse ? Controller.PinMapPulse[(int)pin] : Controller.PinMapTick[(int)pin];
public static void Wait(double seconds) => Thread.Sleep((int)(seconds * 1000));
public static IOModule Analog(double pin) => new Analog(pin);
public static IOModule Digital(double pin) => new Digital(pin);
public static IOModule Sound(double pin, double playtime, double volume) => new Sound(pin, playtime, volume);
public static IOModule Button(double button, double detectPeriod) => new BrainPad.Button(button, detectPeriod);
public static IOModule Accel(double xyz) => new Accel(xyz);
public static IOModule Servo(double pin) => new Servo(pin);
public static IOModule Neopixel(double pin, double lednums) => new Neopixel(pin, lednums);
public static IOModule I2cBus(double address) => new I2cBus((int)address);
public static IOModule Distance(double triggerPin, double echoPin) => new Distance(triggerPin, echoPin);
public static IOModule Touch(double pin, double senstitiveLevel) => new Touch(pin, senstitiveLevel);
public static IOModule Infrared(double receivePin) => new Infrared(receivePin);
public static IOModule Temperature(double unit) => new Temperature(unit);
public static void Print(object obj) => Display.Print(obj.ToString());
public static double In(IOModule module) => module.In();
public static void Out(IOModule module, double[] oValue) => module.Out(oValue);
public static void Out(IOModule module, double oValue) => module.Out(oValue);
public static double OutIn(IOModule module, byte[] dataOut, byte[] dataIn) => module.OutIn(dataOut, dataIn);
public static void Release(object o) {
if (o is IDisposable disposable) disposable.Dispose();
}
internal static int Scale(double value, int originalMin, int originalMax, int scaleMin, int scaleMax) {
var scale = (double)(scaleMax - scaleMin) / (originalMax - originalMin);
var ret = (int)(scaleMin + ((value - originalMin) * scale));
return ret > scaleMax ? scaleMax : (ret < scaleMin ? scaleMin : ret);
}
}
}