import machine
import time
import _thread
from machine import Pin,SoftI2C,PWM,ADC
from ssd1306 import SSD1306_I2C
from dht import DHT11
import framebuf
import micropython
#初始化 oled
#SoftI2C 初始化:scl--> 10, sda --> 11
i2c = SoftI2C(scl=Pin(10), sda=Pin(11))
# OLED 显示屏初始化:128*64 分辨率,OLED 的 I2C 地址是 0x3c
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
#初始化 DS18B20
time.sleep(1)
#创建 DTH11 对象 dt
dt = DHT11(machine.Pin(17))
#LED
LED1 = Pin(10, Pin.OUT)
LED2 = Pin(11, Pin.OUT)
LED3 = Pin(12, Pin.OUT)
LEDS = [LED1,LED2,LED3]
for i in range(3):
LEDS[i].low()
key = Pin(14, Pin.IN, Pin.PULL_UP)
#p2 = Pin(27, Pin.IN)
Light = ADC(1)
LED_idx={'0': 0,'1': 0, '2': 1, '3': 2, '4': 0, '5': 1, '6': 2, '7': 1, '-': 0,'*':0}
# 歌谱子频率
tones = {'0': 0,'1': 262, '2': 294, '3': 330, '4': 349, '5': 392, '6': 440, '7': 494, '-': 0,'*':0}
# 定义音乐旋律 星星,两只老虎,天空之城,欢乐颂
melody = ["1155665-4433221-5544332-5544332-1155665-4433221","1231-1231-345-345-565431-565431-351-351","671713-733-65615-5033-4341-30111-7447-70-*671713-7033-6561503-41771-22310-176675-6012-*3235-2055-1713-300-671722-1550-4321-3*303-655-32101-21225-303-65-32101-21227-6067-6","334554321123322-334554321123211-2231234312343212-5"]
song_name=["Star","Two Tiger","Carrying you","SONG OF JOY"]
flag_stop=False
song_idx=0
song_N=len(melody)
def setup():
global beeper
beeper = PWM(Pin(13))
def music_led_fun():
global flag_stop
oled.fill_rect(5, 5, 96, 10, 0) #song name
oled.text(song_name[song_idx],5,5)
oled.show()
print("change song to "+song_name[song_idx])
flag_stop = False
for tone in melody[song_idx]:
#print(micropython.mem_info())
if flag_stop:
print("return..")
return
freq = tones[tone]
if freq:
LEDS[LED_idx[tone]].high()
#print(LED_idx[tone])
#print(tone)
beeper.init(duty_u16=32768, freq=freq) # 调整PWM的频率,使其发出指定的音调
else:
beeper.duty_ns(0) # 空拍时一样不上电
# 停顿一下 (四四拍每秒两个音,每个音节中间稍微停顿一下)
time.sleep_ms(400)
beeper.duty_ns(0) # 设备占空比为0,即不上电
LEDS[LED_idx[tone]].low()
if flag_stop:
print("return..")
return
time.sleep_ms(100)
next_music()
def next_music():
global song_idx
song_idx=(song_idx+1)%song_N
print("next_music....")
def cb(key):
global flag_stop
flag_stop = True
time.sleep_ms(10) #消除抖动
if key.value()==0: #确认按键被按下
next_music()
print("touch next...")
def t_light():
time.sleep(1)
while True:
#数据显示
#value = p2.value()
value=Light.read_u16()
#print("light is"+str(value))
oled.fill_rect(0, 40,128 , 64, 0) #T&H
oled.fill_rect(0, 30, 90, 10, 0) #L
oled.text('L:'+str(value),0,30)
#oled.text(str(value),60,15)
#dt.measure()
try:
te = dt.temperature # 获取温度值
dh = dt.humidity # 获取湿度值
print("success")
except:
pass
#oled.text("DHT22 test:", 0, 35)
# 温度显示
oled.text("T:"+str(te) , 0, 45)
# 湿度显示
oled.text("H:"+str(dh) , 60, 45)
print(str(value)+"--"+str(te)+"--"+str(dh))
oled.show()
time.sleep_ms(2000)
# Function that initializes execution in the second core
# The second argument is a list or dictionary with the arguments
# that will be passed to the function.
#_thread.start_new_thread(t_light, ())
# Second loop that will block the main thread, and what it will do
# that the internal led blinks every half second
key.irq(cb,Pin.IRQ_FALLING)
setup()
buffer = bytearray(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00|?\x00\x01\x86@\x80\x01\x01\x80\x80\x01\x11\x88\x80\x01\x05\xa0\x80\x00\x83\xc1\x00\x00C\xe3\x00\x00~\xfc\x00\x00L'\x00\x00\x9c\x11\x00\x00\xbf\xfd\x00\x00\xe1\x87\x00\x01\xc1\x83\x80\x02A\x82@\x02A\x82@\x02\xc1\xc2@\x02\xf6>\xc0\x01\xfc=\x80\x01\x18\x18\x80\x01\x88\x10\x80\x00\x8c!\x00\x00\x87\xf1\x00\x00\x7f\xf6\x00\x008\x1c\x00\x00\x0c \x00\x00\x03\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
# Clear the oled display in case it has junk on it.
oled.fill(0)
# Blit the image from the framebuffer to the oled display
oled.blit(fb, 96, 0)
while True:
music_led_fun()