View difference between Paste ID: Ud2uTjLa and dNVfNyLY
SHOW: | | - or go back to the newest paste.
1
-- Code edited by TheMartinWheeler (TheShinyTuxedo) on 6/1/2012
2
-- Code update by Bystander on  8/28/2011
3
------------------------------------------------------------------------------------------------------------
4
POLL_DELAY = 25     -- Sleep time after each poll (beware of number below 25)
5
POLL_FAMILY = "kb"  -- The device polling will take place on.  Prefered to not be on macroed device.
6
KB_EVENTS = true    -- If you don't have a logitech keyboard, make this false/nil or comment out
7
-- LHC_EVENTS = true   -- if you don't have a logitech left handed controller, make this false/nil or comment out
8
------------------------------------------------------------------------------------------------------------
9
10
DEVICE = "kb"
11
color = 1
12
speed = 25  -- this should alwasy be equal or larger than POLL_DELAY
13
disco = 0
14
BLUE = 255
15
DIRECTION = 1
16
17
function _onActivation(event, arg, family)
18
--
19
-- ADD ANY START UP ROUTINES HERE
20
--
21
--    POLL:setMKeyModifier( "default", "mb5", "mb4", "lhc" )  -- Allows the use of Modifiers to change M pages
22
23
	ctDiscoColors = CoThread:new(discoColors)
24
end
25
26
function _OnEvent(event, arg, family)
27
--
28
-- ADD EVENT FUNCTIONALITY HERE
29
-- 
30
31
	if (event == "G_PRESSED" and arg == 1 and GetMKeyState(DEVICE) == 1) then
32
		if color == 1 then
33
			color = 2			
34
			ctDiscoColors:stop()	-- stops randomColors if it was running()
35
			SetBacklightColor(255, 0, 0, DEVICE) -- Red
36
			OutputLogMessage("RED\n")
37
		elseif color == 2 then
38
			color = 3
39
			SetBacklightColor(0, 0, 255, DEVICE) -- Blue
40
			OutputLogMessage("BLUE\n")
41
		elseif color == 3 then
42
			color = 4
43
			SetBacklightColor(255, 0, 255, DEVICE) -- Purple
44
			OutputLogMessage("PURPLE\n")
45
		elseif color == 4 then
46
			color = 1
47
			ctDiscoColors:start()	-- starts discoColors
48
			OutputLogMessage("DISCO\n")
49
		end
50
	end
51
	
52
	if (event == "G_PRESSED" and arg == 3 and disco == 1) then
53
		speed = speed-25
54
		if speed < POLL_DELAY 	then	-- if speed is 0 or less, the program will not reliquish control
55
			speed = POLL_DELAY
56
		end
57
	end
58
59
	if (event == "G_PRESSED" and arg == 6 and disco == 1) then
60
		speed = speed+25
61
	end
62
63
	ctDiscoColors:run() -- this will run discoColors as long as it hasn't finished or been stopped with :stop()
64
end
65
66
function discoColors(ct)
67
	while true do
68
		disco = 1
69
70
		if BLUE == 255 and DIRECTION == 0 then 
71
			DIRECTION = 1
72
		elseif BLUE == 1 and DIRECTION == 1 then
73-
			DIRECTION == 0
73+
			DIRECTION = 0
74
		end
75
		
76
77
		if DIRECTION == 1
78
			SetBacklightColor(0, 0, BLUE, DEVICE) -- BLUE
79
			BLUE = BLUE - 1
80
			ct:sleep(speed)
81
		elseif DIRECTION == 0
82
			SetBacklightColor(0, 0, BLUE, DEVICE) -- BLUE
83
			BLUE = BLUE + 1
84
			ct:sleep(speed)
85
		end
86
87
		if color ~= 1 then	  -- if color == 1 it will continue to loop as before
88
			disco = 0
89
			break
90
		end
91
	end
