mv:保存到文件,读取文件,可用于调节参数。

# Untitled - By: dell - Tue Aug 6 2024
import sensor, image, time
import sensor, image, time
from pyb import UART
from pyb import Timer
from pyb import LED
from pyb import Pin
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 50)
#PB10,PB11
uart = UART(3, 9600, timeout_char=200)
clock = time.clock()

led_board_E3  = Pin('E3', Pin.OUT, Pin.PULL_NONE)

TaskFlag=-1
def Uart_Receive():   #UART接收 改变框小球的颜色阈值
        global TaskFlag
        temp_data0 = uart.readchar()
        if temp_data0==0x55:  #帧头相等
            FunNum = uart.readchar()  #再读下一个
            print(FunNum)
            if FunNum ==0x01:
                TaskFlag =1
                led_board_E3.off() #用状态灯指示

            elif  FunNum ==0x02:
                TaskFlag =2
                led_board_E3.on()






def tick(timer):            # we will receive the timer object when being called
    global TaskFlag
    if uart.any():
        Uart_Receive()



#开机闪烁LED灯几次
#led_board_E3.on()
#time.sleep_ms(200)
#led_board_E3.off()
#time.sleep_ms(200)

#led_board_E3.on()
#time.sleep_ms(200)
#led_board_E3.off()
#time.sleep_ms(200)

#led_board_E3.on()
#time.sleep_ms(200)
#led_board_E3.off()
#time.sleep_ms(200)

#led_board_E3.on()
#time.sleep_ms(200)
#led_board_E3.off()
#time.sleep_ms(200)

#led_board_E3.on()
#time.sleep_ms(200)
#led_board_E3.off()
#time.sleep_ms(200)

#设置定时器,以及定时频率
tim = Timer(4, freq=50)      # create a timer object using timer 4 - trigger at 5Hz
tim.callback(tick)          # set the callback to our tick function


#设置文件路径
#file_abs ="save.txt"
#file_abs = "para.ini"
file_abs = "data.bin"

#with open(file_abs,"r") as f:
#    print(f.read())

#打开文件,读取
f =open(file_abs,"r")
content=""
try :
    while(True):
        chunk = f.read(50) #连续读取50个字符
        #print(chunk)
        if not chunk: #
           break

        content +=chunk
finally:
    f.close()  #及时关闭文件
print(content)

print("open ok and close\r\n,..now write test")

f =open(file_abs,"w")  #读取文件
a0=11100
a1=22441
a2=34147
b0=47414
b1=55475
b2=67645
#写入数据
f.write(str(a0)+","+str(a1)+","+str(a2)+","+str(b0)+","+str(b1)+","+str(b2))
time.sleep_ms(10) #等待写入完成
f.flush() #缓存刷新
f.close() #关闭文件
print("write ok and close\r\n")


#测试写入的数据,读取出来,看下写入是不是成功,能不能从文件读取数据,然后将数据进行运用
f =open(file_abs,"r")
content=""
try :
    while(True):
        chunk = f.read(30) #读取文件
        #print(chunk)
        if not chunk:
           break

        content +=chunk
finally:
    f.close()  #关闭文件
print(content)
print(type(content))
readnum = content.split(",")
print(readnum)

r0=int(readnum[0])+100
r1=int(readnum[1])+100
r2=int(readnum[2])+100
r3=int(readnum[3])+2
r4=int(readnum[4])+2
r5=int(readnum[5])+2
print(r0)
print(r1)
print(r2)
print(r3)
print(r4)



while(True):
    clock.tick()
    img = sensor.snapshot()
    if TaskFlag==1 :  #如果是第一个功能,发送二维码数据
             print('task01:',TaskFlag)
             time.sleep_ms(100)

    if TaskFlag==2 :  #如果是第2个功能,发送颜色数据的位置
           print('task02:',TaskFlag)
           time.sleep_ms(100)

  #  print(clock.fps())

在这里插入图片描述
在这里插入图片描述
首先要在U盘里面新建一个和代码读取文件的文件名字。
后面才能读取。
具体的r,w,等,或者追加的。再看具体应用。

运行程序之后,需要按下复位按键,重新刷新代码才行。
具体的原因没测试过。

可以用于比赛前参数的调试,按键调节参数,然后保存到文件中。
按键1,调节参数;
按键2,读取文件;
按键3,读取文件,调节参数。

后面根据实际需要再调节。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值