dev2919

Untitled

Feb 9th, 2017
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.01 KB | None | 0 0
  1.  
  2. DEVICE = "mouse" -- "kb" for keyboard, "lhc" for left handed controller, such as the G13
  3.  
  4. ------------------------------------------------------------------------------------------------------------
  5.  
  6. POLL_DELAY = 3
  7.  
  8. POLL_FAMILY = "kb" -- The device polling will take place on. Prefered to not be on macroed device.
  9.  
  10. ------------------------------------------------------------------------------------------------------------
  11.  
  12.  
  13.  
  14. speed = POLL_DELAY -- this should alwasy be equal or larger than POLL_DELAY
  15.  
  16. inc = 0.1
  17.  
  18.  
  19. lcd = OutputLCDMessage
  20.  
  21.  
  22.  
  23. function _onActivation(event, arg, family)
  24.  
  25. --
  26.  
  27. -- ADD ANY START UP ROUTINES HERE
  28.  
  29. --
  30.  
  31.  
  32.  
  33. ctDiscoColors = CoThread:new(discoColors)
  34.  
  35. ctDiscoColors:start()
  36.  
  37. Toggle = true
  38.  
  39. end
  40.  
  41.  
  42.  
  43. function _OnEvent(event, arg, family)
  44.  
  45. --
  46.  
  47. -- ADD EVENT FUNCTIONALITY HERE
  48.  
  49. --
  50.  
  51.  
  52.  
  53. if event == "G_PRESSED" and arg == 1 then
  54.  
  55. Toggle = not Toggle
  56.  
  57. if Toggle then
  58.  
  59. lcd("Fade resumed.", 3000)
  60.  
  61. else
  62.  
  63. lcd("Fade paused.", 3000)
  64.  
  65. end
  66.  
  67. end
  68.  
  69. if event == "G_PRESSED" and arg == 2 then
  70.  
  71. speed = speed-POLL_DELAY
  72.  
  73. if speed < POLL_DELAY then -- if speed is 0 or less, the program will not reliquish control
  74.  
  75. speed = POLL_DELAY
  76.  
  77. end
  78.  
  79. lcd(speed .. "ms between updates")
  80.  
  81. end
  82.  
  83. if event == "G_PRESSED" and arg == 3 then
  84.  
  85. speed = speed+POLL_DELAY
  86.  
  87. lcd(speed .. "ms between updates")
  88.  
  89. end
  90.  
  91.  
  92.  
  93. if event == "G_PRESSED" and arg == 4 then
  94.  
  95. inc = inc + 1
  96.  
  97. lcd(inc.." color increment(s)")
  98.  
  99. end
  100.  
  101. if event == "G_PRESSED" and arg == 5 then
  102.  
  103. if inc > 1 then
  104.  
  105. inc = inc - 1
  106.  
  107. end
  108.  
  109. lcd(inc.." color increment(s)")
  110.  
  111. end
  112.  
  113.  
  114.  
  115. if Toggle then
  116.  
  117. ctDiscoColors:run()
  118.  
  119. end
  120.  
  121. end
  122.  
  123.  
  124.  
  125. function discoColors(ct)
  126.  
  127. while true do
  128.  
  129.  
  130.  
  131. for i=0,25,inc do
  132.  
  133. SetBacklightColor(0,255,0,DEVICE)
  134.  
  135. ct:sleep(25-i)
  136.  
  137. SetBacklightColor(0,0,0,DEVICE)
  138.  
  139. ct:sleep(i)
  140.  
  141.  
  142.  
  143. ct:sleep(6)
  144.  
  145.  
  146.  
  147. SetBacklightColor(255,0,0,DEVICE)
  148.  
  149. ct:sleep(25-i)
  150.  
  151. SetBacklightColor(0,0,0,DEVICE)
  152.  
  153. ct:sleep(i)
  154.  
  155.  
  156.  
  157. ct:sleep(6)
  158.  
  159.  
  160. SetBacklightColor(0,0,0,DEVICE)
  161.  
  162. ct:sleep(25-i)
  163.  
  164. SetBacklightColor(0,0,255,DEVICE)
  165. ct:sleep(i)
  166.  
  167.  
  168.  
  169. ct:sleep(6)
  170.  
  171.  
  172.  
  173. SetBacklightColor(0,0,0,DEVICE)
  174.  
  175. ct:sleep(25-i)
  176. SetBacklightColor(0,0,0,DEVICE)
  177.  
  178. ct:sleep(i)
  179.  
  180.  
  181.  
  182. ct:sleep(6)
  183.  
  184.  
  185. SetBacklightColor(0,0,0,DEVICE)
  186.  
  187. ct:sleep(25-i)
  188. SetBacklightColor(0,0,0,DEVICE)
  189.  
  190. ct:sleep(i)
  191.  
  192.  
  193.  
  194. ct:sleep(6)
  195.  
  196.  
  197. SetBacklightColor(0,0,0,DEVICE)
  198. ct:sleep(25-i)
  199. SetBacklightColor(0,255,0,DEVICE)
  200. ct:sleep(i)
  201.  
  202. end
  203.  
  204. ct:sleep(6)
  205.  
  206.  
  207.  
  208. end
  209.  
  210. end
  211.  
  212.  
  213.  
  214. --------------------------------------------------------------------------------------------------------
  215.  
  216. --------------------------------------------------------------------------------------------------------
  217.  
  218. --
  219.  
  220. -- NOTHING BELOW HERE NEEDS TO BE ALTERED
  221.  
  222. --
  223.  
  224. --------------------------------------------------------------------------------------------------------
  225.  
  226.  
  227.  
  228. function OnEvent(event, arg, family)
  229.  
  230. -- POLLING INITIATED HERE
  231.  
  232. _GetRunningTime = GetRunningTime()
  233.  
  234. if event == "PROFILE_ACTIVATED" then
  235.  
  236. ClearLog()
  237.  
  238. POLL = Poll:new(POLL_FAMILY, POLL_DELAY)
  239.  
  240. POLL:start()
  241.  
  242. if KB_EVENTS then
  243.  
  244. kb = EventHandler:new("kb")
  245.  
  246. kb:ProcessEvent(event, arg, family)
  247.  
  248. end
  249.  
  250. if LHC_EVENTS then
  251.  
  252. lhc = EventHandler:new("lhc")
  253.  
  254. lhc:ProcessEvent(event, arg, family)
  255.  
  256. end
  257.  
  258. _onActivation(event, arg, family)
  259.  
  260. else
  261.  
  262. -- POLLING ROUTINES BELOW
  263.  
  264. POLL:run(event,arg,family)
  265.  
  266. if KB_EVENTS then kb:ProcessEvent(event, arg, family) end
  267.  
  268. if LHC_EVENTS then lhc:ProcessEvent(event, arg, family) end
  269.  
  270. _GetRunningTime = GetRunningTime()
  271.  
  272. _OnEvent(event, arg, family) -- Runs myOnEvent to compartmentalize
  273.  
  274. end
  275.  
  276. if KB_EVENTS then kb:CleanupAfterEvent() end
  277.  
  278. if LHC_EVENTS then lhc:CleanupAfterEvent() end
  279.  
  280. end
  281.  
  282.  
  283.  
  284. log = OutputLogMessage
  285.  
  286.  
  287.  
  288. function Type(v)
  289.  
  290. if type(v) == "table" then
  291.  
  292. if v.Type ~= nil then
  293.  
  294. return v.Type
  295.  
  296. elseif v.type ~= nil then
  297.  
  298. return v.type
  299.  
  300. end
  301.  
  302. end
  303.  
  304. return type(v)
  305.  
  306. end
  307.  
  308.  
  309.  
  310. Poll = {}
  311.  
  312. Poll.__index = Poll
  313.  
  314. Poll.__newindex = function(table, key, value)
  315.  
  316. for k,v in pairs(table) do
  317.  
  318. if k == key then
  319.  
  320. rawset(table,key,value) return
  321.  
  322. end
  323.  
  324. end
  325.  
  326. error("'" .. key .. "' is not a member of Poll.", 2)
  327.  
  328. end
  329.  
  330.  
  331.  
  332. function Poll:new(family, delay)
  333.  
  334. local self = {}
  335.  
  336.  
  337.  
  338. self.delay = delay or 50
  339.  
  340. if family ~= "kb" and family ~= "lhc" then
  341.  
  342. error("Poll:new() - 'pFamily' must be 'kb' or 'lhc'", 2)
  343.  
  344. end
  345.  
  346. self.pFamily = family
  347.  
  348. self.presses = 0
  349.  
  350. self.running = false
  351.  
  352. self.runMKeyModifier = false
  353.  
  354. self.MKeyModifier = { nil, nil, nil } -- "default" or Modifiers. i.e. "lshift", "ctrl"
  355.  
  356. self.modFamily = ""
  357.  
  358. self.Type = "Poll"
  359.  
  360.  
  361.  
  362. setmetatable(self, Poll)
  363.  
  364.  
  365.  
  366. return self
  367.  
  368. end
  369.  
  370.  
  371.  
  372. function Poll:setMKeyModifier( m1, m2, m3, family )
  373.  
  374. self.runMKeyModifier = true
  375.  
  376. self.MKeyModifier[1] = m1
  377.  
  378. self.MKeyModifier[2] = m2
  379.  
  380. self.MKeyModifier[3] = m3
  381.  
  382. if family ~= "kb" and family ~= "lhc" then error("Poll:setMKeyModifier() family - needs to be 'kb' or 'lhc'",2) end
  383.  
  384. self.modFamily = family
  385.  
  386. end
  387.  
  388.  
  389.  
  390. function Poll:start()
  391.  
  392. self.running = true
  393.  
  394. self:setMKeyState()
  395.  
  396. end
  397.  
  398.  
  399.  
  400. function Poll:stop()
  401.  
  402. self.running = false
  403.  
  404. end
  405.  
  406.  
  407.  
  408. function Poll:_press()
  409.  
  410. if self.running then
  411.  
  412. self.presses = self.presses + 1
  413.  
  414. end
  415.  
  416. end
  417.  
  418.  
  419.  
  420. function Poll:release()
  421.  
  422. if self.running then
  423.  
  424. self.presses = self.presses - 1
  425.  
  426. end
  427.  
  428. end
  429.  
  430.  
  431.  
  432. function Poll:setMKeyState()
  433.  
  434. if self.runMKeyModifier then
  435.  
  436. local default = nil
  437.  
  438. local i = 1
  439.  
  440. local modMKeyState = nil
  441.  
  442.  
  443.  
  444. for i = 1, 3, 1 do
  445.  
  446. if self.MKeyModifier[i] == "default" then
  447.  
  448. default = i
  449.  
  450. elseif self.MKeyModifier[i] == "" then
  451.  
  452. -- do nothing
  453.  
  454. elseif string.find(self.MKeyModifier[i],"mb%d") then
  455.  
  456. local iMB = tonumber(string.sub(self.MKeyModifier[i],3))
  457.  
  458. if IsMouseButtonPressed(iMB) then
  459.  
  460. modMKeyState = i
  461.  
  462. end
  463.  
  464. elseif IsModifierPressed(self.MKeyModifier[i]) then
  465.  
  466. modMKeyState = i
  467.  
  468. end
  469.  
  470. end
  471.  
  472. if modMKeyState == nil then
  473.  
  474. modMKeyState = default
  475.  
  476. end
  477.  
  478. if modMKeyState ~= nil then
  479.  
  480. if self.pFamily == self.modFamily then
  481.  
  482. SetMKeyState(modMKeyState, self.pFamily)
  483.  
  484. return
  485.  
  486. elseif GetMKeyState(self.modFamily) ~= modMKeyState then
  487.  
  488. SetMKeyState(modMKeyState, self.modFamily)
  489.  
  490. end
  491.  
  492. end
  493.  
  494. end
  495.  
  496. SetMKeyState( GetMKeyState(self.pFamily), self.pFamily )
  497.  
  498. end
  499.  
  500.  
  501.  
  502. function Poll:run(event, arg, family)
  503.  
  504. if self.running and self.pFamily == family then
  505.  
  506. if event == "M_PRESSED" then
  507.  
  508. self:_press()
  509.  
  510. Sleep(self.delay)
  511.  
  512. elseif event == "M_RELEASED" then
  513.  
  514. if self.presses == 1 then
  515.  
  516. self:setMKeyState()
  517.  
  518. Sleep(self.delay)
  519.  
  520. end
  521.  
  522. self:release()
  523.  
  524. end
  525.  
  526. end
  527.  
  528. end
  529.  
  530.  
  531.  
  532. CoThread = {}
  533.  
  534. CoThread.__index = CoThread
  535.  
  536.  
  537.  
  538. function CoThread:new(f) -- f - function(MultiThread)
  539.  
  540. local self = {}
  541.  
  542.  
  543.  
  544. if type(f) ~= "function" then
  545.  
  546. error("A function is needed",2)
  547.  
  548. end
  549.  
  550. self.func = f
  551.  
  552. self.co = nil
  553.  
  554. self.sleepUntilTimeStamp = nil
  555.  
  556. self.Type = "CoThread"
  557.  
  558.  
  559.  
  560. setmetatable(self, CoThread)
  561.  
  562. return self
  563.  
  564. end
  565.  
  566.  
  567.  
  568. function CoThread:recreate()
  569.  
  570. self:kill()
  571.  
  572. self.co = coroutine.create(self.func)
  573.  
  574. end
  575.  
  576.  
  577.  
  578. function CoThread:create()
  579.  
  580. self.co = coroutine.create(self.func)
  581.  
  582. end
  583.  
  584.  
  585.  
  586. function CoThread:isDead()
  587.  
  588. if self.co == nil then
  589.  
  590. return true
  591.  
  592. elseif coroutine.status(self.co) == "dead" then
  593.  
  594. self.co = nil
  595.  
  596. return true
  597.  
  598. else
  599.  
  600. return false
  601.  
  602. end
  603.  
  604. end
  605.  
  606.  
  607.  
  608. function CoThread:run(...)
  609.  
  610. if self:isDead() then
  611.  
  612. return
  613.  
  614. end
  615.  
  616. if self.sleepUntilTimeStamp ~= nil then
  617.  
  618. if _GetRunningTime < self.sleepUntilTimeStamp then
  619.  
  620. return
  621.  
  622. else
  623.  
  624. self.sleepUntilTimeStamp = nil
  625.  
  626. end
  627.  
  628. end
  629.  
  630. coroutine.resume(self.co, self, ...)
  631.  
  632. end
  633.  
  634.  
  635.  
  636. function CoThread:kill()
  637.  
  638. self.co = nil
  639.  
  640. self.sleepUntilTimeStamp = nil
  641.  
  642. end
  643.  
  644.  
  645.  
  646. function CoThread:yield()
  647.  
  648. coroutine.yield()
  649.  
  650. end
  651.  
  652.  
  653.  
  654. function CoThread:sleep(Milliseconds)
  655.  
  656. self.sleepUntilTimeStamp = _GetRunningTime + Milliseconds
  657.  
  658.  
  659.  
  660. coroutine.yield()
  661.  
  662. end
  663.  
  664.  
  665.  
  666. function CoThread:start()
  667.  
  668. self:recreate()
  669.  
  670. end
  671.  
  672.  
  673.  
  674. function CoThread:stop()
  675.  
  676. self:kill()
  677.  
  678. end
  679.  
  680.  
  681.  
  682. function CoThread:isFinished()
  683.  
  684. return self:isDead()
  685.  
  686. end
Add Comment
Please, Sign In to add comment