#define V436
//#define V425
//#define V424
#define GHI
//#define SecretLabs
// 2013.09.06 output/inputのDisposeを追加
// 2015.01.12 DigitalInのコンストラクタ追加
// 2015.02.25 GHI V436に対応
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
#if SecretLabs
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoMini;
#elif GHI
#if V436
using GHI.Pins;
#elif V425
using Cerb = GHI.Hardware.FEZCerb;
#elif V424
using GHI.OSHW.Hardware; // FEZCerberus使用のため追加、参照設定にも追加
using Cerb = GHI.OSHW.Hardware.FEZCerberus;
#endif
#endif
using System.IO.Ports; // Microsoft.SPOT.Hardware.SerialPort
using System.Text; // Encoding.UTF8.GetBytes(str);
// Microsoft.SPOT.Hardware.PWMを参照設定に追加する
using PWM = Microsoft.SPOT.Hardware.PWM;
using AnalogInput = Microsoft.SPOT.Hardware.AnalogInput;
namespace BoeBotLib
{
public class CPU
{
#if GHI
#if V436
public static Cpu.Pin P03 = (Cpu.Pin)Generic.GetPin('A', 8);
public static Cpu.Pin P04 = (Cpu.Pin)Generic.GetPin('A', 13);
public static Cpu.Pin P05 = (Cpu.Pin)Generic.GetPin('A', 7);
public static Cpu.Pin P06 = (Cpu.Pin)Generic.GetPin('A', 6);
public static Cpu.Pin P07 = (Cpu.Pin)Generic.GetPin('C', 10);
public static Cpu.Pin P08 = (Cpu.Pin)Generic.GetPin('A', 14);
public static Cpu.Pin P09 = (Cpu.Pin)Generic.GetPin('C', 11);
public static Cpu.Pin P10 = (Cpu.Pin)Generic.GetPin('B', 4);
public static Cpu.Pin P11 = (Cpu.Pin)Generic.GetPin('B', 9);
public static Cpu.Pin P12 = (Cpu.Pin)Generic.GetPin('B', 3);
public static Cpu.Pin P13 = (Cpu.Pin)Generic.GetPin('D', 2);
public static Cpu.Pin P14 = (Cpu.Pin)Generic.GetPin('C', 12);
public static Cpu.Pin P16 = (Cpu.Pin)Generic.GetPin('B', 8);
public static Cpu.Pin P18 = (Cpu.Pin)Generic.GetPin('B', 7);
public static Cpu.Pin P19 = (Cpu.Pin)Generic.GetPin('B', 6);
public static Cpu.Pin P20 = (Cpu.Pin)Generic.GetPin('B', 5);
public static Cpu.Pin P22 = (Cpu.Pin)Generic.GetPin('C', 0);
public static Cpu.Pin P23 = (Cpu.Pin)Generic.GetPin('C', 1);
public static Cpu.Pin P24 = (Cpu.Pin)Generic.GetPin('C', 2);
public static Cpu.Pin P25 = (Cpu.Pin)Generic.GetPin('C', 3);
public static Cpu.Pin P26 = (Cpu.Pin)Generic.GetPin('A', 0);
public static Cpu.Pin P27 = (Cpu.Pin)Generic.GetPin('A', 1);
public static Cpu.Pin P28 = (Cpu.Pin)Generic.GetPin('A', 2);
public static Cpu.Pin P29 = (Cpu.Pin)Generic.GetPin('A', 3);
public static Cpu.Pin P30 = (Cpu.Pin)Generic.GetPin('A', 4);
public static Cpu.Pin P31 = (Cpu.Pin)Generic.GetPin('A', 5);
public static Cpu.Pin P32 = (Cpu.Pin)Generic.GetPin('B', 10);
public static Cpu.Pin P33 = (Cpu.Pin)Generic.GetPin('B', 11);
public static Cpu.Pin P34 = (Cpu.Pin)Generic.GetPin('B', 14);
public static Cpu.Pin P35 = (Cpu.Pin)Generic.GetPin('B', 15);
public static Cpu.Pin P36 = (Cpu.Pin)Generic.GetPin('C', 6);
public static Cpu.Pin P37 = (Cpu.Pin)Generic.GetPin('C', 7);
public static Cpu.Pin P38 = (Cpu.Pin)Generic.GetPin('C', 8);
public static Cpu.Pin P39 = (Cpu.Pin)Generic.GetPin('C', 9);
#elif V426
public static Cpu.Pin P03 = (Cpu.Pin)Cerb.Pin.PA8;
public static Cpu.Pin P04 = (Cpu.Pin)Cerb.Pin.PA13;
public static Cpu.Pin P05 = (Cpu.Pin)Cerb.Pin.PA7;
public static Cpu.Pin P06 = (Cpu.Pin)Cerb.Pin.PA6;
public static Cpu.Pin P07 = (Cpu.Pin)Cerb.Pin.PC10;
public static Cpu.Pin P08 = (Cpu.Pin)Cerb.Pin.PA14;
public static Cpu.Pin P09 = (Cpu.Pin)Cerb.Pin.PC11;
public static Cpu.Pin P10 = (Cpu.Pin)Cerb.Pin.PB4;
public static Cpu.Pin P11 = (Cpu.Pin)Cerb.Pin.PB9;
public static Cpu.Pin P12 = (Cpu.Pin)Cerb.Pin.PB3;
public static Cpu.Pin P13 = (Cpu.Pin)Cerb.Pin.PD2;
public static Cpu.Pin P14 = (Cpu.Pin)Cerb.Pin.PC12;
public static Cpu.Pin P16 = (Cpu.Pin)Cerb.Pin.PB8;
public static Cpu.Pin P18 = (Cpu.Pin)Cerb.Pin.PB7;
public static Cpu.Pin P19 = (Cpu.Pin)Cerb.Pin.PB6;
public static Cpu.Pin P20 = (Cpu.Pin)Cerb.Pin.PB5;
public static Cpu.Pin P22 = (Cpu.Pin)Cerb.Pin.PC0;
public static Cpu.Pin P23 = (Cpu.Pin)Cerb.Pin.PC1;
public static Cpu.Pin P24 = (Cpu.Pin)Cerb.Pin.PC2;
public static Cpu.Pin P25 = (Cpu.Pin)Cerb.Pin.PC3;
public static Cpu.Pin P26 = (Cpu.Pin)Cerb.Pin.PA0;
public static Cpu.Pin P27 = (Cpu.Pin)Cerb.Pin.PA1;
public static Cpu.Pin P28 = (Cpu.Pin)Cerb.Pin.PA2;
public static Cpu.Pin P29 = (Cpu.Pin)Cerb.Pin.PA3;
public static Cpu.Pin P30 = (Cpu.Pin)Cerb.Pin.PA4;
public static Cpu.Pin P31 = (Cpu.Pin)Cerb.Pin.PA5;
public static Cpu.Pin P32 = (Cpu.Pin)Cerb.Pin.PB10;
public static Cpu.Pin P33 = (Cpu.Pin)Cerb.Pin.PB11;
public static Cpu.Pin P34 = (Cpu.Pin)Cerb.Pin.PB14;
public static Cpu.Pin P35 = (Cpu.Pin)Cerb.Pin.PB15;
public static Cpu.Pin P36 = (Cpu.Pin)Cerb.Pin.PC6;
public static Cpu.Pin P37 = (Cpu.Pin)Cerb.Pin.PC7;
public static Cpu.Pin P38 = (Cpu.Pin)Cerb.Pin.PC8;
public static Cpu.Pin P39 = (Cpu.Pin)Cerb.Pin.PC9;
#endif
// 重大エラーが起きた時にP39のLEDを光らせる
public static void PanicLED()
{
DigitalOut Panic = new DigitalOut(CPU.P39, false);
for (int i = 0; i < 7; i++)
{
Panic.Write(true);
Thread.Sleep(50);
Panic.Write(false);
Thread.Sleep(50);
}
Panic.Dispose();
}
#elif SecretLabs
public static Cpu.Pin P0 = (Cpu.Pin)Pins.GPIO_PIN_5;
public static Cpu.Pin P1 = (Cpu.Pin)Pins.GPIO_PIN_6;
public static Cpu.Pin P2 = (Cpu.Pin)Pins.GPIO_PIN_7;
public static Cpu.Pin P3 = (Cpu.Pin)Pins.GPIO_PIN_8;
public static Cpu.Pin P4 = (Cpu.Pin)Pins.GPIO_PIN_9;
public static Cpu.Pin P5 = (Cpu.Pin)Pins.GPIO_PIN_10;
public static Cpu.Pin P6 = (Cpu.Pin)Pins.GPIO_PIN_11;
public static Cpu.Pin P7 = (Cpu.Pin)Pins.GPIO_PIN_12;
public static Cpu.Pin P8 = (Cpu.Pin)Pins.GPIO_PIN_13;
public static Cpu.Pin P9 = (Cpu.Pin)Pins.GPIO_PIN_14;
public static Cpu.Pin P10 = (Cpu.Pin)Pins.GPIO_PIN_15;
public static Cpu.Pin P11 = (Cpu.Pin)Pins.GPIO_PIN_16;
public static Cpu.Pin P12 = (Cpu.Pin)Pins.GPIO_PIN_17;
public static Cpu.Pin P13 = (Cpu.Pin)Pins.GPIO_PIN_18;
public static Cpu.Pin P14 = (Cpu.Pin)Pins.GPIO_PIN_19;
public static Cpu.Pin P15 = (Cpu.Pin)Pins.GPIO_PIN_20;
#endif
public static void delay(int n)
{
Thread.Sleep(n);
}
public static void wait_ms(int n)
{
Thread.Sleep(n);
}
public static void wait(double n)
{
Thread.Sleep((int)(n * 1000));
}
static inArray[] Din = new inArray[4];// 注意
static int usePin = 0;
public static void setInput(Cpu.Pin pin)
{
InputPort port = new InputPort(pin, false, Port.ResistorMode.PullUp);
Din[usePin].inp = port;
Din[usePin++].pin = pin;
}
public static bool readPin(Cpu.Pin pin)
{
int i;
for (i = 0; i < usePin; i++)
{
if (Din[i].pin == pin) break;
}
if (i == usePin) return false;// exception
return Din[i].inp.Read();
}
static outArray[] Dout = new outArray[4];// 注意
static int usePout = 0;
public static void setOutput(Cpu.Pin pin)
{
OutputPort port = new OutputPort(pin, false);
Dout[usePout].outp = port;
Dout[usePout++].pin = pin;
}
public static bool writePin(Cpu.Pin pin, bool val)
{
int i;
for (i = 0; i < usePout; i++)
{
if (Dout[i].pin == pin) break;
}
if (i == usePout) return false;// exception
Dout[i].outp.Write(val);
return true;
}
public static void Dispose(Cpu.Pin pin)
{
int i;
for (i = 0; i < usePout; i++)
{
if (Dout[i].pin == pin)
{
Dout[i].outp.Dispose();
usePout--;
return;
}
}
for (i = 0; i < usePin; i++)
{
if (Din[i].pin == pin)
{
Din[i].inp.Dispose();
usePin--;
return;
}
}
return;
}
public static readonly int SHIFT_MSB = -1;
public static readonly int SHIFT_LSB = -2;
public static readonly int PRE_CLOCK_MSB = 0;
public static readonly int POST_CLOCK_MSB = 1;
public static readonly int PRE_CLOCK_LSB = 2;
public static readonly int POST_CLOCK_LSB = 3;
// 最大16bit出力
public static void shiftOut(Cpu.Pin dataPort, Cpu.Pin clockPort, int bitCount, int mode, ushort data)
{
ushort Mbit = 1;
CPU.Dispose(dataPort);
CPU.setOutput(dataPort); // 直前に入力したかもしれないから
if (bitCount <= 0 || 16 < bitCount) return;
if (mode == SHIFT_MSB)
{
Mbit <<= bitCount - 1;
for (int i = 0; i < bitCount; i++, data <<= 1)
{
if ((data & Mbit) != 0) CPU.writePin(dataPort, true);
else CPU.writePin(dataPort, false);
Timer.delay(10);
CPU.writePin(clockPort, true); // clock high
Timer.delay(10);
CPU.writePin(clockPort, false);
Timer.delay(10);
}
}
else if (mode == SHIFT_LSB)
{
for (int i = 0; i < bitCount; i++, data >>= 1)
{
if ((data & 1) != 0) CPU.writePin(dataPort, true);
else CPU.writePin(dataPort, false);
Timer.delay(10);
CPU.writePin(clockPort, true); // clock high
Timer.delay(10);
CPU.writePin(clockPort, false);
Timer.delay(10);
}
}
}
public static int shiftIn(Cpu.Pin dataPort, Cpu.Pin clockPort, int bitCount, int mode)
{
int data = 0;
CPU.Dispose(dataPort);
CPU.setInput(dataPort);
if (bitCount <= 0 || 256 < bitCount) return 0;
Timer.delay(10);
switch (mode)
{
case 0: // PRE_CLOCK_MSB
for (int i = 0; i < bitCount; i++)
{
CPU.writePin(clockPort, true); // clock high
Timer.delay(10);
data <<= 1;
if (CPU.readPin(dataPort)) data |= 1;
Timer.delay(10);
CPU.writePin(clockPort, false);
Timer.delay(10);
}
CPU.Dispose(dataPort);
CPU.setOutput(dataPort);
break;
case 1: // POST_CLOCK_MSB
CPU.writePin(clockPort, false);
Timer.delay(10);
for (int i = 0; i < bitCount; i++)
{
CPU.writePin(clockPort, true); // clock high
Timer.delay(20);
CPU.writePin(clockPort, false);
Timer.delay(20);
data <<= 1;
if (CPU.readPin(dataPort)) data |= 1;
}
break;
case 2: // PEW_CLOCK_LSB
for (int i = 0; i < bitCount; i++)
{
CPU.writePin(clockPort, true); // clock high
Timer.delay(20);
CPU.writePin(clockPort, false);
Timer.delay(20);
data >>= 1;
if (CPU.readPin(dataPort)) data |= 0x8000;
}
break;
case 3: // POST_CLOCK_LSB
CPU.writePin(clockPort, false);
Timer.delay(10);
for (int i = 0; i < bitCount; i++)
{
CPU.writePin(clockPort, true); // clock high
Timer.delay(20);
CPU.writePin(clockPort, false);
Timer.delay(20);
data >>= 1;
if (CPU.readPin(dataPort)) data |= 0x8000;
}
break;
} // switch
return data & 0xffff;
}
}
struct inArray
{
public InputPort inp;
public Cpu.Pin pin;
}
struct outArray
{
public OutputPort outp;
public Cpu.Pin pin;
}
public class ServoMotor : PWM
{
Cpu.Pin port;
// Microsoft.SPOT.Hardware.PWM pwm;
uint period, high;
private static Microsoft.SPOT.Hardware.InterruptPort _interruptPort;
public static bool stopState = false; // 全てのサーボモータを止める
public ServoMotor(Cpu.PWMChannel pin)
: this(pin, 20000)
{
}
public ServoMotor(Cpu.Pin pin)
: this(ConvertToPin(pin), 20000)
{
port = pin;
}
public ServoMotor(Cpu.PWMChannel pin, uint period)
: base(pin, period, 0, PWM.ScaleFactor.Microseconds, false)
{
this.period = period;
}
static public Cpu.PWMChannel ConvertToPin(Cpu.Pin pin)
{
#if GHI
Cpu.Pin[] PWMpin = { CPU.P36, CPU.P05, CPU.P37, CPU.P03 };
#elif SecretLabs
Cpu.Pin[] PWMpin = { CPU.P12, CPU.P13, CPU.P14, CPU.P15 };
#endif
Cpu.PWMChannel num = 0;
for (int i = 0; i < PWMpin.Length; i++)
{
if (PWMpin[i] == pin)
{
num = (Cpu.PWMChannel)i;
break;
}
}
return num;
}
static public void setInterrupt(Cpu.Pin pin)
{
_interruptPort = new InterruptPort(pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
_interruptPort.OnInterrupt += new NativeEventHandler(button_OnInterrupt);
stopState = false;
}
public void SetSpeed(uint highTime)
{
this.Duration = highTime; // 周期:20ms
}
public void SetSpeed(double highTime)
{
this.Duration = (uint)(period * highTime); // 周期:20ms
}
public void move(int speed, int forward, int center, int backward)
{
uint t = (uint)((((speed > 0) ? forward : backward) * speed) / 100 + center);
}
public void update(uint hightime, uint period)
{
this.period = period;
this.Period = period;
high = hightime;
this.Duration = hightime;
}
public void update(uint hightime)
{
high = hightime;
this.Duration = hightime;
}
public void Start0()
{
this.Start();
}
public void Stop0()
{
this.Stop();
}
public void Dispose0()
{
this.Dispose(); // Disposeすると何故かHighになるため
}
static void button_OnInterrupt(uint data1, uint data2, DateTime time)
{
stopState = true;
}
}
public class ADcon : IDisposable
{
AnalogInput adc;
public int Value
{
get
{
return adc.ReadRaw();
}
}
public void Dispose()
{
adc.Dispose();
}
public ADcon(Cpu.AnalogChannel pin)
{
adc = new AnalogInput(pin);
}
public ADcon(Cpu.Pin pin)
{
Cpu.AnalogChannel port = Cpu.AnalogChannel.ANALOG_0;
#if GHI
Cpu.Pin[] ADpin = { CPU.P06, CPU.P28, CPU.P29, CPU.P22, CPU.P23, CPU.P30, CPU.P24, CPU.P25 };
#elif SecretLabs
Cpu.Pin[] ADpin = { CPU.P0, CPU.P1, CPU.P2, CPU.P3 };
#endif
for (int i = 0; i < ADpin.Length; i++)
{
if (ADpin[i] == pin)
{
port = (Cpu.AnalogChannel)i;
break;
}
}
adc = new AnalogInput(port);
}
public int GetValue()
{
return adc.ReadRaw();
}
public double Read()
{
return adc.Read();
}
}
/// <summary>
/// 単位は0.1μ秒となる
/// </summary>
public class Timer
{
DateTime markTime;
public Timer()
{
mark();
}
public void mark()
{
markTime = DateTime.Now;
}
public bool timeout(int hi, int lo)
{
if (hi <= (DateTime.Now - markTime).Ticks) return true;
return false;
}
public bool timeout(int hi)
{
if (hi <= (DateTime.Now - markTime).Ticks) return true;
return false;
}
public bool timeout(long hi)
{
if (hi <= (DateTime.Now - markTime).Ticks) return true;
return false;
}
public static void delay(int tm)
{
DateTime markTime = DateTime.Now;
while ((DateTime.Now - markTime).Ticks <= tm) ;
}
public long Time
{
get
{
return (DateTime.Now - markTime).Ticks;
}
}
}
// OutputPortに<<演算子オーバーライドさせ
// LED <<= 1、LED <<= 0 でON/OFFの記述ができるようにする
public class DigitalOut : OutputPort
{
public bool Value
{
get
{
return Read();
}
set
{
Write(value);
}
}
public DigitalOut(Cpu.Pin pin)
: base(pin, false)
{ }
public DigitalOut(Cpu.Pin pin, bool state)
: base(pin, state)
{ }
// glitchFilter: true if the glitch filter is enabled; otherwise, false.
// resistor : The resistor mode that establishes a default state for the output port.
public DigitalOut(Cpu.Pin pin, bool state,
bool glitchFilter, ResistorMode resistor)
: base(pin, state, glitchFilter, resistor)
{ }
public static DigitalOut operator <<(DigitalOut obj, int state)
{
obj.Write(state == 0 ? false : true);
return obj;
}
}
public class DigitalIn : InputPort
{
public bool Value
{
get
{
return Read();
}
}
public DigitalIn(Cpu.Pin pin)
: base(pin, true, Port.ResistorMode.PullUp)
{ }
public DigitalIn(Cpu.Pin pin, bool glitchFilter)
: base(pin, glitchFilter, Port.ResistorMode.PullUp)
{ }
}
}
|
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
100
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|