본문 바로가기

마인크래프트(Minecraft)/Computer Craft

[Computer Craft] 6. 무선 조정을 해보자. rednet

6. 무선 조정을 해보자. rednet


오랜만에 컴크 강좌를 쓰네요.

레드넷은 무선신호를 보내서 그 신호를 받아서 처리하는 기술인데요.

이 기술을 응용하면 채팅서비스. 무선조정. 노트블럭 합주곡까지 가능해요.


이번에는 레드스톤 신호를 사용한 무선조정기술을 보도록해요



1. 타입을 변환 해보자

- tonumber(문자열)

- tostring(정수)


tonumber 는 모든 문자를 정수형으로 변환 해주는게 있어요. 예전에 설명했었지만, 복습하는 차원에서 다시 배우도록해요

local i = "1" -- i 에 1이라는 문자열을 대입 정수가 아니므로 산술 계산 불가능

tonumber(i) -- 문자열 1을 정수 1로 바꿔주죠


tostring 는 tonumber 와 정반대에요

local i = 1

tostring(i) -- 정수 1을 문자열 "1" 로 고쳐주는 작업입니다.



2. 레드넷 통신을 시작하자.

- rednet.open(방향)


레드넷을 하기위해서는 반드시 컴퓨터 주변에 Modem 이라는 아이템을 붙여야하는데요. 모뎀을 붙이는 방향에 따라 프로그래밍 방향이 달라지게되죠.

rednet.open 명령어를 쓰게되면 모뎀 주변이 빨갛게 되면서 통신을 가능한 상태로 되는거죠.



3. 레드넷 신호를 받아보자.

- rednet.receive()


local senderId, message, distance = rednet.receive()

레드넷은 3개의 신호를 받는데요.

첫번 : 보낸컴퓨터의ID

둘째 : 보낸메시지 (문자열 입니다)

셋째 : 보낸컴퓨터와 받는컴퓨터와의 거리


받는컴퓨터는 보낸메시지가 문자열이므로 숫자열로 할려면 여러가지 작업을 해둬야해요.




4. 비트 and 연산을 하자.


- bit.band(정수,정수1,정수2,정수...)


수학시간에 배웠었죠. 1과 0으로 이루어진 기수법

3을 2진수로 고치면 11

2를 2진수로하면 10


and 연산은 11 과 10 에서 동일한 자릿수만 참으로 계산한다는거죠....

그래서 3과 2를 비트연산을하게되면


11

10

-- and

10


10 이라는 2진수 값이 나오면서 10진수로 하면 2가 되죠..... 기수법 배우지 않은 사람은 잘 모를거에요...

저는 설명이 더 어렵네요....


5. 메시지를 보내자.


rednet.broadcast(문자열)

레드넷에는 모든 컴퓨터에게 메시지를 보내는 함수를 제공해요.

rednet.broadcast 이 그 함수죠. 물론 문자열만 보내게되요.




6. 소스 예제


6-1 송신 컴퓨터 소스


rednet.open("top")
term.clear()
term.setCursorPos(1,1)
flag = 0
while true do
  print("Controller")
  print("1. North, Open the door")
  print("2. North, Close the door")
  print("3. East, Open the door")
  print("4. East, Close the door")
  print("5. South, Open the door")
  print("6. South, Close the door")
  print("7. West, Open the door")
  print("8. West, Close the door")
  print("9. All, Open the door")
  print("10. All, Close the door")
  write("Input the Number : ")
  number = tonumber(read()) -- 문자열을 정수로 변경

  if number < 9 then
    number = 2^(number - 1)
    number = tostring(number) -- 정수를 문자열로 변경
    flag = 1
  elseif number == 9 then
    number = "85"
    flag = 1
  elseif number == 10 then
    number = "170"
    flag = 1
  else
    term.clear()
    term.setCursorPos(1,1)
    print("wrong number")
    term.setCursorPos(1,3)
    number = 0
    flag = 0
  end

  if flag == 1 then
    term.clear()
    term.setCursorPos(1,1)
    print("Done")
    term.setCursorPos(1,3)
    rednet.broadcast(number)
    flag = 0
  end

end



6-2 수신컴퓨터 1 (예제에선 송신컴퓨터의 북쪽)


door = rednet.open("top")
while true do
 local senderId, message, distance = rednet.receive() -- 메시지를 받는다.
 if bit.band(tonumber(message),1) == 1 then
  os.sleep(3)
  redstone.setOutput("bottom",true)
 elseif bit.band(tonumber(message),2) == 2 then
  os.sleep(3)
  redstone.setOutput("bottom",false)
 end
end



6-3 수신컴퓨터 2( 예제에선 송신컴퓨터의 동쪽)


door = rednet.open("top")
while true do
 local senderId, message, distance = rednet.receive()
 if bit.band(tonumber(message),4) == 4 then
  os.sleep(3)
  redstone.setOutput("bottom",true)
 elseif bit.band(tonumber(message),8) == 8 then
  os.sleep(3)
  redstone.setOutput("bottom",false)
 end
end



6-4 수신컴퓨터 3 ( 예제에서 송신컴퓨터의 남쪽)


door = rednet.open("top")
while true do
 local senderId, message, distance = rednet.receive()
 if bit.band(tonumber(message),16) == 16 then
  os.sleep(3)
  redstone.setOutput("bottom",true)
 elseif bit.band(tonumber(message),32) == 32 then
  os.sleep(3)
  redstone.setOutput("bottom",false)
 end
end



6-5 수신컴퓨터 4 ( 예제에서 송신컴퓨터의 서쪽 )


door = rednet.open("top")
while true do
 local senderId, message, distance = rednet.receive()
 if bit.band(tonumber(message),64) == 64 then
  os.sleep(3)
  redstone.setOutput("bottom",true)
 elseif bit.band(tonumber(message),128) == 128 then
  os.sleep(3)
  redstone.setOutput("bottom",false)
 end
end



7.  시연영상