Advertisement
BigSHinyToys

Immibis peripheral "Speaker" keybord

May 22nd, 2013
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. --[[
  2.         keys random
  3.         by Big SHiny Toys
  4. ]]--
  5.  
  6. local function openDevice(sType)
  7.     for i,v in pairs(rs.getSides()) do
  8.         if peripheral.isPresent(v) and peripheral.getType(v) == sType then
  9.             return peripheral.wrap(v),v
  10.         end
  11.     end
  12. end
  13.  
  14. local function freq(key)
  15.     return (2^((key-49)/12))*440
  16. end
  17.  
  18. local key = 1
  19. local pitch = freq(key)
  20. local chan = 1
  21. local speaker = openDevice("speaker")
  22.  
  23. if not speaker then
  24.     error("no speaker found")
  25. end
  26.  
  27. term.clear()
  28.  
  29. local function p()
  30.     term.setCursorPos(1,1)
  31.     term.clearLine()
  32.     print(" key "..key.." pitch "..pitch)
  33. end
  34.  
  35. local tKeys = {
  36.     a = 40,
  37.     w = 41,
  38.     s = 42,
  39.     e = 43,
  40.     d = 44,
  41.     f = 45,
  42.     t = 46,
  43.     g = 47,
  44.     y = 48,
  45.     h = 49,
  46.     u = 50,
  47.     k = 51,
  48.     j = 52,
  49.     o = 53,
  50.     l = 54,
  51.     p = 55
  52. }
  53.  
  54. local tTimers = {}
  55.  
  56. local hang = 0.3
  57.  
  58. for k,v in pairs(tKeys) do
  59.     print(k," ",v)
  60. end
  61.  
  62. local offSet = 0
  63.  
  64. local numb = 0
  65.  
  66. while true do
  67.     local event = {os.pullEvent()}
  68.     if event[1] == "char" then
  69.         if tKeys[event[2]] then
  70.             local tab = os.startTimer(hang)
  71.             tTimers[tostring(tab)] = numb
  72.             speaker.start(numb,freq(tKeys[event[2]] + (offSet*12)))
  73.             numb = numb + 1
  74.             if numb > 7 then
  75.                 numb = 0
  76.             end
  77.         end
  78.     elseif event[1] == "timer" then
  79.         local st = tostring(event[2])
  80.         if tTimers[st] then
  81.             speaker.stop(tTimers[st])
  82.             tTimers[st] = nil
  83.         end
  84.     elseif event[1] == "key" then
  85.         if event[2] == 200 then
  86.             offSet = offSet + 1
  87.         elseif event[2] == 208 then
  88.             offSet = offSet - 1
  89.         end
  90.     end
  91. end
  92.  
  93. --[[
  94. while true do
  95.     p()
  96.     local event = {os.pullEvent()}
  97.     if event[1] == "char" then
  98.         if event[2] == "u" then
  99.             key = key + 1
  100.             pitch = freq(key)
  101.             speaker.start(chan,pitch)
  102.         elseif event[2] == "d" then
  103.             key = key - 1
  104.             pitch = freq(key)
  105.             speaker.start(chan,pitch)
  106.         elseif event[2] == "s" then
  107.             speaker.stop(chan)
  108.         elseif event[2] == "p" then
  109.             for i = 1,30000 do
  110.                 speaker.start(chan,i)
  111.                 term.setCursorPos(1,1)
  112.                 term.clearLine()
  113.                 print(" key "..key.." pitch "..i)
  114.                 sleep(0.001)
  115.             end
  116.         end
  117.     end
  118. end
  119. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement