软件:MaixPy IDE或者CanMV IDE,串口助手
1、不管拿到什么,先点个灯
from modules import ybrgb
import time
RGB = ybrgb()
while True:
RGB.set(1, 0, 0)
time.sleep(.5)
RGB.set(0, 1, 0)
time.sleep(.5)
RGB.set(0, 0, 1)
time.sleep(.5)
RGB.set(1, 1, 1)
time.sleep(.5)
RGB.set(0, 0, 0)
time.sleep(.5)
2、再试一试按键
from modules import ybkey
import time
KEY = ybkey()
while True:
state = KEY.is_press()
print("key press:", state)
time.sleep_ms(100)
3、定时器
from machine import Timer
import time
def on_timer(timer):
print("This is on_timer callback")
timer = Timer(Timer.TIMER0, Timer.CHANNEL0,
mode=Timer.MODE_PERIODIC, period=100,
unit=Timer.UNIT_MS, callback=on_timer, arg=None)
last_time = time.ticks_ms()
try:
while True:
if time.ticks_ms() - last_time >= 200:
last_time = time.ticks_ms()
print("This is main loop")
except:
timer.deinit()
del timer
4、PWM呼吸灯
from machine import Timer, PWM
import time
# RED=27, GREEN=26, BLUE=29
LED_PIN = 26
tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
pwm = PWM(tim, freq=500000, duty=50, pin=LED_PIN)
duty=0
add = True
while True:
if add:
duty += 5
else:
duty -= 5
if duty > 100:
duty = 100
add = False
elif duty < 0:
duty = 0
add = True
time.sleep(0.05)
pwm.duty(duty)
5、摄像头
import sensor, lcd
import time
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
sensor.skip_frames(10)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
fps = clock.fps()
img.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 0, 255), scale=2.0)
lcd.display(img)
print(fps)
注:固件库需提前准备