I think that you could get a square wave by simply attaching a 'round' gate to a 'sine' gate.[/b]
You're wrong. This would generate 3 states for the wave:
if sin(x) <= -0.5 -> -1
if -0.5 < sin(x) <= 0.5 -> 0
if sin(x) > 0.5 -> 1
(assuming the round function is working like in real math
).
You would just quantize the continuous function down to 3 values. But this is not a square wave which (when ideal) should have only 2 values changing with some frequency. You can get a square wave from pulser.
Sin wave should be easy to code since it could just output the value of sin(accumulatedvalue) where accumulatedvalue can be calculated like it is done in accumulator gate.
Code:
GateActions["sinwave"] = {
Â*Â*Â*Â*group = "Time",
Â*Â*Â*Â*name = "Sinusoidal Wave",
Â*Â*Â*Â*inputs = { "Rate", "Hold", "Reset" },
Â*Â*Â*Â*timed = true,
Â*Â*Â*Â*output = function(gate, Rate, Hold, Reset)
Â*Â*Â*Â*Â*Â*Â*Â*gate.accumulator = gate.accumulator or {}
Â*Â*Â*Â*Â*Â*Â*Â*local accvalue = GateActions["accumulator"].output(gate.accumulator, Rate, Hold, Reset)
Â*Â*Â*Â*Â*Â*Â*Â*return math.sin(math.rad(accvalue*90))
Â*Â*Â*Â*end,
Â*Â*Â*Â*reset = function(gate)
Â*Â*Â*Â*Â*Â*Â*Â*gate.accumulator = {}
Â*Â*Â*Â*Â*Â*Â*Â*GateActions["accumulator"].reset(gate.accumulator)
Â*Â*Â*Â*end,
Â*Â*Â*Â*label = function(Out, Rate, Hold, Reset)
Â*Â*Â*Â*Â*Â*Â*Â*return "Rate:"..Rate.." Hold:"..Hold.." Reset:"..Reset.." = "..Out
Â*Â*Â*Â*end
}
I put accvalue*90 because I think that without this the change of the value would be too slow, but anyway this can be adjusted by the Rate input. It takes the value from Accumulator gate so the Rate input works like the A input in the Accumulator gate.
I didn't copy the whole code from the Accumulator on purpose, because I like when the code is reusable (there is smaller chance of having error somewhere, and if the error will be found in accumulator code and gets fixed this will automatically fix the sin wave for example).
----edit----
You cant, that won't be square (That will look like steps)[/b]
Hehe you were faster than me
.
----edit----
I'll explain why I created the gate.accumulator variable. This is used to simulate the calls that are done by wiremod to the gate's functions. It also stores the state for the simulated Accumulator gate inside my gate's gate variable (so it is persisted through all calls to the output function to the SinWave gate). This can (and should) be done for all gates that store their values through invocations and we want to use them inside our gates (this make the code more readable).
----edit----
Sawtooth
Code:
GateActions["sawtoothwave"] = {
Â*Â*Â*Â*group = "Time",
Â*Â*Â*Â*name = "Sawtooth Wave",
Â*Â*Â*Â*inputs = { "Rate", "Hold", "Reset" },
Â*Â*Â*Â*timed = true,
Â*Â*Â*Â*output = function(gate, Rate, Hold, Reset)
Â*Â*Â*Â*Â*Â*Â*Â*gate.accumulator = gate.accumulator or {}
Â*Â*Â*Â*Â*Â*Â*Â*gate.prevValue = gate.prevValue or 0
Â*Â*Â*Â*Â*Â*Â*Â*if gate.prevValue >= 1 then Reset = 1 end
Â*Â*Â*Â*Â*Â*Â*Â*local accvalue = GateActions["accumulator"].output(gate.accumulator, Rate, Hold, Reset)
Â*Â*Â*Â*Â*Â*Â*Â*gate.prevValue = accvalue
Â*Â*Â*Â*Â*Â*Â*Â*return accvalue
Â*Â*Â*Â*end,
Â*Â*Â*Â*reset = function(gate)
Â*Â*Â*Â*Â*Â*Â*Â*gate.accumulator = {}
Â*Â*Â*Â*Â*Â*Â*Â*GateActions["accumulator"].reset(gate.accumulator)
Â*Â*Â*Â*Â*Â*Â*Â*gate.prevValue = 0
Â*Â*Â*Â*end,
Â*Â*Â*Â*label = function(Out, Rate, Hold, Reset)
Â*Â*Â*Â*Â*Â*Â*Â*return "Rate:"..Rate.." Hold:"..Hold.." Reset:"..Reset.." = "..Out
Â*Â*Â*Â*end
}
Triangle
Code:
GateActions["trianglewave"] = {
Â*Â*Â*Â*group = "Time",
Â*Â*Â*Â*name = "Triangle Wave",
Â*Â*Â*Â*inputs = { "Rate", "Hold", "Reset" },
Â*Â*Â*Â*timed = true,
Â*Â*Â*Â*output = function(gate, Rate, Hold, Reset)
Â*Â*Â*Â*Â*Â*Â*Â*gate.accumulator = gate.accumulator or {}
Â*Â*Â*Â*Â*Â*Â*Â*gate.prevValue = gate.prevValue or 0
Â*Â*Â*Â*Â*Â*Â*Â*gate.RateDir = gate.RateDir or 1
Â*Â*Â*Â*Â*Â*Â*Â*if (gate.prevValue >= 1 and gate.RateDir == 1) then gate.RateDir = -1 end
Â*Â*Â*Â*Â*Â*Â*Â*if (gate.prevValue <= 0 and gate.RateDir == -1) then gate.RateDir = 1 end
Â*Â*Â*Â*Â*Â*Â*Â*if Reset == 1 then gate.RateDir = 1 end
Â*Â*Â*Â*Â*Â*Â*Â*local accvalue = GateActions["accumulator"].output(gate.accumulator, Rate * gate.RateDir, Hold, Reset)
Â*Â*Â*Â*Â*Â*Â*Â*gate.prevValue = accvalue
Â*Â*Â*Â*Â*Â*Â*Â*return accvalue
Â*Â*Â*Â*end,
Â*Â*Â*Â*reset = function(gate)
Â*Â*Â*Â*Â*Â*Â*Â*gate.accumulator = {}
Â*Â*Â*Â*Â*Â*Â*Â*GateActions["accumulator"].reset(gate.accumulator)
Â*Â*Â*Â*Â*Â*Â*Â*gate.prevValue = 0
Â*Â*Â*Â*Â*Â*Â*Â*gate.RateDir = 1
Â*Â*Â*Â*end,
Â*Â*Â*Â*label = function(Out, Rate, Hold, Reset)
Â*Â*Â*Â*Â*Â*Â*Â*return "Rate:"..Rate.." Hold:"..Hold.." Reset:"..Reset.." = "..Out
Â*Â*Â*Â*end
}
I hope I didn't do any mistake
Bookmarks