P.I. Engineering User Forum

Product Support, Technical Questions and Answers, and Examples for P.I. Engineering Products

You are not logged in.

#1 2017-01-06 12:43:20

DiGiMac
Member
From: Lake Garda, Italy
Registered: 2017-01-06
Posts: 3

Get current backlight status

Hugely enjoying my XK24. Months of tweaking and streamlining until I have a layout I'm happy with.
Now I want to play with the backlights!

Using SetSingleBacklightLED I can:
     press key 1 to perform action A and light up red
     press key 2 to perform action B and turn off key 1
But is there a way to read the current state of the backlight, so a second press on key 1 will perform action B and turn itself off?

Offline

#2 2017-01-06 13:01:29

PIE Amber
Technical Software Developer
From: Williamston, MI
Registered: 2016-01-21
Posts: 196
Website

Re: Get current backlight status

Unfortunately, no, the state of the light cannot be read. We recommend keeping track of your lights in a variable when using advanced light manipulation.

It sounds like essentially all you need is ability to toggle. You'll do that with a variable that determines whether the key has been pressed. This does require modifying the underlying code of your script file. I will provide my example here in a bit in case it is useful to you or anyone else passing by.

I had to split this into multiple posts. No idea why, but our forum software was fighting me on posting it all at once. The following posts in series are all part of the same tutorial.

Last edited by PIE Amber (2017-01-06 14:13:33)


Amber from P.I. Engineering

Offline

#3 2017-01-06 14:12:13

PIE Amber
Technical Software Developer
From: Williamston, MI
Registered: 2016-01-21
Posts: 196
Website

Re: Get current backlight status

For this, you’ll need to combine two separate concepts: toggling, and backlight manipulation. First, let’s cover the toggling.

Note: I do not cover adding your keystrokes/normal macro to this in detail. Essentially, if you want the key to send the same keystrokes or macro every time it is pressed, add it in the GUI as normal. If you want the key to send two separate sets of keystrokes, and cycle between them, the code must be included in the flag structure by hand – let me know if you need more details on this.

Toggling

Show your script file. To do this, go to Window > Script Code in MacroWorks 3.1.

Find the variables section. It’ll be near the top, under the header information, and look like this:

'=↓=↓=↓= Initialize Variables Start =↓=↓=↓=↓=↓=↓=↓=↓=[MWTAG3]

Add in your toggle flag. This is what tells MacroWorks 3.1 whether or not you have pressed the button – “0” means you have not, “1” means you have. So you’ll want to start with an assignment of “0.” This can go anywhere in the list, but go ahead and add it at the bottom under the “Dim Layer_Red…” variable:

Dim AppTitle As String = "" 'Active application title
Dim Layer_Red As Integer = 0 'Red,3
Dim flag1 As Integer = 0

Find a button. Scroll down a bit more until you see lines marked with Case… Let’s start with button 1, which is “Case 1001” in the script file. An empty button case statement (a button that has not been programmed yet) looks like this:

Case 1001 'Button 001
     If State 'Pressed          [MWTAG10]
     Else 'Released             [MWTAG11]
     End If 'Press-Release      [/MWTAG10/MWTAG11]

Add in your toggling logic. What we want is for the first time we press the button for the flag1 to be set to “1”, and the second time for it to be set back to “0.” Here’s what you should enter in:


Amber from P.I. Engineering

Offline

#4 2017-01-06 14:12:30

PIE Amber
Technical Software Developer
From: Williamston, MI
Registered: 2016-01-21
Posts: 196
Website

Re: Get current backlight status

Case 1001 'Button 001
     If State 'Pressed          [MWTAG10]
           '<Custom Code>
	   If flag1 = 0 Then
		flag1 = 1
           ElseIf flag1 = 1 Then
		flag1 = 0
           End If
           '</Custom Code>
     Else 'Released             [MWTAG11]
     End If 'Press-Release      [/MWTAG10/MWTAG11] 

Add in your key programming. For this example, we’ll use a simple text macro that will send “a” the first time the key is pressed, and “b” the second time. It will cycle between them. Note that ANY macro of any amount of lines can be in the place of the unicode string lines, depending on your use.


Amber from P.I. Engineering

Offline

#5 2017-01-06 14:12:43

PIE Amber
Technical Software Developer
From: Williamston, MI
Registered: 2016-01-21
Posts: 196
Website

Re: Get current backlight status

Case 1001 'Button 001
     If State 'Pressed          [MWTAG10]
           ‘<Custom Code>
           If flag1 = 0 Then
                MW3.sendUnicodeString(“a”)
                flag1 = 1
           ElseIf flag1 = 1 Then
                MW3.sendUnicodeString(“b”)
                flag1 = 0
           End If
           ‘</Custom Code>
     Else 'Released             [MWTAG11]
     End If 'Press-Release      [/MWTAG10/MWTAG11]

