5、自控行人过马路交通信号灯
#MicroPython动手做(22)——掌控板之无线广播
#自控行人过马路交通信号灯(行人控制端)
行人端红绿灯:
①行人方向的灯是红色的,等待行人按下A键,
②当行人需要过马路的时候,按下按钮,同时给行车方向的红绿灯发射信号,等待缓冲时间,红灯转绿灯。
③行人走完之后(绿灯亮一段时间),黄灯闪烁3次,再变为红灯。
#MicroPython动手做(22)——掌控板之无线广播
#自控行人过马路交通信号灯(行人控制端)
import radio
from mpython import *
import time
import framebuf
import font.digiface_44
def Flash():
global i, j, k, m, n, x
for m in range(1, 4):
rgb.fill((int(255), int(255), int(0)))
rgb.write()
time.sleep_ms(1)
time.sleep_ms(500)
rgb.fill( (0, 0, 0) )
rgb.write()
time.sleep_ms(1)
time.sleep_ms(500)
def _E4_BF_A1_E6_81_AF_E6_8F_90_E7_A4_BA():
global i, j, k, m, n, x
oled.fill(0)
oled.DispChar("行人如需过马路", 0, 16, 1)
oled.DispChar("请按下A键", 0, 32, 1)
oled.show()
def upRange(start, stop, step):
while start <= stop:
yield start
start += abs(step)
def downRange(start, stop, step):
while start >= stop:
yield start
start -= abs(step)
def _E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(x):
global i, j, k, m, n
for n in (1 <= int(x)) and upRange(1, int(x), 1) or downRange(1, int(x), 1):
oled.fill(0)
display_font(font.digiface_44, (str((x + 1) - n)), 32, 8, False, 2)
oled.show()
time.sleep_ms(1000)
def on_button_a_down(_):
global i, j, k, m, n, x
time.sleep_ms(10)
if button_a.value() == 1: return
radio.send("1")
_E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(10)
rgb.fill((int(51), int(255), int(51)))
rgb.write()
time.sleep_ms(1)
_E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(12)
Flash()
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
_E4_BF_A1_E6_81_AF_E6_8F_90_E7_A4_BA()
def display_font(_font, _str, _x, _y, _wrap, _z=0):
_start = _x
for _c in _str:
_d = _font.get_ch(_c)
if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],
framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d[2]
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
radio.on()
radio.config(channel=11)
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
_E4_BF_A1_E6_81_AF_E6_8F_90_E7_A4_BA()
|