-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMP3.ts
201 lines (185 loc) · 6.06 KB
/
MP3.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
*Obloq implementation method.
*/
//% weight=10 color=#7fcbcd icon="\uf001" block="MP3"
namespace MP3 {
export enum playType {
//% block="Play"
Play = 0x01,
//% block="Pause"
Pause = 0x02,
//% block="Next"
Next = 0x03,
//% block="Prev"
Prev = 0x04,
//% block="Stop"
Stop = 0x0E,
//% block="Add"
Add = 0x05,
//% block="Sub"
Sub = 0x06
}
export enum EM_PlayMode {
//% block="AllLoop"
AllLoop = 0x00,
//% block="FolderLoop"
FolderLoop = 0x01,
//% block="SingleLoop"
SingleLoop = 0x02,
//% block="RandomLoop"
RandomLoop = 0x03,
//% block="SinglePlay"
SinglePlay = 0x04
}
export enum EM_PlayEQ {
//% block="None"
None = 0x00,
//% block="Pop"
Pop = 0x01,
//% block="Rock"
Rock = 0x02,
//% block="Jazz"
Jazz = 0x03,
//% block="Classic"
Classic = 0x04,
//% block="Bass"
Bass = 0x05
}
/**
* Disconnect the serial port.
* @param rx to rx ,eg: SerialPin.P1
* @param tx to tx ,eg: SerialPin.P2
*/
//% weight=100
//% rx.fieldEditor="gridpicker" rx.fieldOptions.columns=3
//% tx.fieldEditor="gridpicker" tx.fieldOptions.columns=3
//% blockId="MP3_pin_set"
//% blockGap=20
//% block="MP3 pin set | receive data(rx): %rx | send data(tx): %tx"
//% blockExternalInputs=true
//% inlineInputMode=inline
export function EM_MP3_pin_set(rx: SerialPin, tx: SerialPin): void {
serial.redirect(
tx,
rx,
BaudRate.BaudRate9600
)
}
/**
* Disconnect the serial port.
* @param type to type ,eg: playType.Play
*/
//% weight=90
//% type.fieldEditor="gridpicker" type.fieldOptions.columns=1
//% blockId="MP3_play_control"
//% blockGap=20
//% block="MP3 play control %type"
//% blockExternalInputs=true
export function EM_MP3_play_control(type: playType): void {
let buffer = pins.createBuffer(4);
buffer.setNumber(NumberFormat.UInt8BE, 0, 0x7e)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x02)
buffer.setNumber(NumberFormat.UInt8BE, 2, type)
buffer.setNumber(NumberFormat.UInt8BE, 3, 0xef)
serial.writeBuffer(buffer)
}
/**
* Disconnect the serial port.
* @param num to num ,eg: 1
*/
//% weight=80
//% num.min=1 num.max=65535
//% blockGap=20
//% blockId="MP3_assign_song"
//% block="MP3 play list %num"
export function EM_MP3_assign_song(num: number): void {
num = num < 1 ? 1 : (num > 65535 ? 65535 : num)
let buffer = pins.createBuffer(6);
let num_h = (num>>8) & 0xFF
let num_l = num & 0xFF
buffer.setNumber(NumberFormat.UInt8BE, 0, 0x7e)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x04)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x41)
buffer.setNumber(NumberFormat.UInt8BE, 3, num_h)
buffer.setNumber(NumberFormat.UInt8BE, 4, num_l)
buffer.setNumber(NumberFormat.UInt8BE, 5, 0xef)
serial.writeBuffer(buffer)
}
/**
* Disconnect the serial port.
* @param name to name ,eg: 001
*/
//% weight=81
//% blockGap=20
//% blockId="EM_MP3_play_song_name"
//% block="MP3 play name %name"
export function EM_MP3_play_song_name(name: number): void {
name = name < 1 ? 1 : (name > 65535 ? 65535 : name)
let buffer = pins.createBuffer(6);
let num_h = (name>>8) & 0xFF
let num_l = name & 0xFF
buffer.setNumber(NumberFormat.UInt8BE, 0, 0x7e)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x04)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x49)
buffer.setNumber(NumberFormat.UInt8BE, 3, num_h)
buffer.setNumber(NumberFormat.UInt8BE, 4, num_l)
buffer.setNumber(NumberFormat.UInt8BE, 5, 0xef)
serial.writeBuffer(buffer)
}
/**
* Disconnect the serial port.
* @param type to type ,eg: EM_PlayMode.AllLoop
*/
//% weight=70
//% type.fieldEditor="gridpicker" type.fieldOptions.columns=1
//% blockGap=20
//% blockId="MP3_play_type"
//% block="MP3 play type %type"
export function EM_MP3_play_type(type: EM_PlayMode): void {
let buffer = pins.createBuffer(5);
buffer.setNumber(NumberFormat.UInt8BE, 0, 0x7e)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x03)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x33)
buffer.setNumber(NumberFormat.UInt8BE, 3, <number>type)
buffer.setNumber(NumberFormat.UInt8BE, 4, 0xef)
serial.writeBuffer(buffer)
}
/**
* Disconnect the serial port.
* @param vol to vol ,eg: 70
*/
//% weight=60
//% vol.min=0 vol.max=100
//% blockGap=20
//% blockId="MP3_volume_set"
//% block="MP3 set volume %vol"
export function EM_MP3_volume_set(vol: number): void {
vol = vol < 0 ? 0 : (vol > 100 ? 100 : vol)
vol = (30 * vol) / 100
let buffer = pins.createBuffer(5);
buffer.setNumber(NumberFormat.UInt8BE, 0, 0x7e)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x03)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x31)
buffer.setNumber(NumberFormat.UInt8BE, 3, <number>vol)
buffer.setNumber(NumberFormat.UInt8BE, 4, 0xef)
serial.writeBuffer(buffer)
}
/**
* Disconnect the serial port.
* @param eq to eq ,eg: EM_PlayEQ.None
*/
//% weight=30
//% eq.fieldEditor="gridpicker" eq.fieldOptions.columns=1
//% blockGap=20
//% blockId="MP3_eq_set"
//% block="MP3 set EQ %eq"
export function EM_MP3_eq_set(eq: EM_PlayEQ): void {
let buffer = pins.createBuffer(5);
buffer.setNumber(NumberFormat.UInt8BE, 0, 0x7e)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x03)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x32)
buffer.setNumber(NumberFormat.UInt8BE, 3, <number>eq)
buffer.setNumber(NumberFormat.UInt8BE, 4, 0xef)
serial.writeBuffer(buffer)
}
}