-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.ts
178 lines (161 loc) · 4.76 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* DFPlayer mini的函數
*/
//% weight=0 color=#b9a0e6 icon="\uf001" block="MP3 Player"
namespace dfplayer {
serial.onDataReceived("E", () => {
})
let Start_Byte = 0x7E
let Version_Byte = 0xFF
let Command_Length = 0x06
let End_Byte = 0xEF
let Acknowledge = 0x00
let CMD=0x00
let para1=0x00
let para2=0x00
let highByte=0x00
let lowByte=0x00
let MP3_tx=SerialPin.P1
let MP3_rx=SerialPin.P2
let dataArr: number[] = [Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, para1, para2, highByte, lowByte, End_Byte]
export enum playType {
//% block="Play"
type1 = 0x0D,
//% block="Stop"
type2 = 0x16,
//% block="PlayNext"
type3 = 0x01,
//% block="PlayPrevious"
type4 = 0x02,
//% block="Pause"
type5 = 0x0E
}
export enum yesOrNot {
//% block="no"
type1 = 0,
//% block="yes"
type2 = 1
}
/**
* @param pinRX to pinRX ,eg: SerialPin.P2
* @param pinTX to pinTX ,eg: SerialPin.P1
*/
//% blockId="MP3_setSerial" block="set DFPlayer mini RX to %pinTX| TX to %pinRX"
//% weight=100 blockGap=20
export function MP3_setSerial(pinTX: SerialPin, pinRX: SerialPin): void {
MP3_tx=pinTX;
MP3_rx=pinRX;
serial.redirect(
MP3_tx,
MP3_rx,
BaudRate.BaudRate9600
)
basic.pause(100)
}
//% blockId="MP3_serialReconnect" block="set DFPlayer reconnect to micro:bit serial"
//% weight=99 blockGap=20
export function MP3_serialReconnect(): void {
serial.redirect(
MP3_tx,
MP3_rx,
BaudRate.BaudRate9600
)
basic.pause(20)
}
function checkSum():void {
let total=0;
for(let i=1;i<7;i++){
total+=dataArr[i]
}
total=65536 - total
lowByte = total & 0xFF;
highByte = total >> 8;
dataArr[7]=highByte
dataArr[8]=lowByte
}
//% blockId="execute" block="execute procedure:%myType"
//% weight=90 blockExternalInputs=true blockGap=20
export function execute(myType: playType):void{
CMD=myType
para1=0x00
para2=0x00
dataArr[3]=CMD
dataArr[5] = para1
dataArr[6] = para2
checkSum()
sendData()
}
//% blockId="setTracking" block="play the mp3 on the track:%tracking|repeat:%myAns"
//% weight=85 blockGap=20 tracking.min=1 tracking.max=255
export function setTracking(tracking:number,myAns:yesOrNot):void{
CMD=0x03
para1=0x00
para2=tracking
dataArr[3]=CMD
dataArr[5] = para1
dataArr[6] = para2
checkSum()
sendData()
execute(0x0D)
if (myAns==1)
execute(0x19)
}
//% blockId="folderPlay" block="play the mp3 in the folder:%folderNum|filename:%fileNum|repeat:%myAns"
//% weight=80 blockGap=20 folderNum.min=1 folderNum.max=99 fileNum.min=1 fileNum.max=255
export function folderPlay(folderNum:number, fileNum:number,myAns:yesOrNot):void{
CMD=0x0F
para1=folderNum
para2=fileNum
dataArr[3]=CMD
dataArr[5] = para1
dataArr[6] = para2
checkSum()
sendData()
if (myAns==1)
execute(0x19)
}
//% blockId="setLoop" block="loop play all the MP3s in the SD card"
//% weight=75 blockGap=20
export function setLoop():void{
CMD=0x11
para1=0
para2=0x01
dataArr[3]=CMD
dataArr[5] = para1
dataArr[6] = para2
checkSum()
sendData()
}
//% blockId="setLoopFolder" block="loop play all the MP3s in the folder:%folderNum"
//% weight=73 blockGap=20 folderNum.min=1 folderNum.max=99
export function setLoopFolder(folderNum:number):void{
CMD=0x17
para1=0
para2=folderNum
dataArr[3]=CMD
dataArr[5] = para1
dataArr[6] = para2
checkSum()
sendData()
}
//% blockId="setVolume" block="set volume(0~30):%volume"
//% weight=70 blockGap=20 volume.min=0 volume.max=30
export function setVolume(volume:number):void{
CMD=0x06
para1=0
para2=volume
dataArr[3]=CMD
dataArr[5] = para1
dataArr[6] = para2
checkSum()
sendData()
}
function sendData():void{
let myBuff = pins.createBuffer(10);
for(let i=0;i<10;i++){
myBuff.setNumber(NumberFormat.UInt8BE, i, dataArr[i])
}
serial.writeBuffer(myBuff)
basic.pause(100)
}
}