Advertisement
Doob

[OpenComputers] morse

Jun 13th, 2015
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | None | 0 0
  1. local computer = require('computer')
  2. local term = require('term')
  3. local unicode = require('unicode')
  4. local duration = 0.1 --длительность точки
  5. local frequency = 1000 --частота
  6.  
  7. tMorse = {
  8.   {[' '] = ' '},
  9.   {['.'] = '.-.-.-'},
  10.   {[','] = '--..--'},
  11.   {[':'] = '---...'},
  12.   {[';'] = '-.-.-.'},
  13.   {["'"] = '.----.'},
  14.   {['"'] = '.-..-.'},
  15.   {['/'] = '-..-.'},
  16.   {['_'] = '..--.-'},
  17.   {['='] = '-...-'},
  18.   {['+'] = '.-.-.'},
  19.   {['-'] = '-....-'},
  20.   {['?'] = '..--..'},
  21.   {['!'] = '-.-.--'},
  22.   {['@'] = '.--.-.'},
  23.  
  24.   {['0'] = '-----'},
  25.   {['1'] = '.----'},
  26.   {['2'] = '..---'},
  27.   {['3'] = '...--'},
  28.   {['4'] = '....-'},
  29.   {['5'] = '.....'},
  30.   {['6'] = '-....'},
  31.   {['7'] = '--...'},
  32.   {['8'] = '---..'},
  33.   {['9'] = '----.'},
  34.  
  35.   {['A'] = '.-'},
  36.   {['B'] = '-...'},
  37.   {['C'] = '-.-.'},
  38.   {['D'] = '-..'},
  39.   {['E'] = '.'},
  40.   {['F'] = '..-.'},
  41.   {['G'] = '--.'},
  42.   {['H'] = '....'},
  43.   {['I'] = '..'},
  44.   {['J'] = '.---'},
  45.   {['K'] = '-.-'},
  46.   {['L'] = '.-..'},
  47.   {['M'] = '--'},
  48.   {['N'] = '-.'},
  49.   {['O'] = '---'},
  50.   {['P'] = '.--.'},
  51.   {['Q'] = '--.-'},
  52.   {['R'] = '.-.'},
  53.   {['S'] = '...'},
  54.   {['T'] = '-'},
  55.   {['U'] = '..-'},
  56.   {['V'] = '...-'},
  57.   {['W'] = '.--'},
  58.   {['X'] = '-..-'},
  59.   {['Y'] = '-.--'},
  60.   {['Z'] = '--..'},
  61.  
  62.   {['А'] = '.-'},
  63.   {['Б'] = '-...'},
  64.   {['В'] = '.--'},
  65.   {['Г'] = '--.'},
  66.   {['Д'] = '-..'},
  67.   {['Е'] = '.'},
  68.   {['Ж'] = '...-'},
  69.   {['З'] = '--..'},
  70.   {['И'] = '..'},
  71.   {['Й'] = '.---'},
  72.   {['К'] = '-.-'},
  73.   {['Л'] = '.-..'},
  74.   {['М'] = '--'},
  75.   {['Н'] = '-.'},
  76.   {['О'] = '---'},
  77.   {['П'] = '.--.'},
  78.   {['Р'] = '.-.'},
  79.   {['С'] = '...'},
  80.   {['Т'] = '-'},
  81.   {['У'] = '..-'},
  82.   {['Ф'] = '..-.'},
  83.   {['Х'] = '....'},
  84.   {['Ц'] = '-.-.'},
  85.   {['Ч'] = '---.'},
  86.   {['Ш'] = '----'},
  87.   {['Щ'] = '--.-'},
  88.   {['Ъ'] = '--.--'},
  89.   {['Ы'] = '-.--'},
  90.   {['Ь'] = '-..-'},
  91.   {['Э'] = '..-..'},
  92.   {['Ю'] = '..--'},
  93.   {['Я'] = '.-.-'}
  94. }
  95.  
  96. local function unserializer(tbl)
  97.   for k, v in pairs(tbl) do
  98.     return k, v
  99.   end
  100. end
  101.  
  102. function txt_to_morse(str, wrt) -- txt_to_morse(string[текст], boolean[вывод результата на экран])
  103.   str = unicode.upper(str)
  104.  
  105.   for i = 1, unicode.len(str) do --проходимся по строке
  106.     iString = unicode.sub(str, i, i)
  107.        
  108.     for j = 1, #tMorse do --проходимся по таблице символов
  109.         index, code = unserializer(tMorse[j])
  110.        
  111.      if index == iString then
  112.          
  113.        if wrt == true then
  114.          x, y = term.getCursor()
  115.          term.clearLine()
  116.          term.write(index..' '..code)
  117.          term.setCursor(x, y)
  118.        end
  119.            
  120.        for h = 1, unicode.len(code) do --проходимся по строке кода символа
  121.          mBit = unicode.sub(code, h, h)
  122.                  
  123.          if mBit == '.' then
  124.            computer.beep(frequency, duration) --пикаем точку
  125.          elseif mBit == '-' then
  126.            computer.beep(frequency, duration*3) --пикаем тире
  127.          elseif mBit == ' ' then
  128.            os.sleep(duration*7) -- пробел
  129.          end
  130.                  
  131.          os.sleep(duration) --пробел между Морзе-битами
  132.                  
  133.        end
  134.            
  135.        os.sleep(duration*3) --пробел между символами
  136.            
  137.      end
  138.          
  139.    end
  140.    
  141.   end
  142.  
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement