-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
146 lines (129 loc) · 3.47 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
/**
* Use this file to define custom functions and blocks.
* Read more at https://makecode.microbit.org/blocks/custom
*/
/**
* Kontrola ekrana
*/
//% weight=70 icon="\uf108" color=#EC7505
namespace ekran {
/**
* Obriši ekran
*/
//% blockId=obrisi block="obriši ekran"
export function obrisi(): void {
serial.writeString("CLS\n");
}
/**
* Ispiši
*/
//% blockId=ispisi
//% block="ispiši tekst %tekst| na poziciji x %x| y %y"
//% blockExternalInputs=true
export function ispisi(tekst: string, x: number, y: number): void {
serial.writeString(tekst + ";" + x.toString() + ";" + y.toString() + "\n");
}
}
enum KojiMotor {
//% block="motor 1"
motor01 = 0,
//% block="motor 2"
motor02 = 1,
//% block="motor 3"
motor03 = 2,
//% block="motor 4"
motor04 = 3
}
enum KojeStanje {
//% block="stop"
stop,
//% block="smjer A"
smjer_a,
//% block="smjer B"
smjer_b
}
/**
* Custom blocks
*/
//% weight=100 color=#0fbc11 icon=""
namespace motor {
//% weight=100
//% blockId=motor_conv
//% block="%kojeg"
export function motor_conv(kojeg: KojiMotor): number {
return <number>kojeg;
}
//% weight=99
//% blockId=stanje_conv
//% block="%koje"
export function stanje_conv(koje: KojeStanje): string {
switch (koje) {
case KojeStanje.stop: return "stop";
case KojeStanje.smjer_a: return "smjer_a";
case KojeStanje.smjer_b: return "smjer_b";
default: return null;
}
}
let motorPinMapa = [
[DigitalPin.P8, DigitalPin.P9],
[DigitalPin.P10, DigitalPin.P12],
[DigitalPin.P13, DigitalPin.P14],
[DigitalPin.P15, DigitalPin.P16]
];
/**
* Promjeni stanje motora
*/
//% weight=98
//% blockId=promjeni_stanje
//% block="promjeni stanje| motora %broj_motora=motor_conv| u %stanje=stanje_conv"
//% blockExternalInputs=true
export function promjeni_stanje(broj_motora: number, stanje: string): void {
let p = motorPinMapa[broj_motora];
switch (stanje) {
case "smjer_a":
pins.digitalWritePin(p[0], 1);
pins.digitalWritePin(p[1], 0);
break;
case "smjer_b":
pins.digitalWritePin(p[0], 0);
pins.digitalWritePin(p[1], 1);
break;
case "stop":
pins.digitalWritePin(p[0], 0);
pins.digitalWritePin(p[1], 0);
break;
default:
pins.digitalWritePin(p[0], 0);
pins.digitalWritePin(p[1], 0);
break;
}
}
//%
/**
* Stopiraj motor
*/
//% weight=97
//% blockId=motor_stop
//% block="stopiraj motor %broj_motora=motor_conv"
export function stop(broj_motora: number): void {
promjeni_stanje(broj_motora, "stop");
}
/**
* Vrti motor u smjeru A
*/
//% weight=96
//% blockId=motor_vrtiA
//% block="vrti motor %broj_motora=motor_conv| u smjeru A"
export function smjerA(broj_motora: number): void {
promjeni_stanje(broj_motora, "smjer_a");
}
/**
* Vrti motor u smjeru B
*/
//% weight=95
//% blockId=motor_vrtiB
//% block="vrti motor %broj_motora=motor_conv| u smjeru B"
export function smjerB(broj_motora: number): void {
promjeni_stanje(broj_motora, "smjer_b");
}
}