Skip to content

Instantly share code, notes, and snippets.

@DinsFire64
Last active March 26, 2023 19:53
Show Gist options
  • Save DinsFire64/b0b65241ada9a65fc229e093cf8dc527 to your computer and use it in GitHub Desktop.
Save DinsFire64/b0b65241ada9a65fc229e093cf8dc527 to your computer and use it in GitHub Desktop.
Connects to mame's network socket, grabs 573 lighting info, writes it to the piuio using djpohly's kernel driver.
#!/usr/bin/env python
# =============================================================================
# Author: din
# Requirements: Linux and mame-git
# Description: Set mame "output" option in mame.ini to "network" and then
# run this script after launching mame.
# =============================================================================
import socket
import os
import time
PIUIO_OUTPUT_LOCATION = "/sys/class/leds/piuio::output{}/brightness"
#mame name: piuio output number
light_order = {
"foot 1p left": 20,
"foot 1p right": 21,
"foot 1p up": 18,
"foot 1p down": 19,
"foot 2p left": 4,
"foot 2p right": 5,
"foot 2p up": 2,
"foot 2p down": 3,
"body right high": 26,
"body right low": 24,
"body left high": 23,
"body left low": 25 ,
#PIUIO doesn't have the player button lights.
#"lamp0": 0,
#"lamp1": 0,
"speaker": 10,
}
def set_piuio_light(light_num, state):
light_loc = PIUIO_OUTPUT_LOCATION.format(light_num)
#print(light_loc, " > ", state)
try:
os_file = os.open(light_loc,os.O_RDWR)
os.write(os_file, b"%d\n" % int(state))
os.close(os_file)
except:
#print("Could not update light state to PIUIO")
pass
def decode_mame_light(recv_string):
light_state = recv_string.split(' = ')
try:
set_piuio_light(light_order[light_state[0]], light_state[1])
except KeyError:
#print("Unknown light: ", light_state[0])
pass
mame_line = ''
printed_error_msg = False
while 1:
try:
#mame statically defines a network port of 8000 to connect to.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 8000))
print("\r\nConnected.")
print("Writing MAME lighting state to PIUIO...")
while 1:
char = s.recv(1)
#buffer up an entire line until we see a carrage return
if char == b'\r':
decode_mame_light(mame_line)
mame_line = ''
else:
mame_line += char[0:1].decode("utf-8")
except KeyboardInterrupt:
break
except:
if not printed_error_msg:
print("Connection to mame failed, trying again.", end='', flush=True)
printed_error_msg = True
else:
print('.', end='', flush=True)
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment