micropython: add micropython component

This commit is contained in:
KY-zhang-X
2022-09-29 12:10:37 +08:00
parent 1514f1cb9b
commit dd76146324
2679 changed files with 354110 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
This is a SDdatalogger, to log data from the accelerometer to the SD-card. It also functions as card reader, so you can easily get the data on your PC.
To run, put the boot.py, cardreader.py and datalogger.py files on either the flash or the SD-card of your pyboard.
Upon reset, the datalogger script is run and logs the data. If you press the user button after reset and hold it until the orange LED goes out, you enter the cardreader mode and the filesystem is mounted to your PC.

View File

@@ -0,0 +1,25 @@
# boot.py -- runs on boot-up
# Let's you choose which script to run.
# > To run 'datalogger.py':
# * press reset and do nothing else
# > To run 'cardreader.py':
# * press reset
# * press user switch and hold until orange LED goes out
import pyb
pyb.LED(3).on() # indicate we are waiting for switch press
pyb.delay(2000) # wait for user to maybe press the switch
switch_value = pyb.Switch()() # sample the switch at end of delay
pyb.LED(3).off() # indicate that we finished waiting for the switch
pyb.LED(4).on() # indicate that we are selecting the mode
if switch_value:
pyb.usb_mode("VCP+MSC")
pyb.main("cardreader.py") # if switch was pressed, run this
else:
pyb.usb_mode("VCP+HID")
pyb.main("datalogger.py") # if switch wasn't pressed, run this
pyb.LED(4).off() # indicate that we finished selecting the mode

View File

@@ -0,0 +1,2 @@
# cardread.py
# This is called when the user enters cardreader mode. It does nothing.

View File

@@ -0,0 +1,33 @@
# datalogger.py
# Logs the data from the acceleromter to a file on the SD-card
import pyb
# creating objects
accel = pyb.Accel()
blue = pyb.LED(4)
switch = pyb.Switch()
# loop
while True:
# wait for interrupt
# this reduces power consumption while waiting for switch press
pyb.wfi()
# start if switch is pressed
if switch():
pyb.delay(200) # delay avoids detection of multiple presses
blue.on() # blue LED indicates file open
log = open("/sd/log.csv", "w") # open file on SD (SD: '/sd/', flash: '/flash/)
# until switch is pressed again
while not switch():
t = pyb.millis() # get time
x, y, z = accel.filtered_xyz() # get acceleration data
log.write("{},{},{},{}\n".format(t, x, y, z)) # write data to file
# end after switch is pressed again
log.close() # close file
blue.off() # blue LED indicates file closed
pyb.delay(200) # delay avoids detection of multiple presses