Ejemplo n.º 1
0
def monitor(state, value1, value2):
    if (state == "motor"):
        import machine
        a1 = machine.Pin(4, machine.Pin.OUT, value=0)
        a2 = machine.Pin(15, machine.Pin.OUT, value=0)
        b1 = machine.Pin(14, machine.Pin.OUT, value=0)
        b2 = machine.Pin(12, machine.Pin.OUT, value=0)
        if (value1 == "A"):
            if (value2 == "1"):
                a1.value(0)
                a2.value(1)
            elif (value2 == "2"):
                a1.value(1)
                a2.value(0)
            else:
                a1.value(0)
                a2.value(0)
        elif (value1 == "B"):
            if (value2 == "1"):
                b1.value(0)
                b2.value(1)
            elif (value2 == "2"):
                b1.value(1)
                b2.value(0)
            else:
                b1.value(0)
                b2.value(0)
        del machine, a1, a2, b1, b2
    elif (state == "oled"):
        import oled
        if value1 != '' or value2 != '':
            oled.clear()
            oled.header(value1)
            oled.body(value2)
            oled.show()
            del oled
        else:
            oled.clear()
            oled.finished('OLED Monitor')
            del oled
    elif (state == "beep"):
        import beeper
        beeper.speaker(int(value1), int(value2))
        del beeper
    elif (state == "sensor"):
        import machine, time
        adc = machine.ADC(0)
        while (True):
            send("monitor:sensor:" + str(adc.read()))
            time.sleep_ms(500)
        del machine, time, adc
Ejemplo n.º 2
0
from microbit import *
import oled

while True:
    raw = i2c.read(0x20, 1)[0]
    res = 0
    for i in range(8):
        res = res * 2 + (raw & 1)
        raw //= 2
    oled.show(0, 0, b'\xb6\xfe\xbd\xf8\xd6\xc6%10s' % bin(res))
    oled.show(3, 0, b'\xca\xae\xbd\xf8\xd6\xc6%4d' % res)
    sleep(200)
Ejemplo n.º 3
0
import neo_color, neo_img
import oled, mb
from microbit import *

grps = (1, )
neo_color.setup(grps, addr=SLOT)
for row in range(4):
    pool = [mb.get_type(chr(ord('a') + row * 4 + i)) for i in range(4)]
    oled.show(row * 2, 0, pool)
display.show(Image.HAPPY)

while 1:
    for i in range(24):
        neo_color.set_pixel(0, i, (i * 2, i * 2, 48 - i * 2))
    for i in range(24):
        neo_color.set_pixel(0, i, (0, 0, 0))
Ejemplo n.º 4
0
from microbit import *
import oled, mb

while True:
    r = mb.get_bin()
    oled.show(0, 0, b'\xb6\xfe\xbd\xf8\xd6\xc60b%8s' % r)
    oled.show(3, 0, b'\xca\xae\xbd\xf8\xd6\xc6%4d' % int(r, 2))
    sleep(200)
Ejemplo n.º 5
0
# 简介:
# 读取温湿度模块示数并显示在OLED显示屏上
# 增加条件结构以对当前温湿度状态分级
# 增加循环结构进行持续判断,并使用GB2312编码改用中文显示
# 通过Python条件生成式对代码进行压缩

import temp_humi, oled  # 导入模块控制库
from microbit import sleep

sleep(0)  # 若读数为0则增大该处停顿时长等待温湿度模块加载完成

# 循环进行
while True:
    # 读取温度、湿度
    tmp, hum = temp_humi.temp_humi()

    # 在OLED显示读数(中文)
    oled.clear()
    oled.show(0, 0, b'\xce\xc2\xb6\xc8\xa3\xba%s\xa1\xe6' % tmp)
    oled.show(2, 0, b'\xca\xaa\xb6\xc8\xa3\xba%s\xa3\xa5' % hum)

    # 判断温湿度状态
    tmp_state = b'\xb5\xcd\xce\xc2' if tmp < 20 else b'\xca\xca\xd6\xd0' if tmp < 30 else b'\xb8\xdf\xce\xc2'  # 低温<适中<高温
    hum_state = b'\xb8\xc9\xd4\xef' if hum < 50 else b'\xb3\xb1\xca\xaa'  # 干燥<潮湿

    # 在OLED显示状态
    oled.show(6, 0, tmp_state + b', ' + hum_state)

    # 1秒后再次读取
    sleep(1000)
import oled
import wifi
import network
import time
import machine

ssid = "WUSI-601"
password = "******"

oled.text("connecting WIFI", 0, 0)
oled.show()
if (wifi.do_connect(ssid, password)):
    wlan = network.WLAN(network.STA_IF)
    status = wlan.ifconfig()
    oled.fill(0)
    oled.text("conneted", 0, 0)
    oled.text(status[0], 0, 8)
    oled.show()
else:
    oled.fill(0)
    oled.text("error", 0, 0)
    oled.text("not connected", 0, 8)
    oled.text("check ssid or pa", 0, 16)
    oled.text("ssword", 0, 24)
    oled.show()

time.sleep_ms(1000)
import dht
DHT = dht.DHT11(machine.Pin(12))
while (1):
    DHT.measure()