92
end
93
94
--------------------------------------------------------------------------------------------------------
95
--------------------------------------------------------------------------------------------------------
96
--
97
--                             NOTHING BELOW HERE NEEDS TO BE ALTERED
98
--
99
--------------------------------------------------------------------------------------------------------
100
--------------------------------------------------------------------------------------------------------
101
-- function Type(v)  -- returns what type of object v is.  It works with my objects as well
102
-- function log(...) -- MessageLogOutput(...)
103
--------------------------------------------------------------------------------------------------------
104
--------------------------------------------------------------------------------------------------------
105
-- function CoThread:new(f)      f - function(coThread,...)   coThread is the controlling CoThread
106
-- function CoThread:recreate()  kill existing coroutine and recreate
107
-- function CoThread:create()    creates coroutine
108
-- function CoThread:isDead()    true if coroutine is dead or nil/false
109
-- function CoThread:run(...)    run coroutine with variables don't include mt
110
-- function CoThread:kill()      kill coroutine
111
-- function CoThread:yield()     use in function(coThread,...) to yield
112
-- function CoThread:sleep(Milliseconds) use in function(coThread,...) to sleep for Milliseconds
113
-- function CoThread:start()     does the same thing as recreate()
114
-- function CoThread:stop()      does the same thing as kill()
115
-- function CoThread:isFinished() same as isDead()
116
--------------------------------------------------------------------------------------------------------
117
--------------------------------------------------------------------------------------------------------
118
-- function Poll:new(family, delay)         delay is sleep time between polls
119
-- function Poll:run(event, arg, family)    handles events and performs the polling as long as it's running
120
-- function Poll:setMKeyModifier( m1, m2, m3, family )  Allows the use of Modifiers to change M pages
121
-- function Poll:start()                    turn on polling
122
-- function Poll:stop()                     stop polling
123
--      function Poll:_press()                  NOT TO BE USED
124
--      function Poll:release()                 NOT TO BE USED
125
--      function Poll:setMKeyState(MKeyState)   NOT TO BE USED
126
------------------------------------------------------------------------------------------------------------
127
128
function OnEvent(event, arg, family)
129
-- POLLING INITIATED HERE
130
    _GetRunningTime = GetRunningTime()
131
    if event == "PROFILE_ACTIVATED" then
132
        ClearLog()
133
        POLL = Poll:new(POLL_FAMILY, POLL_DELAY)
134
        POLL:start()
135
        _onActivation(event, arg, family)
136
    else
137
-- POLLING ROUTINES BELOW
138
        POLL:run(event,arg,family)
139
	    _GetRunningTime = GetRunningTime()
140
        _OnEvent(event, arg, family)  -- Runs myOnEvent to compartmentalize
141
    end
142
end
143
144
function log(...)
145
    if ... == nil then
146
        error("log(...) cannot display nil\n",2)
147
    end
148
    OutputLogMessage(...)
149
end
150
151
function Type(v)
152
    if type(v) == "table" then
153
        if v.Type ~= nil then
154
            return v.Type
155
        elseif v.type ~= nil then
156
            return v.type
157
        end
158
    end
159
    return type(v)
160
end
161
162
------------------------------------------------------------------------------------------------------------
163
-- function Poll:new(family, delay)         delay is sleep time between polls
164
-- function Poll:run(event, arg, family)    handles events and performs the polling as long as it's running
165
-- function Poll:setMKeyModifier( m1, m2, m3, family )  Allows the use of Modifiers to change M pages
166
-- function Poll:start()                    turn on polling
167
-- function Poll:stop()                     stop polling
168
--      function Poll:_press()                   NOT TO BE USED
169
--      function Poll:release()                 NOT TO BE USED
170
--      function Poll:setMKeyState(MKeyState)   NOT TO BE USED
171
------------------------------------------------------------------------------------------------------------
172
173
Poll = {}
174
Poll.__index = Poll
175
Poll.__newindex = function(table, key, value)
176
    for k,v in pairs(table) do
177
        if k == key then
178
            rawset(table,key,value) return
179
        end
180
    end
181
    error("'" .. key .. "' is not a member of Poll.", 2)
182
end
183
184
function Poll:new(family, delay)
185
    local self = {}
186
187
    self.delay = delay or 50
188
    if family ~= "kb" and family ~= "lhc" then
189
        error("Poll:new() - 'pFamily' must be 'kb' or 'lhc'", 2)
190
    end
191
    self.pFamily = family
192
    self.presses = 0
193
    self.running = false
194
    self.runMKeyModifier = false
195
    self.MKeyModifier = { nil, nil, nil }   -- "default" or Modifiers. i.e. "lshift", "ctrl"
196
    self.modFamily = ""
197
    self.Type = "Poll"
198
199
    setmetatable(self, Poll)
200
201
    return self
202
end
203
204
--------------------------------------------------------------------------------------------
205
--  Poll:setMKeyModifier( m1, m2, m3, family )
206
--  Sets what modifiers will change you to the associated M page of the device on family
207
--  "shift", "ctrl", "alt", "lshift", "lctrl", "lalt"... are all modifiers
208
--  "default" is the page that will be set when none of the listed modifiers are pressed
209
--  "" will cause a page to be ignored.
210
--  I.E. - Poll:setMKeyModifier( "default", "shift", "ctrl", "lhc" )
211
--  will set the default page to m1, shift will change you to m2, ctrl to m3 on a left hand controler
212
--------------------------------------------------------------------------------------------
213
function Poll:setMKeyModifier( m1, m2, m3, family )
214
    self.runMKeyModifier = true
215
    self.MKeyModifier[1] = m1
216
    self.MKeyModifier[2] = m2
217
    self.MKeyModifier[3] = m3
218
    if family ~= "kb" and family ~= "lhc" then  error("Poll:setMKeyModifier() family - needs to be 'kb' or 'lhc'",2) end
219
    self.modFamily = family
