add micropython demo on bearpi board

This commit is contained in:
KY-zhang-X
2022-09-29 12:15:02 +08:00
parent afeaebd1b9
commit 3e4ceb8afe
49 changed files with 14399 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
import uos as os
# test mkdir()
print('{:-^40}'.format('test mkdir'))
print(os.listdir())
os.mkdir('test_dir')
print(os.listdir())
# test file open() and write()
print('{:-^40}'.format('test write'))
f = open('test_dir/test_file', 'w')
f.write("Hello TencentOS-tiny\n")
print("Hello MicroPython", file=f)
f.close()
print(os.listdir('test_dir'))
# test file open() and read()
print('{:-^40}'.format('test read'))
f = open('test_dir/test_file', 'r')
print(f.readlines())
f.close()
# test 'with' statement and iterator
print('{:-^40}'.format('test with statement'))
with open('test_dir/test_file', 'r') as file:
for line in file:
print(line)
# test rename()
print('{:-^40}'.format('test rename'))
os.rename('test_dir', 'test_dir2')
print(os.listdir())
print(os.listdir('test_dir2'))
# test unlink()
print('{:-^40}'.format('test unlink'))
os.unlink('test_dir2/test_file')
print(os.listdir('test_dir2'))
os.unlink('test_dir2')
print(os.listdir())

View File

@@ -0,0 +1,28 @@
import umachine as machine
import utime as time
running = True
def key1_irq_callback(pin):
if pin.value() == 0:
print('Key1 is pressed')
else:
print('Key1 is released')
def key2_irq_callback(pin):
global running
running = False
if __name__ == '__main__':
led = machine.Pin("LED", mode=machine.Pin.OUT)
key1 = machine.Pin("KEY1", mode=machine.Pin.IN_PULLUP)
key1.irq(trigger=machine.Pin.IRQ_RISING|machine.Pin.IRQ_FALLING, handler=key1_irq_callback)
key2 = machine.Pin("KEY2", mode=machine.Pin.IN_PULLUP)
key2.irq(trigger=machine.Pin.IRQ_FALLING, handler=key2_irq_callback)
while running:
led.on()
time.sleep(0.5)
led.off()
time.sleep(0.5)

View File

@@ -0,0 +1,19 @@
import _thread
lock = _thread.allocate_lock()
n_thread = 4
n_finished = 0
def thread_entry(no):
print(no)
with lock:
global n_finished
n_finished += 1
if __name__ == '__main__':
for i in range(n_thread):
_thread.start_new_thread(thread_entry, (i,))
while n_finished < n_thread:
pass
print("done")