From 0bc8505ed8a39776e056b312521f786c5a92fdf3 Mon Sep 17 00:00:00 2001 From: Neucrack Date: Mon, 13 May 2024 20:10:26 +0800 Subject: [PATCH] add pwm demo and backlight setting demo --- examples/peripheral/pwm/pwm_servo.py | 26 ++++++++++++++++++++ examples/vision/display/display_backlight.py | 19 ++++++++++++++ maix/version.py | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 examples/peripheral/pwm/pwm_servo.py create mode 100644 examples/vision/display/display_backlight.py diff --git a/examples/peripheral/pwm/pwm_servo.py b/examples/peripheral/pwm/pwm_servo.py new file mode 100644 index 00000000..ad501de2 --- /dev/null +++ b/examples/peripheral/pwm/pwm_servo.py @@ -0,0 +1,26 @@ +from maix import pwm, time, pinmap + +SERVO_PERIOD = 50 # 50Hz 20ms +SERVO_MIN_DUTY = 2.5 # 2.5% -> 0.5ms +SERVO_MAX_DUTY = 12.5 # 12.5% -> 2.5ms + +# Use PWM7 +pwm_id = 7 +# !! set pinmap to use PWM7 +pinmap.set_pin_function("A19", "PWM7") + + + +def angle_to_duty(percent): + return (SERVO_MAX_DUTY - SERVO_MIN_DUTY) * percent / 100.0 + SERVO_MIN_DUTY + + +out = pwm.PWM(pwm_id, freq=SERVO_PERIOD, duty=angle_to_duty(0), enable=True) + +for i in range(100): + out.duty(angle_to_duty(i)) + time.sleep_ms(100) + +for i in range(100): + out.duty(angle_to_duty(100 - i)) + time.sleep_ms(100) diff --git a/examples/vision/display/display_backlight.py b/examples/vision/display/display_backlight.py new file mode 100644 index 00000000..130d57d4 --- /dev/null +++ b/examples/vision/display/display_backlight.py @@ -0,0 +1,19 @@ +from maix import pwm, time, display, image + +disp = display.Display() + +def show(i): + img = image.Image(disp.width(), disp.height()) + img.draw_circle(disp.width() // 2, disp.height() //2, 50, image.COLOR_RED, thickness=-1) + img.draw_string(2, 2, f"{i}%", image.COLOR_WHITE, scale=2) + disp.show(img) + +for i in range(100): + disp.set_backlight(i) + show(i) + time.sleep_ms(50) + +for i in range(100): + disp.set_backlight(100 - i) + show(100 - i) + time.sleep_ms(50) diff --git a/maix/version.py b/maix/version.py index f4d66a0d..7042eff9 100644 --- a/maix/version.py +++ b/maix/version.py @@ -3,6 +3,6 @@ version_major = 4 version_minor = 1 -version_patch = 0 +version_patch = 2 __version__ = "{}.{}.{}".format(version_major, version_minor, version_patch)