Page 1 of 1

Bit Driven Button Highlight

Posted: Sat Oct 31, 2020 4:09 am
by HowHardCanItBe
Trying to change a button's color ("Highlight") based on Virtual Bit 55. This button will eventually run a KMotion C program (Initialize file - Script not set up yet in attached pictures - forever loop sets Bit55 after initializing, clears same if need to re-initialize occurs).

The button's "Highlight" doesn't change when I cycle Bit 55 (as the Label for Tool Length, But30, does).
If I change the Style (e.g. to Label, then click Set, then back to Button, click Set), it does change, but not as expected (see attached);

Bit55=0 (57=1). Note black button.
Init Bit55 OFF.png

Bit55=1 (57=0) . Note Red button but with yellow font.
Init Bit55 ON.png
From https://www.dynomotion.com/wiki/index.p ... een_Editor;
Buttons can have all 4 options (foreground and background colors in on- and off-state) or they can also be bitmaps. Labels can load actions/scripts when the main screen is loaded.

What am I missing?

Re: Bit Driven Button Highlight

Posted: Sat Oct 31, 2020 4:02 pm
by TomKerekes
Try using a Toggle Button.

Also don't set the text color and text background both to the same color (0 = black)

Re: Bit Driven Button Highlight

Posted: Sat Oct 31, 2020 7:41 pm
by HowHardCanItBe
Thanks for the quick reply.

I was using your color fields incorrectly. Here is how I think they work for a Style=ToggleButton button;
  • First column of color fields is for the text color (top when bit=0, bottom when bit=1),
  • Second column of fields is for the fill color.
Screen Editor Text and Fill.png
Screen Editor Text and Fill.png (1.9 KiB) Viewed 2378 times
And pressing the button in KMotionCNC immediately toggles the assigned bit (bit in the Var field) and executes whatever action has been assigned.

If the bit changes later, so does the button's text and fill attributes.

Since I don't want the button click events to determine the assigned bit's state (I want that to be dependent on successful completion of the assigned action - a C file, and subsequent events, like an axis disabling, etc.), I added code to clear the bit state at the beginning of my C file (presuming the ToggleButton's toggle event precedes my C code's ClearBit statement - perhaps I'll see it flash).

Please let me know if I'm missing anything.

What awesome support!

Re: Bit Driven Button Highlight

Posted: Sat Oct 31, 2020 9:40 pm
by TomKerekes
That all sounds correct. Thx

Re: Bit Driven Button Highlight

Posted: Fri May 07, 2021 1:42 am
by greg9504
Hello,

I'm trying to do something similar but the c code is not running when I toggle the button. If I change the button type to button from togglebutton then the c code runs but then the bitmaps don't update based on the bit state and the bit doesn't toggle. I tried "Program" and "Program/wait" as the action type, neither worked.

I'm running an older version 4.34j, with a slightly updated screen builder (that added scaling).

If I update can you confirm that the toggle button will run C code? Just don't want to go through the updating if the bug is still there. Or perhaps I'm doing something wrong :)
Thanks.
kmotioncnc_screensetup_toggle_runc.PNG
FWIW the goal is to add a toggle button that displays the state of the axis enabled. If a limit switch is tripped, and the axis is disabled, the toggle button could be used to re-enable the axis so it can be moved away from the limit switch. I have code that runs in the main thread that updates the bit state based on the enabled state of the axis.

code that gets run when the toggle button is pressed:

Code: Select all

#include "GregCommon.h"
int Enabled(int axis_num)
{
	return chan[axis_num].Enable;
}

main()
{
	// this is to allow the user to re-enable the axis after a limit switch fault, axis can be moved away from limit
	int desiredState = ReadBit(XAXIS_ENABLED_BIT);
	int currentState = Enabled(XAXIS_NUM); // chan[axis_num].Enable;
	printf("Current axis enabled state: %d, desired axis enabled state: %d\n", currentState, desiredState);
	if (desiredState == 1) {
		EnableAxis(XAXIS_NUM);
	} else {
		DisableAxis(XAXIS_NUM);
	}
	currentState = Enabled(XAXIS_NUM);
	printf("Axis state: %d\n", currentState);
}
main thread code, ReadAxisEnabledState() called in forever loop

Code: Select all

void SetAxisEnabledStatusBit(int axisEnableBit, int enabled)
{
	if (enabled) {
		SetBit(axisEnableBit);
	}
	else {
		ClearBit(axisEnableBit);
	}
}
void ReadAxisEnabledState()
{
	int xstate = Enabled(XAXIS_NUM);
	int ystate = Enabled(YAXIS_NUM);
	int astate = Enabled(AAXIS_NUM);
	SetAxisEnabledStatusBit(XAXIS_ENABLED_BIT, xstate);
	SetAxisEnabledStatusBit(YAXIS_ENABLED_BIT, ystate);
	SetAxisEnabledStatusBit(AAXIS_ENABLED_BIT, astate);
}

Re: Bit Driven Button Highlight

Posted: Fri May 07, 2021 6:59 pm
by TomKerekes
Yes toggle buttons were changed to also execute Actions in V4.35c