ePaper display driven by a Raspberry Pi Pico
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

48 lines
1.5 KiB

  1. import machine
  2. import sdcard
  3. import uos
  4. import random
  5. class Storage:
  6. def __init__(self):
  7. self.cs = machine.Pin(17, machine.Pin.OUT)
  8. self.spi = machine.SPI(0,
  9. baudrate=1000000,
  10. polarity=0,
  11. phase=0,
  12. bits=8,
  13. firstbit=machine.SPI.MSB,
  14. sck=machine.Pin(18),
  15. mosi=machine.Pin(19),
  16. miso=machine.Pin(16))
  17. self.sd = None
  18. self.vfs = None
  19. self.sd_path = "/sd"
  20. def mount(self):
  21. self.sd = sdcard.SDCard(self.spi, self.cs)
  22. self.vfs = uos.VfsFat(self.sd)
  23. uos.mount(self.vfs, self.sd_path)
  24. def umount(self):
  25. uos.umount(self.sd_path)
  26. def get_root_path(self):
  27. return self.sd_path
  28. def load_joke(root):
  29. filename = root + "/jokes.json"
  30. num_lines = sum(1 for line in open(filename))
  31. with open(filename, mode="r") as f:
  32. joke = None
  33. start_pattern = "\"joke\": \""
  34. end_pattern = "\""
  35. for i in range(random.randrange(num_lines)):
  36. line = f.readline()
  37. for i in range(10):
  38. line = f.readline()
  39. joke_start = line.find(start_pattern)
  40. joke_end = line.rfind(end_pattern)
  41. if joke_start >= 0 and joke_end >= 0:
  42. joke = line[joke_start+len(start_pattern):joke_end]
  43. return joke
  44. return None