if __name__ == "__main__": sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time=2000) i2c = I2C(I2C.I2C0, freq=100000, scl=6, sda=7) # devices = i2c.scan() # print(devices) fm.register(8, fm.fpioa.GPIOHS0, force=True) XSHUT = GPIO(GPIO.GPIOHS0, GPIO.OUT) XSHUT.value(1) distance = VL53L1X(i2c) while True: mm = distance.read() utime.sleep_ms(100) img = sensor.snapshot() img.draw_string(10, lcd.height() - 30, "range: " + str(mm / 10) + " cm", lcd.RED, scale=2) lcd.display(img)
import uos import os import KPU as kpu from fpioa_manager import * from machine import I2C from Maix import I2S, GPIO # # initialize # lcd.init() lcd.rotation(2) fm.register(board_info.SPK_SD, fm.fpioa.GPIO0) spk_sd = GPIO(GPIO.GPIO0, GPIO.OUT) spk_sd.value(1) #Enable the SPK output fm.register(board_info.SPK_DIN, fm.fpioa.I2S0_OUT_D1) fm.register(board_info.SPK_BCLK, fm.fpioa.I2S0_SCLK) fm.register(board_info.SPK_LRCLK, fm.fpioa.I2S0_WS) wav_dev = I2S(I2S.DEVICE_0) fm.register(board_info.BUTTON_A, fm.fpioa.GPIO1) but_a = GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! fm.register(board_info.BUTTON_B, fm.fpioa.GPIO2) but_b = GPIO(GPIO.GPIO2, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! def checkForM5StickV():
labelText = class_labels[len(class_labels) - 1].strip() unknown = True acc = ('{:.1f}%'.format(pmax * 100)) fps = clk.fps() time_per_frame = 1000.0 / fps lcd.display(img) lcd.draw_string(10, 10, labelText) if unknown == False: lcd.draw_string(10, 30, acc) lcd.draw_string(10, 60, (str(int(fps)) + ' fps')) lcd.draw_string(10, 80, (str(round(time_per_frame, 1)) + ' ms')) # Read btn if btn_a.value() == 0: btn_trigger = True utime.sleep_ms(500) # Store image of button/ts was pressed if btn_trigger and img_logging: annotation = labelText + ' (' + acc + ')' log_name = get_filename(img_log_path) log_path = img_log_path + '/' + log_name save_image(img, log_path, annotation, 10, 10) btn_trigger = False elif op_mode == 1: # Operation mode single shot icon_dir = '/sd/media/'
i2c = I2C(I2C.I2C0, freq = 100000, scl = 28, sda = 29) devices = i2c.scan() time.sleep_ms(10) print("i2c", devices) # レジスタを設定 fm.register(board_info.SPK_SD, fm.fpioa.GPIO0) fm.register(board_info.SPK_DIN, fm.fpioa.I2S0_OUT_D1) fm.register(board_info.SPK_BCLK, fm.fpioa.I2S0_SCLK) fm.register(board_info.SPK_LRCLK, fm.fpioa.I2S0_WS) # GPIO設定 spk_sd = GPIO(GPIO.GPIO0, GPIO.OUT) # スピーカーを有効化 spk_sd.value(1) # 再生デバイスを設定 wav_dev = I2S(I2S.DEVICE_0) ################################################## # function ################################################## # 音声ファイルを再生 def play_sound(filename): try: # 音声ファイルを読み込み player = audio.Audio(path = filename) # 音量を設定 player.volume(100) # 再生情報を取得
autosave_cnt = findMaxBMP_IDinDir("/sd/rawautosave") + 1 print("------------------") print("##: Current raw auto save file index: " + str(autosave_cnt)) print("------------------") RGB_LED_PURPLE() time.sleep(0.5) except: currentImage = 0 print("Get current raw auto save file index failed") time.sleep(0.2) pass print("##: press Btn_A to save img as MJPEG movie") time.sleep(0.5) if Btn_A.value() == 0 and isButtonPressedA == 0: print("##: MJPG_mode set!") MJPG_mode = True try: os.mkdir("/sd/rawmoviestream") print("mkdir: /sd/rawmoviestream") except Exception as e: pass else: MJPG_mode = False print("##: BMP mode !") gc.collect() if OV77XX_EN: #sensor.reset(freq=20000000, set_regs=True, dual_buff=False) #OV7740 Loop Time :155ms, run fps:6.451613 #sensor.reset(freq=20000000, set_regs=True, dual_buff=True) #OV7740 Loop Time :91ms, run fps:10.98901
print("UnitV facedetect demo v1.1 by Zell, 24.04.2020") print("GROVE port GND, VCC_5V, G35,G34 function: NA") print("UnitV Btn_A and Btn_B init.") #UnitV btns #Btn A, capature image fm.register(18, fm.fpioa.GPIO1) Btn_A=GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! #Btn B fm.register(19, fm.fpioa.GPIO2) Btn_B = GPIO(GPIO.GPIO2, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! isButtonPressedA = 0 isButtonPressedB = 0 if Btn_A.value() == 0: #If dont want to run the demo sys.exit() time.sleep(0.5) # Delay for few seconds to see the start-up screen :p import sensor import KPU as kpu err_counter = 0 while 1: try: #sensor.reset() #Reset sensor may failed, let's try sometimes
lcd.init() lcd.draw_string(0, 0, "Button Sample", lcd.WHITE, lcd.BLACK) # init button fm.register(board_info.BUTTON_A, fm.fpioa.GPIO1) fm.register(board_info.BUTTON_B, fm.fpioa.GPIO2) button_a = GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! button_b = GPIO(GPIO.GPIO2, GPIO.IN, GPIO.PULL_UP) #PULL_UP is required here! try: while(True): lcd.draw_string(0, 15, "Running", lcd.BLACK, lcd.RED) lcd.draw_string(0, 30, "ButtonA : " + str(button_a.value()), lcd.WHITE, lcd.BLACK) lcd.draw_string(0, 45, "ButtonB : " + str(button_b.value()), lcd.GREEN, lcd.BLACK) button_function(button_a, 60) button_function(button_b, 75) utime.sleep(0.001) except Exception as e: sys.print_exception(e) finally: print("Finished") sys.exit()
fm.register(board_info.BUTTON_B, fm.fpioa.GPIO2) # GPIO設定 button_a = GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) button_b = GPIO(GPIO.GPIO2, GPIO.IN, GPIO.PULL_UP) ################################################## # main ################################################## # ボタン押下中フラグ button_a_pressed = 0 button_b_pressed = 0 while True: # Aボタンが押された場合 if button_a.value() == 0 and button_a_pressed == 0: print("Button A Press") button_a_pressed = 1 # Aボタンが放された場合 if button_a.value() == 1 and button_a_pressed == 1: print("Button A release") button_a_pressed = 0 # Bボタンが押された場合 if button_b.value() == 0 and button_b_pressed == 0: print("Button B Press") button_b_pressed = 1 # Bボタンが放された場合 if button_b.value() == 1 and button_b_pressed == 1: print("Button B release") button_b_pressed = 0
basename = "capture" ext = ".avi" no = 1 while True: while True: img = sensor.snapshot() img.draw_string(img.width() // 2 - 100, img.height() // 2 - 4, "STOP", color=(0, 0, 255), scale=2, mono_space=False) lcd.display(img) if but_a.value() == 0: while but_a.value() != 0: break break if but_b.value() == 0: sensor.run(0) lcd.clear() sys.exit() lcd.clear() print("Start") try: f = open("/sd/" + noname, "r")
fm.register(board_info.BUTTON_B, fm.fpioa.GPIO2) but_b = GPIO(GPIO.GPIO2, GPIO.IN, GPIO.PULL_UP) isButtonPressedA = 0 isButtonPressedB = 0 lcd.init() lcd.rotation(2) sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_windowing((224, 224)) sensor.run(1) cnt = 0 while True: if but_a.value() == 0 and isButtonPressedA == 0: cnt = cnt + 1 isButtonPressedA = 1 if but_a.value() == 1 and isButtonPressedA == 1: isButtonPressedA = 0 img = sensor.snapshot() if cnt == 1: img.negate() img.draw_string(10, 60, "negate", color=(255, 0, 0)) elif cnt == 2: img.cartoon(seed_threshold=0.05, floating_thresholds=0.05) img.draw_string(10, 60, "cartoon", color=(255, 0, 0)) elif cnt == 3: img.histeq(adaptive=True, clip_limit=3)
import utime from Maix import GPIO from fpioa_manager import fm fm.register(34, fm.fpioa.GPIOHS0) p34 = GPIO(GPIO.GPIOHS0, GPIO.OUT) while True: p34.value(0) utime.sleep_ms(1000) p34.value(1) utime.sleep_ms(1000)
import sensor, image, time, lcd from FingerBitShield import * from Maix import GPIO from fpioa_manager import fm fm.register(23, fm.fpioa.GPIOHS7, force=True) led_bl = GPIO(GPIO.GPIOHS7, GPIO.OUT) led_bl.value(1) #按钮初始化,设置两按钮为上拉模式 fm.register(37, fm.fpioa.GPIOHS6, force=True) fm.register(36, fm.fpioa.GPIOHS5, force=True) key1 = GPIO(GPIO.GPIOHS6, GPIO.PULL_UP) key2 = GPIO(GPIO.GPIOHS5, GPIO.PULL_UP) motor = motorPro() #初始化电机 motor.setSpeed(0, 0) #设置速度为0 lcd.init(freq=60000000) #初始化屏幕 lcd.rotation(1) #旋转屏幕 sensor.reset() #重置并初始化传感器。 sensor.set_pixformat(sensor.RGB565) #将像素格式设置为RGB565 sensor.set_hmirror(0) #摄像头水平镜像设置 sensor.set_vflip(1) #摄像头垂直镜像设置 sensor.set_framesize(sensor.QQVGA) #设置图像格式QQVGA(160*120) sensor.skip_frames(time=2000) #等待设置生效 ROI = (60, 40, 40, 40) #感兴趣区域 DEVIATION = 25 #颜色偏差值 AREX_MAX = 10000 #图像最大面积 AREX_MIN = 2000 #图像最小面积
import sensor,image,lcd,time import KPU as kpu from Maix import I2S, GPIO from fpioa_manager import fm from machine import UART import struct from time import sleep_ms, ticks_ms, ticks_diff ######## UART for Temperature fm.register (2, fm.fpioa.UART1_TX) fm.register (3, fm.fpioa.UART1_RX) uart_temp = UART (UART.UART1, 115200, 8, None, 1, timeout = 1000, read_buf_len = 4096) ######## GPIO For trig thermometer fm.register(10, fm.fpioa.GPIO1, force=True) triger=GPIO(GPIO.GPIO1,GPIO.OUT) triger.value(0) ######## Config Camera and Display sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_windowing((320, 224)) sensor.set_vflip(1) sensor.run(1) lcd.init(type=2, freq=20000000, color=lcd.BLACK) ######### config facemask detection task = kpu.load(0x400000) a = kpu.set_outputs(task, 0, 10,7,35) anchor = (0.212104,0.261834, 0.630488,0.706821, 1.264643,1.396262, 2.360058,2.507915, 4.348460,4.007944) a = kpu.init_yolo2(task, 0.5, 0.5, 5, anchor) ######### config detection stage = 0
from fpioa_manager import * import os, Maix, lcd, image from Maix import FPIOA, GPIO from main import main import utime test_pin = 16 fpioa = FPIOA() fpioa.set_function(test_pin, FPIOA.GPIO7) test_gpio = GPIO(GPIO.GPIO7, GPIO.IN) lcd.init(color=(255, 0, 0)) lcd.direction(lcd.YX_LRUD) lcd.draw_string(lcd.width() // 2 - 68, lcd.height() // 2 - 4, "Hackspace NSU", lcd.WHITE, lcd.RED) utime.sleep(2) if test_gpio.value() == 0: print('PIN 16 pulled down, enter test mode') # import sensor # import image # sensor.reset() # sensor.set_pixformat(sensor.RGB565) # sensor.set_framesize(sensor.QVGA) # sensor.run(1) # lcd.freq(16000000) # while True: # img = sensor.snapshot() # lcd.display(img) else: main()
from fpioa_manager import * from Maix import I2S, GPIO import audio ########### settings ############ WIFI_EN_PIN = 8 # AUDIO_PA_EN_PIN = None # Bit Dock and old MaixGo AUDIO_PA_EN_PIN = 32 # Maix Go(version 2.20) # AUDIO_PA_EN_PIN = 2 # Maixduino # disable wifi fm.register(WIFI_EN_PIN, fm.fpioa.GPIO0) wifi_en=GPIO(GPIO.GPIO0, GPIO.OUT) wifi_en.value(0) # open audio PA if AUDIO_PA_EN_PIN: fm.register(AUDIO_PA_EN_PIN, fm.fpioa.GPIO1) wifi_en=GPIO(GPIO.GPIO1, GPIO.OUT) wifi_en.value(1) # register i2s(i2s0) pin fm.register(34,fm.fpioa.I2S0_OUT_D1) fm.register(35,fm.fpioa.I2S0_SCLK) fm.register(33,fm.fpioa.I2S0_WS) # init i2s(i2s0) wav_dev = I2S(I2S.DEVICE_0) # init audio
# I2C Check i2c = I2C(I2C.I2C0, freq=100000, scl=28, sda=29) devices = i2c.scan() print(devices) lcd.init() devices = os.listdir("/") os.chdir("/sd") print(os.listdir()) #################### Speaker fm.register(board_info.SPK_SD, fm.fpioa.GPIO0) spk_sd=GPIO(GPIO.GPIO0, GPIO.OUT) spk_sd.value(1) fm.register(board_info.SPK_DIN,fm.fpioa.I2S0_OUT_D1) fm.register(board_info.SPK_BCLK,fm.fpioa.I2S0_SCLK) fm.register(board_info.SPK_LRCLK,fm.fpioa.I2S0_WS) wav_dev = I2S(I2S.DEVICE_0) def play_wav(fname): player = audio.Audio(path = fname) player.volume(70) wav_info = player.play_process(wav_dev) wav_dev.channel_config(wav_dev.CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE) wav_dev.set_sample_rate(wav_info[1]) while True: ret = player.play()
class Upgrade(): def __init__(self, ssid=None, passwd=None): if not ssid: raise Exception("no SSID") self.ssid = ssid self.passwd = passwd print("[init SSID:", ssid, ", PASSWD:", passwd) fm.register(0, fm.fpioa.GPIOHS1, force=True) wifi_io0_en = GPIO(GPIO.GPIOHS1, GPIO.OUT) wifi_io0_en.value(0) fm.register(8, fm.fpioa.GPIOHS0) self.wifi_en = GPIO(GPIO.GPIOHS0, GPIO.OUT) fm.register(board_info.WIFI_RX, fm.fpioa.UART2_TX) fm.register(board_info.WIFI_TX, fm.fpioa.UART2_RX) self.uart = UART(UART.UART2, 115200, timeout=1000, read_buf_len=4096) self.update_step = 0 def wifi_enable(self, en): self.wifi_en.value(en) def reboot(self): print("reoobt") self.wifi_enable(0) time.sleep_ms(200) self.wifi_enable(1) time.sleep_ms(2000) print("reoobt end") def cmd_set_station_mode(self): print("[cmd station mode]") recv = self.uart.read() print(recv) self.uart.write(b'AT+CWMODE_DEF=1\r\n') time.sleep_ms(200) recv = self.uart.read() print(recv) if b"OK" in recv: return True return False def cmd_join_ap_and_wait(self): print("[cmd join ap]") recv = self.uart.read() print(recv) if self.passwd: self.uart.write(b'AT+CWJAP_DEF="{}","{}"\r\n'.format( self.ssid, self.passwd)) else: self.uart.write(b'AT+CWJAP_DEF="{}"\r\n'.format(self.ssid)) time.sleep_ms(200) print("[wait join ap -- 0]") read = b"" tim = time.ticks_ms() while 1: if time.ticks_ms() - tim > 10000: return False recv = self.uart.read() if recv: print(recv) else: print(".", end='') if recv: read += recv if b"GOT IP" in read: return True def wait_join_ap(self): print("[wait join ap]") read = b"" tim = time.ticks_ms() while 1: if time.ticks_ms() - tim > 10000: raise Exception("wait for join AP timeout") recv = self.uart.read() if recv: print(recv) else: print(".", end='') if recv: read += recv if b"GOT IP" in read: break time.sleep_ms(1000) read = self.uart.read() print(read) def cmd_upgrade(self): print("[cmd upgrade]") self.update_step = 0 recv = self.uart.read() print(recv) self.uart.write(b'AT+CIUPDATE\r\n') time.sleep_ms(200) print("[wait upgrade process -- 0]") read = b"" tim = time.ticks_ms() while 1: if time.ticks_ms() - tim > 3000: return False recv = self.uart.read() if recv: print(recv) else: print(".", end='') if recv: read += recv if b"+CIPUPDATE:" in read: if b":4" in read: self.update_step = 4 return True time.sleep_ms(200) def cmd_restore(self): print("[cmd restore]") recv = self.uart.read() print(recv) self.uart.write(b'AT+RESTORE\r\n') self.wait_boot_up() def wait_upgrade(self): print("[wait upgrade process]") read = b"" tim = time.ticks_ms() while 1: if time.ticks_ms() - tim > 80000: raise Exception("wait for update timeout") recv = self.uart.read() if recv: print(recv) else: print(".", end='') if recv: read += recv if self.update_step != 4 and b"+CIPUPDATE:4" in read: self.update_step = 4 if self.update_step == 4 and b"OK" in read: break time.sleep_ms(200) def check_version(self): read = self.uart.read() print(read) self.uart.write("AT+GMR\r\n") time.sleep_ms(200) read = self.uart.read() print(read) if b"version" in read: return read.split(b"\r\n")[1:-2] else: return None def wait_boot_up(self): print("[wait boot up]") read = b"" tim = time.ticks_ms() while 1: if time.ticks_ms() - tim > 5000: raise Exception("wait boot up timeout") recv = self.uart.read() if recv: print(recv) else: print(".", end='') if recv: read += recv if b"ready\r\n" in read: break def upgrade(self): # reboot self.reboot() # wait boot up self.wait_boot_up() # set mode while not self.cmd_set_station_mode(): time.sleep_ms(1) # config AP # wait for join ok while not self.cmd_join_ap_and_wait(): time.sleep_ms(1) # reboot # maybe firmware v1.5 reboot by itself (bug) self.reboot() # wait for connect to AP self.wait_join_ap() # upgrade while not self.cmd_upgrade(): time.sleep_ms(1) self.wait_upgrade() time.sleep_ms(2000) self.wait_boot_up() self.cmd_restore() version = self.check_version() print("===============") for v in version: print(v.decode()) print("===============")
from fpioa_manager import fm from board import board_info ################################################## # initialize ################################################## # レジスタを設定 fm.register(board_info.BUTTON_A, fm.fpioa.GPIO1) fm.register(board_info.BUTTON_B, fm.fpioa.GPIO2) # GPIO設定 button_a = GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) button_b = GPIO(GPIO.GPIO2, GPIO.IN, GPIO.PULL_UP) ################################################## # main ################################################## while True: # AボタンとBボタンが同時に押されている場合 if button_a.value() == 0 and button_b.value() == 0: time.sleep_ms(200) print("Button A and B Press") # Aボタンのみ押されている場合 elif button_a.value() == 0: time.sleep_ms(200) print("Button A Press") # Bボタンのみ押されている場合 elif button_b.value() == 0: time.sleep_ms(200) print("Button B Press")
sensor.run(1) cnt_save = 0 cnt_show = 0 img_read = image.Image() print(os.listdir()) try: while True: img = sensor.snapshot() lcd.display(img) # Take Photos if button_a.value() == 0: cnt_save+=1 #fname = "/sd/photos/"+str(cnt_save)+".jpg" print("/sd/photos/mam1.jpg") img.save("/sd/photos/mam1.jpg", quality=95) # Display Photos on Screen if button_b.value() == 0: cnt_show+=1 img_read = image.Image("/sd/photos/man"+str(cnt_show)+".jpg") # "/sd/photos/man1.jpg" lcd.display(img_read) print("Show Image " + str(cnt_show)) time.sleep(1) except:
lcd.rotation(2) #Rotate the lcd 180deg try: img = image.Image("/flash/startup.jpg") lcd.display(img) except: lcd.draw_string(lcd.width()//2-100,lcd.height()//2-4, "Error: Cannot find start.jpg", lcd.WHITE, lcd.RED) from Maix import I2S, GPIO import audio from Maix import GPIO from fpioa_manager import * fm.register(board_info.SPK_SD, fm.fpioa.GPIO0) spk_sd=GPIO(GPIO.GPIO0, GPIO.OUT) spk_sd.value(1) #Enable the SPK output fm.register(board_info.SPK_DIN,fm.fpioa.I2S0_OUT_D1) fm.register(board_info.SPK_BCLK,fm.fpioa.I2S0_SCLK) fm.register(board_info.SPK_LRCLK,fm.fpioa.I2S0_WS) wav_dev = I2S(I2S.DEVICE_0) try: player = audio.Audio(path = "/flash/ding.wav") player.volume(10) wav_info = player.play_process(wav_dev) wav_dev.channel_config(wav_dev.CHANNEL_1, I2S.TRANSMITTER,resolution = I2S.RESOLUTION_16_BIT, align_mode = I2S.STANDARD_MODE) wav_dev.set_sample_rate(wav_info[1]) while True: ret = player.play()
# w,h,channels which must match the shape of the last layer of the model a = kpu.set_outputs(task, 0,7,7,30) # the anchors to be used by the yolo v2 algorithm. These are the anchors for standard yolo v2 tiny. # even though it is possible to recalculate them on our datasets, it wasn't noticed any difference anchor = (0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828) # initialize the yolo v2 network a = kpu.init_yolo2(task, 0.25, 0.3, 5, anchor) # clock is required to print the fps clock = time.clock() # for new MaixGO board, if not, remove it fm.register(0, fm.fpioa.GPIOHS1, force=True) wifi_io0_en=GPIO(GPIO.GPIOHS1, GPIO.OUT) wifi_io0_en.value(0) # En SEP8285 fm.register(8, fm.fpioa.GPIOHS0, force=True) wifi_en=GPIO(GPIO.GPIOHS0,GPIO.OUT) fm.register(board_info.WIFI_RX,fm.fpioa.UART2_TX, force=True) fm.register(board_info.WIFI_TX,fm.fpioa.UART2_RX, force=True) def wifi_enable(en): global wifi_en wifi_en.value(en) def wifi_reset(): global uart wifi_enable(0) time.sleep_ms(200)
#Unit V GPIO_UART #fm.register(35, fm.fpioa.UART2_TX, force=True) #fm.register(34, fm.fpioa.UART2_RX, force=True) #uart_Port = UART(UART.UART2, 115200,8,0,0, timeout=1000, read_buf_len= 4096) #UnitV ButtonA fm.register(19, fm.fpioa.GPIO1) but_a = GPIO(GPIO.GPIO1, GPIO.IN, GPIO.PULL_UP) fm.register(18, fm.fpioa.GPIO2) but_b = GPIO(GPIO.GPIO2, GPIO.IN, GPIO.PULL_UP) ########################## while (True): clock.tick() img = sensor.snapshot() a = lcd.display(img) ##print(clock.fps()) if but_b.value() == 0: print("b pushed") if but_a.value() == 0: print("a pushed") code = kpu.run_yolo2(task, img) print(clock.fps()) if code: for i in code: a = img.draw_rectangle(i.rect()) a = lcd.display(img) print(code) for i in code: ##lcd.draw_string(i.x(), i.y(), classes[i.classid()], lcd.RED, lcd.WHITE) ##lcd.draw_string(i.x(), i.y()+12, '%f1.3'%i.value(), lcd.RED, lcd.WHITE) ##lcd.draw_string(50, 50, '%f1.3'%i.value(), lcd.RED, lcd.WHITE) print(classes[i.classid()])
class SpmodTest(): test_conut = 0 def __init__(self, mosi=8, miso=15, cs=20, clk=21): self.is_load = False self.spi = SPI(SPI.SPI_SOFT, mode=SPI.MODE_MASTER, baudrate=400 * 1000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=clk, mosi=mosi, miso=miso) fm.register(cs, fm.fpioa.GPIO6, force=True) self.cs = GPIO(GPIO.GPIO6, GPIO.OUT) def test_event(self): if self.work_data != None and self.work_data == b'\x0b\x17': Report.Spmod_Test = True sample_page.next() def load(self): if Report.Spmod_Test: sample_page.next() if self.is_load == False: # i2c init() sample_page.btn.enable = False self.isError = None self.work_info = [] self.work_data = None self.agent = agent() self.agent.event(250, self.check) self.agent.event(1500, self.test_event) self.is_load = True def free(self): if self.is_load: # i2c deinit() sample_page.btn.enable = True self.is_load = False def check(self): try: tmp = [] self.cs.value(0) write_data = bytearray([0x90, 0x00, 0x00, 0x00]) self.spi.write(write_data) id_buf = bytearray(2) self.spi.readinto(id_buf, write=0xff) self.work_data = id_buf self.cs.value(1) tmp.append("Flash ReadID\n\n" + str(self.work_data)) self.work_info = tmp except Exception as e: Report.Spmod_Test = False Report.isError = str(e) print(e) def work(self): self.agent.parallel_cycle() ui.canvas.draw_string(10, 10, "4 Spmod Test", (0, 255, 127), scale=2) if self.work_data: for i in range(len(self.work_info)): ui.canvas.draw_string(20, 20 * i + 90, "{0}".format(str(self.work_info[i])), scale=2) if self.isError != None: ui.canvas.draw_string(40, 80, self.isError, (255, 255, 255), scale=2) sample_page.next()
''' 实验名称: 按键 版本: v1.0 日期: 2019.12 作者: 01Studio 社区: www.01studio.org ''' from Maix import GPIO from fpioa_manager import fm #注册IO,蓝灯-->IO12,KEY-->IO16 fm.register(12, fm.fpioa.GPIO0) fm.register(16, fm.fpioa.GPIO1) #初始化IO LED_B = GPIO(GPIO.GPIO0, GPIO.OUT) KEY = GPIO(GPIO.GPIO1, GPIO.IN) while True: if KEY.value()==0: #按键被按下接地 LED_B.value(0) #点亮LED_B,蓝灯 else: LED_B.value(1) #熄灭LED
import utime from Maix import GPIO from board import board_info from fpioa_manager import fm def test_irq(GPIO, pin_num): print("key", pin_num) fm.register(board_info.BOOT_KEY, fm.fpioa.GPIOHS0) key = GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_NONE) key.irq(test_irq, GPIO.IRQ_BOTH, GPIO.WAKEUP_NOT_SUPPORT, 7) i = 0 while i < 20: key.value() utime.sleep_ms(500) i += 1 key.disirq() fm.unregister(board_info.BOOT_KEY, fm.fpioa.GPIOHS0)
PIR_PIN_NUM = 23 PIR_FPIOA = fm.fpioa.GPIOHS0 PIR_GPIO = GPIO.GPIOHS0 x = 0 y = 0 # Init lcd lcd.init(freq=15000000, color=(0, 0, 0)) lcd.rotation(1) lcd.clear((0, 0, 0)) # Init backlight fm.register(BACKLIGHT_PIN_NUM, BACKLIGHT_FPIOA) bl = GPIO(BACKLIGHT_GPIO, GPIO.OUT) bl.value(1) fm.register(LED_PIN_NUM, LED_FPIOA) led = GPIO(LED_GPIO, GPIO.OUT) def irqHande(x): led.value(not led.value()) # pir fm.register(PIR_PIN_NUM, PIR_FPIOA) pir = GPIO(PIR_GPIO, GPIO.IN) pir.irq(irqHande, GPIO.IRQ_RISING, GPIO.WAKEUP_NOT_SUPPORT, 7) # button
class RGB_LED: def __init__(self, clk, data, number_leds, clk_gpiohs=fm.fpioa.GPIOHS0, data_gpiohs=fm.fpioa.GPIOHS1, force_register_io = False ): if force_register_io: fm.register(clk, clk_gpiohs, force=True) fm.register(data, data_gpiohs, force=True) else: ret = fm.register(clk, clk_gpiohs, force=False) if ret != 1: raise ValueError("pin %d has been registered to func %d" %(ret[0], ret[1])) ret = fm.register(data, data_gpiohs, force=False) if ret != 1: raise ValueError("pin %d has been registered to func %d" %(ret[0], ret[1])) self.clk = GPIO(GPIO.GPIOHS0+clk_gpiohs-fm.fpioa.GPIOHS0, GPIO.OUT) self.data = GPIO(GPIO.GPIOHS0+data_gpiohs-fm.fpioa.GPIOHS0, GPIO.OUT) self.clk.value(1) self.data.value(0) self.status = [] for i in range(number_leds): self.status.append([0,0,0]) def check_RGB(self, value): if not value in range(0,256): raise ValueError("value: [0, 255]") def check_HSB(self, value): if not value in range(0.0,1.0): raise ValueError("value: [0, 1]") # red, green, blue def set_RGB(self, led, r, g, b): self.check_RGB(r) self.check_RGB(g) self.check_RGB(b) self.send_byte(0x00) self.send_byte(0x00) self.send_byte(0x00) self.send_byte(0x00) for i in range(len(self.status)): if i == led: self.status[i]=[r, g, b] self.send_color(self.status[i][0], self.status[i][1], self.status[i][2]) self.send_byte(0x00) self.send_byte(0x00) self.send_byte(0x00) self.send_byte(0x00) # hue, saturation, brightness def set_HSB(self, led, h, s, b): self.check_HSB(h) self.check_HSB(s) self.check_HSB(b) if s == 0: r = b g = b b = b else: q = b*(1.0+s) if b<0.5 else b+s-b*s p = 2.0 * b -q r = int(self.hue_to_rgb(p, q, h + 1/3)) & 0xFF g = int(self.hue_to_rgb(p, q, hue)) & 0xFF b = int(self.hue_to_rgb(p, q, hue - 1/3)) & 0xFF self.set_RGB(led, r, g, b) def send_byte(self, data): for i in range(8): if data & 0x80: self.data.value(1) else: self.data.value(0) self.write_clk() data <<= 1 def write_clk(self): self.clk.value(0) time.sleep_us(20) self.clk.value(1) time.sleep_us(20) def send_color(self, r, g, b): prefix = 0xC0 if (b & 0x80) == 0: prefix |= 0x20 if (b & 0x40) == 0: prefix |= 0x10 if (g & 0x80) == 0: prefix |= 0x08 if (g & 0x40) == 0: prefix |= 0x04 if (r & 0x80) == 0: prefix |= 0x02 if (r & 0x40) == 0: prefix |= 0x01 self.send_byte(prefix) self.send_byte(b) self.send_byte(g) self.send_byte(r) def hue_to_rgb(self, p, q, t): if t < 0: t += 1 if t > 1: t -= 1 if t < 1/6: return p + (q - p) * 6.0 * t if t < 1/2: return q if t < 2/3: return p + (q - p) * (2/3 - t) * 6 return p
print("no '/sd/dataset.csv'") clock = time.clock() while True: tim = time.ticks_ms() try: if (tim - tim_b) >= 50: tim_b = tim print(clock.fps()) clock.tick() img = sensor.snapshot() tong = i2c.readfrom(0x12, 3) if (int(tong[2]) == 4) and (int(tong[0]) > 135): if tong3 == 0: tong3 = 1 pin12.value(1) else: tong3 = 0 pin12.value(0) idpg = idpg + 1 dirfoc = dirfoc + 1 if tong[1] < 85: img_co_l = img_co_l + 1 elif tong[1] < 170: img_co_f = img_co_f + 1 else: img_co_r = img_co_r + 1 f.write( str(dirfo) + "/" + str(idpg) + "," + str(int(tong[0])) + "," + str(int(tong[1])) + "\n") f.flush()
fm.register(25,fm.fpioa.GPIOHS10, force=True)#cs from Maix import GPIO cs = GPIO(GPIO.GPIOHS10, GPIO.OUT) fm.register(28,fm.fpioa.SPI1_D0, force=True)#mosi fm.register(26,fm.fpioa.SPI1_D1, force=True)#miso fm.register(27,fm.fpioa.SPI1_SCLK, force=True)#sclk spi1 = SPI(SPI.SPI1, mode=SPI.MODE_MASTER, baudrate=10000000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB) while True: w = b'\xFF' r = bytearray(1) cs.value(0) print(spi1.write_readinto(w, r)) cs.value(1) print(w, r) time.sleep(0.1) ''' from machine import SPI spi1 = SPI(SPI.SPI1, mode=SPI.MODE_MASTER, baudrate=10000000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=28, mosi=29, miso=30) w = b'1234' r = bytearray(4) spi1.write(w) spi1.write_readinto(w, r) spi1.read(5, write=0x00) spi1.readinto(r, write=0x00) '''
8, None, 1, timeout=1000, read_buf_len=4096) #GROVE_UART = UART(UART.UART1, 115200, 8, None, 1, timeout=1000, read_buf_len=4096) # fit to m5stack fire grove C: pin 3 TX, pin4 RX #fm.register(GROVE_pin3, fm.fpioa.UART1_RX , GPIO.PULL_UP ) #fm.register(GROVE_pin4, fm.fpioa.UART1_TX , GPIO.PULL_UP ) #uart_out = UART(UART.UART1, 115200, 8, None, 1, timeout=1000, read_buf_len=4096) print("##:wait for BtnA pressing for face detect...") time.sleep(0.5) if ((Btn_A.value() == 0) and (Btn_B.value() == 0)): print("-->Both A and B are pressed!") print("-->System reset now!") time.sleep(0.2) machine.reset() if Btn_A.value() == 0: #import face print("-->BtnA is pressed!") # enter boot.py code if BtnB not pressed time.sleep(0.2) elif Btn_B.value() == 0: #import face print("-->BtnB is pressed!") print("VGA image auto save demo now!") time.sleep(0.2) import VGA_auto_save