Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
Signed-off-by: huangziyi <[email protected]>
  • Loading branch information
MrThanlon committed Oct 21, 2024
1 parent d845fd9 commit 9897f57
Show file tree
Hide file tree
Showing 158 changed files with 960 additions and 107 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions share/qtcreator/examples/02-Media/rtsp_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Description: This example demonstrates how to stream video and audio to the network using the RTSP server.
#
# Note: You will need an SD card to run this example.
#
# You can run the rtsp server to stream video and audio to the network

from time import *
from media.rtspserver import * #导入rtsp server 模块
import os
import time

def rtsp_server_test():
rtspserver = RtspServer() #创建rtsp server对象
rtspserver.start() #启动rtsp server
print("rtsp server start:",rtspserver.get_rtsp_url()) #打印rtsp server start

time_start = time.time() #获取当前时间
try:
while(time.time() - time_start < 30):
time.sleep(0.1)
os.exitpoint()
except KeyboardInterrupt as e:
print("user stop: ", e)
except BaseException as e:
sys.print_exception(e)

rtspserver.stop() #停止rtsp server
print("rtsp server stop") #打印rtsp server stop

if __name__ == "__main__":
os.exitpoint(os.EXITPOINT_ENABLE)
rtsp_server_test()
print("rtsp server done")
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from media.media import *
import time, os

# NOT WORK NOW!!!

def venc_test():
print("venc_test start")
width = 1280
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions share/qtcreator/examples/03-Machine/adc/adc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from machine import ADC

# 实例化ADC通道0
adc = ADC(0)
# 获取ADC通道0采样值
print(adc.read_u16())
# 获取ADC通道0电压值
print(adc.read_uv(), "uV")
34 changes: 34 additions & 0 deletions share/qtcreator/examples/03-Machine/fft/fft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 基础示例
#
# 欢迎使用CanMV IDE, 点击IDE左下角的绿色按钮开始执行脚本

from machine import FFT
import array
import math
from ulab import numpy as np
PI = 3.14159265358979323846264338327950288419716939937510

rx = []
def input_data():
for i in range(64):
data0 = 10 * math.cos(2 * PI * i / 64)
data1 = 20 * math.cos(2 * 2 * PI * i / 64)
data2 = 30 * math.cos(3 * 2 * PI * i / 64)
data3 = 0.2 * math.cos(4 * 2 * PI * i / 64)
data4 = 1000 * math.cos(5 * 2 * PI * i / 64)
rx.append((int(data0 + data1 + data2 + data3 + data4)))
input_data() #初始化需要进行FFT的数据,列表类型
print(rx)
data = np.array(rx,dtype=np.uint16) #把列表数据转换成数组
print(data)
fft1 = FFT(data, 64, 0x555) #创建一个FFT对象,运算点数为64,偏移是0x555
res = fft1.run() #获取FFT转换后的数据
print(res)

res = fft1.amplitude(res) #获取各个频率点的幅值
print(res)

res = fft1.freq(64,38400) #获取所有频率点的频率值
print(res)


18 changes: 18 additions & 0 deletions share/qtcreator/examples/03-Machine/fpioa/fpioa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from machine import FPIOA

# 实例化FPIOA
fpioa = FPIOA()
# 打印所有引脚配置
fpioa.help()
# 打印指定引脚详细配置
fpioa.help(0)
# 打印指定功能所有可用的配置引脚
fpioa.help(FPIOA.IIC0_SDA, func=True)
# 设置Pin0为GPIO0
fpioa.set_function(0, FPIOA.GPIO0)
# 设置Pin2为GPIO2, 同时配置其它项
fpioa.set_function(2, FPIOA.GPIO2, ie=1, oe=1, pu=0, pd=0, st=1, sl=0, ds=7)
# 获取指定功能当前所在的引脚
fpioa.get_pin_num(FPIOA.UART0_TXD)
# 获取指定引脚当前功能
fpioa.get_pin_func(0)
27 changes: 27 additions & 0 deletions share/qtcreator/examples/03-Machine/i2c/i2c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from machine import I2C

i2c4=I2C(4) # init i2c4

a=i2c4.scan() #scan i2c slave
print(a)