When done, click “Build.” If you do not get any errors, you are good to go.

And that’s it! The toggling idea can be applied to any type of macros.

Some notes: each button that you wish to toggle needs to have a separate flag in the variables section. I recommend keeping it so the button number and flag numbers match. Flag1 for button1, flag2 for button2, and so on. Code indentation does not matter (it is simply to make it more easily readable).

Add in Backlighting

Let’s start with the code to change the backlighting of a single key (you can generate this through MacroWorks 3.1’s “Single Backlight” function in the GUI):

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

The main thing to pay attention to is the numbers there: 1, 0, 1. The first number is the button number which should be self-explanatory, the second is the bank (0 = blue, 1 = red), and the last is “state” (1 turns the light on, 0 turns it off). So if I wanted to turn on the red LED on button 2, it would look like this:

MW3.SetSingleBacklightLED(MyDevice, 2, 1, 1)

In context, here is what the code is to toggle the red LED on and the blue LED off when the button is pressed the first time, then to switch it back to blue on and red off:

Case 1001 'Button 001
     If State 'Pressed          [MWTAG10]
           ‘<Custom Code>
           If flag1 = 0 Then
		MW3.SetSingleBacklightLED(MyDevice, 1, 1, 1) ‘turns button 1 red on
		MW3.SetSingleBacklightLED(MyDevice, 1, 0, 0) ‘turns blue 1 off
                flag1 = 1
           ElseIf flag1 = 1 Then
		MW3.SetSingleBacklightLED(MyDevice, 1, 1, 0) ‘turns button 1 red off
		MW3.SetSingleBacklightLED(MyDevice, 1, 0, 1) ‘turns blue 1 on
                flag1 = 0
           End If
           ‘</Custom Code>
     Else 'Released             [MWTAG11]
     End If 'Press-Release      [/MWTAG10/MWTAG11]

End of Tutorial


Amber from P.I. Engineering

Offline

#6 2017-01-09 11:37:21

DiGiMac
Member
From: Lake Garda, Italy
Registered: 2017-01-06
Posts: 3

Re: Get current backlight status

Hi Amber,
That works perfectly. I even added value "2" for flag1 so I could have triple-state toggling: off -> red -> blue -> off
No need for triple now but it was an interesting experiment and I'm sure that now I know it can be done, I'll find a way to implement it!
For the moment plain toggling is what I wanted in order to set up the mixer functions "arm", "mute" and "solo" in a DAW application.
Keep up the good work all at PIE.

(Will we ever see OLED X-Keys so the whole button face can change completely depending on the active program? It would be expensive I'm sure but so cool)

Offline

#7 2017-04-11 07:04:02

DiGiMac
Member
From: Lake Garda, Italy
Registered: 2017-01-06
Posts: 3

Re: Get current backlight status

I just wanted to thank you again for the tutorial.
I've abandoned any idea of using my XKeys for fun - I have too many macros already set up for work and it was too difficult to keep track of the multiple functions of each key!   :-)
But I found a great use for the toggle function at work. As well as the regular toggle key to access the red layer I now have three keys - "Page", "Magnify" and "Object" - that do nothing except turn on their own backlights and set flags. Then I have 2 keys "+" and "-".
If the "Page" key is lit, + and - move me through a document in Word, a pdf, a presentation in PP, layers in Gimp or worksheets in Excel. The red layer jumps me to the end or the beginning.
"Magnification" works in a similar way: increase or decrease zoom in steps using + or - and max or min on the red layer.
"Object" changes the size of an image or object in Office, or a layer in Gimp.
Various other keys also change their function all based on the flags set by "Page", "Magnify" and "Object". This is saving me so much time (and freed up a load of keys on my XK24)!
I also found a use for multiple toggling - one key will cycle through the colours I use most often for whatever text is selected.
Some of these rely on Office VBA macros and any program that lets me customise shortcuts I will try to match similar functions in different programs and only occupy one key.
Two negative consequences of becoming so dependent on my X-Keys:
- I struggle to do really simple tasks on any other PC and my left hand keeps reaching for the extra keys that aren't there
- 24 keys are never going to be enough unless you give us OLEDs    :-)

Thanks again to P.I.E.

Offline

#8 2017-05-17 23:10:06

htmlcolor
Member
Registered: 2017-05-17
Posts: 1

Re: Get current backlight status

Thank you for sharing them! I hope you will continue to have similar posts to share with everyone!

Offline

Board footer

Powered by FluxBB