220
end
221
222
function Poll:start()
223
    self.running = true
224
    self:setMKeyState()
225
end
226
227
function Poll:stop()
228
    self.running = false
229
end
230
231
function Poll:_press()
232
    if self.running then
233
        self.presses = self.presses + 1
234
    end
235
end
236
237
function Poll:release()
238
    if self.running then
239
        self.presses = self.presses - 1
240
    end
241
end
242
243
function Poll:setMKeyState()
244
    if self.runMKeyModifier then
245
        local default = nil
246
        local i = 1
247
        local modMKeyState = nil
248
249
        for i = 1, 3, 1 do
250
            if self.MKeyModifier[i] == "default" then
251
                default = i
252
            elseif self.MKeyModifier[i] == "" then
253
                -- do nothing
254
            elseif string.find(self.MKeyModifier[i],"mb%d") then
255
                local iMB = tonumber(string.sub(self.MKeyModifier[i],3))
256
                if IsMouseButtonPressed(iMB) then
257
                    modMKeyState = i
258
                end
259
            elseif IsModifierPressed(self.MKeyModifier[i]) then
260
                modMKeyState = i
261
            end
262
        end
263
        if modMKeyState == nil then
264
            modMKeyState = default
265
        end
266
        if modMKeyState ~= nil then
267
            if self.pFamily == self.modFamily then
268
                SetMKeyState(modMKeyState, self.pFamily)
269
                return
270
            elseif GetMKeyState(self.modFamily) ~= modMKeyState then
271
                SetMKeyState(modMKeyState, self.modFamily)
272
            end
273
        end
274
    end
275
    SetMKeyState( GetMKeyState(self.pFamily), self.pFamily )
276
end
277
278
function Poll:run(event, arg, family)
279
    if self.running and self.pFamily == family then
280
        if event == "M_PRESSED" then
281
            self:_press()
282
        elseif event == "M_RELEASED" then
283
            if self.presses == 1 then
284
                self:setMKeyState()
285
                Sleep(self.delay)
286
            end
287
            self:release()
288
        end
289
    end
290
end
291
292
--------------------------------------------------------------------------------------------------------
293
-- function CoThread:new(f)      f - function(coThread,...)   coThread is the controlling CoThread
294
-- function CoThread:recreate()  kill existing coroutine and recreate
295
-- function CoThread:create()    creates coroutine
296
-- function CoThread:isDead()    true if coroutine is dead or nil/false
297
-- function CoThread:run(...)    run coroutine with variables don't include mt
298
-- function CoThread:kill()      kill coroutine
299
-- function CoThread:yield()     use in function(coThread,...) to yield
300
-- function CoThread:sleep(Milliseconds) use in function(coThread,...) to sleep for Milliseconds
301
-- function CoThread:start()     does the same thing as recreate()
302
-- function CoThread:stop()      does the same thing as kill()
303
-- function CoThread:isFinished() same as isDead()
304
--------------------------------------------------------------------------------------------------------
305
306
CoThread = {}
307
CoThread.__index = CoThread
308
309
function CoThread:new(f) -- f - function(MultiThread)
310
	local self = {}
311
312
	if type(f) ~= "function" then
313
		error("A function is needed",2)
314
	end
315
    self.func = f
316
    self.co = nil
317
    self.sleepUntilTimeStamp = nil
318
    self.Type = "CoThread"
319
320
	setmetatable(self, CoThread)
321
	return self
322
end
323
324
function CoThread:recreate()
325
    self:kill()
326
    self.co = coroutine.create(self.func)
327
end
328
329
function CoThread:create()
330
    self.co = coroutine.create(self.func)
331
end
332
333
function CoThread:isDead()
334
    if self.co == nil then
335
        return true
336
	elseif coroutine.status(self.co) == "dead" then
337
        self.co = nil
338
		return true
339
	else
340
        return false
341
    end
342
end
343
344
function CoThread:run(...)
345
	if self:isDead() then
346
        return
347
    end
348
    if self.sleepUntilTimeStamp ~= nil then
349
        if _GetRunningTime < self.sleepUntilTimeStamp then
350
            return
351
        else
352
            self.sleepUntilTimeStamp = nil
353
        end
354
    end
355
    coroutine.resume(self.co, self, ...)
356
end
357
358
function CoThread:kill()
359
    self.co = nil       -- lua automatically deallocated
360
    self.sleepUntilTimeStamp = nil
361
end
362
363
function CoThread:yield()
364
    coroutine.yield()
365
end
366
367
function CoThread:sleep(Milliseconds)
368
    self.sleepUntilTimeStamp = _GetRunningTime + Milliseconds
369
370
    coroutine.yield()
371
end
372
373
function CoThread:start()     
374
    self:recreate()
375
end
376
377
function CoThread:stop()
378
    self:kill()
379
end
380
381
function CoThread:isFinished()
382
    return self:isDead()
383
end