i2c4.writeto_mem(0x3b,0xff,bytes([0x80]),mem_size=8) # write hdmi page address(0x80)
i2c4.readfrom_mem(0x3b,0x00,1,mem_size=8) # read hdmi id0 ,value =0x17
i2c4.readfrom_mem(0x3b,0x01,1,mem_size=8) # read hdmi id1 ,value =0x2

i2c4.writeto(0x3b,bytes([0xff,0x80]),True) # write hdmi page address(0x80)
i2c4.writeto(0x3b,bytes([0x00]),True) #send the address0 of being readed
i2c4.readfrom(0x3b,1) #read hdmi id0 ,value =0x17
i2c4.writeto(0x3b,bytes([0x01]),True) #send the address1 of being readed
i2c4.readfrom(0x3b,1) #read hdmi id0 ,value =0x17

i2c4.writeto_mem(0x3b,0xff,bytes([0x80]),mem_size=8) # write hdmi page address(0x80)
a=bytearray(1)
i2c4.readfrom_mem_into(0x3b,0x0,a,mem_size=8) # read hdmi id0 into a ,value =0x17
print(a) #printf a,value =0x17

i2c4.writeto(0x3b,bytes([0xff,0x80]),True) # write hdmi page address(0x80)
i2c4.writeto(0x3b,bytes([0x00]),True) #send the address0 of being readed
b=bytearray(1)
i2c4.readfrom_into(0x3b,b) #read hdmi id0 into b ,value =0x17
print(b) #printf a,value =0x17
34 changes: 34 additions & 0 deletions share/qtcreator/examples/03-Machine/pin/pin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from machine import Pin
from machine import FPIOA

# 实例化FPIOA
fpioa = FPIOA()
# 设置Pin2为GPIO2
fpioa.set_function(2, FPIOA.GPIO2)

# 实例化Pin2为输出
pin = Pin(2, Pin.OUT, pull=Pin.PULL_NONE, drive=7)
# 设置输出为高
pin.value(1)
# pin.on()
# pin.high()
# 设置输出为低
pin.value(0)
# pin.off()
# pin.low()
# 初始化Pin2为输入
pin.init(Pin.IN, pull=Pin.PULL_UP, drive=7)
# 获取输入
print(pin.value())
# 设置模式
pin.mode(Pin.IN)
# 获取模式
print(pin.mode())
# 设置上下拉
pin.pull(Pin.PULL_NONE)
# 获取上下拉
print(pin.pull())
# 设置驱动能力
pin.drive(7)
# 获取驱动能力
print(pin.drive())
17 changes: 17 additions & 0 deletions share/qtcreator/examples/03-Machine/pwm/pwm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from machine import PWM
from machine import FPIOA

# 实例化FPIOA
fpioa = FPIOA()
# 设置PIN60为PWM通道0
fpioa.set_function(60, fpioa.PWM0)
# 实例化PWM通道0,频率为1000Hz,占空比为50%,默认使能输出
pwm0 = PWM(0, 1000, 50, enable = True)
# 关闭通道0输出
pwm0.enable(0)
# 调整通道0频率为2000Hz
pwm0.freq(2000)
# 调整通道0占空比为40%
pwm0.duty(40)
# 打开通道0输出
pwm0.enable(1)
8 changes: 8 additions & 0 deletions share/qtcreator/examples/03-Machine/rtc/rtc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from machine import RTC

# 实例化RTC
rtc = RTC()
# 获取当前时间
print(rtc.datetime())
# 设置当前时间
rtc.init((2024,2,28,2,23,59,0,0))
35 changes: 35 additions & 0 deletions share/qtcreator/examples/03-Machine/spi/spi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from machine import SPI
from machine import FPIOA
a = FPIOA()

a.help(14)
a.set_function(14,a.QSPI0_CS0)
a.help(14)

a.help(15)
a.set_function(15,a.QSPI0_CLK)
a.help(15)

a.help(16)
a.set_function(16,a.QSPI0_D0)
a.help(16)

a.help(17)
a.set_function(17,a.QSPI0_D1)
a.help(17)

spi=SPI(1,baudrate=5000000, polarity=0, phase=0, bits=8) # spi init clock 5MHz, polarity 0, phase 0, data bitwide 8bits

