Shopping Cart
We accept VISA Mastercard American Express Discover

Windows, Mac OS X, Linux, and Android logos

XK-24 Backlighting & Indicator LED Tutorial

backlight

The controllable backlighting on an XK-24 (and other Pi3 devices) is a somewhat advanced feature, as it requires some Visual Basic programming. Don’t let that scare you though, because it can be done by anyone if they follow this tutorial.

First Steps

The very first thing you need to do is open MacroWorks 3.1 (you can press the programming switch at the top of your device, or open it via the system tray icon). Then, you’ll need to go to View and then Advanced View in order to look at the script that’s running your device.

It is okay if there is a lot in there you do not understand; you don’t necessarily need to. There are three areas in the script that you should be concerned with.

The first one is near the top and is what happens when your script first loads. You’ll see that it’s calling something named LEDset().

Public Sub ScriptLoad() Implements Interfaces.IScript.ScriptLoad
 LEDset()
End Sub

The next section you need to be aware of is another instance of LEDset() being called. It is towards the bottom of your script, just under all the button case statements. You’ll find this:

Case 1026 'ProgSwitch -restricted-
 If State 'Pressed [MWTAG10]
  '<Open-Close GUI>
  If MW3.IsGUIOpen(MyDevice)
   MW3.SetGUI(MyDevice,0,false)
  Else
   MW3.SetGUI(MyDevice,0,true)
  End If
  '</Open-Close GUI>
 Else 'Released [MWTAG11]
 End If 'Press-Release [/MWTAG10/MWTAG11]
End Select 'Button Check End [/MWTAG9]

If Repeat = 0
 LEDset() ' Set the Indicator LEDs
End If

End Sub

This means that anytime a button is pressed, it executes LEDset() at the end of it. So what is LEDset()?

That’s the third area you need to be aware of. You’ll find an area of the script that looks like this:

'=?=?=?=Indicator LEDs Start =?=?=?=?=?=?=?=?=?=?=?=?=?=?=[MWTAG5]
 Public Sub LEDset()
  If Layer_Red = 1
   MW3.SetLED(MyDevice,3001,0)
   MW3.SetLED(MyDevice,3002,1) 'Turn on the Red Indicator LED
   MW3.SetAllBacklightLED(MyDevice,0,0) ' Turn blue off
   MW3.SetAllBacklightLED(MyDevice,1,1) ' Turn red on
   MW3.SetBacklightIntensity(MyDevice,255,255) 'blue and red max intensity
  Else
   MW3.SetLED(MyDevice,3002,0)'Turn off the Red Indicator LED
   MW3.SetLED(MyDevice,3001,1)
   MW3.SetAllBacklightLED(MyDevice,1,0) ' Turn red off
   MW3.SetAllBacklightLED(MyDevice,0,1) ' Turn blue on
   MW3.SetBacklightIntensity(MyDevice,255,255) 'blue and red max intensity
  End If
 End Sub
'=?=?=?= Indicator LEDs End ?=?=?=?=?=?=?=?=?=?=?=?=?=?=?=[/MWTAG5]

What this code does is tell the backlighting how to behave when the device is connected, when layers change, or resets the backlighting after a button press. Because we are manipulating the backlighting ourselves, we don’t really want MW3 to do this anymore.

You can insert an apostrophe before any of these lines to have MacroWorks 3.1 ignore them. For testing purposes, we’ll comment out (insert an apostrophe in front of) the LEDset() calls in both the script load and button press areas, but leave the actual LEDset() code alone. So, you’ll want each to look something like this:

Public Sub ScriptLoad() Implements Interfaces.IScript.ScriptLoad
 'LEDset()
End Sub

You can study the code in the LEDset() section, and play with it, if you want, as a lot of what we’re discussing later is included there. I’ll explain in a bit.

Manipulating the Indicator LEDs

Now we’re going to insert some of our own code. First thing we’ll do is play with the indicator LEDs, the red and green lights on the upper left corner of the device.

The MW3.SetLED function turns the indicator LEDs on or off. 3001 refers to the green one, 3002 refers to the red one. 0 is off, 1 is on. So you could make it so red is on and green is off:

MW3.SetLED(MyDevice,3001,0)
MW3.SetLED(MyDevice,3002,1)

Or even that both are on:

