forked from Hiwonder/4LineFollowSensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
87 lines (81 loc) · 2.49 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
/*
four_line_follow package
*/
//% weight=10 icon="\uf013" color=#2896ff
namespace four_line_follow {
export enum LineFollowerSensors {
//% block="S1"
S1,
//% block="S2"
S2,
//% block="S3"
S3,
//% block="S4"
S4
}
export enum LineColor {
//% block="Black"
Black,
//% block="White"
White
}
const LINE_FOLLOWER_I2C_ADDR = 0x78
//% weight=96 blockId=line_followers block="4 Line follower %lineFollowerSensor at port 4 in %lineColor ?"
//% inlineInputMode=inline
export function line_followers(lineFollowerSensor: LineFollowerSensors, lineColor: LineColor): boolean {
pins.i2cWriteNumber(LINE_FOLLOWER_I2C_ADDR, 1, NumberFormat.UInt8BE);
let data = pins.i2cReadNumber(LINE_FOLLOWER_I2C_ADDR, NumberFormat.UInt8BE);
let status = false;
switch (lineFollowerSensor) {
case LineFollowerSensors.S1:
if (data & 0x01) {
if (lineColor == LineColor.Black) {
status = true;
}
}
else {
if (lineColor == LineColor.White) {
status = true;
}
}
break;
case LineFollowerSensors.S2:
if (data & 0x02) {
if (lineColor == LineColor.Black) {
status = true;
}
}
else {
if (lineColor == LineColor.White) {
status = true;
}
}
break;
case LineFollowerSensors.S3:
if (data & 0x04) {
if (lineColor == LineColor.Black) {
status = true;
}
}
else {
if (lineColor == LineColor.White) {
status = true;
}
}
break;
case LineFollowerSensors.S4:
if (data & 0x08) {
if (lineColor == LineColor.Black) {
status = true;
}
}
else {
if (lineColor == LineColor.White) {
status = true;
}
}
break;
}
return status;
}
}