Every keyboard action can be caught with SetWindowsHookEx(), but not the sleep button. You can set it to "Do nothing" in the control panel, but I want to do something useful with it. Is this possible? (I'm using a Das Keyboard 4 Professional).
This old MSDN page lists the scan codes for the power action keys:
The following defines the correct scan codes for keyboard power switches for OnNow hardware:
Power event
Set1: Make = E0, 5E Break = E0, DE
Set2: Make = E0, 37 Break = E0, F0, 37
Sleep event
Set1: Make = E0, 5F Break = E0, DF
Set2: Make = E0, 3F Break = E0, F0, 3F
Wake event
Set1: Make = E0, 63 Break = E0, E3
Set2: Make = E0, 5E Break = E0, F0, 5E
The Set 2 break codes also need the 0xE0 prefix, otherwise the system will see the new key going down but see some different key coming up.
but if you look at the USB HID tables there is something called "Power Controls"
Power controls can step the system through the following states: Full Power, Low Power, and Power Down
Power control usages found in a
System Control
collection affect system
level power.
System Sleep
OSC – Asserted when the intended action is to initiate system-wide low
power mode now. If the system is already in the Low Power state, there is no
effect.
so it is possible that the keyboard is using the USB protocol directly to sleep.
Related
I am working through Hassan Ait-Kaci's book Warren's Abstract Machine - A Tutorial Reconstruction. Currently I am stuck on section 2.4, "Argument Registers".
To be precise, what I don't understand is how to get from these register assignments (p.22) (for query p(Z,h(Z,W),f(W))):
A1 = Z
A2 = h(A1,X4)
A3 = f(X4)
X4 = W
to these instructions (p.24):
put_variable X4,A1
put_structure h/2,A2
set_value X4
set_variable X5
put_structure f/1,A3
set_value X5
call p/3
Like, where does X5 come from all of a sudden? In the register assignments, X4 refers to variable W, and there's no X5. But in the instructions, X5 refers to (what is/was essentially) W, and X4 now refers to Z. I am not seeing an explanation in the book. What am I missing?
I tried to understand this but to no avail. Nothing in the errata. Here are some notes:
Instruction review
put_structure f/n,Xi: push a new STR (and adjoining functor) cell onto the heap and copy that cell into the allocated register address;
set_variable Xi: push a new REF cell onto the heap containing its own address, and copy it into the given register;
set_value Xi: push a new cell onto the heap and copy into the register's value.
In case of query:
put_variable Xn,Ai: the first occurrence of a variable in i-th argument position pushes a new unbound REF cell onto the heap and copies it into that variable's register as well as argument register Ai; and
put_value Xn,Ai (used for query): a later occurrence copies its value into argument register Ai.
In case of fact:
get_variable Xn,Ai: the first occurrence of a variable in i-th argument position sets it to the value of argument register Ai; and
get_value Xn,Ai: a later occurrence unifies with the value of Ai.
So, about that query
p(Z,h(Z,W),f(W)))
It seems to be coded differently on pages 17 and 19
Page 17 in the print edition Page 19 in the print edition
(given as is) (translated by me from the WAM code)
A1 = Z A1 = Z
A2 = h(A1,X4) A2 = h(X4,X5)
A3 = f(X4) A3 = f(X5)
X4 = W X4 = Z
X5 = W
Edit: It seems the code on the left allows variables appearing in non-root-positions to be in "argument registers" which is disallowed on the right, hence indirection.
The code
The Ai are the argument registers, the Xi are some value registers.
put_variable X4,A1 Z is a argument root freshvar created in X4
and also goes into A1
put_structure h/2,A2 h/2 functor goes into A2
(1)
set_value X4 New (empty) cell is created, goes into the
value of X4
(2)
set_variable X5 W is a non-root freshvar created in X5
(3)
put_structure f/1,A3 f/1 functor goes into A3
set_value X5 New (empty) cell is created, goes into the
value of X5
(4)
call p/3 Go!
At position (1), so far, so good. X4 and X5 seem to implicitly be the first and second arguments of the h/2 in A2 (is that right?)
X4 ----+---> [unbound REF] = Z (variable appearing at root)
|
A1 ----+
A2 --------> [h/2] = h(X4,X5)
At (2)
X4 ----+---> [] = Z (variable appearing at root)
|
A1 ----+
A2 --------> [h/2] = h(X4,X5)
At (3)
X4 ----+---> [] = Z (variable appearing at root)
|
A1 ----+
A2 --------> [h/2] = h(X4,X5)
X5 --------> [myself REF] (variable not appearing at root)
At (4)
X4 ----+---> [] = Z (variable appearing at root)
|
A1 ----+
A2 --------> [h/2] = h(X4,X5)
A3 --------> [f/1] = f(X5)
X5 --------> [] = W
I need to construct a pushdown automation for the following language: L = {a^n b^m | 2n>=m }
Can someone help me with it?
There are two approaches that come to mind:
try to write a PDA from scratch using the stack in a sensible way
write a CFG and then rely on the construction that shows PDAs accept the languages of all CFGs
To take approach 1, recognize that PDAs are like NFAs that can push symbols onto a stack and pop them off. If we want 2n >= m, that means we want the number of a's to be at least half the number of b's. That is, we want at least one a for every two b's. That means if we push all the a's on the stack, we need to read no more than two b's for every a on the stack. This suggests a PDA that works like this:
1. read all the a's, pushing into the stack
2. read b's, popping an a for every two b's you see
3. if you run out of stack but still have b's to process, reject
4. if you run out of input at the same time or sooner than you run out of stack, accept
In terms of states and transitions:
Q S E Q' S'
q0 Z a q0 aZ // these transitions read all the a's and
q0 a a q0 aa // push them onto the stack
q0 a b q1 a // these transitions read all the b's and
q1 a b q2 - // pop a's off the stack for every complete
q2 a b q1 a // pair of b's we see. if we run out of a's
// it will crash and reject
q0 Z - q3 Z // these transitions just let us guess at any
q0 a - q3 a // point that we have read all the input and
q1 Z - q3 Z // go to the accepting state. note that if
q1 a - q3 a // we guessed wrong the PDA will crash there
q2 Z - q3 Z // since we have no transitions defined.
q2 a - q3 a // crashing rejects the input.
Here, the accept condition is being in state q3 with no more stack.
To do the second option, you'd write a CFG like this:
S -> aSbb | aS | e
Then your PDA could do the following:
push S onto the stack
pop from the stack and push onto the stack one of the productions for S
if you pop a terminal off the stack, consume the nonterminal from input
if you pop a nonterminal off the stack, replace it with some production for that nonterminal
This nondeterministically generates every possible derivation according to the CFG. If the input is a string in the language, then one of these derivations will consume all the input and leave the stack empty, which is the halt/accept condition.
We need to construct a pushdown automation for the following language: L = {a^n b^m | 2n>=m }
The given condition is 2n >= m, that means we want the number of a's to be at least half the number of b's. That is, we want at least one a for every two b's. That means if we push all the a's on the stack, we need to read no more than two b's for every a on the stack. This suggests a PDA that works like this:
read all the a's, pushing into the stack
read b's, popping an a for every two b's you see
if you run out of stack but still have b's to process, reject
if you run out of input at the same time or sooner than you run out of stack, accept.
The PDA for the problem is as follows −
Transition functions
The transition functions are as follows −
Step 1: δ(q0, a, Z) = (q0, aZ)
Step 2: δ(q0, a, a) = (q0, aa)
Step 3: δ(q0, b, a) = (q1, a)
Step 4: δ(q1, b, a) = (q2, ε)
Step 5: δ(q2, b, a) = (q1, a)
Step 6: δ(q2,b, Z) = dead state.
Step 7: δ(q2,ε, Z) = (qf, Z)
When i execute this code(shown below) , it always sets implicit kind of constraint.
As you can see below , it always says that D1 = D2 but there is no such explicit constraints nor any pattern matching which forces this.
Or in otherwords there is some reference between D1 and D2 such that whenever D1 gets initialized, D2 gets automatically initialized. I can't see how this is happening. Can someone explain me, i tried ot figure it out with debugger but it did not help.
It's a puzzle "GERALD + DONALD = ROBERT", initially three lists contains these variables.
I add code below if anyone wants to test it:
sum(N1,N2,N):-
sum1(N1,N2,N,0,0,[0,1,2,3,4,5,6,7,8,9],_).
sum1([],[],[],0,0,Digits,Digits).
sum1([D1|N1],[D2|N2],[D|N],C1,C,Digs1,Digs2):-
sum1(N1,N2,N,C1,C2,Digs1,Digs2),
digitSum(D1,D2,C2,D,C,Digs2,Digs).
digitSum(D1,D2,C1,D,C,Digs1,Digs):-
del(D1,Digs1,Digs2),
del(D2,Digs2,Digs3),
del(D,Digs3,Digs),
S is D1 + D2 + C1,
D is S mod 10,
C is D div 10.
del(A,L,L):-
nonvar(A),!.
del(A,[A|L],L).
del(A,[B|L],[B|L1]):-
del(A,L,L1).
Query:
?- sum( [D,O,N,A,L,D], [G,E,R,A,L,D], [R,O,B,E,R,T] ).
When it says D1 = D2 then you should see that both D1 and D2 have same variable from the list and List is a functor and one variable is visible in whole functor.
You should see that when the recursion ends, then you have have D's(last element) from GERALD and DONALD , since this D is visible in whole functor, D1 and D2 both refer to same variable.
For accordion music sheet notation it is common to specify
the pitches in parenthesis, e.g. (E in parenthesis in the snippet below). This is my target:
The pitches in parenthesis (1) are not played and should not take up additional logical time in a measure (they are used for player's convenience when reading the music sheet), (2) they should stay near the chord.
My LilyPond code now is as follows:
\version "2.18.2"
\include "deutsch.ly"
#(set-global-staff-size 20.0)
\relative c {
\clef bass
e8 <gis d' e>^7 h, q
\override Stem.details.beamed-lengths = #'(0)
\grace \parenthesize e
\revert Stem.details
e <gis d' e> gis, q
\override Stem.details.beamed-lengths = #'(0)
\grace \parenthesize e'
\revert Stem.details
}
Using lilypond --pdf sample.ly I get the following as a result:
The result I obtain in LilyPond has several problems: (i) stem length has no effect on grace notes, (ii) grace note is moved to the next bar and ideally it should stay near the chord, (iii) the parenthesis are too small (although it is a minor remark).
How can I achieve the desired effect (i.e. as in the first picture) using LilyPond?
P.S. edit:
I was able to create a workaround (see below), which is not elegant, but does the job:
\version "2.18.2"
\include "deutsch.ly"
#(set-global-staff-size 20.0)
\relative c {
\clef bass
e8 <gis d' e>^7 h, \afterGrace q
{
\override Stem.thickness = #-1.0
\parenthesize e4
\revert Stem.thickness
}
e8 <gis d' e> gis,_B \afterGrace q
{
\override Stem.thickness = #-1.0
\parenthesize e'4
\revert Stem.thickness
}
}
The output of the above snippet is as follows:
Is there a better way to achieve it?
Instead of using \override Stem.thickness = #-1.0, a more elegant solution would be to use the stencil property. This property can be used to omit stems, flags, note heads, etc. Also, you can use the statement \once \override <something> if you want to override just a single note/chord and automatically revert back after it. Finally, you can also define a musical function to take care of all of this automatically, particularly if you are using this construction often. In this example below, the arguments are the note/chord immediately before the grace note and the grace note itself, see:
\version "2.18.2"
\include "deutsch.ly"
#(set-global-staff-size 20.0)
accordionGrace = #(define-music-function
(parser location firstNote secondNote)
(ly:music? ly:music?)
#{
\afterGrace
$firstNote
{
\once \override Stem.stencil = ##f
\once \override Flag.stencil = ##f
\parenthesize $secondNote
}
#}
)
\relative c {
\clef bass
e8 <gis d' e>^7 h,
\accordionGrace q e8
e8 <gis d' e> gis,_B
\accordionGrace q e'8
}
Most Haskell FRP frameworks like AFRP, Yampa and Reactive-banana make a difference between continuous time-varying functions and discrete ones. Usually they call them behaviors and events.
One exception is Netwire, which uses an inhibition monoid to model events. What are pros and cons of such an approach?
In particular, I'm interested in application of FRP to robot controlling. For example, this paper http://haskell.cs.yale.edu/?post_type=publication&p=182 show a way to encode a task and HSM abstractions in FRP using events. Can this be directly translated to Netwire?
The advantage of events as potentially inhibited signals is that it allows you to encode most even complicated reactive formulas very concisely. Imagine a switch that displays "yes" when pressed and "no" otherwise:
"yes" . switchPressed <|> "no"
The idea is that switchPressed acts like the identity wire if its corresponding event occurs and inhibits otherwise. That's where <|> comes in. If the first wire inhibits, it tries the second. Here is a hypothetical robot arm controlled by two buttons (left and right):
robotArm = integral_ 0 . direction
direction =
((-1) . leftPressed <|> 0) +
(1 . rightPressed <|> 0)
While the robot arm is hypothetical, this code is not. It's really the way you would write this in Netwire.
After some trials I've implemented the behavior I needed. Basically, You write a custom inhibitor type which catches the concept of events you need. In my case it was
data Inhibitor = Done | Timeout | Interrupt deriving Show
Done means normal finishing and the rest constructors signal some kind of an error.
After it, you write any custom combinators you need. In my case I needed a way to stop computations and signal a error further:
timeout deadline w | deadline <= 0 = inhibit Timeout
| otherwise = mkGen $ \dt a -> do
res <- stepWire w dt a
case res of
(Right o, w') -> return (Right o, timeout (deadline - dt) w')
(Left e, _) -> return (Left e, inhibit e)
This is a variant of switchBy which allows you to change the wire once. Note, it passes the inhibition signal of a new wire:
switchOn new w0 =
mkGen $ \dt x' ->
let select w' = do
(mx, w) <- stepWire w' dt x'
case mx of
Left ex -> stepWire (new ex) dt x'
Right x -> return (Right x, switchOn new w)
in select w0
And this is a variant of (-->) which catches the idea of interrupting the task chain.
infixr 1 ~>
w1 ~> w2 = switchOn ( \e -> case e of
Done -> w2
_ -> inhibit e
) w1