Anda di halaman 1dari 12

-- Code update by Sadik Bircan on 25/02/2016

DEVICE = "mouse" -- "kb" for keyboard, "lhc" for left handed controller, such as
the G13

-----------------------------------------------------------------------------------
-------------------------

POLL_DELAY = 1

POLL_FAMILY = "kb" -- The device polling will take place on. Prefered to not be on
macroed device.

-----------------------------------------------------------------------------------
-------------------------

speed = POLL_DELAY -- this should alwasy be equal or larger than POLL_DELAY

inc = 1

lcd = OutputLCDMessage

function _onActivation(event, arg, family)

--

-- ADD ANY START UP ROUTINES HERE

--

ctDiscoColors = CoThread:new(discoColors)

ctDiscoColors:start()

Toggle = true

end

function _OnEvent(event, arg, family)

--

-- ADD EVENT FUNCTIONALITY HERE

--

if event == "G_PRESSED" and arg == 1 then


Toggle = not Toggle

if Toggle then

lcd("Fade resumed.", 3000)

else

lcd("Fade paused.", 3000)

end

end

if event == "G_PRESSED" and arg == 2 then

speed = speed-POLL_DELAY

if speed < POLL_DELAY then -- if speed is 0 or less, the program will not reliquish
control

speed = POLL_DELAY

end

lcd(speed .. "ms between updates")

end

if event == "G_PRESSED" and arg == 3 then

speed = speed+POLL_DELAY

lcd(speed .. "ms between updates")

end

if event == "G_PRESSED" and arg == 4 then

inc = inc + 1

lcd(inc.." color increment(s)")

end

if event == "G_PRESSED" and arg == 5 then

if inc > 1 then

inc = inc - 1

end

lcd(inc.." color increment(s)")

end
if Toggle then

ctDiscoColors:run()

end

end

function discoColors(ct)

while true do

for i=0,25,inc do

SetBacklightColor(0,0,255,DEVICE)

ct:sleep(25-i)

SetBacklightColor(255,0,255,DEVICE)

ct:sleep(i)

end

ct:sleep(6)

for i=0,25,inc do

SetBacklightColor(255,0,255,DEVICE)

ct:sleep(25-i)

SetBacklightColor(255,0,0,DEVICE)

ct:sleep(i)

end

ct:sleep(6)

for i=0,25,inc do

SetBacklightColor(255,0,0,DEVICE)

ct:sleep(25-i)

SetBacklightColor(255,255,0,DEVICE)

ct:sleep(i)

end
ct:sleep(6)

for i=0,25,inc do

SetBacklightColor(255,255,0,DEVICE)

ct:sleep(25-i)

SetBacklightColor(0,255,0,DEVICE)

ct:sleep(i)

end

ct:sleep(6)

for i=0,25,inc do

SetBacklightColor(0,255,0,DEVICE)

ct:sleep(25-i)

SetBacklightColor(0,255,255,DEVICE)

ct:sleep(i)

end

ct:sleep(6)

for i=0,25,inc do

SetBacklightColor(0,255,255,DEVICE)

ct:sleep(25-i)

SetBacklightColor(0,0,255,DEVICE)

ct:sleep(i)

end

ct:sleep(6)

end

end

-----------------------------------------------------------------------------------
---------------------

-----------------------------------------------------------------------------------
---------------------

--
-- NOTHING BELOW HERE NEEDS TO BE ALTERED

--

-----------------------------------------------------------------------------------
---------------------

function OnEvent(event, arg, family)

-- POLLING INITIATED HERE

_GetRunningTime = GetRunningTime()

if event == "PROFILE_ACTIVATED" then

ClearLog()

POLL = Poll:new(POLL_FAMILY, POLL_DELAY)

POLL:start()

if KB_EVENTS then

kb = EventHandler:new("kb")

kb:ProcessEvent(event, arg, family)

end

if LHC_EVENTS then

lhc = EventHandler:new("lhc")

lhc:ProcessEvent(event, arg, family)

end

_onActivation(event, arg, family)

else

-- POLLING ROUTINES BELOW

POLL:run(event,arg,family)

if KB_EVENTS then kb:ProcessEvent(event, arg, family) end

if LHC_EVENTS then lhc:ProcessEvent(event, arg, family) end

_GetRunningTime = GetRunningTime()

_OnEvent(event, arg, family) -- Runs myOnEvent to compartmentalize

end

if KB_EVENTS then kb:CleanupAfterEvent() end


