Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add masked input checking for buttons #64

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions ThumbyGames/lib-emulator/thumby.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@

from thumbySprite import Sprite

from thumbyButton import buttonA, buttonB, buttonU, buttonD, buttonL, buttonR
from thumbyButton import inputPressed, inputJustPressed, dpadPressed, dpadJustPressed, actionPressed, actionJustPressed
from thumbyButton import buttonA, buttonB, buttonU, buttonD, buttonL, buttonR, buttonMaskA, buttonMaskB, buttonMaskU, buttonMaskD, buttonMaskL, buttonMaskR
from thumbyButton import inputPressed, inputJustPressed, dpadPressed, dpadJustPressed, actionPressed, actionJustPressed, isPressed, isJustPressed

from thumbyAudio import audio

Expand Down
3 changes: 0 additions & 3 deletions ThumbyGames/lib-emulator/thumbyAudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
from time import ticks_ms, ticks_diff
import emulator

# Last updated 14-Dec-2022
__version__ = '1.9'

# Audio class, from which the audio namespace is defined.
class AudioClass:
def __init__(self, pwm):
Expand Down
45 changes: 42 additions & 3 deletions ThumbyGames/lib-emulator/thumbyButton.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
from thumbyHardware import swL, swR, swU, swD, swA, swB
import emulator

# Last updated 14-Dec-2022
__version__ = '1.9'

class ButtonClass:
def __init__(self, pin):
self.pin = pin
Expand Down Expand Up @@ -66,6 +63,14 @@ def update(self):
buttonL = ButtonClass(swL) # D-pad left
buttonR = ButtonClass(swR) # D-pad right

# Masks for isPressed / isJustPressed
buttonMaskA = const(0b00100000)
buttonMaskB = const(0b00010000)
buttonMaskD = const(0b00001000)
buttonMaskU = const(0b00000100)
buttonMaskR = const(0b00000010)
buttonMaskL = const(0b00000001)

# Returns true if any buttons are currently pressed on the thumby.
@micropython.native
def inputPressed():
Expand Down Expand Up @@ -95,3 +100,37 @@ def actionPressed():
@micropython.native
def actionJustPressed():
return (buttonA.justPressed() or buttonB.justPressed())

# Returns true if any of the masked buttons are pressed on the thumby.
@micropython.native
def isPressed(mask):
if((mask & 1) > 0 and buttonL.pressed()):
return True
elif((mask & 2) > 0 and buttonR.pressed()):
return True
elif((mask & 4) > 0 and buttonU.pressed()):
return True
elif((mask & 8) > 0 and buttonD.pressed()):
return True
elif((mask & 16) > 0 and buttonB.pressed()):
return True
elif((mask & 32) > 0 and buttonA.pressed()):
return True
return False

# Returns true if any of the masked buttons were just pressed on the thumby.
@micropython.native
def isJustPressed(mask):
if((mask & 1) > 0 and buttonL.justPressed()):
return True
elif((mask & 2) > 0 and buttonR.justPressed()):
return True
elif((mask & 4) > 0 and buttonU.justPressed()):
return True
elif((mask & 8) > 0 and buttonD.justPressed()):
return True
elif((mask & 16) > 0 and buttonB.justPressed()):
return True
elif((mask & 32) > 0 and buttonA.justPressed()):
return True
return False
26 changes: 12 additions & 14 deletions ThumbyGames/lib-emulator/thumbyGraphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
from thumbyButton import buttonA, buttonB, buttonU, buttonD, buttonL, buttonR
import emulator

# Last updated 15-Dec-2022
__version__ = '1.9'

# Graphics class, from which the gfx namespace is defined.
class GraphicsClass:
def __init__(self, display, width, height):
self.display = display
self.buffer = memoryview(display.buffer)
self.initEmuScreen()
self.width = width
self.height = height
Expand All @@ -47,7 +45,7 @@ def __init__(self, display, width, height):

@micropython.viper
def initEmuScreen(self):
emulator.screen_breakpoint(ptr16(self.display.buffer))
emulator.screen_breakpoint(ptr16(self.buffer))