Ejemplo n.º 7
0
oled.clear()
led.off()
timer=0
light_read=0
tmp,hum='--','--'
while True:
    timer+=1

    # 每500帧更新温湿度读数
    if timer>500:
        timer-=500
        t,h=temp_humi.temp_humi()
        if t!=None:
            tmp,hum=t,h
        oled.clear()
        oled.show(1,8,b'\xce\xc2\xb6\xc8\xa3\xba%s\xa1\xe6'%tmp)
        oled.show(4,8,b'\xca\xaa\xb6\xc8\xa3\xba%s\xa3\xa5'%hum)
    
    # 每50帧更新光敏电阻读数,显示于LED上
    if timer%50==0:
        l=light.value()
        if l!=None:
            light_read=l
        if light_read>1000:
            if not led_on:
                led_on=1
                led.on()
        else:
            if led_on:
                led_on=0
                led.off()
Ejemplo n.º 8
0
# 简介:
# 读取温湿度模块示数并显示在OLED显示屏上
# 增加条件结构以对当前温湿度状态分级

import temp_humi, oled  # 导入模块控制库
from microbit import sleep

sleep(0)  # 若读数为0则增大该处停顿时长等待温湿度模块加载完成

# 读取温度、湿度
tmp, hum = temp_humi.temp_humi()

# 在OLED显示读数
oled.clear()
oled.show(0, 0, b'Temp: %s\'C' % tmp)
oled.show(2, 0, b'Humi: %s%%' % hum)

# 判断温湿度状态
if tmp < 20:
    tmp_state = b'Cold'
elif tmp < 30:
    tmp_state = b'Cool'
else:
    tmp_state = b'Hot'

if hum < 50:
    hum_state = b'Dry'
else:
    hum_state = b'Wet'
Ejemplo n.º 9
0
		continue
	if ta > tb:
		ta, tb = tb, ta
	if ta == 1 and tb == 2:
		# 测试OLED、温湿度、拨码
		import oled, temp_humi
		display.clear()
		while not (button_a.get_presses() + button_b.get_presses()):
			chn = i2c.read(0x20, 1)[0]
			ptr = 1
			for i in range(10):
				display.set_pixel(i % 4, i // 4, 9 * bool(chn & ptr))
				ptr *= 2
			oled.clear()
			temp, humi = temp_humi.temp_humi()
			oled.show(0, 0, b'\xce\xc2\xb6\xc8\xa3\xba%s\xa1\xe6' % temp)
			oled.show(2, 0, b'\xca\xaa\xb6\xc8\xa3\xba%s%%' % humi)
			temp, humi = temp_humi.temp(), temp_humi.humi()
			oled.show(4, 0, b'\xce\xc2\xb6\xc8\xa3\xba%s\xa1\xe6' % temp)
			oled.show(6, 0, b'\xca\xaa\xb6\xc8\xa3\xba%s%%' % humi)
			sleep(500)
		break
	elif ta == 3 and tb == 4:
		# 测试LED、电位器
		display.show(Image.HAPPY)
		import led, poten
		led.off()
		flag, counter = 0, 0
		while not (button_a.get_presses() + button_b.get_presses()):
			counter += poten.value()
			if counter >= 4096:
Ejemplo n.º 10
0
import oled

oled.show(0, 0, "F**k!")
Ejemplo n.º 11
0
            tmp = joypad.keys()
            if tmp != None:
                if tmp[1]:
                    input = 'a'
                elif tmp[2]:
                    input = 'b'
                elif tmp[4]:
                    input = 'c'
        if other == 'x':  # 等待对方输入
            tmp = radio.receive()
            if tmp and tmp in 'xabc':  # 接收合法性判断
                other = tmp

        # 重复发送己方出拳
        radio.send(input)
        sleep(10)

    # 显示并记录当局结果
    time = running_time() + 2000  # 结果展示状态持续两秒
    res = check_result(input, other)  # 计算比赛结果
    display.show(img_map[res])
    res_pool.append((input, other, res))  # 保留至多3条比赛记录
    if len(res_pool) > 3:
        res_pool.pop(0)
    oled.clear()
    oled.show(0, 0, b'%2d\xca\xa4 %2d\xb8\xba %2d\xc6\xbd' %
              tuple(res_count))  # 第一行显示累计对局结果
    for i in range(len(res_pool)):  # 显示最近至多3条比赛记录
        oled.show(2 * i + 2, 0, show_log(res_pool[i]))
    while running_time() < time:  # 比赛结果显示保留2秒
        sleep(10)
Ejemplo n.º 12
0
from microbit import *
import music
from random import randrange
import oled,joypad
score = 0
pos = 2
oled.clear()
oled.show(0,0,b'\xb5\xc3\xb7\xd6\xa3\xba0')# 得分:0
while 1:
    dropy = 0
    dropx = randrange(5) 
    while dropy<=4:
        keys=joypad.keys()
        if keys[3]:
            pos=max(0,pos-1)
        elif keys[4]:
            pos=min(4,pos+1)
        display.clear()
        display.set_pixel(dropx,dropy,9)
        display.set_pixel(pos,4,9)
        dropy+=1
        sleep(max(200,500-5*score))
    if dropx==pos:
        score+=1
        music.play('C7:1',wait=False)
        oled.show(0,0,b'\xb5\xc3\xb7\xd6\xa3\xba%d'%score)# 得分:{score}
    else:
        break
oled.show(2,0,b'\xd3\xce\xcf\xb7\xbd\xe1\xca\xf8')# 游戏结束
music.play(music.POWER_DOWN)