DispatcherTimer blocks the UI from being updated? - mvvm

I have two Buttons in the View, one for recording some sound, one plays the recording when recording is done. When recording is done, I set the VM property of recording_done to 1, which is used for CanPlay(), which enables the Play button. Everything works fine so far.
I added a timer to auto stop the recording after two seconds. after some trials and errors, I settled with a DispatcherTimer. My problem is the UI (Play button) does not gets enabled until I clicked the WPF window (somewhere/anywhere on the windows). Clearly the debug msg says the recording_done is set to 1. That should trigger the UI update, but it does not, until I clicked on the windows.
I tried both ways, 1. Recording button bind to Command in ViewModel, there I start the timer and stop the timer. 2. put the recording button handler and timer code in the view's code behind. similar problems.
Any suggestion? Is this a threading/UI update issue? Thanks.

If your play button is bound to a command, and the CanExecute() of the command is bound indirectly to the recording_done property or field, it won't update until the CanExecuteChanged event on the ICommand is raised, or until WPF decided to requery the commands. I suspect this is the problem - you need to have the ICommand raise CanExecuteChanged.

Related

RENODE How press a button for 5s and invert the methods Press and Release to be compatible with button with pullup configuration

I want to simulate in RENODE my stm32f030 to pressing a button for 5 seconds. But I have some problems related to the time and also with the RENODE method to simulate a Press and a Release of the button
In my firmware I configured the pin that is connected to the button as pull-up, that is, the button without pressing it has Status=TRUE. When the button is pressed, it goes to GND and Status=FALSE.
I did a 'debug' in the RENODE methods, I simulate a Press and then with the Pressed method it returns the status, you can check that:
Press results in True status
Release results in False status
Is there a possibility to reverse this method? Or do I put it as an alias in the robot framework? I found very little documentation, any light on my problem is already welcome
About the problem with time, when I execute the Press method and then the Release method and wait for close to 20 seconds it executes the action which is to blink together the Red and Blue LEDs, see below. However in my firmware it is configured to occur an interruption in the timer after 5s.
I read a bit about this in the official documentation (https://renode.readthedocs.io/en/latest/advanced/time_framework.html) and I suspect it is related to this, but I haven't identified what I need to do to fix it.
Reversing the state of a button is quite easy.
As in the miv-board.repl file, you can do the following:
user_switch_0: Miscellaneous.Button # gpioInputs 0
invert: true <--- this is relevant here
-> gpioInputs#0
Regarding time flow, Renode operates in virtual time domain to ensure determinism of execution, regardless of the host machine. It can be similar to real time flow, but depends heavily on the type of application you run. If you poll a lot and not use interrupts to sleep - things will be slower.
To get information about current virtual time you can run emulation GetTimeSourceInfo and see the output like this:
(machine) emulation GetTimeSourceInfo
Elapsed Virtual Time: 00:00:00.304409 <--- you want this to be 5s
Elapsed Host Time: 00:00:00.831891
Current load: 2.5
Cumulative load: 2.73280684868056
State: WaitingForReportBack
Advance immediately: False
Quantum: 00:00:00.000100

How to get Notify of Input Mouse Released when using GameplayCueNotify_Looping

I have been working on a project based on Lyra Framework and am currently trying to implement a Weapon Ability: Charge_Ability - like Halo energy pistol. Here is my issue:
In the GCNL, I am attempting to Bind the OnReleased Mouse Click Event to the OnLoopingStart. Doing so, I expect the OnReleased Mouse Click Event to be fired when the input broadcast its message so I can release my “Charge Ability Bolt Fx”.
However, when running debug printString, the OnLoopingStart is never called. Only the OnRecurring Event is fire.
How can I properly register the OnRelease Event from the GCNL to retrieve input released from mouse click?
Thanks!

How to make a button trigger an event (Timer)?

I am working on a fire evacuation project and would like to trigger an event after clicking a button. Basically I want to begin the fire evacuation process manually using the button feature but would like to trigger a timer for about 60 seconds to give the pedestrians time to evacuate using the event feature.
I have tried to make pedestrians stop the evacuation process by manually clicking another button but would like to use an event trigger timer instead as this will be a better way to conduct my simulation. I cannot seem to trigger an event once the button is clicked. I've tried changing the trigger type but not sure where to go from there. Is there any code that is required or another step that is required?
Even though a dynamic event is fine in your application, you can also use a user control event:
And then in your button you can use the following code:
event.restart(60,SECOND); //you can choose any timeout here
or
event.restart(); // this will use the configuration, which in this case I set up to 60 seconds
The difference between a dynamic event and a user controlled event is that if you click the button again:
With the dynamic event, you will generate another instance of the event, meaning that if you click the button at t=0 seconds, and t=30 seconds, you will generate 2 events at t=60 seconds and t=90 seconds
With the user control event, you will restart the same event. if you click the button at t=0 seconds, and t=30 seconds, you will generate a unique event at t=90 seconds.
So depending on which one you prefer... you can choose. I would prefer the user control in case you click the button 2 times... or if you regret clicking it so fast... I don't know
Use the Dynamic Event for this.
Set it up by pulling it from the AnyLogic Agent Library to the Workspace and give it a name (here: MyDynamicEvent).
Add the code you want to have executed when the timer runs off in the Action field
Set one (or several) timed instance for this event by using the code create_MyDynamicEvent(60,SECOND);. Note: The syntax of this statement is always create_ followed by the actual name you gave your DynamicEvent type.

Continuosly pressing a button in a traitsui -based GUI

I'm building a GUI for a machine with Traits and TraitsUI. I would like the machine to perform an action as long as a button on the GUI is pressed and of course having the GUI not freeze and display the outcome of this continuous action, i.e. in a separate thread.
I can define in the class which inherits HasTraits a button and a function which performs the actions when the button is pressed (def _button_fired:), but it seems to me that the way the _button_fired is defined, the GUI actually waits for the button-pressing to be over before performing the "Action". As stated above, I would like it to execute a function the second I press the button and stop this function execution (or execute a different function) when I release the button.
You can take as an example code snippet no. 7 from Gael's tutorial and just imagine that while pressing the button, the counter should increase.
Your help will be greatly appreciated.
Hellbourne

Inner Workings of Unity3d's GUI.Button

I'm still pretty new to scripting in Unity3D, and I'm following along with a tutorial that uses GUI.Button() to draw a button on the screen.
I am intrigued by how this function works. Looking through the documentation, the proper use of GUI.Button is to invoke the function in an if statement and put the code to be called when the button is pushed within the if statement's block.
What I want to know is, how does Unity3D "magically" delay the code in the if statement until after the button is clicked? If it was being passed in as a callback function or something, then I could understand what was going on. Perhaps Unity is using continuations under the hood to delay the execution of the code, but then I feel like it would cause code after the if statement to be executed multiple times. I just like to understand how my code is working, and this particular function continues to remain "magical" to me.
I don't know if it's the right term, but I usually refer to such system as immediate mode GUI.
how does Unity3D "magically" delay the code in the if statement until
after the button is clicked?
GUI.Button simply returns true if a click event happened inside the button bounds during last frame. Basically calling that function you are polling: every frame for every button asking the engine if an event which regards that button (screen area) is happened.
If it was being passed in as a callback function or something, then I
could understand what was going on
You are probably used to an MVC like pattern, where you pass a controller delegate that's called when an UI event is raised from the view. This is something really different.
Perhaps Unity is using continuations under the hood to delay the
execution of the code, but then I feel like it would cause code after
the if statement to be executed multiple times.
No. The function simply returns immediately and return true only if an event happened. If returns false the code after the if won't be executed at all.
Side notes:
That kind of system is hard to maintain, especially for complex structured GUI.
It has really serious performance implications (memory allocation, 1 drawcall for UI element)
Unless you are writing an editor extension or custom inspector code, I'd stay away from it. If you want to build a menu implement your own system or use an external plugin (there are several good ones NGUI, EZGUI,..).
Unity has already announced a new integrated UI System, it should be released soon.
Good question. The unity3d gui goes through several event phases, or in the documentation
Events correspond to user input (key presses, mouse actions), or are UnityGUI layout or rendering events.
For each event OnGUI is called in the scripts; so OnGUI is potentially called multiple times per frame. Event.current corresponds to "current" event inside OnGUI call."
In OnGUI you can find out which event is currently happening with >Event.current
The following events are processed link:
Types of UnityGUI input and processing events.
-MouseDown
-MouseUp,mouse button was released
-MouseMove,Mouse was moved (editor views only)
-MouseDrag,Mouse was dragged
-KeyDown, A keyboard key was pressed
-KeyUp A keyboard key was released.
-ScrollWheel The scroll wheel was moved.
-Repaint A repaint event. One is sent every frame.
-Layout A layout event.
-DragUpdated Editor only: drag & drop operation updated.
-DragPerform Editor only: drag & drop operation performed.
-DragExited Editor only: drag & drop operation exited.
-Ignore Event should be ignored.
-Used Already processed event.
-ValidateCommand Validates a special command (e.g. copy & paste).
-ExecuteCommand Execute a special command (eg. copy & paste).
-ContextClick User has right-clicked (or control-clicked on the mac).
Unity GUI has much improved lately and is quite usefull if you want to handle things programmatically. If you want to handle things visually, i recommend looking at the plugins heisenbug refers to.
If you decide to use unity gui, i recommend using only one object with ongui, and let this object handle all your gui.