#MicroPython动手做(22)——掌控板之无线广播
#自控行人过马路交通信号灯(行车同步端)
行车端红绿灯:
①亮绿灯,等待接收广播指令;
②当接收到广播指令,等待缓冲时间,黄灯闪亮3次,红灯亮,等待15秒,行人通过后,恢复绿灯。
#MicroPython动手做(22)——掌控板之无线广播
#自控行人过马路交通信号灯(行车同步端)
from mpython import *
from machine import Timer
import radio
import ubinascii
_radio_msg_list = []
def radio_callback(_msg):
global _radio_msg_list
try: radio_recv(_msg)
except: pass
if _msg in _radio_msg_list:
eval('radio_recv_' + bytes.decode(ubinascii.hexlify(_msg)) + '()')
tim13 = Timer(13)
def timer13_tick(_):
_msg = radio.receive()
if not _msg: return
radio_callback(_msg)
tim13.init(period=20, mode=Timer.PERIODIC, callback=timer13_tick)
import time
import framebuf
import font.digiface_44
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 k, j
for k 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) - k)), 32, 8, False, 2)
oled.show()
time.sleep(1)
oled.fill(0)
oled.show()
def flash():
global x, k, j
for j 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)
_radio_msg_list.append('1')
def radio_recv_31():
global i, x, y
_E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(7)
flash()
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
_E6_97_B6_E9_97_B4_E6_8F_90_E7_A4_BA(15)
rgb.fill((int(0), int(153), int(0)))
rgb.write()
time.sleep_ms(1)
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]
radio.on()
radio.config(channel=11)
rgb.fill((int(255), int(0), int(0)))
rgb.write()
time.sleep_ms(1)
|