if LHC_EVENTS then lhc:CleanupAfterEvent() end

end

log = OutputLogMessage

function Type(v)

if type(v) == "table" then

if v.Type ~= nil then

return v.Type

elseif v.type ~= nil then

return v.type

end

end

return type(v)

end

Poll = {}

Poll.__index = Poll

Poll.__newindex = function(table, key, value)

for k,v in pairs(table) do

if k == key then

rawset(table,key,value) return

end

end

error("'" .. key .. "' is not a member of Poll.", 2)

end

function Poll:new(family, delay)

local self = {}
self.delay = delay or 50

if family ~= "kb" and family ~= "lhc" then

error("Poll:new() - 'pFamily' must be 'kb' or 'lhc'", 2)

end

self.pFamily = family

self.presses = 0

self.running = false

self.runMKeyModifier = false

self.MKeyModifier = { nil, nil, nil } -- "default" or Modifiers. i.e. "lshift",


"ctrl"

self.modFamily = ""

self.Type = "Poll"

setmetatable(self, Poll)

return self

end

function Poll:setMKeyModifier( m1, m2, m3, family )

self.runMKeyModifier = true

self.MKeyModifier[1] = m1

self.MKeyModifier[2] = m2

self.MKeyModifier[3] = m3

if family ~= "kb" and family ~= "lhc" then error("Poll:setMKeyModifier() family -


needs to be 'kb' or 'lhc'",2) end

self.modFamily = family

end

function Poll:start()

self.running = true
self:setMKeyState()

end

function Poll:stop()

self.running = false

end

function Poll:_press()

if self.running then

self.presses = self.presses + 1

end

end

function Poll:release()

if self.running then

self.presses = self.presses - 1

end

end

function Poll:setMKeyState()

if self.runMKeyModifier then

local default = nil

local i = 1

local modMKeyState = nil

for i = 1, 3, 1 do

if self.MKeyModifier[i] == "default" then

default = i

elseif self.MKeyModifier[i] == "" then


-- do nothing

elseif string.find(self.MKeyModifier[i],"mb%d") then

local iMB = tonumber(string.sub(self.MKeyModifier[i],3))

if IsMouseButtonPressed(iMB) then

modMKeyState = i

end

elseif IsModifierPressed(self.MKeyModifier[i]) then

modMKeyState = i

end

end

if modMKeyState == nil then

modMKeyState = default

end

if modMKeyState ~= nil then

if self.pFamily == self.modFamily then

SetMKeyState(modMKeyState, self.pFamily)

return

elseif GetMKeyState(self.modFamily) ~= modMKeyState then

SetMKeyState(modMKeyState, self.modFamily)

end

end

end

SetMKeyState( GetMKeyState(self.pFamily), self.pFamily )

end

function Poll:run(event, arg, family)

if self.running and self.pFamily == family then

if event == "M_PRESSED" then

self:_press()

Sleep(self.delay)
elseif event == "M_RELEASED" then

if self.presses == 1 then

self:setMKeyState()

Sleep(self.delay)

end

self:release()

end

end

end

CoThread = {}

CoThread.__index = CoThread

function CoThread:new(f) -- f - function(MultiThread)

local self = {}

if type(f) ~= "function" then

error("A function is needed",2)

end

self.func = f

self.co = nil

self.sleepUntilTimeStamp = nil

self.Type = "CoThread"

setmetatable(self, CoThread)

return self

end

function CoThread:recreate()
self:kill()

self.co = coroutine.create(self.func)

end

function CoThread:create()

self.co = coroutine.create(self.func)

end

function CoThread:isDead()

if self.co == nil then

return true

elseif coroutine.status(self.co) == "dead" then

self.co = nil

return true

else

return false

end

end

function CoThread:run(...)

if self:isDead() then

return

end

if self.sleepUntilTimeStamp ~= nil then

if _GetRunningTime < self.sleepUntilTimeStamp then

return

else

self.sleepUntilTimeStamp = nil

end

end
coroutine.resume(self.co, self, ...)

end

function CoThread:kill()

self.co = nil

self.sleepUntilTimeStamp = nil

end

function CoThread:yield()

coroutine.yield()

end

function CoThread:sleep(Milliseconds)

self.sleepUntilTimeStamp = _GetRunningTime + Milliseconds

coroutine.yield()

end

function CoThread:start()

self:recreate()

end

function CoThread:stop()

self:kill()

end

function CoThread:isFinished()

return self:isDead()

end

Anda mungkin juga menyukai