-
Notifications
You must be signed in to change notification settings - Fork 0
/
gesture_simple_music.py
421 lines (333 loc) · 10.1 KB
/
gesture_simple_music.py
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/python
# -*- coding:utf-8 -*-
import time
import smbus
# from weather_tts import *
import os
import subprocess
import pygame
pygame.init()
pygame.mixer.init()
#---
# init
#---
# _dir=os.path.dirname(os.path.abspath(__file__))
# playlist_path=os.path.join(_dir,'music')
#---
#
#---
#---
#
#---
#i2c地址
PAJ7620U2_I2C_ADDRESS = 0x73
#寄存器Bank选择
PAJ_BANK_SELECT = 0xEF #Bank0== 0x00,Bank1== 0x01
#寄存器Bank 0
PAJ_SUSPEND = 0x03 #I2C suspend命令(写= 0x01进入挂起状态)。I2C唤醒命令是奴隶ID唤醒。参考主题“I2C总线定时特性和协议”
PAJ_INT_FLAG1_MASK = 0x41 #手势检测中断标志掩码
PAJ_INT_FLAG2_MASK = 0x42 #手势/PS检测中断标志掩码
PAJ_INT_FLAG1 = 0x43 #手势检测中断标志
PAJ_INT_FLAG2 = 0x44 #手势/PS检测中断标志
PAJ_STATE = 0x45 #手势检测状态指示灯(仅在手势检测模式下起作用)
PAJ_PS_HIGH_THRESHOLD = 0x69 #PS迟滞高阈值(仅在接近检测模式下起作用)
PAJ_PS_LOW_THRESHOLD = 0x6A #PS迟滞低阈值(仅在接近检测模式下起作用)
PAJ_PS_APPROACH_STATE = 0x6B #PS趋近状态,趋近= 1,(8位PS数据>= PS高门限),不趋近= 0,(8位PS数据<= PS低门限)(仅在接近检测模式下有效)
PAJ_PS_DATA = 0x6C #PS 8位数据(仅在手势检测模式下有效)
PAJ_OBJ_BRIGHTNESS = 0xB0 #物体亮度(最大255)
PAJ_OBJ_SIZE_L = 0xB1 #对象大小(低8位)
PAJ_OBJ_SIZE_H = 0xB2 #对象大小(高8位)
#寄存器Bank 1
PAJ_PS_GAIN = 0x44 #PS增益设置(仅在接近检测模式下有效)
PAJ_IDLE_S1_STEP_L = 0x67 #空闲S1步长,用于设置S1,响应系数(低8位)
PAJ_IDLE_S1_STEP_H = 0x68 #空闲S1步长,用于设置S1,响应系数(高8位)
PAJ_IDLE_S2_STEP_L = 0x69 #空闲S2步长,用于设置S2,响应因子(低8位)
PAJ_IDLE_S2_STEP_H = 0x6A #空闲S2步长,用于设置S2,响应因子(高8位)
PAJ_OPTOS1_TIME_L = 0x6B #OPtoS1 Step,用于将操作状态的OPtoS1时间设置为待机1状态(低8位)
PAJ_OPTOS2_TIME_H = 0x6C #OPtoS1 Step,用于将OPtoS1运行状态时间设置为待机1 stateHigh 8位)
PAJ_S1TOS2_TIME_L = 0x6D #S1toS2步,用于将待机1状态的S1toS2时间设置为待机2状态(低8位)
PAJ_S1TOS2_TIME_H = 0x6E #S1toS2步,用于将待机1状态的S1toS2时间设置为待机2状态高8位)
PAJ_EN = 0x72 #启用/禁用PAJ7620U2
#手势检测中断标志
PAJ_RIGHT = 0x01
PAJ_LEFT = 0x02
PAJ_UP = 0x04
PAJ_DOWN = 0x08
PAJ_FORWARD = 0x10
PAJ_BACKWARD = 0x20
PAJ_CLOCKWISE = 0x40
PAJ_COUNT_CLOCKWISE = 0x80
PAJ_WAVE = 0x100
#启动初始化阵列
Init_Register_Array = (
(0xEF,0x00),
(0x37,0x07),
(0x38,0x17),
(0x39,0x06),
(0x41,0x00),
(0x42,0x00),
(0x46,0x2D),
(0x47,0x0F),
(0x48,0x3C),
(0x49,0x00),
(0x4A,0x1E),
(0x4C,0x20),
(0x51,0x10),
(0x5E,0x10),
(0x60,0x27),
(0x80,0x42),
(0x81,0x44),
(0x82,0x04),
(0x8B,0x01),
(0x90,0x06),
(0x95,0x0A),
(0x96,0x0C),
(0x97,0x05),
(0x9A,0x14),
(0x9C,0x3F),
(0xA5,0x19),
(0xCC,0x19),
(0xCD,0x0B),
(0xCE,0x13),
(0xCF,0x64),
(0xD0,0x21),
(0xEF,0x01),
(0x02,0x0F),
(0x03,0x10),
(0x04,0x02),
(0x25,0x01),
(0x27,0x39),
(0x28,0x7F),
(0x29,0x08),
(0x3E,0xFF),
(0x5E,0x3D),
(0x65,0x96),
(0x67,0x97),
(0x69,0xCD),
(0x6A,0x01),
(0x6D,0x2C),
(0x6E,0x01),
(0x72,0x01),
(0x73,0x35),
(0x74,0x00),
(0x77,0x01),
)
#寄存器初始化数组
Init_PS_Array = (
(0xEF,0x00),
(0x41,0x00),
(0x42,0x00),
(0x48,0x3C),
(0x49,0x00),
(0x51,0x13),
(0x83,0x20),
(0x84,0x20),
(0x85,0x00),
(0x86,0x10),
(0x87,0x00),
(0x88,0x05),
(0x89,0x18),
(0x8A,0x10),
(0x9f,0xf8),
(0x69,0x96),
(0x6A,0x02),
(0xEF,0x01),
(0x01,0x1E),
(0x02,0x0F),
(0x03,0x10),
(0x04,0x02),
(0x41,0x50),
(0x43,0x34),
(0x65,0xCE),
(0x66,0x0B),
(0x67,0xCE),
(0x68,0x0B),
(0x69,0xE9),
(0x6A,0x05),
(0x6B,0x50),
(0x6C,0xC3),
(0x6D,0x50),
(0x6E,0xC3),
(0x74,0x05),
)
#手势寄存器初始化数组
Init_Gesture_Array = (
(0xEF,0x00),
(0x41,0x00),
(0x42,0x00),
(0xEF,0x00),
(0x48,0x3C),
(0x49,0x00),
(0x51,0x10),
(0x83,0x20),
(0x9F,0xF9),
(0xEF,0x01),
(0x01,0x1E),
(0x02,0x0F),
(0x03,0x10),
(0x04,0x02),
(0x41,0x40),
(0x43,0x30),
(0x65,0x96),
(0x66,0x00),
(0x67,0x97),
(0x68,0x01),
(0x69,0xCD),
(0x6A,0x01),
(0x6B,0xB0),
(0x6C,0x04),
(0x6D,0x2C),
(0x6E,0x01),
(0x74,0x00),
(0xEF,0x00),
(0x41,0xFF),
(0x42,0x01),
)
#---
# init
#---
from glob import glob
music_index = 0
# music_dir='/media/arthur/DATA/gaodianzhuo/pi/weather_tts/music'
# mp3_files = glob('/media/arthur/DATA/gaodianzhuo/pi/weather_tts/music/*.mp3')
_dir=os.path.dirname(os.path.abspath(__file__))
playlist_path=os.path.join(_dir,'music')+'/*.mp3'
mp3_files = glob(playlist_path)
#---
# music control
#---
def play(music_file):
try:
pygame.mixer.music.stop()
except:
pass
# for music_file in music_list:
pygame.mixer.music.load(music_file)
# pygame.mixer.music.load(file)
pygame.mixer.music.play()
return 0
def play(music_list):
'''error 无法循环播放'''
try:
pygame.mixer.music.stop()
except:
pass
for music_file in music_list:
print(music_file)
pygame.mixer.music.load(music_file)
# pygame.mixer.music.load(file)
pygame.mixer.music.play()
return 0
# play(mp3_files)
# play(mp3_files[0])
def stop():
pygame.mixer.music.stop()
return 0
#---
#
#---
class PAJ7620U2(object):
def __init__(self,address=PAJ7620U2_I2C_ADDRESS):
self._address = address
self._bus = smbus.SMBus(1)
time.sleep(0.5)
if self._read_byte(0x00) == 0x20:
print("\nGesture Sensor OK\n")
for num in range(len(Init_Register_Array)):
self._write_byte(Init_Register_Array[num][0],Init_Register_Array[num][1])
else:
print("\nGesture Sensor Error\n")
self._write_byte(PAJ_BANK_SELECT, 0)
for num in range(len(Init_Gesture_Array)):
self._write_byte(Init_Gesture_Array[num][0],Init_Gesture_Array[num][1])
def _read_byte(self,cmd):
return self._bus.read_byte_data(self._address,cmd)
def _read_u16(self,cmd):
LSB = self._bus.read_byte_data(self._address,cmd)
MSB = self._bus.read_byte_data(self._address,cmd+1)
return (MSB << 8) + LSB
def _write_byte(self,cmd,val):
self._bus.write_byte_data(self._address,cmd,val)
# def check_gesture(self):
# Gesture_Data=self._read_u16(PAJ_INT_FLAG1)
# if Gesture_Data == PAJ_UP:
# print("Up\r\n")
# elif Gesture_Data == PAJ_DOWN:
# print("Down\r\n")
# elif Gesture_Data == PAJ_LEFT:
# print("Left\r\n")
# elif Gesture_Data == PAJ_RIGHT:
# print("Right\r\n")
# elif Gesture_Data == PAJ_FORWARD:
# print("Forward\r\n")
# elif Gesture_Data == PAJ_BACKWARD:
# print("Backward\r\n")
# elif Gesture_Data == PAJ_CLOCKWISE:
# print("Clockwise\r\n")
# elif Gesture_Data == PAJ_COUNT_CLOCKWISE:
# print("AntiClockwise\r\n")
# elif Gesture_Data == PAJ_WAVE:
# print("Wave\r\n")
# return Gesture_Data
def check_gesture(self):
Gesture_Data=self._read_u16(PAJ_INT_FLAG1)
if Gesture_Data == PAJ_FORWARD:
print("FORWARD\r\n")
#speak()
pidc = subprocess.Popen(["python3", "weather_tts.py"])
elif Gesture_Data == PAJ_DOWN:
# !!ps -ef | grep weather_tts | grep -v grep | awk '{print $2}' | xargs kill -9
try:
os.system("ps -ef | grep weather_tts | grep -v grep | awk '{print $2}' | xargs kill -9")
# os.system("ps -ef | grep pulseaudio | grep -v grep | awk '{print $2}' | xargs kill -9")
pygame.mixer.music.stop()
finally:
print("Down\r\n")
elif Gesture_Data == PAJ_LEFT:
print("Left\r\n")
global music_index
if music_index >= len(mp3_files):
music_index = 0
play(mp3_files[music_index])
music_index+=1
# play()
# pygame.mixer.music.load(playlist_path+'/浮白.mp3')
# pygame.mixer.music.load(file)
# pygame.mixer.music.play()
# elif Gesture_Data == PAJ_RIGHT:
# print("Right\r\n")
# elif Gesture_Data == PAJ_FORWARD:
# print("Forward\r\n")
# elif Gesture_Data == PAJ_BACKWARD:
# print("Backward\r\n")
# elif Gesture_Data == PAJ_CLOCKWISE:
# print("Clockwise\r\n")
# elif Gesture_Data == PAJ_COUNT_CLOCKWISE:
# print("AntiClockwise\r\n")
# elif Gesture_Data == PAJ_WAVE:
# print("Wave\r\n")
return Gesture_Data
if __name__ == '__main__':
import time
print("\nGesture Sensor Test Program ...\n")
paj7620u2=PAJ7620U2()
while True:
time.sleep(0.05)
paj7620u2.check_gesture()
#---
#
#---
# import pygame
# dir_path = os.path.dirname(os.path.realpath(__file__))
# path = os.path.join(dir_path, "mp3s", 'info.mp3')
# pygame.mixer.init()
# mysound = pygame.mixer.Sound(file="zuo2.wav")
# pygame.mixer.Sound.play(mysound)
# import pygame
# file = '关山酒.mp3'
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(playlist_path+'/浮白.mp3')
# pygame.mixer.music.load(file)
pygame.mixer.music.play()
# pygame.event.wait()