Author Topic: how to run an external script while navigating in a theme  (Read 4642 times)

tipoto

  • Full Member
  • ***
  • Posts: 43
    • View Profile
how to run an external script while navigating in a theme
« on: September 21, 2016, 02:35:35 AM »
I was wondering if there was a command that allowed to run an external script (a python script for example) while we are navigating in a theme, like moving to another game or letter?
I use an Arduino to control a bunch of LEDs and while I know how to communicate with it at the moment I launch a game, I don't know how to do it while I'm just navigating in AM


tipoto

  • Full Member
  • ***
  • Posts: 43
    • View Profile
Re: how to run an external script while navigating in a theme
« Reply #2 on: September 21, 2016, 06:52:32 PM »
Oh cool!
It doesn't look that complicated! I completely missed this command.
I'll try this and leave a message to confirm it worked (or not ;) ).

Thank you for your help!

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: how to run an external script while navigating in a theme
« Reply #3 on: September 21, 2016, 07:05:02 PM »
Oh cool!
It doesn't look that complicated! I completely missed this command.
I'll try this and leave a message to confirm it worked (or not ;) ).

Thank you for your help!

Sounds good. If you cant figure it out, paste your code in a post and ill look at it. Still without a home isp, so editing examples is easier for me than providing them right now.

tipoto

  • Full Member
  • ***
  • Posts: 43
    • View Profile
Re: how to run an external script while navigating in a theme
« Reply #4 on: September 22, 2016, 02:27:38 AM »
It worked beautifully! :) Thanks keilmillerjr !!!

In case other people are interested, here is a simplified version of the code I used:
Code: [Select]
fe.add_transition_callback( "update_lb" );
function update_lb( ttype, var, ttime )
{
switch ( ttype )
{
case Transition.ToNewSelection:
local romname = fe.game_info( Info.Name );
fe.plugin_command_bg( "/home/odroid/.attract/emulators/RGB_leds/am_arduino.sh", romname );
break;
}
}
This will run the script called "am_arduino.sh" with the rom name as parameter, everytime I select a new game on my list.
An executable can be launched instead of a script of course.
Also, it's always good to add a little delay before the command is launched, because if you change your selection very quickly, it could mess everything up. In my example there is no delay.

solecize

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: how to run an external script while navigating in a theme
« Reply #5 on: December 03, 2016, 01:22:14 AM »
So did it work? This project is similar to what I'm doing. I'd like to take the rom name and pass it off to a script that will write to an Arduino which will, in turn, display a graphic on my LED matrix.

Did you figure out how much delay worked best? any pitfalls?

tipoto

  • Full Member
  • ***
  • Posts: 43
    • View Profile
Re: how to run an external script while navigating in a theme
« Reply #6 on: December 03, 2016, 02:56:16 AM »
Actually I didn't physically connect the arduino yet since I'm working on other parts of my project, so I will maybe have to adjust the delay afterwards, but I also use the same technique to play audios which say the rom names while I'm navigating over the library, and the best timing for that inside the callback function is 20 (I don't know if it's millisecond), I'm expecting to use the same timing with the arduino, but maybe I will have to adjust it.

Here is what I use as code to create a delay:

Code: [Select]
local romname_delay = 0;
local romname_set = 0;

// CALLBACK FUNCTIONS
fe.add_transition_callback( "update_lb" );
function update_lb( ttype, var, ttime )
{
switch ( ttype )
{
case Transition.StartLayout:
romname_delay = 0; romname_set = 0;
break;

case Transition.ToNewSelection:
romname_delay = 0; romname_set = 0;
break;

case Transition.ToNewList:
romname_delay = 0; romname_set = 0;
break;
}
}

fe.add_ticks_callback( "tick_fn" );
function tick_fn( ttime )
{
romname_delay++;
if ( romname_delay > 20 && romname_set == 0 ) // - (DELAY: 20 LOOPS), then it sends rom name info to am_arduino.sh and "<system>+<rom name>" to am_tts_titles.sh
{
local romname = fe.game_info( Info.Name );
local system = fe.game_info( Info.Emulator );
local sys_rom = ( system + "+" + romname );
fe.plugin_command_bg( "/home/odroid/.attract/emulators/RGB_leds/am_arduino.sh", romname );
fe.plugin_command_bg( "/home/odroid/.attract/emulators/scripts/tools/text_to_speech/am_tts_titles.sh", sys_rom );
romname_set = 1;
}
}

You need to edit the code to make it work with your project, but everything is there.
"romname_delay" variable is my delay, every-time a new game is selected, this variable is reseted, so the external script will be only launched if I stay on the same selection longer than 20 loops in the callback.
Now that the base of this system works, I'm planning to do a lot of things with it, but I'm not focusing on this right now, so it's still a work in progress feature on my project. ;)
« Last Edit: December 03, 2016, 02:58:09 AM by tipoto »

tipoto

  • Full Member
  • ***
  • Posts: 43
    • View Profile
Re: how to run an external script while navigating in a theme
« Reply #7 on: December 03, 2016, 11:03:24 AM »
Just to make sure I answer completely your question... My connection between my Odroid XU4 (Raspberry Pi like) and my Arduino works perfectly.
I can lit any LED I want with any color I want by sending orders from the XU4, but since the work on my Arduino has been done months ago, way before I decide to add this new feature with Attract-Mode, now I need to slightly modify the code in the arduino, because currently it launches an animation with all the LEDs before displaying the final result with the right LEDs, and so I need to remove this to make it reacting immediately and I also need to add a new part in my code to ask the Arduino to return to the Attract-Menu mode  after 2 or 3 seconds.
Like I said, I'm not working on this part of my project currently, the Arduino and the RGB module are on their boxes and the modifications are not done yet, but I have no doubt at all that it's going to work, the difficult part is already done (code + electronic).