-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
101 lines (92 loc) · 1.96 KB
/
main.js
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
import Cocoro from "./cocoro.js";
class CocoroKit {
static get CONST() {
return {
SPIN: {
RIGHT: {
HIGH_PIN: 1,
LOW_PIN: 2,
},
LEFT: {
HIGH_PIN: 4,
LOW_PIN: 3,
},
},
REVERSE: {
RIGHT: {
HIGH_PIN: 2,
LOW_PIN: 1,
},
LEFT: {
HIGH_PIN: 3,
LOW_PIN: 4,
},
},
STOP: {
RIGHT: [1, 2],
LEFT: [3, 4],
},
COLOR: {
RED: "5",
GREEN: "6",
BLUE: "7",
},
RESET: {
ALL: "all",
MOTOR: "motor",
LED: "led",
},
};
}
constructor(onConnected) {
this._cocoro = new Cocoro(onConnected);
}
async connect() {
await this._cocoro.connect("cocorokit").catch((error) => {
console.log(error);
});
}
/**
*
* @param {*} motorSet Cocorokit.CONST.SPIN.RIGHT|LEFT
* @param {*} ratio 0 - 100
*/
async spin(motorSet, ratio) {
await this._cocoro.setPwmRatio(motorSet.HIGH_PIN, ratio);
await this._cocoro.setPwmRatio(motorSet.LOW_PIN, 0);
}
/**
*
* @param {*} motorSet Cocorokit.CONST.REVERSE.RIGHT|LEFT
* @param {*} ratio 0 - 100
*/
async reverse(motorSet, ratio) {
await this._cocoro.setPwmRatio(motorSet.HIGH_PIN, ratio);
await this._cocoro.setPwmRatio(motorSet.LOW_PIN, 0);
}
/**
*
* @param {*} motorSet Cocorokit.CONST.STOP.RIGHT|LEFT
*/
async stop(motorSet) {
await this._cocoro.setPwmRatio(motorSet[0], 0);
await this._cocoro.setPwmRatio(motorSet[1], 0);
}
/**
*
* @param {*} color Cocorokit.CONST.COLOR.RED|GREEN|BLUE
* @param {*} ratio 0 - 100
*/
async color(color, ratio) {
await this._cocoro.setPwmRatio(color, ratio);
}
/**
*
* @param {*} type Cocorokit.CONST.RESET.ALL|MOTOR|LED
*/
async reset(type) {
await this._cocoro.reset(type);
}
}
module.exports = CocoroKit;
module.exports.default = CocoroKit;