#define V425
//#define V424
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.OSHW.Hardware; // FEZCerberus使用のため追加、参照設定にも追加
using System.IO.Ports; // Microsoft.SPOT.Hardware.SerialPort
using System.Text; // Encoding.UTF8.GetBytes(str);
#if V425
using Cerb = GHI.Hardware.FEZCerb;
#elif V424
using Cerb = GHI.OSHW.Hardware.FEZCerberus;
#endif
// Microsoft.SPOT.Hardware.PWMを参照設定に追加する
namespace BoeBotLib
{
public class CPU
{
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;
// 重大エラーが起きた時にP39のLEDを光らせる
public static void PanicLED()
{
DigitalOut Panic = new DigitalOut(Cerb.Pin.PC9, false);
for (int i = 0; i < 7; i++)
{
Panic.Write(true);
Thread.Sleep(50);
Panic.Write(false);
Thread.Sleep(50);
}
Panic.Dispose();
}
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;
}
}
struct inArray
{
public InputPort inp;
public Cpu.Pin pin;
}
struct outArray
{
public OutputPort outp;
public Cpu.Pin pin;
}
public class ServoMotor
{
Cpu.PWMChannel port;
PWM pwm;
uint period, high;
private static Microsoft.SPOT.Hardware.InterruptPort _interruptPort;
public static bool stopState = false;
public uint Dulation
{
get { return high; }
set
{
high = value;
pwm.Duration = value;
}
}
public double DutyCycle
{
get { return pwm.DutyCycle; }
set
{
pwm.DutyCycle = value;
}
}
public ServoMotor(Cpu.PWMChannel pin)
{
pwm = new PWM(pin, 20000, 1500, PWM.ScaleFactor.Microseconds, false);
}
public ServoMotor(Cpu.Pin pin)
{
Cpu.Pin[] PWMpin = { CPU.P36, CPU.P05, CPU.P37, CPU.P03 };
for (int i = 0; i < PWMpin.Length; i++)
{
if (PWMpin[i] == pin)
{
port = (Cpu.PWMChannel)i;
break;
}
}
pwm = new PWM(port, 20000, 1500, PWM.ScaleFactor.Microseconds, false);
}
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)
{
pwm.Duration = highTime; // 周期:20ms
}
public void SetSpeed(double highTime)
{
pwm.Duration = (uint)(20000 * 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 lowTime)
{
period = highTime + lowTime;
high = highTime;
pwm.Duration = highTime;
}
public void Start()
{
pwm.Start();
}
public void Stop()
{
pwm.Stop();
}
public void Dispose()
{
pwm.Dispose();
}
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;
Cpu.Pin[] ADpin = { CPU.P06, CPU.P28, CPU.P29, CPU.P22, CPU.P23, CPU.P30, CPU.P24, CPU.P25 };
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();
}
}
///
/// 単位は0.1μ秒となる
///
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, false, 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
|