Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Camera to Hologram Sender with Recording
- -- Uses available APIs: read(), sleep(), parallel, fs
- -- Configuration
- local cameraName = "camera_0"
- local hologramName = "hologram_0"
- local recordingFile = "recording.dat"
- -- State
- local camera = nil
- local hologram = nil
- local isRunning = true
- local isRecording = false
- local isPlaying = false
- local recordedFrames = {}
- local frameIndex = 1
- local frameInterval = 0.1 -- 10 FPS
- -- Colors
- local colors = colors or {
- white = 1,
- orange = 2,
- magenta = 4,
- lightBlue = 8,
- yellow = 16,
- lime = 32,
- pink = 64,
- gray = 128,
- lightGray = 256,
- cyan = 512,
- purple = 1024,
- blue = 2048,
- brown = 4096,
- green = 8192,
- red = 16384,
- black = 32768
- }
- -- Find peripherals
- local function findPeripherals()
- local names = peripheral.getNames()
- for i, name in ipairs(names) do
- local pType = peripheral.getType(name)
- if pType == "camera" and not camera then
- cameraName = name
- camera = peripheral.wrap(name)
- print("Found camera: " .. name)
- elseif pType == "hologram" and not hologram then
- hologramName = name
- hologram = peripheral.wrap(name)
- print("Found hologram: " .. name)
- hologram.on()
- end
- end
- return camera ~= nil and hologram ~= nil
- end
- -- Save recording to file
- local function saveRecording()
- if #recordedFrames == 0 then
- print("No frames to save!")
- return false
- end
- local file = fs.open(recordingFile, "w")
- if not file then
- print("Failed to open file for writing")
- return false
- end
- -- Write frame count
- file.writeLine(tostring(#recordedFrames))
- -- Write each frame
- for i, frame in ipairs(recordedFrames) do
- file.writeLine(textutils.serialize(frame))
- end
- file.close()
- print("Saved " .. #recordedFrames .. " frames to " .. recordingFile)
- return true
- end
- -- Load recording from file
- local function loadRecording()
- if not fs.exists(recordingFile) then
- print("Recording file not found: " .. recordingFile)
- return false
- end
- local file = fs.open(recordingFile, "r")
- if not file then
- print("Failed to open file for reading")
- return false
- end
- local countStr = file.readLine()
- local count = tonumber(countStr) or 0
- recordedFrames = {}
- for i = 1, count do
- local line = file.readLine()
- if line then
- local frame = textutils.unserialize(line)
- if frame then
- table.insert(recordedFrames, frame)
- end
- end
- end
- file.close()
- print("Loaded " .. #recordedFrames .. " frames from " .. recordingFile)
- return #recordedFrames > 0
- end
- -- Capture a single frame from camera (entities + center)
- local function captureFrame()
- if not camera then return nil end
- local success, entities = pcall(function()
- return camera.get3DEntityData()
- end)
- local centerSuccess, center = pcall(function()
- return camera.getNetworkCenter()
- end)
- if success and entities and centerSuccess and center then
- return { entities = entities, center = center }
- end
- return nil
- end
- -- Send frame to hologram
- local function sendFrame(frameData)
- if not hologram or not frameData then return end
- -- Send entity data with center position
- pcall(function()
- if frameData.entities and frameData.center then
- hologram.setEntityData(frameData.entities, frameData.center)
- end
- end)
- end
- -- Draw the main menu
- local function drawMenu()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- print("=== Camera Hologram Sender ===")
- term.setTextColor(colors.white)
- print("")
- if camera then
- term.setTextColor(colors.lime)
- print("Camera: " .. cameraName .. " [OK]")
- else
- term.setTextColor(colors.red)
- print("Camera: NOT FOUND")
- end
- if hologram then
- term.setTextColor(colors.lime)
- print("Hologram: " .. hologramName .. " [OK]")
- else
- term.setTextColor(colors.red)
- print("Hologram: NOT FOUND")
- end
- term.setTextColor(colors.white)
- print("")
- print("Recorded frames: " .. #recordedFrames)
- print("")
- term.setTextColor(colors.cyan)
- print("Commands:")
- term.setTextColor(colors.white)
- print(" 1 = Live stream")
- print(" 2 = Start recording")
- print(" 3 = Stop recording")
- print(" 4 = Play recording")
- print(" 5 = Stop playback")
- print(" 6 = Save to file")
- print(" 7 = Load from file")
- print(" 8 = Clear recording")
- print(" q = Quit")
- print("")
- if isRecording then
- term.setTextColor(colors.red)
- print("[RECORDING] Frames: " .. #recordedFrames)
- elseif isPlaying then
- term.setTextColor(colors.lime)
- print("[PLAYING] Frame: " .. frameIndex .. "/" .. #recordedFrames)
- end
- term.setTextColor(colors.white)
- print("")
- print("Enter command: ")
- end
- -- Live stream mode
- local function liveStream()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- print("=== LIVE STREAM MODE ===")
- term.setTextColor(colors.white)
- print("")
- print("Enter duration in seconds (0 = 10 sec):")
- local input = read()
- local duration = tonumber(input) or 10
- if duration <= 0 then duration = 10 end
- print("")
- print("Streaming for " .. duration .. " seconds...")
- local frameCount = 0
- local endTime = os.clock() + duration
- while os.clock() < endTime do
- local frame = captureFrame()
- if frame then
- sendFrame(frame)
- frameCount = frameCount + 1
- term.setCursorPos(1, 6)
- local remaining = math.floor(endTime - os.clock())
- print("Frames: " .. frameCount .. " | Time left: " .. remaining .. "s ")
- end
- sleep(frameInterval)
- end
- print("")
- print("Stream complete. Total frames: " .. frameCount)
- sleep(1)
- end
- -- Recording mode
- local function recordMode()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.red)
- print("=== RECORDING MODE ===")
- term.setTextColor(colors.white)
- print("")
- print("Enter recording duration in seconds:")
- local input = read()
- local duration = tonumber(input) or 10
- if duration <= 0 then duration = 10 end
- print("")
- print("Recording for " .. duration .. " seconds...")
- isRecording = true
- recordedFrames = {}
- local endTime = os.clock() + duration
- while os.clock() < endTime do
- local frame = captureFrame()
- if frame then
- table.insert(recordedFrames, frame)
- -- Also send to hologram for preview
- sendFrame(frame)
- term.setCursorPos(1, 6)
- local remaining = math.floor(endTime - os.clock())
- print("Frames: " .. #recordedFrames .. " | Time left: " .. remaining .. "s ")
- end
- sleep(frameInterval)
- end
- isRecording = false
- print("")
- print("Recording complete! Total frames: " .. #recordedFrames)
- sleep(1)
- end
- -- Playback mode
- local function playbackMode()
- if #recordedFrames == 0 then
- print("No recording to play!")
- sleep(1)
- return
- end
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.lime)
- print("=== PLAYBACK MODE ===")
- term.setTextColor(colors.white)
- print("")
- print("Loop playback? (y/n):")
- local loopInput = read()
- local loopMode = (loopInput == "y" or loopInput == "Y")
- print("")
- if loopMode then
- print("Enter number of loops (0 = infinite):")
- local loopCountInput = read()
- local maxLoops = tonumber(loopCountInput) or 1
- print("")
- print("Playing " .. #recordedFrames .. " frames...")
- print("(Press Ctrl+T to terminate)")
- isPlaying = true
- frameIndex = 1
- local loopCount = 0
- while isPlaying do
- if frameIndex <= #recordedFrames then
- sendFrame(recordedFrames[frameIndex])
- term.setCursorPos(1, 7)
- print("Frame: " .. frameIndex .. "/" .. #recordedFrames .. " | Loop: " .. (loopCount + 1) .. " ")
- frameIndex = frameIndex + 1
- else
- loopCount = loopCount + 1
- if maxLoops > 0 and loopCount >= maxLoops then
- break
- end
- frameIndex = 1
- end
- sleep(frameInterval)
- end
- else
- print("Playing " .. #recordedFrames .. " frames...")
- isPlaying = true
- for i, frame in ipairs(recordedFrames) do
- sendFrame(frame)
- term.setCursorPos(1, 6)
- print("Frame: " .. i .. "/" .. #recordedFrames .. " ")
- sleep(frameInterval)
- end
- end
- isPlaying = false
- print("")
- print("Playback complete!")
- sleep(1)
- end
- -- Main program loop
- local function main()
- term.clear()
- term.setCursorPos(1, 1)
- print("Initializing Camera Hologram Sender...")
- print("")
- -- Find peripherals
- if not findPeripherals() then
- term.setTextColor(colors.red)
- if not camera then
- print("ERROR: No camera found!")
- end
- if not hologram then
- print("ERROR: No hologram found!")
- end
- term.setTextColor(colors.white)
- print("")
- print("Connect peripherals and restart.")
- return
- end
- print("")
- print("Press Enter to continue...")
- read()
- -- Main loop
- while isRunning do
- drawMenu()
- local input = read()
- if input == "1" then
- liveStream()
- elseif input == "2" then
- recordMode()
- elseif input == "3" then
- isRecording = false
- print("Recording stopped.")
- sleep(0.5)
- elseif input == "4" then
- playbackMode()
- elseif input == "5" then
- isPlaying = false
- print("Playback stopped.")
- sleep(0.5)
- elseif input == "6" then
- saveRecording()
- sleep(1)
- elseif input == "7" then
- loadRecording()
- sleep(1)
- elseif input == "8" then
- recordedFrames = {}
- print("Recording cleared.")
- sleep(0.5)
- elseif input == "q" or input == "Q" then
- isRunning = false
- print("Goodbye!")
- end
- end
- end
- -- Run the program
- main()
Advertisement
Add Comment
Please, Sign In to add comment