@micropython.native
def setFont(self, fontFile, width, height, space):
Expand Down Expand Up @@ -95,12 +93,12 @@ def brightness(self,setting):
# Fill the buffer with a given color.
@micropython.viper
def fill(self, color:int):
buf = ptr8(self.display.buffer)
buf = ptr8(self.buffer)
if int(color)==int(0):
for i in range(int(len(self.display.buffer))):
for i in range(int(len(self.buffer))):
buf[int(i)]=0
else:
for i in range(int(len(self.display.buffer))):
for i in range(int(len(self.buffer))):
buf[i]=0xff

@micropython.viper
Expand All @@ -111,7 +109,7 @@ def setPixel(self, x:int, y:int, color:int):
return
if not 0<=y<screenHeight:
return
buf = ptr8(self.display.buffer)
buf = ptr8(self.buffer)
if(color==int(1)):
buf[(y >> 3) * screenWidth + x] |= 1 << (y & 0x07)
elif(color==int(0)):
Expand All @@ -126,15 +124,15 @@ def getPixel(self, x:int, y:int) -> int:
return 0
if not 0<=y<int(screenHeight):
return 0
buf = ptr8(self.display.buffer)
buf = ptr8(self.buffer)
if(buf[(y >> 3) * int(screenWidth) + x] & 1 << (y & 0x07)):
return 1
return 0

