VHDL Push Button & LED - vhdl

New to VHDL, familiarizing myself with everything.
I got my FPGA to turn on an LED when the button is pressed (code below), but the button has to be held down for the LED to stay on. I would like the LED to turn and stay on when the button is pushed and released (and turned off when pressed again) but I'm confused on how this is done.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity ButtonLED is Port (BUTTON: in STD_LOGIC:='1';
LED : out STD_LOGIC:='0');
end ButtonLED;
architecture Behavioral of ButtonLED is
begin
LED <= not BUTTON;
end Behavioral;

(Warning: I am not giving you the answer. I am only addressing your question on "how it is done")
The statement LED <= not BUTTON defines that the LED is directly connected to the BUTTON through an inverting element. Therefore, your LED will always follow the opposite current state of the BUTTON. If BUTTON is in high logic level, then LED will be set to a low logic level, and vice versa.
I would like the LED to turn and stay on when the button is pushed and released (and turned off when pressed again) but I'm confused on how this is done.
For implementing this feature you must be capable of (1) detecting whenever the button is pressed and released, and (2) to "save" the current state of your LED. Detecting the button "movement" is simply detecting a change in state from high to low, and low to high. As mentioned in the comments it is also important to insert a de-bouncer mechanism between your edge detector and the button. Since FPGA buttons are very sensitive, the de-bouncer will ensure that you won't interpret glitches/noises as an actual press. I advise you to check it out yourself, implementation a version with and without the de-bouncer. Depending on how you press your button you may see the LED toggling multiple times when your hardware doesn't filter the input.
Once you know how to detect when the button was pressed and released, you have to simply add a memory element that toggles every time your condition is met. Now, instead of using LED <= not BUTTON, you'll have an internal variable that will control the LED behavior (i.e., LED <= led_state). This internal variable can be controlled through a process that is sensitive to your edge detection "module". Moreover, this variable will ensure your LED state only changes when your condition is met (i.e., pressing and releasing the button), instead of following the BUTTON inverse state.
Hope this gives you a general overview of your solution.

Related

counter using VHSIC Hardware Description Language

The counter must be programmed to a pushbutton to count every time that specific pushbutton is
pushed. Specifically, if the pushbutton is held, the counter must not increase. It must only increase
by one when the pushbutton is released and pushed in again.
There must be three separate counters implemented. Three different dipswitches must select the
specific counter and the FPGA must remember each count. A separate switch must be used to reset
the counter. Only one reset switch may be used for all three counters.
In order to count when the push button is pressed you can make a flag and an input variable to read the current state of the push button. if the variable of the current state is '1' you would go and check the flag if it is false you would increment the counter and change the flag to true. Otherwise if the flag is true this means that the button is still pressed so you will do nothing. if current state of the button returns to '0' this means that the button is not pressed so you change the flag to false.

Programmatically disable "Shake to find pointer" MacOS feature without hiding cursor

I am aware of this question, but that's different to what I am looking for, as the accepted answer hides the cursor.
I'm writing software that remaps the left mouse button to the left CTRL key.
I have it working, with one problem: when I hold down the CTRL key and drag the mouse pointer this feature often activates, which messes up my drag operation (it feels like trying to change direction running on ice).
So the Apple engineers must have disabled this feature while the left mouse button is down and re-enabled it when the button is released.
Something like this -- however when I inject my simulated mouse events, the feature does not disable.
What I'm getting at is: there must be some programmatic way of doing it, even if it is an internal Apple function. Unless Apple are using the solution in the linked answer, i.e. NSCursor.hide() and then manually drawing the cursor icon at the cursor location.
How can I accomplish this cleanly?
EDIT: A CGEventTap for NSEventTypeGesture or NSEventTypeMagnify fails to catch anything.

VHDL - connect switch and LED

I have the Xilinx Spartan6 and next VHDL code:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity Switches_Leds is
Port (switch_0: in STD_LOGIC;
LED_0 : out STD_LOGIC);
end Switches_Leds;
architecture Behavioral of Switches_Leds is
begin
LED_0 <= switch_0;
end;
Here is my User constraint file:
NET "switch_0" LOC = C3;
NET "LED_0" LOC = P4;
My question is: Why the led is always is turned on, but it goes off if I click on button ?
I explain: I program my fpga - the led is turned on, I press the switch button - the led is turned off, I unpress the button - led goes on.
The inversion can happen in two places:
In the button.
In the LED.
It all depends on how they are connected. I made a diagram:
(Had to draw it as we do not have a schematic editor here as on the EE site)
If you look at the diagram: buttons can be connected in two ways. They can either generate a high when pushed or a low.
LEDs can also be connected in two ways: they can light up when the output is high or they can light up when the output is low.
That gives you four combinations from which two give an LED lighting up when the button is pushed and two give an LED which goes off when the button is pushed.

wxHaskell Button State

I'm writing an application using wxHaskell and I want to be able to detect the state of a button (whether or not it is pressed at any given time). I'm having a bit of trouble figuring out how to do this, however. First I thought that there might be a "button is pressed" attribute that I could use, but there didn't seem to be. Then I had the idea of maintaining an IORef which I update on button-up and button-down events. However, that would require that the Button object actually have button-up and button-down events, which is does not appear to. It is an instance of Commanding, but I assume that the command event is fired on button-up only, which isn't enough for that idea. Does anyone have any other suggestions?
Workaround
You can implement this yourself by detecting the low-level actions that trigger those events (eg. mouse button down, space bar down).
In WX you can use the following function and constructor:
mouse :: Reactive w => Event w (EventMouse -> IO ())
data EventMouse = ... | MouseLeftDown !Point !Modifiers
And, as you suggest, you could keep the state yourself in an IORef. My suspicion is that left button here means main button (right for left-handed users).
UI design principles
The second question, which you haven't asked by I'll answer, is whether this is good UI design.
The behaviour of a button (assuming interaction using a mouse) is that click events are reported when the user releases the mouse button in the button area after pressing the mouse button down in the same area. If the user moves away and releases, or presses 'Escape', there is no click.
Taking any action on a button being pressed (not clicked) would feel unnatural for users.
In practice, the only acceptable way to use this would be, imho, to take an action whose effects can only be witnessed after releasing and which is immediately undone if the click is cancelled (ie. mouse button released outside button area).
EDIT: Please, also, take into account that users with accessibility requirements may have OS settings enabled that affect how and when button clicks are reported (but not down/up mouse events).
There is no way to know if a wxButton is pressed or not because it is an abstraction of a push button which intentionally hides this implementation detail. If you need to know the button state, use a wxToggleButton instead.

Setting Virtual Key/MouseButton State Without Triggering Events

Is it possible to set the virtual key state / mouse button state for all programs on a computer without triggering the associated events at the same time (like setting the left mouse button to be currently pressed without sending a mouse down event). Could this be achieved by setting up a fake keyboard or mouse driver for a fake keyboard or mouse that when queried as to the current state of a key would reply giving an answer of your choice or by any other means?
Edit:
This has to affect programs that I do not have the code for running in other threads ect...
Well, I don't have a complete answer for you but...
The Win32 function SetKeyboard State() changes the state of the keyboard for the thread that called it.
I know this does not affect the entire system, but if you only need the keyboard state changed for applications you are developing, then this could be called by each program, reading in the array passed to it from a temporary file on the harddrive.
Otherwise... don't know of anything else offhand...

Resources