-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
420 lines (397 loc) · 13.8 KB
/
main.ts
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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
enum eureka_IO {
Aポート,
Bポート,
Cポート
}
enum eureka_denki {
Aポート,
Bポート
}
enum eureka_tlp {
Aポート,
Bポート
}
enum eureka_p1416 {
Aポート,
Bポート
}
enum onoff {
ON,
OFF
}
enum color {
RED,
YELLOW,
BLUE
}
enum etc {
AKARUSA,
JINKAN
}
enum L9110moter {
seiten,
gyakuten,
seisi
}
//% color="#74ad1d" block="ユーレカIO"
namespace eureka_blocks {
//% shim=DS18B20::Temperature
//% group="4_センサの値"
export function Temperature(p: number): number {
// Fake function for simulator
return 0;
}
/*
//% color="#ff7b00" weight=7 blockId="Temperature_string"
//% block="温度センサDS(文字返し) |%p|"
//% p.fieldEditor="gridpicker" p.fieldOptions.columns=4
//% group="4_センサの値"
export function TemperatureString(p: eureka_IO): string {
let temp = Temperature(p);
let x = Math.round((temp / 100))
let y = Math.round((temp % 100))
let z = ''
if (temp >= 0) {
z = x.toString()
}
else if (temp < 0) {
z = '-' + (-x).toString()
}
return z
}
*/
//% weight=10 blockId="Temperature_number"
//% block="温度センサDS |%p|"
//% p.fieldEditor="gridpicker" p.fieldOptions.columns=4
//% group="4_センサの値"
export function TemperatureNumber(p: eureka_IO): number {
let temp = Temperature(p);
let x = Math.round(temp / 100);
return x;
}
//% color="#4741f1" weight=54 blockId=eureka_tl_blue block="青信号機 点灯|%mode| |%pin|" group="1_信号機ユニット"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=10
export function eureka_tl_blue(mode: onoff, pin: eureka_tlp) {
switch (pin) {
case eureka_tlp.Aポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P14, 1);
} else {
return pins.digitalWritePin(DigitalPin.P14, 0);
}
case eureka_tlp.Bポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P16, 1);
} else {
return pins.digitalWritePin(DigitalPin.P16, 0);
}
}
}
//% color="#ffa800" weight=53 blockId=eureka_tl_yellow block="黄信号機 点灯|%mode| |%pin|" group="1_信号機ユニット"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
export function eureka_tl_yellow(mode: onoff, pin: eureka_tlp) {
switch (pin) {
case eureka_tlp.Aポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P13, 1);
} else {
return pins.digitalWritePin(DigitalPin.P13, 0);
}
case eureka_tlp.Bポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P15, 1);
} else {
return pins.digitalWritePin(DigitalPin.P15, 0);
}
}
}
//% color="#ff4940" weight=52 blockId=eureka_tl_red block="赤信号機 点灯|%mode| |%pin|" group="1_信号機ユニット"
//% pin.fieldEditor="gridpicker" pin.fieldOptions.columns=4
export function eureka_tl_red(mode: onoff, pin: eureka_tlp) {
switch (pin) {
case eureka_tlp.Aポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P0, 1);
} else {
return pins.digitalWritePin(DigitalPin.P0, 0);
}
case eureka_tlp.Bポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P1, 1);
} else {
return pins.digitalWritePin(DigitalPin.P1, 0);
}
}
}
//% color="#1E90FF" weight=51 block="待ち時間(秒)|%second|" group="1_信号機ユニット"
export function driveForwards(second: number): void {
basic.pause(second * 1000);
}
//% color="#006B8F" weight=27 block="光センサ値が |%limit| より暗い |%tlp|" group="2_電気の利用ユニット"
//% limit.min=0 limit.max=100
export function decideLight(limit: number, tlp: eureka_tlp): boolean {
switch (tlp) {
case eureka_tlp.Aポート:
if ((pins.analogReadPin(AnalogPin.P0) / 1023) * 100 < limit) {
return true;
} else {
return false;
}
break;
case eureka_tlp.Bポート:
if ((pins.analogReadPin(AnalogPin.P1) / 1023) * 100 < limit) {
return true;
} else {
return false;
}
break;
}
}
//% color="#006B8F" weight=26 blockId=eureka_denkitemp block="光センサ値 |%pin|" group="2_電気の利用ユニット"
export function eureka_denkitemp(pin: eureka_denki): number {
switch (pin) {
case eureka_denki.Aポート:
return Math.round((pins.analogReadPin(AnalogPin.P0) / 1023) * 100);
case eureka_denki.Bポート:
return Math.round((pins.analogReadPin(AnalogPin.P1) / 1023) * 100);
}
}
//% color="#009A00" weight=25 block="人が動いたら |%pin|" group="2_電気の利用ユニット"
export function humanDetection(pin: eureka_p1416): boolean {
pins.setPull(DigitalPin.P14, PinPullMode.PullNone);
pins.setPull(DigitalPin.P16, PinPullMode.PullNone);
switch (pin) {
case eureka_p1416.Aポート:
if (pins.digitalReadPin(DigitalPin.P14) == 1) {
return true;
} else {
return false;
}
break;
case eureka_p1416.Bポート:
if (pins.digitalReadPin(DigitalPin.P16) == 1) {
return true;
} else {
return false;
}
break;
}
}
//% color="#009A00" weight=24 blockId=eureka_denkihuman block="人感センサ値 |%pin|" group="2_電気の利用ユニット"
export function eureka_denkihuman(pin: eureka_denki): number {
switch (pin) {
case eureka_denki.Aポート:
pins.setPull(DigitalPin.P14, PinPullMode.PullNone);
return pins.digitalReadPin(DigitalPin.P14);
case eureka_denki.Bポート:
pins.setPull(DigitalPin.P16, PinPullMode.PullNone);
return pins.digitalReadPin(DigitalPin.P16);
}
}
//% color="#f5a142" weight=23 blockId=eureka_denkiwhite block="LED |%mode| |%pin|" group="2_電気の利用ユニット"
export function eureka_denkiwhite(mode: onoff, port: eureka_denki ) {
switch (port) {
case eureka_denki.Aポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P13, 1);
} else {
return pins.digitalWritePin(DigitalPin.P13, 0);
}
case eureka_denki.Bポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P15, 1);
} else {
return pins.digitalWritePin(DigitalPin.P15, 0);
}
}
}
//% color="#ff3d03" weight=19 blockId=eureka_buz_set block="ユーレカIOで音をならす" group="3_ユーレカ装置"
export function eureka_buz_set() {
pins.analogSetPitchPin(AnalogPin.P8);
}
//% color="#6041f1" weight=23 blockId=eureka_L9110 block="モーターL |%mode| |%pin|" group="3_ユーレカ装置"
//% mode.min=-100 mode.max=100
export function L9110driver(port: eureka_denki, mode: number) {
switch (port) {
case eureka_denki.Aポート:
if (mode > 0) {
pins.analogWritePin(AnalogPin.P0, Math.round(mode*10.23));
pins.digitalWritePin(DigitalPin.P13, 0);
}
if (mode < 0) {
pins.digitalWritePin(DigitalPin.P0, 0);
pins.analogWritePin(AnalogPin.P13, Math.round(-mode*10.23));}
if (mode == 0) {
pins.digitalWritePin(DigitalPin.P0, 0);
pins.digitalWritePin(DigitalPin.P13, 0);
}
break;
case eureka_denki.Bポート:
if (mode > 0) {
pins.analogWritePin(AnalogPin.P1, Math.round(mode*10.23));
pins.digitalWritePin(DigitalPin.P15, 0);
}
if (mode < 0) {
pins.digitalWritePin(DigitalPin.P1, 0);
pins.analogWritePin(AnalogPin.P15, Math.round(-mode*10.23));
}
if (mode == 0) {
pins.digitalWritePin(DigitalPin.P1, 0);
pins.digitalWritePin(DigitalPin.P15, 0);
}
break;
}
}
//% color="#525252" weight=18 blockId=eureka_relay block="単体_リレー |%mode||%pin|" group="3_ユーレカ装置"
export function eureka_relay(pin: eureka_IO, mode: onoff) {
switch (pin) {
case eureka_IO.Aポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P0, 1);
} else {
return pins.digitalWritePin(DigitalPin.P0, 0);
}
case eureka_IO.Bポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P1, 1);
} else {
return pins.digitalWritePin(DigitalPin.P1, 0);
}
case eureka_IO.Cポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P2, 1);
} else {
return pins.digitalWritePin(DigitalPin.P2, 0);
}
}
}
//% color="#40a6ff" weight=17 blockId=eureka_white block="単体_LED |%mode||%pin|" group="3_ユーレカ装置"
export function eureka_white(port: eureka_IO, mode: onoff) {
switch (port) {
case eureka_IO.Aポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P0, 1);
} else {
return pins.digitalWritePin(DigitalPin.P0, 0);
}
case eureka_IO.Bポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P1, 1);
} else {
return pins.digitalWritePin(DigitalPin.P1, 0);
}
case eureka_IO.Cポート:
if (mode == onoff.ON) {
return pins.digitalWritePin(DigitalPin.P2, 1);
} else {
return pins.digitalWritePin(DigitalPin.P2, 0);
}
}
}
//% color="#c3c900" weight=8 blockId=eureka_light block="単体_光センサ |%pin|" group="4_センサの値"
export function eureka_light(pin: eureka_IO): number {
switch (pin) {
case eureka_IO.Aポート:
return pins.analogReadPin(AnalogPin.P0);
case eureka_IO.Bポート:
return pins.analogReadPin(AnalogPin.P1);
case eureka_IO.Cポート:
return pins.analogReadPin(AnalogPin.P2);
}
}
//% color="#858585" weight=8 blockId=eureka_human block="単体_人感センサ |%pin|" group="4_センサの値"
export function eureka_human(pin: eureka_IO): number {
switch (pin) {
case eureka_IO.Aポート:
pins.setPull(DigitalPin.P0, PinPullMode.PullNone);
return pins.digitalReadPin(DigitalPin.P0);
case eureka_IO.Bポート:
pins.setPull(DigitalPin.P1, PinPullMode.PullNone);
return pins.digitalReadPin(DigitalPin.P1);
case eureka_IO.Cポート:
pins.setPull(DigitalPin.P2, PinPullMode.PullNone);
return pins.digitalReadPin(DigitalPin.P2);
}
}
//% color="#ff7b00" weight=9 blockId=eureka_temp block="温度センサMCP |%pin|" group="4_センサの値"
export function eureka_temp(pin: eureka_IO): number {
switch (pin) {
case eureka_IO.Aポート:
return Math.round(
((pins.analogReadPin(AnalogPin.P0) * 3250) / 1024 - 500) / 10
);
case eureka_IO.Bポート:
return Math.round(
((pins.analogReadPin(AnalogPin.P1) * 3250) / 1024 - 500) / 10
);
case eureka_IO.Cポート:
return Math.round(
((pins.analogReadPin(AnalogPin.P2) * 3250) / 1024 - 500) / 10
);
}
}
//% weight=6 blockId=sonar_ping block="単体_超音波距離センサ" group="4_センサの値"
export function ping() {
// send
pins.setPull(DigitalPin.P16, PinPullMode.PullNone);
pins.digitalWritePin(DigitalPin.P8, 0);
control.waitMicros(2);
pins.digitalWritePin(DigitalPin.P8, 1);
control.waitMicros(10);
pins.digitalWritePin(DigitalPin.P8, 0);
// read
const d = pins.pulseIn(DigitalPin.P16, PulseValue.High, 500 * 58);
return Math.idiv(d, 58);
}
//% color="#f071bd" weight=5 blockId=eureka_CdS block="単体_フォトリフレクター |%pin|" group="4_センサの値""
export function eureka_CdS(pin: eureka_IO): number {
switch (pin) {
case eureka_IO.Aポート:
return pins.analogReadPin(AnalogPin.P0);
case eureka_IO.Bポート:
return pins.analogReadPin(AnalogPin.P1);
case eureka_IO.Cポート:
return pins.analogReadPin(AnalogPin.P2);
}
}
}
//% color="#f5a142" block="ユーレカ車" icon="\uf1b9"
namespace eureka_blocks_car {
//% blockId=servos_forward
//% block="前に進む 出力調整 左へ |%le| 右へ" group="基本のうごき"
//% le.min=-30 le.max=30
export function forward(le: number): void {
pins.servoWritePin(AnalogPin.P14, 30 + le);
pins.servoWritePin(AnalogPin.P13, 150 + le);
}
//% blockId=servos_backward
//% block="うしろにすすむ" group="基本のうごき"
export function backward(): void {
pins.servoWritePin(AnalogPin.P13, 180);
pins.servoWritePin(AnalogPin.P14, 0);
}
//% blockId=servos_left
//% block="ひだりにまがる" group="基本のうごき"
export function left(): void {
pins.servoWritePin(AnalogPin.P13, 0);
pins.servoWritePin(AnalogPin.P14, 0);
}
//% blockId=servos_right
//% block="みぎにまがる" group="基本のうごき"
export function right(): void {
pins.servoWritePin(AnalogPin.P13, 180);
pins.servoWritePin(AnalogPin.P14, 180);
}
//% blockId=servos_stop
//% block="とまる" group="基本のうごき"
export function stop(): void {
pins.analogWritePin(AnalogPin.P13, 90);
pins.analogWritePin(AnalogPin.P14, 90);
}
//% blockId=servos_neutral
//% block="サーボをニュートラルにします" group="設定"
export function neutral(): void {
pins.digitalWritePin(DigitalPin.P13, 0);
pins.digitalWritePin(DigitalPin.P14, 0);
}
}