Open Hardware Summit badge: Magic 8-Ball app

Thanks to @Steve Pomeroy for creating this MicroPython demo app for the Open Hardware Summit badge:


# created by Steve Pomeroy
# modified by Drew Fustini to run once and exit
# blog post:
# photo gallery:
# transfer to Open Hardware Summit badge using FTP:

import gxgde0213b1
import font16
import font12
from machine import I2C, Pin, TouchPad
import struct
import time
import urandom
from ohsbadge import epd
from ohsbadge import fb

class TouchButton(object):
   def __init__(self, pin, on_pressed, threshold=400, debounce_ms=50):
       self._touchpad = machine.TouchPad(pin)
       self._on_pressed = on_pressed
       self._threshold = threshold
       self._debounce_ms = debounce_ms
       self._down_ms = None
       self._pressed = False

   def read(self):
       if  self._debounce_ms:
                       self._pressed = True
           self._pressed = False
           self._down_ms = None

# from Magic 8-Ball app by Steve Pomeroy
class MagicBall():
   def clear_screen():

   def show_message(message):
       epd.display_string_at(fb, 0, 52, message, font16, gxgde0213b1.COLORED)

   def read_accel(i2c):
       i2c.writeto_mem(30, 0x18, b'\x80')
       x = struct.unpack("h", i2c.readfrom_mem(30, 0x6, 2))
       y = struct.unpack("h", i2c.readfrom_mem(30, 0x8, 2))
       z = struct.unpack("h", i2c.readfrom_mem(30, 0xA, 2))
       return (x[0], y[0], z[0])

   def get_orientation(i2c):
       new_orientation = None
       pos = MagicBall.read_accel(i2c)

       if pos[2] > 13000:
           new_orientation = "upright"
       elif pos[2] < -13000:
           new_orientation = "prone"

       return new_orientation

   def main(f):
           phrases = ["It is certain.", "It is decidedly so.", "Without a doubt.", "Yes - definitely.", "You may rely on it.", "As I see it, yes.", "Most likely.", "Outlook good.", "Yes.", "Signs point to yes.", "Reply hazy, try again", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again.", "Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."]
           i2c = machine.I2C(scl=Pin(22), sda=Pin(21))
           prev_orientation = None

           keep_on = [True]

           def exit_loop():
               keep_on[0] = False

           exit_button = TouchButton(Pin(32), exit_loop)

           while keep_on[0]:
               orientation = MagicBall.get_orientation(i2c)

               if orientation and orientation != prev_orientation:
                   if orientation == 'upright':
                   elif orientation == 'prone':
               prev_orientation = orientation

ball = MagicBall()

This Python file can be transferred to Open Hardware Summit badge using the FTP server built into the MicroPython firmware.


Open Hardware Summit badge: Magic 8-Ball app

One thought on “Open Hardware Summit badge: Magic 8-Ball app

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s