MW3.SetLED(MyDevice,3001,1)
MW3.SetLED(MyDevice,3002,1)

And so on. You can insert this code directly into the ScriptLoad() section of the script, or even just under a key. Here’s an example of controlling the indicator LED’s via a button press. This will turn the red LED on, and the green off, when you press the key and then reverse it when you let go. Take a look:

Case 1001 'Button 001
 If State 'Pressed [MWTAG10]
  MW3.SetLED(MyDevice,3001,0)
  MW3.SetLED(MyDevice,3002,1)
 Else 'Released [MWTAG11]
  MW3.SetLED(MyDevice,3001,1)
  MW3.SetLED(MyDevice,3002,0)
 End If 'Press-Release [/MWTAG10/MWTAG11]

Controlling the Backlighting as a Whole

In MacroWorks 3, you can turn the red and blue backlighting on and off as you wish. Want your keypad to only glow red? Sure! Want to get rid of the backlighting entirely? We can certainly do that.

MW3.SetAllBacklightLED turns the entire red or blue backlighting bank on or off. It works like this: MW3.SetAllBacklightLED(MyDevice,[0 blue, 1 red],[0 off, 1 on]) so blue on and red off looks like this:

MW3.SetAllBacklightLED(MyDevice,0,1)
MW3.SetAllBacklightLED(MyDevice,1,0)

To press a key to turn the red backlighting on, then let it go back to blue when you let go, you’d use code like this:

Case 1001 'Button 001
 If State 'Pressed [MWTAG10]
  MW3.SetAllBacklightLED(MyDevice,0,0)
  MW3.SetAllBacklightLED(MyDevice,1,1)
 Else 'Released [MWTAG11]
  MW3.SetAllBacklightLED(MyDevice,0,1)
  MW3.SetAllBacklightLED(MyDevice,1,0)
End If 'Press-Release [/MWTAG10/MWTAG11]

To control the brightness of the backlighting banks, you’ll use the MW3.SetBacklightIntensity function.

You can use this function to adjust it from 0 (no brightness) to 255 (full brightness). The first value is bank one (blue) the second value is bank two (red). Here’s the code for both at full power:

MW3.SetBacklightIntensity(MyDevice,255,255)

And this would be blue at full power and red at (slightly less than) half power:

MW3.SetBacklightIntensity(MyDevice,255,125)

If you wanted to turn both backlighting banks on at full power at start up, it would look something like this:

Public Sub ScriptLoad() Implements Interfaces.IScript.ScriptLoad
 'LEDset()
 MW3.SetBacklightIntensity(MyDevice,255,255)
 MW3.SetAllBacklightLED(MyDevice,0,1)
 MW3.SetAllBacklightLED(MyDevice,1,1)
End Sub

Programming the Backlight LEDs Individually

First you’ll need to declare your "CIDS" (the numbers that identify the lights) as an integer. You can do this at the top of your script under "Initialize Variables Start" or you can do it in line, right before you intend to call them.

Which LED is which number? Each individual blue or red LED has a five digit CID. They will be similar to the button numbers. So Button 1001 has two LEDs: 10001 (blue) and 11001 (red). Button 1024 also has two: 10024 (blue) and 11024 (red).

Declare, for example, like this:

Dim CIDS As Integer() = {10001, 11002}

The above references the blue LED that corresponds to Button 1001 and the red LED that corresponds to Button 1002.

Now you’ll want to use your variable in your code. In order to turn those lights on, use:

MW3.SetBacklightLED(MyDevice,CIDS,1)

And to turn them off:

MW3.SetBacklightLED(MyDevice,CIDS,0)

Here's a full example:

First I declare my CIDS:

Dim CIDS as Integer () = {11001, 11007, 11013, 11019, 11002, 11008, 11014, 11020, 11005, 11006}

Then I use them:

Public Sub ScriptLoad() Implements Interfaces.IScript.ScriptLoad
 'LEDset()
 MW3.SetAllBacklightLED(MyDevice,0,1)
 MW3.SetAllBacklightLED(MyDevice,1,0)
 MW3.SetBacklightIntensity(MyDevice,125,255)
 MW3.SetBacklightLED(MyDevice,CIDS,1) 
End Sub

This turns on all my blue LED’s at half power and strategically lights up some of the red ones at full power, to make certain groups of keys easier to see.