Skip to content

Instantly share code, notes, and snippets.

@jadudm
Last active November 8, 2017 19:20
Show Gist options
  • Save jadudm/8fd488ee6250410dd073f95e6c7ec3bc to your computer and use it in GitHub Desktop.
Save jadudm/8fd488ee6250410dd073f95e6c7ec3bc to your computer and use it in GitHub Desktop.
Flipping Data between Files on a Micro:Bit
import os
from microbit import *
def init_state():
# Hopefully, this creates zero-length files
# in the filesystem
A_size = 0
B_size = 0
try:
A_size = os.size("A.txt")
except:
pass
# display.scroll("A" + str(A_size), wait = False)
if A_size == 0:
with open("A.txt", "w") as A:
A.write("")
try:
B_size = os.size("B.txt")
except:
pass
if B_size == 0:
with open("B.txt", "w") as B:
B.write("")
def extend_data(data):
A_size = os.size("A.txt")
B_size = os.size("B.txt")
# If A is bigger, we copy from A to B, and
# then proceed to extend B.
if A_size > B_size:
with open("B.txt", "w") as B:
try:
with open("A.txt", "r") as A:
line = A.readline()
while len(line) > 1:
B.write(line)
line = A.readline()
except:
pass
B.write(data)
B.write("\n")
# Otherwise, B is bigger, and we will
# overwrite A, copying B + the extension.
else:
with open("A.txt", "w") as A:
try:
with open("B.txt", "r") as B:
line = B.readline()
while len(line) > 1:
A.write(line)
line = B.readline()
except:
pass
A.write(data)
A.write("\n")
# Setup
init_state()
# Loop
counter = 0
while True:
sleep(1000)
extend_data(str(counter) + "," + str(temperature()))
counter = counter + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment