-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathDFPlayer.js
More file actions
122 lines (108 loc) · 2.78 KB
/
DFPlayer.js
File metadata and controls
122 lines (108 loc) · 2.78 KB
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
+(function (global, factory) {
if (typeof exports === 'undefined') {
factory(global.webduino || {});
} else {
module.exports = factory;
}
}(this, function (scope) {
'use strict';
var self;
var proto;
var sendArray = [];
var sending = false;
var sendAck = '';
var sendCallback;
var Module = scope.Module;
var BoardEvent = scope.BoardEvent;
var sendAndAckCount = 0;
var waitAckAndSend = [];
var _play;
function DFPlayer(board, RX, TX) {
Module.call(this);
this._board = board;
this._rx = RX;
this._tx = TX;
self = this;
board.on(BoardEvent.SYSEX_MESSAGE, function () {
sendAndAckCount--;
sending = false;
if (waitAckAndSend.length > 0) {
var cmd = waitAckAndSend.shift();
self._board.send(cmd);
}
});
startQueue(board);
}
DFPlayer.prototype = proto = Object.create(Module.prototype, {
constructor: {
value: DFPlayer
},
play: {
get: function () {
return _play;
},
set: function (val) {
_play = val;
}
}
});
proto.init = function () {
var cmd = [0xF0, 0x04, 0x19, 0x0 /*init*/ , this._rx, this._tx, 0xF7];
sendAndAckCount++;
this._board.send(cmd);
};
proto.play = function (num) {
var cmd = [0xF0, 0x04, 0x19, 0x01, num, 0xF7];
sendAndAckCount++;
waitAckAndSend.push(cmd);
};
proto.start = function () {
sendAndAckCount++;
waitAckAndSend.push([0xF0, 0x04, 0x19, 0x02 /*Start*/ , 0xF7]);
};
proto.stop = function () {
sendAndAckCount++;
waitAckAndSend.push([0xF0, 0x04, 0x19, 0x03 /*Stop*/ , 0xF7]);
};
proto.pause = function () {
sendAndAckCount++;
waitAckAndSend.push([0xF0, 0x04, 0x19, 0x04 /*Pause*/ , 0xF7]);
};
proto.volume = function (volume) {
sendAndAckCount++;
waitAckAndSend.push([0xF0, 0x04, 0x19, 0x05, volume, 0xF7]);
};
proto.previous = function () {
sendAndAckCount++;
waitAckAndSend.push([0xF0, 0x04, 0x19, 0x06 /*Previous*/ , 0xF7]);
};
proto.next = function () {
sendAndAckCount++;
waitAckAndSend.push([0xF0, 0x04, 0x19, 0x07 /*Next*/ , 0xF7]);
};
proto.loop = function (num) {
sendAndAckCount++;
waitAckAndSend.push([0xF0, 0x04, 0x19, 0x08, num, 0xF7]);
};
function startQueue(board) {
setInterval(function () {
if (sendAndAckCount == waitAckAndSend.length && waitAckAndSend.length > 0) {
var cmd = waitAckAndSend.shift();
board.send(cmd);
}
if (sending || sendArray.length == 0) {
return;
}
sending = true;
var sendObj = sendArray.shift();
sendAck = sendObj.ack;
if (sendAck > 0) {
board.send(sendObj.obj);
} else {
sending = false;
sendCallback();
}
}, 0);
}
scope.module.DFPlayer = DFPlayer;
}));