add bearpi_micropython doc and scripts

This commit is contained in:
KY-zhang-X
2022-10-12 18:53:59 +08:00
parent ed310f51bf
commit 525cfda2d8
57 changed files with 485 additions and 379 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,36 @@
import network
import umachine as machine
import usocket as socket
import utime as time
WIFI_SSID='KYzhang'
WIFI_PASSWORD='kyzhangx'
SERVER_HOST='117.50.111.72'
SERVER_PORT=8001
if __name__ == '__main__':
# register esp8266 as NIC
uart0 = machine.UART(0) # BearPi LPUART1
esp8266 = network.ESP8266(uart0)
print("esp8266 init done")
esp8266.connect(ssid=WIFI_SSID, pwd=WIFI_PASSWORD)
print("esp8266 connect done")
# get socket and establish tcp connection
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("socket init done")
tcp_socket.connect((SERVER_HOST, SERVER_PORT))
print("socket connect done")
# tcp send and receive
cnt = 0
while cnt < 5:
send_len = tcp_socket.send("This is TCP test!\n")
print(f"send len:{send_len}")
data = tcp_socket.recv(1024)
if not data:
break
print(f"recv data:{data}")
cnt += 1
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")