Lua Reference

Important notes

Please make sure you did not switch lua off in the config otherwise it will not work.

Also if you already have a license make sure it includes the lua runtime. Check the logfile and look out init.lua gets searched and loaded and for your lua functions being called when you trigger tests.

See the lua example videos on my youtube channel: e.g. https://www.youtube.com/watch?v=Se2uvYuHbf0

Global variabels

config: all the main config parameters from the raspisound.ini file as lua table. That means you can access it like config.say_version.

version: the version of the TILT!Audion firmware as string. e.g. “1.29”

musicPlayingId: the number of the currently playing background music (or 0 if none).

effectiveVolume: the effectiveVolume (overall volume).

Functions

playSound(id, volume, index): play a sound effect (or background music) from sound pack.
id: the sound effect id.
volume (optional): if set (0-30) override the standard volume from sound pack and play with given volume.
index (optional): if set, use a specfic sound file for the effect. This is only relevant, when the sound pack defines more than one sound file for this effect.

getSound( id ): returns a lua table (or nil) for the sound effect with given id. the lua table contains the properties:
name: the name of the sound effect.
samples: number if sound files for the effect (normally 1).
type: 1-6 depending on sound type: music, voice, effect ..
command: string if the effect has an command attached

getCurrentSoundPack(): returns the current sound pack as lua table.

port( pinNumber, value): set port to high or low. pinNumber is one of the valid Raspberry Pi pins the can be used for output (basically the shaker pin) or 100-115 for port extender pins. value = 0 -> LOW output, value != 0 -> HIGH output.

portseq( pinNumber, time1, time2, … ): queue a sequence of HIGH, LOW changes for a GPIO pin. pinNumber see above. time1, time2 … define the intervals in milliseconds, that will make the output go HIGH-LOW-HIGH …

send( command ): send a string command to attached device (either serial or via TCP, depending on config). Can be used with the serial media server.

sendI2C( deviceAdress, reg, data): send a command to an attached i2c device. deviceAddress is 1 Byte device address on i2c bus. reg is 1 byte register number, data is one byte data to write to register.

sendI2CW( deviceAdress, reg, data): send a command to an attached i2c device. deviceAddress is 1 Byte device address on i2c bus. reg is 1 byte register number, data is two byte data to write to register.

readI2C( deviceAdress, reg): read data from an n I2C device and returns an int.

log( logLevel, message ): logs a message into main logfile. logLevel: 1=ERROR up to 4=DEBUG, message a string with log message. NOTE: normal print output will also go to the logfile if DEBUG is switched on.

setVolume( newVolume ): set a new volume, value 0-100

decVolume(): decrease volume

incVolume(): increase volume

nextSoundSet(): cycle through installed sound packs

restart(): restart TILT!Audio firmware

reboot(): reboot the raspberry pi completely

pauseMusic(): pause background music

resumeMusic( ms ): resume pause background music and fade back in ms = milli seconds

stopMusic(): stops background music

fadeMusic( milliSeconds ): fades background music in milliSeconds

fadeOutID( ID, milliSeconds): fades out a specific sound (referred by ID) in milliSeconds

fadeOutSfx( milliSeconds): fades out sfx sounds in milliSeconds

fadeOutVoice( milliSeconds): fades out voice sounds in milliSeconds

fadeOutAllEffects( milliSeconds): fades out both woice and sfx in milliSeconds

stopAllSamples(): stops all running sound effects other than background music

omx player commands (beta)

omxPause(): pause player playback

omxAction( action ): perform an action on player. action numbers see OMXControl.h

omxSetPos( pos ): set playback position absolute in milli seconds

omxStop(): stop player playback

omxPlay( file ): play a new file

omxGetPos(): get the current playing pos in milli seconds.

omxGetStatus(): return current player status: 0 = pause, 1 = playing

Handlers (beta)

There are two special lua functions in your script recognized at startup. They are called handlers. Just put a function named timer or eventHandler and it will be called like described:

