Skip to content

Commit 96e97ed

Browse files
author
Sebastian Plamauer
committed
created SDdatalogger example
1 parent 5fc400c commit 96e97ed

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

examples/SDdatalogger/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
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.
2+
3+
To run, put the boot.py, cardreader.py and datalogger.py files on either the flash or the SD-card of your pyboard.
4+
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.

examples/SDdatalogger/README.md~

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
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.
2+
3+
To run, put the boot.py, cardreader.py and datalogger.py files on either the flash or the SD-card of your pyboard.
4+
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.

examples/SDdatalogger/boot.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# boot.py -- runs on boot-up
2+
# Let's you choose which script to run.
3+
# > To run 'datalogger.py':
4+
# * press reset and do nothing else
5+
# > To run 'cardreader.py':
6+
# * press reset
7+
# * press user switch and hold until orange LED goes out
8+
9+
import pyb
10+
11+
pyb.LED(3).on()
12+
pyb.delay(2000)
13+
pyb.LED(4).on()
14+
pyb.LED(3).off()
15+
switch = pyb.Switch() # check if switch was pressed decision phase
16+
17+
if switch():
18+
pyb.usb_mode('CDC+MSC')
19+
pyb.main('cardreader.py') # if switch was pressed, run this
20+
else:
21+
pyb.usb_mode('CDC+HID')
22+
pyb.main('datalogger.py') # if switch wasn't pressed, run this
23+
24+
pyb.LED(4).off()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# cardread.py
2+
# This is called when the user enters cardreader mode. It does nothing.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# datalogger.py
2+
# Logs the data from the acceleromter to a file on the SD-card
3+
4+
import pyb
5+
6+
# creating objects
7+
accel = pyb.Accel()
8+
blue = pyb.LED(4)
9+
switch = pyb.Switch()
10+
11+
# loop
12+
while True:
13+
14+
# start if switch is pressed
15+
if switch():
16+
pyb.delay(200) # delay avoids detection of multiple presses
17+
blue.on() # blue LED indicates file open
18+
log = open('1:/log.csv', 'w') # open file on SD (SD: '1:/', flash: '0/)
19+
20+
# until switch is pressed again
21+
while not switch():
22+
t = pyb.millis() # get time
23+
x, y, z = accel.filtered_xyz() # get acceleration data
24+
log.write('{},{},{},{}\n'.format(t,x,y,z)) # write data to file
25+
26+
# end after switch is pressed again
27+
log.close() # close file
28+
blue.off() # blue LED indicates file closed
29+
pyb.delay(200) # delay avoids detection of multiple presses

0 commit comments

Comments
 (0)