# Draw a line from (x1, y1) to (x2, y2) in a given color- taken from MicroPython FrameBuf implementation
@micropython.viper
def drawLine(self, x1:int, y1:int, x2:int, y2:int, color:int):
buf = ptr8(self.display.buffer)
buf = ptr8(self.buffer)
dx = int(x2 - x1)
sx = int(1)
if (dx <= 0):
Expand Down Expand Up @@ -215,7 +213,7 @@ def drawFilledRectangle(self, x:int, y:int, width:int, height:int, color:int):
width=int(self.max_x)-x
if(y+height>int(self.max_y)):
height=int(self.max_y)-y
buf = ptr8(self.display.buffer)
buf = ptr8(self.buffer)
yMax=y+height+1
screenWidth=int(self.width)
if(color==int(1)):
Expand All @@ -239,7 +237,7 @@ def drawText(self, stringToPrint:ptr8, x:int, y:int, color:int):
xPos=int(x)
charNum=int(0)
charBitMap=int(0)
ptr = ptr8(self.display.buffer)
ptr = ptr8(self.buffer)
sprtptr = ptr8(self.textBitmap)
screenWidth=int(self.width)
screenHeight=int(self.height)
Expand Down Expand Up @@ -295,7 +293,7 @@ def blit(self, sprtptr:ptr8, x:int, y:int, width:int, height:int, key:int, mirro
return
xStart=int(x)
yStart=int(y)
ptr = ptr8(self.display.buffer)
ptr = ptr8(self.buffer)
screenWidth = int(self.width)
screenHeight = int(self.height)

Expand Down Expand Up @@ -354,7 +352,7 @@ def blitWithMask(self, sprtptr:ptr8, x:int, y:int, width:int, height:int, key:in
return
xStart=int(x)
yStart=int(y)
ptr = ptr8(self.display.buffer)
ptr = ptr8(self.buffer)

yFirst=0-yStart
blitHeight=height
Expand Down
5 changes: 1 addition & 4 deletions ThumbyGames/lib-emulator/thumbyHardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
from machine import Pin, PWM, SPI, reset as machineReset
import emulator

# Last updated 27-Dec-2022
__version__ = '1.9'

# Pin definitions for button inputs & buzzer.
swL = Pin(3, Pin.IN, Pin.PULL_UP) # D-pad left
swR = Pin(5, Pin.IN, Pin.PULL_UP) # D-pad right
Expand Down Expand Up @@ -65,4 +62,4 @@

# Wrap machine.reset() to be accessible as thumby.reset()
def reset():
machineReset()
machineReset()
5 changes: 1 addition & 4 deletions ThumbyGames/lib-emulator/thumbyLink.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
from time import ticks_ms, ticks_diff
import emulator

# Last updated 14-Dec-2022
__version__ = '1.9'

class LinkClass:
def __init__(self):
self.initialized = False
Expand Down Expand Up @@ -152,4 +149,4 @@ def receive(self):
self.sent = False

# Link instantiation
link = LinkClass()
link = LinkClass()
3 changes: 0 additions & 3 deletions ThumbyGames/lib-emulator/thumbySaves.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
from os import chdir, getcwd, mkdir, rename, remove, stat
import emulator

# Last updated 20-Dec-2022
__version__ = '1.9'

class SavesClass:
def __init__(self):

Expand Down
5 changes: 1 addition & 4 deletions ThumbyGames/lib-emulator/thumbySprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
from os import stat
import emulator

# Last updated 14-Dec-2022
__version__ = '1.9'

# Sprite class for holding pixel data
class Sprite:
@micropython.native
Expand Down Expand Up @@ -65,4 +62,4 @@ def setFrame(self, frame):
self.file.seek(offset)
self.file.readinto(self.bitmap)
elif type(self.bitmapSource)==bytearray:
self.bitmap = memoryview(self.bitmapSource)[offset:offset+self.bitmapByteCount]
self.bitmap = memoryview(self.bitmapSource)[offset:offset+self.bitmapByteCount]
8 changes: 4 additions & 4 deletions ThumbyGames/lib/thumby.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

from machine import freq

# Last updated 9-Jan-2023
__version__ = '1.9'
# Last updated 11-Jan-2023
__version__ = '2.0tr1'

# Grab initial frequency
__f0 = freq()
Expand All @@ -35,8 +35,8 @@

from thumbySprite import Sprite

from thumbyButton import buttonA, buttonB, buttonU, buttonD, buttonL, buttonR
from thumbyButton import inputPressed, inputJustPressed, dpadPressed, dpadJustPressed, actionPressed, actionJustPressed
from thumbyButton import buttonA, buttonB, buttonU, buttonD, buttonL, buttonR, buttonMaskA, buttonMaskB, buttonMaskU, buttonMaskD, buttonMaskL, buttonMaskR
from thumbyButton import inputPressed, inputJustPressed, dpadPressed, dpadJustPressed, actionPressed, actionJustPressed, isPressed, isJustPressed

from thumbyAudio import audio

Expand Down
3 changes: 0 additions & 3 deletions ThumbyGames/lib/thumbyAudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
from machine import Timer
from time import ticks_ms, ticks_diff

# Last updated 14-Dec-2022
__version__ = '1.9'

# Audio class, from which the audio namespace is defined.
class AudioClass:
def __init__(self, pwm):
Expand Down
45 changes: 42 additions & 3 deletions ThumbyGames/lib/thumbyButton.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

from thumbyHardware import swL, swR, swU, swD, swA, swB

# Last updated 14-Dec-2022
__version__ = '1.9'

class ButtonClass:
def __init__(self, pin):
self.pin = pin
Expand Down Expand Up @@ -64,6 +61,14 @@ def update(self):
buttonL = ButtonClass(swL) # D-pad left
buttonR = ButtonClass(swR) # D-pad right

# Masks for isPressed / isJustPressed
buttonMaskA = const(0b00100000)
buttonMaskB = const(0b00010000)
buttonMaskD = const(0b00001000)
buttonMaskU = const(0b00000100)
buttonMaskR = const(0b00000010)
buttonMaskL = const(0b00000001)

# Returns true if any buttons are currently pressed on the thumby.
@micropython.native
def inputPressed():
Expand Down Expand Up @@ -93,3 +98,37 @@ def actionPressed():
@micropython.native
def actionJustPressed():
return (buttonA.justPressed() or buttonB.justPressed())

# Returns true if any of the masked buttons are pressed on the thumby.
@micropython.native
def isPressed(mask):
if((mask & 1) > 0 and buttonL.pressed()):
return True
elif((mask & 2) > 0 and buttonR.pressed()):
return True
elif((mask & 4) > 0 and buttonU.pressed()):
return True
elif((mask & 8) > 0 and buttonD.pressed()):
return True
elif((mask & 16) > 0 and buttonB.pressed()):
return True
elif((mask & 32) > 0 and buttonA.pressed()):
return True
return False

# Returns true if any of the masked buttons were just pressed on the thumby.
@micropython.native
def isJustPressed(mask):
if((mask & 1) > 0 and buttonL.justPressed()):
return True
elif((mask & 2) > 0 and buttonR.justPressed()):
return True
elif((mask & 4) > 0 and buttonU.justPressed()):
return True
elif((mask & 8) > 0 and buttonD.justPressed()):
return True
elif((mask & 16) > 0 and buttonB.justPressed()):
return True
elif((mask & 32) > 0 and buttonA.justPressed()):
return True
return False
Loading