|
| 1 | +import time |
| 2 | +import random |
| 3 | +import displayio |
| 4 | +from adafruit_bitmap_font import bitmap_font |
| 5 | +from adafruit_display_text import label |
| 6 | +from adafruit_clue import clue |
| 7 | + |
| 8 | +#--| User Config |------------------------------- |
| 9 | +BACKGROUND_COLOR = 0xCFBC17 |
| 10 | +HEXAGRAM_COLOR = 0xBB0000 |
| 11 | +FONT_COLOR = 0x005500 |
| 12 | +SHAKE_THRESHOLD = 20 |
| 13 | +MELODY = ( (1000, 0.1), # (freq, duration) |
| 14 | + (1200, 0.1), |
| 15 | + (1400, 0.1), |
| 16 | + (1600, 0.2)) |
| 17 | +#--| User Config |------------------------------- |
| 18 | + |
| 19 | +# Defined in order treating each hexagram as a 6 bit value. |
| 20 | +HEXAGRAMS = ( |
| 21 | + "EARTH", "RETURN", "THE ARMY", "PREVAILING", "MODESTY", " CRYING\nPHEASANT", |
| 22 | + "ASCENDANCE", "PEACE", "WEARINESS", "THUNDER", "LETTING\n LOOSE", |
| 23 | + "MARRYING\n MAIDEN", " SMALL\nEXCESS", "ABUNDANCE", "STEADFASTNESS", |
| 24 | + " GREAT\nINJURY", "SUPPORT", "RETRENCHMENT", "WATER", "FRUGALITY", |
| 25 | + "ADMONISHMENT", "FULFILLMENT", "THE WELL", "WAITING", "ILLNESS", |
| 26 | + "THE CHASE", "TRAPPED", "LAKE", "CUTTING", "REVOLUTION", " GREAT\nEXCESS", |
| 27 | + "STRIDE", "LOSS", "THE CHEEKS", "BLINDNESS", "DECREASE", "MOUNTAIN", |
| 28 | + "DECORATION", "WORK", " BIG\nCATTLE", "ADVANCE", "BITING", "UNFULFILLMENT", |
| 29 | + "ABANDONED", "TRAVELER", "FIRE", " THE\nCAULDRON", " GREAT\nHARVEST", |
| 30 | + "VIEW", "INCREASE", "FLOWING", "SINCERITY", "PROGRESS", "FAMILY", "WIND", |
| 31 | + " SMALL\nCATTLE", "OBSTRUCTION", "PROPRIETY", "THE COURT", "TREADING", |
| 32 | + "LITTLE\n PIG", "GATHERING", "RENDEZVOUS", "HEAVEN", |
| 33 | +) |
| 34 | + |
| 35 | +# Grab the CLUE's display |
| 36 | +display = clue.display |
| 37 | + |
| 38 | +# Background fill |
| 39 | +bg_bitmap = displayio.Bitmap(display.width, display.height, 1) |
| 40 | +bg_palette = displayio.Palette(1) |
| 41 | +bg_palette[0] = BACKGROUND_COLOR |
| 42 | +background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette) |
| 43 | + |
| 44 | +# Hexagram setup |
| 45 | +sprite_sheet = displayio.Bitmap(11, 4, 2) |
| 46 | +palette = displayio.Palette(2) |
| 47 | +palette.make_transparent(0) |
| 48 | +palette[0] = 0x000000 |
| 49 | +palette[1] = HEXAGRAM_COLOR |
| 50 | + |
| 51 | +for x in range(11): |
| 52 | + sprite_sheet[x, 0] = 1 # - - 0 YIN |
| 53 | + sprite_sheet[x, 1] = 0 |
| 54 | + sprite_sheet[x, 2] = 1 # --- 1 YANG |
| 55 | + sprite_sheet[x, 3] = 0 |
| 56 | +sprite_sheet[5, 0] = 0 |
| 57 | + |
| 58 | +tile_grid = displayio.TileGrid(sprite_sheet, pixel_shader=palette, |
| 59 | + width = 1, |
| 60 | + height = 6, |
| 61 | + tile_width = 11, |
| 62 | + tile_height = 2) |
| 63 | + |
| 64 | +hexagram = displayio.Group(max_size=1, x=60, y=15, scale=10) |
| 65 | +hexagram.append(tile_grid) |
| 66 | + |
| 67 | +# Hexagram name label |
| 68 | +# font credit: https://www.instagram.com/cove703/ |
| 69 | +font = bitmap_font.load_font("/christopher_done_24.bdf") |
| 70 | +font.load_glyphs(b'ABCDEFGHIJKLMNOPQRSTUVWXYZ') |
| 71 | +hexname = label.Label(font, text=" "*40, color=FONT_COLOR) |
| 72 | +# this will initially hold the "shake for reading" message |
| 73 | +hexname.text = " SHAKE\n FOR\nREADING" |
| 74 | +hexname.anchor_point = (0.5, 0.0) |
| 75 | +hexname.anchored_position = (120, 120) |
| 76 | + |
| 77 | +# Set up main display group (splash) |
| 78 | +splash = displayio.Group() |
| 79 | +display.show(splash) |
| 80 | + |
| 81 | +# Add background and text label |
| 82 | +splash.append(background) |
| 83 | +splash.append(hexname) |
| 84 | + |
| 85 | +def show_hexagram(number): |
| 86 | + for i in range(6): |
| 87 | + tile_grid[5-i] = (number >> i) & 0x01 |
| 88 | + |
| 89 | +def show_name(number): |
| 90 | + hexname.text = HEXAGRAMS[number] |
| 91 | + hexname.anchored_position = (120, 180) |
| 92 | + |
| 93 | +#=================================== |
| 94 | +# MAIN CODE |
| 95 | +#=================================== |
| 96 | +print("shake") |
| 97 | +# wait for shake |
| 98 | +while not clue.shake(shake_threshold=SHAKE_THRESHOLD): |
| 99 | + pass |
| 100 | + |
| 101 | +# calibrate the mystic universe |
| 102 | +x, y, z = clue.acceleration |
| 103 | +random.seed(int(time.monotonic() + abs(x) + abs(y) + abs(z))) |
| 104 | + |
| 105 | +# cast a reading |
| 106 | +reading = random.randrange(64) |
| 107 | +print("reading = ", reading, HEXAGRAMS[reading]) |
| 108 | + |
| 109 | +# play a melody |
| 110 | +for note, duration in MELODY: |
| 111 | + clue.play_tone(note, duration) |
| 112 | + |
| 113 | +# prompt to show |
| 114 | +display.auto_refresh = False |
| 115 | +hexname.text = " GOT IT\n\nPRESS BUTTON\n TO SEE" |
| 116 | +hexname.anchored_position = (120, 120) |
| 117 | +display.auto_refresh = True |
| 118 | +while not clue.button_a and not clue.button_b: |
| 119 | + pass |
| 120 | + |
| 121 | +# and then show it |
| 122 | +display.auto_refresh = False |
| 123 | +splash.append(hexagram) |
| 124 | +show_hexagram(reading) |
| 125 | +show_name(reading) |
| 126 | +display.auto_refresh = True |
| 127 | + |
| 128 | +# hold here until reset |
| 129 | +while True: |
| 130 | + pass |
0 commit comments