-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMP3.ts
More file actions
228 lines (206 loc) · 6.88 KB
/
MP3.ts
File metadata and controls
228 lines (206 loc) · 6.88 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*!
* @file Obloq/Obloq.ts
* @brief DFRobot's obloq makecode library.
* @n [Get the module here](http://www.dfrobot.com.cn/goods-1577.html)
* @n Obloq is a serial port of WIFI connection module, Obloq can connect
* to Microsoft Azure IoT and other standard MQTT protocol IoT.
*
* @copyright [DFRobot](http://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
*
* @author [email](xin.li@dfrobot.com)
* @version V1.0
* @date 2018-03-20
*/
/**
*Obloq implementation method.
*/
//% weight=10 color=#FFC0CB icon="\uf001" block="MP3"
namespace MP3 {
export enum playType {
//% block="Play"
Play = 0x02,
//% block="Pause"
Pause = 0x03,
//% block="Last"
Last = 0x05,
//% block="Next"
Next = 0x06,
//% block="Stop"
Stop = 0x04
}
export enum cycleType {
//% block="Single cycle"
SingleCycle = 0x01,
//% block="Single stop"
SingleStop = 0x02,
//% block="List cycle"
ListCycle = 0x00,
//% block="Shuffle"
Shuffle = 0x03,
//% block="Order"
Order = 0x07
}
export enum equalizer {
//% block="Normal"
Normal = 0x00,
//% block="Pop"
Pop = 0x01,
//% block="Rock"
Rock = 0x02,
//% block="Jazz"
Jazz = 0x03,
//% block="Classic"
Classic = 0x04
}
/*function Obloq_wifi_icon_display(): void {
}*/
/**
* 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
export function MP3_pin_set(rx: SerialPin, tx: SerialPin): void {
let item = ""
serial.redirect(
tx,
rx,
BaudRate.BaudRate9600
)
//First send data through usb, avoid the first data scrambled.
item = serial.readString()
}
/**
* 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 MP3_play_control(type: playType): void {
let buffer = pins.createBuffer(4);
buffer.setNumber(NumberFormat.UInt8BE, 0, 0xAA)
buffer.setNumber(NumberFormat.UInt8BE, 1, type)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x00)
buffer.setNumber(NumberFormat.UInt8BE, 3, (0xAA + type + 0x00))
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 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, 0xAA)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x07)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x02)
buffer.setNumber(NumberFormat.UInt8BE, 3, num_h)
buffer.setNumber(NumberFormat.UInt8BE, 4, num_l)
buffer.setNumber(NumberFormat.UInt8BE, 5, (0xAA + 0x07 + 0x02 + num_h + num_l))
serial.writeBuffer(buffer)
}
/**
* Disconnect the serial port.
* @param type to type ,eg: cycleType.ListCycle
*/
//% weight=70
//% type.fieldEditor="gridpicker" type.fieldOptions.columns=1
//% blockGap=20
//% blockId="MP3_play_type"
//% block="MP3 play type %type"
export function MP3_play_type(type: cycleType): void {
let buffer = pins.createBuffer(5);
buffer.setNumber(NumberFormat.UInt8BE, 0, 0xAA)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x18)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x01)
buffer.setNumber(NumberFormat.UInt8BE, 3, <number>type)
buffer.setNumber(NumberFormat.UInt8BE, 4, <number>(0xAA + 0x18 + 0x01 + <number>type))
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 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, 0xAA)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x13)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x01)
buffer.setNumber(NumberFormat.UInt8BE, 3, <number>vol)
buffer.setNumber(NumberFormat.UInt8BE, 4, <number>(0xAA + 0x13 + 0x01 + <number>vol))
serial.writeBuffer(buffer)
}
/**
* Disconnect the serial port.
*/
//% weight=50
//% blockGap=20
//% blockId="MP3_volume_up"
//% block="MP3 volume +"
export function MP3_volume_up(): void {
let buffer = pins.createBuffer(4);
buffer.setNumber(NumberFormat.UInt8BE, 0, 0xAA)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x14)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x00)
buffer.setNumber(NumberFormat.UInt8BE, 3, 0xBE)
serial.writeBuffer(buffer)
}
/**
* Disconnect the serial port.
*/
//% weight=40
//% blockGap=20
//% blockId="MP3_volume_down"
//% block="MP3 volume -"
export function MP3_volume_down(): void {
let buffer = pins.createBuffer(4);
buffer.setNumber(NumberFormat.UInt8BE, 0, 0xAA)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x15)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x00)
buffer.setNumber(NumberFormat.UInt8BE, 3, 0xBF)
serial.writeBuffer(buffer)
}
/**
* Disconnect the serial port.
* @param eq to eq ,eg: equalizer.Normal
*/
//% weight=30
//% eq.fieldEditor="gridpicker" eq.fieldOptions.columns=1
//% blockGap=20
//% blockId="MP3_eq_set"
//% block="MP3 set equalizer %eq"
export function MP3_eq_set(eq: equalizer): void {
let buffer = pins.createBuffer(5);
buffer.setNumber(NumberFormat.UInt8BE, 0, 0xAA)
buffer.setNumber(NumberFormat.UInt8BE, 1, 0x1A)
buffer.setNumber(NumberFormat.UInt8BE, 2, 0x01)
buffer.setNumber(NumberFormat.UInt8BE, 3, <number>eq)
buffer.setNumber(NumberFormat.UInt8BE, 4, <number>(0xAA + 0x1A + 0x01 + <number>eq))
serial.writeBuffer(buffer)
}
}