forked from keithtanner037/merlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
135 lines (117 loc) · 4.24 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# to learn display
#
#
# # Merlin emulator
# games:
# tic tac toe
# Music Machine
# Echo (Simon)
# Blackjack 13
# Magic square
# Mindbender
import os
import displayio
import terminalio
from adafruit_display_shapes.rect import Rect
from adafruit_display_text import label
from adafruit_macropad import MacroPad
from mindbender import mindbender
from magic_square import magic_square
from echo import echo
from simon import simon
from music_machine import music_machine
#from rainbowio import colorwheel
macropad = MacroPad()
# little bit of cleanuo
macropad.pixels.fill((0,0,0))
# configuration
# scale
tones = [196, 220, 247, 262, 294, 330, 349, 392, 440, 494, 523, 587]
#length = 0.5
# create the objects for each game
# put them in an array
# turning dial switches to a different item in the array
# need to have the same API
games = dict()
games['Magic Square'] = magic_square(macropad, tones)
games['Mindbender'] = mindbender(macropad, tones)
games['Echo'] = echo(macropad, tones)
games['Simon'] = simon(macropad, tones)
games['Music Machine'] = music_machine(macropad, tones)
#do display setup
#macropad.display.auto_refresh = False
#macropad.pixels.auto_write = False
bitmap = displayio.OnDiskBitmap("MerlinChrome.bmp")
tile_grid = displayio.TileGrid(
bitmap,
pixel_shader=getattr(bitmap, 'pixel_shader', displayio.ColorConverter()))
# Create a Group to hold the TileGrid
group = displayio.Group()
# Add the TileGrid to the Group
group.append(tile_grid)
group.append(label.Label(terminalio.FONT, text='choose your game:', color=0xffffff,
anchored_position=(macropad.display.width//2, 31),
anchor_point=(0.5, 0.0)
))
group.append(label.Label(terminalio.FONT, text=' '*20, color=0xffffff,
anchored_position=(macropad.display.width//2, 45),
anchor_point=(0.5, 0.0)
))
macropad.display.show(group)
last_position = None
last_encoder_switch = None
#force a game selection
modechange =1
print ("modechange on")
# MAIN LOOP ----------------------------
while True:
position = macropad.encoder
if position != last_position:
if modechange:
pass#cycle through the games
#game_index = position % len(games)
# print the name
#print (list(games.keys())[game_index])
else:
# what to do with knob twiddles without change mode? Nothing.
current_game.encoderChange(position, last_position)
last_position = position
# If code reaches here, a key or the encoder button WAS pressed/released
# and there IS a corresponding macro available for it...other situations
# are avoided by 'continue' statements above which resume the loop.
macropad.encoder_switch_debounced.update()
encoder_switch = macropad.encoder_switch_debounced.pressed
if encoder_switch != last_encoder_switch:
last_encoder_switch = encoder_switch
if encoder_switch:
if modechange:
modechange =0
print ("modechange off")
group[1].text = "now playing:"
#get the new selected game and make it happen
#do the change of mode
current_game = games[list(games.keys())[macropad.encoder % len(games)]]
current_game.new_game()
else:
modechange =1
#show text for mode change
group[1].text = "choose your game:"
# show lights for mode change
macropad.pixels.fill((50,0,0))
elif modechange:
#read the encoder
current_knob = macropad.encoder % len(games)
group[2].text = list(games.keys())[current_knob]
else:
key_event = macropad.keys.events.get()
if key_event:
key_number = key_event.key_number
pressed = key_event.pressed
if pressed:
if key_number < 12: # one of the game buttons
#print ("pressed key",key_number)
current_game.button(key_number)
else:
# is it possible to get here?
# pass for now
pass