Load a text file from the SD card for the Adafruit PyPortal.

This is some basic code that will print a list of files on the SD card and load a given text files contents. Then it converts the JSON text into a dictionary that makes it easy to list out and display values.

import time
import json

import os
import board
from adafruit_pyportal import PyPortal

# Create the PyPortal object
pyportal = PyPortal(status_neopixel=board.NEOPIXEL)

# Default location to look is in internal memory
FILE_DIRECTORY = "/sd"

RECIPE_FILE = "Cookies"

def print_directory(path, tabs=0):
    for file in os.listdir(path):
        stats = os.stat(path + "/" + file)
        filesize = stats[6]
        isdir = stats[0] & 0x4000

        if filesize < 1000:
            sizestr = str(filesize) + " by"
        elif filesize < 1000000:
            sizestr = "%0.1f KB" % (filesize / 1000)
        else:
            sizestr = "%0.1f MB" % (filesize / 1000000)

        prettyprintname = ""
        for _ in range(tabs):
            prettyprintname += "   "
        prettyprintname += file
        if isdir:
            prettyprintname += "/"
        print('{0:<20} Size: {1:>6}'.format(prettyprintname, sizestr))

        # recursively print directory contents
        if isdir:
            print_directory(path + "/" + file, tabs + 1)

try:
    print_directory(FILE_DIRECTORY)
except OSError as error:
    raise Exception("No files found on flash or SD Card")
    
try:
    with open("/sd/" + RECIPE_FILE + ".txt", "r") as fp:
        x = fp.read()
        # parse x:
        y = json.loads(x)
        ingredients = y["ingredients"]
        for i in ingredients:
            print('{} - {}'.format(i.get("quantity"), i.get("ingredient")))
        
except OSError as e:
    raise Exception("Could not read text file.")

On the MicroSD card there is a file named Cookies.txt that is made up of the following JSON.

{
   "ingredients":[
      {
         "quantity":"1 cup",
         "ingredient":"butter, softened"
      },
      {
         "quantity":"1 cup",
         "ingredient":"white sugar"
      },
      {
         "quantity":"1 cup",
         "ingredient":"packed brown sugar"
      },
      {
         "quantity":"Two",
         "ingredient":"eggs"
      },
      {
         "quantity":"2 tsp",
         "ingredient":"vanilla extract"
      },
      {
         "quantity":"1 tsp",
         "ingredient":"baking soda"
      },
      {
         "quantity":"2 tsp",
         "ingredient":"hot water"
      },
      {
         "quantity":"1/2 tsp",
         "ingredient":"salt"
      },
      {
         "quantity":"3 cups",
         "ingredient":"all-purpose flour"
      },
      {
         "quantity":"2 cups",
         "ingredient":"semisweet chocolate chips"
      },
      {
         "quantity":"1 cup",
         "ingredient":"chopped walnuts"
      }
   ]
}

This should result in the following output:

code.py output:
ESP firmware: bytearray(b'1.2.2\x00')
Set background to  0
Coleslaw Dressing.txt Size: 426 by
Bread Dough.txt      Size: 426 by
Pancakes.txt         Size: 426 by
Cookies.txt          Size: 1.0 KB
1 cup - butter, softened
1 cup - white sugar
1 cup - packed brown sugar
Two - eggs
2 tsp - vanilla extract
1 tsp - baking soda
2 tsp - hot water
1/2 tsp - salt
3 cups - all-purpose flour
2 cups - semisweet chocolate chips
1 cup - chopped walnuts

What is the best programming language?

The thing I get asked the most by parents and people trying to figure out how to get into the tech world is where to start. While a knowledge of at least some hardware is important, this post will be more focused on the structure of programming languages and why it is better to know one over another. This really boils down to the common modern day argument over C++ vs JavaScript vs Python. Continue reading What is the best programming language?

A few thoughts on Pinhead

 

The Hellraiser series provided us with plenty of nightmare fuel in its day. However, when I think back to the many epic monologues the Pinhead delivered, his message is one of hope and optimism.

image“We have such sights to show you.”
Pinhead just wants to share the unique sights that his alternate realm have to offer. I am sure this would be an unforgettable experience…

“Your suffering will be legendary, even in Hell.”
While suffering is not ideal, it is a critical part of what defines us as individuals. I think that what Pinhead is really saying here is that while he understands that we all make mistakes that lead to suffering,  Kirsty is a strong and independent woman who can handle more suffering than most.

“Oh no tears please, its a waste of good suffering.”
The job of a Cenobite is likely not an easy one. Someone opens a dimensional portal to “Hell” and maybe then suddenly they want to close it once they have a look. Ever the professional, Pinhead defuses the situation by letting them know that it’s not so bad and it is really not worth crying over. As bad as you are, he has had worse.

So there you go, positive words of wisdom from a guy who has truly been to Hell and back.