-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjfive-motor.html
101 lines (90 loc) · 3.07 KB
/
jfive-motor.html
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
<link rel="import" href="../polymer/polymer.html">
<dom-module id="jfive-motor">
<script>
class JFiveMotor {
beforeRegister() {
this.is = 'jfive-motor';
this.properties = {
pwmPin: {
type: String
},
dirPin: {
type: String
},
cdirPin: {
type: String
},
on: {
type: Boolean,
observer: 'onChanged'
},
speed: {
type: Number,
value: 255,
observer: 'speedChanged'
},
reverse: {
type: Boolean,
value: false,
observer: 'reverseChanged'
}
};
this.observers = [
'init(pwmPin, dirPin, cdirPin)'
];
this.jFive = require('johnny-five');
const Raspio = require('raspi-io');
try {
const board = new this.jFive.Board({
io: new Raspio(),
repl: false
});
}
catch(error) {
console.log(error);
}
}
init() {
try {
this.motor = new this.jFive.Motor({
pins: {
pwm: this.pwmPin,
dir: this.dirPin,
cdir: this.cdirPin
}
});
}
catch(error) {
console.log(error);
}
this.onChanged();
}
onChanged() {
//TODO look into using a multi-property observer to declare this.motor as a dependency. It might not work because we want this called very time the on property is changed
if (!this.motor) return;
if (this.on) {
this.refreshMotor();
}
else {
this.motor.stop();
}
}
speedChanged() {
if (this.on) {
this.refreshMotor();
}
}
reverseChanged() {
if (this.on) {
this.refreshMotor();
}
}
refreshMotor() {
//TODO look into using a multi-property observer to declare this.motor as a dependency. It might not work because we want this called very time the on property is changed
if (!this.motor) return;
this.reverse ? this.motor.reverse(this.speed) : this.motor.forward(this.speed);
}
}
Polymer(JFiveMotor);
</script>
</dom-module>