K210-About the basic operations of K210

1.Light up the RGB light

from modules import ybrgb
RGB = ybrgb()
#设置RGB颜色:RGB.set(r, g, b)
#参数r控制红灯开关,
#参数g控制绿灯开关,
#参数b控制蓝灯开关,
#输入0表示关闭,输入1表示开启。
RGB.set(1, 0, 0)

2.Button function

from modules import ybkey
from modules import ybrgb
import time


KEY = ybkey()
RGB = ybrgb()

while True:
#读取按键K1是否被按下,返回0表示释放状态,返回1表示按下状态。
    state = KEY.is_press()
    if state == 1 : RGB.set(1,0,0)
    else :RGB.set(0,0,0)
    print("key press:", state)    
    time.sleep_ms(100)

3. Timer

timer = machine.Timer(id, channel, 
mode=Timer.MODE_ONE_SHOT, 
period=1000, 
unit=Timer.UNIT_MS,
callback=None, 
arg=None, 
start=True,
priority=1,
div=0)

Insert image description here

from modules import ybkey
from modules import ybrgb
from machine import Timer
import time


KEY = ybkey()
RGB = ybrgb()

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(time.ticks_ms())
except:
    timer.deinit()
    del timer

4.PWM breathing light experiment

Since the PWM signal needs to come from a timer, create a new timer object and set the parameters to timer 0, channel 0, and PWM mode. PWM output depends on timers. Currently, K210 has a total of 3 timers, each timer has 4 channels, so it can output up to 12 PWM signals. PWM duty cycle is the ratio of output high level time to period.

tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)

Create a new PWM object with specified parameters

pwm = machine.PWM(tim, freq, duty, pin, enable=True)

Insert image description here
The pins corresponding to the RGB lights are
red RED=27
green GREEN=26
blue BLUE=29

from machine import Timer, PWM
import time


tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)

pwm = PWM(tim, 1000, 0, 27, enable=True)


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)

LCD display

import lcd
import time
# 初始化lcd显示屏。
lcd.init()
#把屏幕设置为蓝色背景,在屏幕左上角显示“Hello Yahboom”。
lcd.clear(lcd.PURPLE)
time.sleep(1)
lcd.rotation(0)
lcd.draw_string(30, 30, "Hello Yahboom", lcd.WHITE, lcd.BLUE)

Among them, lcd.clear(color)the parameter color is the setting color, optional BLACK (black)
NAVY (navy blue)
DARKGREEN (dark green)
DARKCYAN (dark cyan)
MAROON (brown)
PURPLE (purple)
OLIVE (olive)
LIGHTGREY (light gray)
DARKGREY (Dark grey)
BLUE (blue)
GREEN (green)
CYAN (cyan)
RED (red)
MAGENTA (magenta)
YELLOW (yellow)
WHITE (white)
ORANGE (orange)
GREENYELLOW (yellow-green)
PINK (pink).

lcd.rotation(dir)The parameter dir is to set the rotation direction
dir=0 for front display
dir=1 means the screen rotates 90 degrees clockwise
dir=2 means the screen rotates 180 degrees clockwise
dir=3 means the screen rotates 270 degrees clockwise.

lcd.draw_string(x, y, "contents", color_text, color_background)The parameters
x and y represent the starting coordinates of the text,
"contents" represents the displayed characters
, color_text represents the display color of the string,
color_background represents the background color of the string

time.sleep(1) means delaying for 1 second to ensure enough time to display the content.

6. Touchpad to read coordinates

image.draw_string(x, y, text[, color[, scale=1[, x_spacing=0[, y_spacing=0[, mono_space=True]]]]])
Indicates adding string
x, y: Indicates the coordinates of the starting point of the string
text: Indicates the displayed string content
color: Indicates the color RGB value
scale: Indicates the string size
x_spacing: Indicates the word spacing, allowing to add between characters (if it is positive number) or subtract (if negative) x pixels
y_spacing: Indicates line spacing, allowing adding (if positive) or subtracting (if negative) y pixels between characters
mono_space: Forces text spacing to be fixed, defaults to True. For large text, you can set False to obtain non-fixed width character spacing, which will have better display effects.

(status, x, y) = ts.read()Indicates reading the status of the current screen and the coordinate value of the touched point; the return value is a tuple composed of 3 integer values, status: status, the values ​​​​are STATUS_RELEASE=1, STATUS_PRESS=2, STATUS_MOVE=3, x : X-axis coordinate of the touch point, y: Y-axis coordinate of the touch point;

img.draw_line(x0, y0, x1, y1[, color[, thickness=1]])Indicates drawing a line on the img image, x0, y0 represents the starting point coordinates, x1, y1 represents the end point coordinates, color represents the color, the default is white, and thickness represents the thickness of the line in pixels;

lcd.display(img)Indicates that the LCD displays the img image.

import touchscreen as ts
import lcd, image
import time

lcd.init()
#初始化触摸屏幕
ts.init()
#通过image库新建一个空白图像
img = image.Image()
img.draw_string(100, 0, "Please touch the screen", color=(0, 0, 255), scale=1)
#初始化相关变量
status_last = ts.STATUS_IDLE
x_last = 0
y_last = 0


lcd.display(img)
while True:
    #读取屏幕的状态
    (status, x, y) = ts.read()
    #如果状态改变
    if status_last != status:
        print(status, x, y)
        status_last = status
    #如果状态为移动状态
    if status == ts.STATUS_MOVE:
        在图片上(x_last,y_last)到(x, y)画一条线
        img.draw_line(x_last, y_last, x, y)
    #如果是按下状态
    elif status == ts.STATUS_PRESS:
        img.draw_line(x, y, x, y)
    #显示img图片
    lcd.display(img)
    x_last = x
    y_last = y

7. Read and write files from memory card

import sensor, lcd
import time

#初始化LCD显示屏和摄像头,初始化完成后打印“init ok”。
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
sensor.skip_frames(10)
print("init ok")

#创建图片保存路径img_path
img_path = "/sd/image-1.jpg"

#打开摄像头捕获一帧图像,保存为img,并把img保存到内存卡中,名称为image-1.jpg。
img = sensor.snapshot()
print("save image")
img.save(img_path)

#将image-1.jpg从内存卡读取出来,并在LCD上显示。
#print("read image")
#img_read = image.Image(img_path)
#lcd.display(img_read)
#print("ok")

Guess you like

Origin blog.csdn.net/ai_moe/article/details/132672748