spi.write(bytes([0x66])) # enable gd25lq128 reset

spi.write(bytes([0x99])) # gd25lq128 reset

a=bytes([0x9f]) # send buff
b=bytearray(3) # receive buf
spi.write_readinto(a,b) # read gd25lq128 id
print(b) # bytearray(b'\xc8`\x18')

a=bytes([0x90,0,0,0]) # send buff
b=bytearray(2) # receive buf
spi.write_readinto(a,b) # read gd25lq128 id
print(b) # bytearray(b'\xc8\x17')
13 changes: 13 additions & 0 deletions share/qtcreator/examples/03-Machine/timer/timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from machine import Timer
import time

# 实例化一个软定时器
tim = Timer(-1)
# 初始化定时器为单次模式,周期100ms
tim.init(period=100, mode=Timer.ONE_SHOT, callback=lambda t:print(1))
time.sleep(0.2)
# 初始化定时器为周期模式,频率为1Hz
tim.init(freq=1, mode=Timer.PERIODIC, callback=lambda t:print(2))
time.sleep(2)
# 释放定时器资源
tim.deinit()
7 changes: 7 additions & 0 deletions share/qtcreator/examples/03-Machine/touch/touch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from machine import TOUCH

# 实例化TOUCH设备0
tp = TOUCH(0)
# 获取TOUCH数据
p = tp.read()
print(p)
23 changes: 23 additions & 0 deletions share/qtcreator/examples/03-Machine/uart/uart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from machine import UART
from machine import FPIOA

# 实例化FPIOA
fpioa = FPIOA()
# 设置PIN60为PWM通道0
fpioa.set_function(5, fpioa.UART2_TXD)
fpioa.set_function(6, fpioa.UART2_RXD)
# UART2: baudrate 115200, 8bits, parity none, one stopbits
uart = UART(UART.UART2, baudrate=115200, bits=UART.EIGHTBITS, parity=UART.PARITY_NONE, stop=UART.STOPBITS_ONE)
# UART write
r = uart.write("UART test")
print(r)
# UART read
r = uart.read()
print(r)
# UART readline
r = uart.readline()
print(r)
# UART readinto
b = bytearray(8)
r = uart.readinto(b)
print(r)
49 changes: 49 additions & 0 deletions share/qtcreator/examples/03-Machine/uart/uart1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from machine import UART
from machine import FPIOA
import time

# 实例化FPIOA
from machine import FPIOA
fpioa = FPIOA()
#pin3 设置为串口1发送管脚
fpioa.set_function(3, fpioa.UART1_TXD)
#设置pin4为串口1接收管脚
fpioa.set_function(4, fpioa.UART1_RXD)
#使能pin3的输入输出功能
fpioa.set_function(3,set_ie=1,set_oe=1)
#使能pin3的输入输出功能
fpioa.set_function(4,set_ie=1,set_oe=1)


#fpioa.set_function(5, fpioa.UART2_TXD)
#fpioa.set_function(6, fpioa.UART2_RXD)


#UART: baudrate 115200, 8bits, parity none, one stopbits
uart = UART(UART.UART1, baudrate=115200, bits=UART.EIGHTBITS, parity=UART.PARITY_NONE, stop=UART.STOPBITS_ONE)
#打印串口配置
print(uart)
# UART write
r = uart.write("UART test wwwwwwwwwwwww")
print(r)
# UART read
r = uart.read()
print(r)
# UART readline
r = uart.readline()
print(r)
# UART readinto
b = bytearray(8)
r = uart.readinto(b)
print(r)
i=0
while True:
#print( "xxx %d" % (0) )
uart.write("i={0}".format(0))
i=i+1
#print(uart.read())
a=uart.read()
if len(a) > 1 :
print(a,len(a))
time.sleep(0.1)

9 changes: 9 additions & 0 deletions share/qtcreator/examples/03-Machine/wdt/wdt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import time
from machine import WDT

# 实例化wdt1,timeout为3s
wdt1 = WDT(1,3)
time.sleep(2)
# 喂狗操作
wdt1.feed()
time.sleep(2)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9897f57

Please sign in to comment.