|
- import machine
- import sdcard
- import uos
- import random
-
- class Storage:
- def __init__(self):
- self.cs = machine.Pin(17, machine.Pin.OUT)
- self.spi = machine.SPI(0,
- baudrate=1000000,
- polarity=0,
- phase=0,
- bits=8,
- firstbit=machine.SPI.MSB,
- sck=machine.Pin(18),
- mosi=machine.Pin(19),
- miso=machine.Pin(16))
- self.sd = None
- self.vfs = None
- self.sd_path = "/sd"
- def mount(self):
- self.sd = sdcard.SDCard(self.spi, self.cs)
- self.vfs = uos.VfsFat(self.sd)
- uos.mount(self.vfs, self.sd_path)
- def umount(self):
- uos.umount(self.sd_path)
- def get_root_path(self):
- return self.sd_path
-
-
- def load_joke(root):
- filename = root + "/jokes.json"
- num_lines = sum(1 for line in open(filename))
- with open(filename, mode="r") as f:
- joke = None
- start_pattern = "\"joke\": \""
- end_pattern = "\""
- for i in range(random.randrange(num_lines)):
- line = f.readline()
- for i in range(10):
- line = f.readline()
- joke_start = line.find(start_pattern)
- joke_end = line.rfind(end_pattern)
- if joke_start >= 0 and joke_end >= 0:
- joke = line[joke_start+len(start_pattern):joke_end]
- return joke
- return None
|