Last active
May 26, 2020 05:02
-
-
Save mongonta0716/cef928a17dc58d1e69e29eb32ac1e491 to your computer and use it in GitHub Desktop.
M5StickV _thread test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import video,time,lcd | |
from Maix import I2S, GPIO | |
from fpioa_manager import * | |
import lcd | |
import uos | |
import sys | |
import pmu | |
import image | |
import audio | |
from machine import I2C | |
import _thread | |
global loop_flag | |
loop_flag = True | |
# Init | |
lcd.init() | |
lcd.rotation(2) | |
lcd.draw_string(10, 10, "test" , lcd.WHITE, lcd.BLACK) | |
i2c = I2C(I2C.I2C0, freq=400000, scl=28, sda=29) | |
fm.register(board_info.SPK_SD, fm.fpioa.GPIO0) | |
spk_sd=GPIO(GPIO.GPIO0, GPIO.OUT) | |
spk_sd.value(1) #Enable the SPK output | |
fm.register(board_info.SPK_DIN,fm.fpioa.I2S0_OUT_D1) | |
fm.register(board_info.SPK_BCLK,fm.fpioa.I2S0_SCLK) | |
fm.register(board_info.SPK_LRCLK,fm.fpioa.I2S0_WS) | |
fm.register(board_info.BUTTON_A, fm.fpioa.GPIO1) | |
but_a=GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! | |
fm.register(board_info.BUTTON_B, fm.fpioa.GPIO2) | |
but_b = GPIO(GPIO.GPIO2, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! | |
fm.register(board_info.LED_W, fm.fpioa.GPIO3) | |
led_w = GPIO(GPIO.GPIO3, GPIO.OUT) | |
led_w.value(1) #RGBW LEDs are Active Low | |
fm.register(board_info.LED_R, fm.fpioa.GPIO4) | |
led_r = GPIO(GPIO.GPIO4, GPIO.OUT) | |
led_r.value(1) #RGBW LEDs are Active Low | |
fm.register(board_info.LED_G, fm.fpioa.GPIO5) | |
led_g = GPIO(GPIO.GPIO5, GPIO.OUT) | |
led_g.value(1) #RGBW LEDs are Active Low | |
fm.register(board_info.LED_B, fm.fpioa.GPIO6) | |
led_b = GPIO(GPIO.GPIO6, GPIO.OUT) | |
led_b.value(1) #RGBW LEDs are Active Low | |
debug_str = "MaixPy" | |
but_stu = 1 | |
wait = 0.02 | |
def spitWater(fm, board_info): | |
# loop_flag = True | |
print("spitWater Start") | |
import lcd,image | |
from Maix import GPIO | |
from fpioa_manager import * | |
fm.register(board_info.BUTTON_A, fm.fpioa.GPIO1) | |
but_a=GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! | |
wait = 0.02 | |
imagea = image.Image("/sd/jpg/fugu_normal.jpg") | |
imageb = image.Image("/sd/jpg/fugu_water1.jpg") | |
imagec = image.Image("/sd/jpg/fugu_water2.jpg") | |
imaged = image.Image("/sd/jpg/fugu_water3.jpg") | |
while loop_flag: | |
lcd.display(imageb) | |
time.sleep(wait) | |
lcd.display(imagec) | |
time.sleep(wait) | |
lcd.display(imaged) | |
time.sleep(wait) | |
print("spitWater out") | |
def playMusic(fm, board_info): | |
print("playMusic start") | |
import audio | |
from Maix import I2S, GPIO | |
wav_dev = I2S(I2S.DEVICE_0) | |
fm.register(board_info.BUTTON_A, fm.fpioa.GPIO1) | |
but_a=GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! | |
while loop_flag: | |
# wav_file = "/sd/waterfall.wav" | |
wav_file = "/sd/wav/dounidemonareloop.wav" | |
# wav_file = "/flash/Ding.wav" | |
print("play:" + wav_file) | |
player = audio.Audio(path = wav_file) | |
player.volume(20) | |
wav_dev.channel_config(wav_dev.CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE) | |
wav_info = player.play_process(wav_dev) | |
wav_dev.set_sample_rate(wav_info[1]) | |
while True: | |
ret = player.play() | |
if ret == None: | |
print("fin :" + wav_file) | |
player.finish() | |
wav_info = player.play_process(wav_dev) | |
break | |
elif ret==0: | |
print("end"); | |
break | |
time.sleep(0.001); | |
print("playMusic out") | |
try: | |
loop_flag = True | |
_thread.start_new_thread(spitWater, (fm, board_info)) | |
_thread.start_new_thread(playMusic, (fm, board_info)) | |
while True: | |
if(but_a.value() == 0): | |
print("Button A pressed"); | |
loop_flag = False | |
# time while stop thread | |
time.sleep(1) | |
lcd.init() | |
lcd.rotation(2) | |
lcd.draw_string(10, 10, "Finish" , lcd.WHITE, lcd.BLACK) | |
sys.exit() | |
time.sleep(0.5) | |
except Exception as e: | |
sys.print_exception(e) | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment