|
1 |
| -# Wait a bit for startup to complete. |
2 |
| -ESP32::System.delay(500) |
| 1 | +# Get the number of previous boots from a file. |
| 2 | +boot_count = nil |
| 3 | +File.open('/storage/boot_count.txt', 'a+') { |f| boot_count = f.gets } |
3 | 4 |
|
4 |
| -string1 = "testing " |
5 |
| -string2 = "1,2,3..." |
| 5 | +# Increment it. |
| 6 | +boot_count = 0 unless boot_count |
| 7 | +boot_count = boot_count.to_i |
| 8 | +boot_count += 1 |
6 | 9 |
|
7 |
| -puts "Writing to /storage/test.txt: \"#{string1}#{string2}\"" |
8 |
| -File.open('/storage/test.txt', 'w') { |f| f.write(string1) } |
9 |
| -File.open('/storage/test.txt', 'a') { |f| f.write(string2) } |
| 10 | +# Write new count back to file. |
| 11 | +File.open('/storage/boot_count.txt', 'w') { |f| f.puts(boot_count) } |
10 | 12 |
|
11 |
| -print "Read from /storage/text.txt: " |
| 13 | +# Display it. |
| 14 | +puts "Boot count: #{boot_count}" |
| 15 | + |
| 16 | +# If first boot, write then append to /storage/test.txt. |
| 17 | +if boot_count == 1 |
| 18 | + string1 = "testing " |
| 19 | + string2 = "1,2,3..." |
| 20 | + puts "Writing to /storage/test.txt: \"#{string1}#{string2}\"" |
| 21 | + |
| 22 | + File.open('/storage/test.txt', 'w') { |f| f.write(string1) } |
| 23 | + |
| 24 | + # WARNING: littlefs append mode starts with the file pointer at 0 (start of file). |
| 25 | + File.open('/storage/test.txt', 'a') do |f| |
| 26 | + # Move file pointer to the end with #seek. |
| 27 | + f.seek(0, File::SEEK_END) |
| 28 | + f.write(string2) |
| 29 | + end |
| 30 | +end |
| 31 | + |
| 32 | +# Read from /storage/test.txt on every boot. |
| 33 | +print "Read from /storage/test.txt: " |
12 | 34 | File.open('/storage/test.txt', 'r') { |f| f.each_line { |l| puts l } }
|
0 commit comments