dev2919

Untitled

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