timer( millis ): this function gets called every milli second (approximately). It is run in the main loop of TILT!Audio firmware (will be changed maybe), so don’t block anything and keep the function short and fast.

eventHander( stringMessage): it the event handler feature is enabled with TILT!Audio, every time the firmware receives an event, the function gets called.

Shaker control with pwm

Using a Arduino mini pro as extension to a TILT!Audio board, you can easily control a shaker motor via pwm (pulse width modulation).

First we need a motor driver module that is capable of driving the high current motor power. e.g. something like this:

Dc motor driver module for up to 10A

The picture is more or less self-explaining the only thing that need to be connected to the Arduino mini pro is the GND / PWM input.

Now looking at the Arduino mini pro we use pin 5 as pwm output and to connect to the TILT!Audio board we use the i2c bus the is controlled by the SCL / SDC pins (together with GND / Vcc of course):

Be sure to choose a 5v type for Arduino mini pro as there are also available in 3.3v but the TILT!Audio connector for the i2c bus provides 5v. Also the 5v type runs on 16Mhz instead of only 8Mhz for 3.3v with is also good.

So connections are:

FromTo
TA-Board: GNDGND (right side)
TA-Board: VccVCC (right side)
TA-Board: SDASDA (green)
TA-Board: SCLSCL (green)
Arduino: GNDPower Driver: GND (right)
Arduino: Pin5 (PWM)Power Driver: PWM (right)

Now we create a script for the Arduino mini pro like this (see https://github.com/sker65/tiltaudio-extensions). We start with initial setup stuff:

First thing we do is switch shaker on, off and set speed (within a limited range to not make to pinball machine jump):

Second we wire these functions to i2c control:

And additionally we can create “sequences”. A sequence would play back some schema of on and off or speed up and speed downs. The idea is to define a sequence as 3 integers: command byte, value, interval in milliseconds. We define these commands:

  • 0x00: stop -> end of the sequence
  • 0x01: switch on, value -> speed
  • 0x02: switch off, value -> don’t matter
  • 0x03: ramp up, value -> target speed at end of interval

We delegate playing those sequences to the Arduino and later just trigger it from the TILT!Audio board by sending some command to the i2c bus:

And again create a function that triggers the sequence playback and wire it to i2c control:

Find the complete example on github.

To get an idea, how it finally will play all together I did a test installation:

Test a TILT!Audio board

In order to test your TILTAudio board including the WPC bus interface to the pinball machine you can use an arduino mega board.

Just connect the mega with the TILT!Audio board like this:

Connections from the Arduino mega to WPC bus on TILT!Audio

Connecting also the 5v power line is optional. You need to choose either to power everything from on source or just skip the red 5v wire and power the 5v on the TA board and the Arduino separately.

For the address decoder (the smd chips) to work the raspberry pi needs to be plugged in as the 3v3 power for the decoder comes from the pi’s onboard regulator.

Also the resistor network RN3-5 must be installed, without them bus connections are not there. 12V can be skipped for testing, which means amps will not work, but you could also connect a speaker either to the PI’s audio jack directly or use the audio jack on the DAC module.

Then load the following ino script to the Arduino:

This way you can emulate sound effect commands to would normally come from the pinball machine.

I will upload a more detailed example on the github repo: https://github.com/sker65/tiltaudio-extensions

Connections needed:

ArduinoTILT!Audio
PA0:22CD0 wpc15
PA1:23CD1 wpc13
PA2:24CD2 wpc11
PA3:25CD3 wpc9
PA4:26CD4 wpc7
PA5:27CD5 wpc5
PA6:28CD6 wpc3
PA7:29CD7 wpc1
PC0:37CA0 wpc25
PC1:36CA1 wpc23
PC2:35CA2 wpc21
PC3:34CA3 wpc19
PC4:33CA4 wpc17
PC5:32RW:wpc31
PC6:31WDEN:wpc29
GNDGND