202 lines
4.8 KiB
Python
202 lines
4.8 KiB
Python
import max7219
|
|
from machine import Pin, SPI, ADC, PWM
|
|
from machine import I2C
|
|
from lcd_api import LcdApi
|
|
from pico_i2c_lcd import I2cLcd
|
|
import time
|
|
import os
|
|
import utime
|
|
|
|
#init buzzer
|
|
|
|
#read notes
|
|
tonefile = open("tones.txt", 'r')
|
|
tones = tonefile.read()
|
|
tones = eval(tones)
|
|
tonefile.close()
|
|
|
|
buzzer = PWM(Pin(15))
|
|
buzzer.duty_u16(0)
|
|
def tone(freq, duration):
|
|
buzzer.freq(freq)
|
|
buzzer.duty_u16(1000)
|
|
time.sleep(duration)
|
|
buzzer.duty_u16(0)
|
|
|
|
def playsong(song):
|
|
for i in song:
|
|
note = i.split(":")[0]
|
|
duration = i.split(":")[1]
|
|
tone(tones[note], float(duration))
|
|
|
|
introsong = ['DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'E4:0.01', 'E4:0.01', 'E4:0.01', 'E4:0.01', 'E4:0.01', 'E4:0.01', 'E4:0.01', 'E4:0.01', 'E4:0.01', 'FS4:0.01', 'FS4:0.01', 'FS4:0.01', 'FS4:0.01', 'FS4:0.01', 'FS4:0.01', 'FS4:0.01', 'FS4:0.01', 'FS4:0.01', 'G4:0.01', 'G4:0.01', 'G4:0.01', 'G4:0.01', 'G4:0.01', 'G4:0.01', 'G4:0.01', 'G4:0.01', 'G4:0.01', 'GS4:0.01', 'GS4:0.01', 'GS4:0.01', 'GS4:0.01', 'GS4:0.01', 'GS4:0.01', 'GS4:0.01', 'GS4:0.01', 'GS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01', 'DS4:0.01']
|
|
|
|
playsong(introsong)
|
|
|
|
# init game display
|
|
spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3))
|
|
display = max7219.Matrix8x8(spi, Pin(5), 2)
|
|
display.show()
|
|
|
|
# init text display
|
|
I2C_ADDR = 0x27
|
|
I2C_NUM_ROWS = 2
|
|
I2C_NUM_COLS = 16
|
|
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
|
|
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
|
|
lcd.clear()
|
|
lcd.putstr("booting...")
|
|
time.sleep(1)
|
|
lcd.clear()
|
|
lcd.putstr("dot matrix...OK")
|
|
time.sleep(0.1)
|
|
lcd.clear()
|
|
lcd.putstr("buzzer...OK")
|
|
time.sleep(0.1)
|
|
lcd.clear()
|
|
lcd.putstr("LCD display...OK")
|
|
|
|
#init joystick
|
|
time.sleep(0.1)
|
|
lcd.clear()
|
|
lcd.putstr("joystick...")
|
|
class joystickc():
|
|
def __init__(self):
|
|
self.__vertpin = ADC(28)
|
|
self.__horzpin = ADC(27)
|
|
self.__selpin = Pin(8, Pin.IN, Pin.PULL_UP)
|
|
|
|
def getvertical(self):
|
|
temp = self.__vertpin.read_u16()
|
|
temp = temp * (3.3 / 65535)
|
|
if temp > 2:
|
|
return 1
|
|
elif temp < 1:
|
|
return -1
|
|
else:
|
|
return 0
|
|
|
|
def gethorizontal(self):
|
|
temp = self.__horzpin.read_u16()
|
|
temp = temp * (3.3 / 65535)
|
|
if temp > 2:
|
|
return 1
|
|
elif temp < 1:
|
|
return -1
|
|
else:
|
|
return 0
|
|
|
|
def getsel(self):
|
|
while True:
|
|
temp = self.__selpin.value()
|
|
print(temp)
|
|
if temp == 1:
|
|
return True
|
|
elif temp == 0:
|
|
return False
|
|
else:
|
|
print("joystick error.")
|
|
|
|
joystick = joystickc()
|
|
|
|
lcd.clear()
|
|
lcd.putstr("joystick...OK")
|
|
|
|
#init action buttons
|
|
time.sleep(0.1)
|
|
lcd.clear()
|
|
lcd.putstr("action buttons...")
|
|
|
|
class actionbuttonsc():
|
|
def __init__(self):
|
|
self.button1 = False
|
|
self.button2 = False
|
|
|
|
self.__action1butt = Pin(6, Pin.IN, Pin.PULL_DOWN)
|
|
self.__action2butt = Pin(7, Pin.IN, Pin.PULL_DOWN)
|
|
|
|
self.__action1butt.irq(trigger=Pin.IRQ_RISING, handler=self.button1on)
|
|
self.__action1butt.irq(trigger=Pin.IRQ_FALLING, handler=self.button1off)
|
|
|
|
self.__action2butt.irq(trigger=Pin.IRQ_RISING, handler=self.button2on)
|
|
self.__action2butt.irq(trigger=Pin.IRQ_FALLING, handler=self.button2off)
|
|
|
|
def button1on(self):
|
|
self.button1 = True
|
|
|
|
def button1off(self):
|
|
self.button1 = False
|
|
|
|
def button2on(self):
|
|
self.button2 = True
|
|
|
|
def button2off(self):
|
|
self.button2 = False
|
|
|
|
actionbuttons = actionbuttonsc()
|
|
|
|
lcd.clear()
|
|
lcd.putstr("action buttons...OK")
|
|
|
|
time.sleep(3)
|
|
|
|
def selectoption(title, options):
|
|
selectedoption = 0
|
|
while True:
|
|
lcd.move_to(0, 0)
|
|
lcd.clear()
|
|
lcd.putstr(title)
|
|
lcd.move_to(0, 1)
|
|
lcd.putstr(options[selectedoption])
|
|
while True:
|
|
temp = joystick.gethorizontal()
|
|
selected = joystick.getsel()
|
|
if selected == True:
|
|
break
|
|
if temp == 1:
|
|
if selectedoption != 0:
|
|
selectedoption -= 1
|
|
else:
|
|
selectedoption = len(options)-1
|
|
time.sleep(0.1)
|
|
break
|
|
elif temp == -1:
|
|
if selectedoption != len(options)-1:
|
|
selectedoption += 1
|
|
else:
|
|
selectedoption = 0
|
|
time.sleep(0.1)
|
|
break
|
|
if selected == True:
|
|
break
|
|
lcd.move_to(0, 0)
|
|
lcd.clear()
|
|
return selectedoption
|
|
|
|
option = selectoption("select mode", ["test mode.", "game mode."])
|
|
if option == 1:
|
|
while True:
|
|
lcd.clear()
|
|
lcd.move_to(0, 0)
|
|
lcd.putstr("horz: "+str(joystick.gethorizontal()))
|
|
lcd.move_to(0, 1)
|
|
lcd.putstr("vert: "+str(joystick.getvertical()))
|
|
|
|
lcd.clear()
|
|
lcd.putstr("loading games...")
|
|
|
|
games = {}
|
|
for i in os.listdir("games"):
|
|
path = "games/"+i
|
|
lcd.clear()
|
|
lcd.putstr(i)
|
|
codefile = open(path+"main.py", 'r')
|
|
code = codefile.read()
|
|
codefile.close()
|
|
games[i] = code
|
|
|
|
lcd.clear()
|
|
lcd.putstr("<- spiele ->")
|
|
|
|
buzzer.duty_u16(0)
|