Advertisement
Astrornith

EventMail.lua

Mar 30th, 2023 (edited)
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.60 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local speaker = peripheral.find("speaker")
  3. local modem = peripheral.find("modem")
  4. local drive = peripheral.find("drive")
  5. local dfpwm = require("cc.audio.dfpwm")
  6. local decoder = dfpwm.make_decoder()
  7. local repeattime = 10
  8. local event, id
  9. local mail
  10.  
  11. local defaultsong="/startup/youve-got-mail-sound.dfpwm"
  12. song=defaultsong
  13.  
  14. if drive.isDiskPresent then
  15.     print("Disk present")
  16.     if fs.find("/disk/*.dfpwm") then
  17.         floppysong=fs.find("/disk/*.dfpwm")
  18.         song=floppysong[1]
  19.         print("Floppy Song is ", song)
  20.     end
  21. end
  22.  
  23. if song==nil then
  24.     song=defaultsong
  25. end
  26.  
  27. print("Song is ", song)
  28.  
  29. modem.open(42)
  30. monitor.clear()
  31. monitor.setCursorPos(1,1)
  32. monitor.write("Checking for mail")
  33. modem.transmit(42, 42, "Checking for mail")
  34. os.startTimer(repeattime)
  35.  
  36.  
  37.  
  38. while true do
  39.     local eventData = {os.pullEvent()}
  40.     event = eventData[1]
  41.    
  42.     if event == "redstone" then
  43.         monitor.clear()
  44.         monitor.setCursorPos(1,1)
  45.         if redstone.getInput("bottom") then
  46.             mail = true
  47.             monitor.write("You've got Mail")
  48.             modem.transmit(42, 42, "You've got Mail")
  49.            
  50.             --play audio
  51.             for chunk in io.lines(song, 16 * 1024) do
  52.                 local buffer = decoder(chunk)
  53.                 while not speaker.playAudio(buffer,128) do
  54.                     os.pullEvent("speaker_audio_empty")
  55.                 end
  56.             end
  57.             os.startTimer(repeattime)
  58.         else
  59.             mail = false
  60.             monitor.write("Checking for mail")
  61.             modem.transmit(42, 42, "Checking for mail")
  62.         end
  63.        
  64.     elseif event == "timer" then
  65.         if redstone.getInput("bottom") then
  66.             --play audio
  67.             for chunk in io.lines(song, 16 * 1024) do
  68.                 local buffer = decoder(chunk)
  69.                 while not speaker.playAudio(buffer,128) do
  70.                     os.pullEvent("speaker_audio_empty")
  71.                 end
  72.             end
  73.             os.startTimer(repeattime)
  74.            
  75.         else
  76.             monitor.clear()
  77.             monitor.setCursorPos(1,1)
  78.             monitor.write("Checking for mail")
  79.             modem.transmit(42, 42, "Checking for mail")
  80.         end
  81.  
  82.     elseif event == "disk_eject" then
  83.         song=defaultsong
  84.         print("Song is ", song)
  85.    
  86.     elseif event == "disk" then
  87.         if fs.find("/disk/*.dfpwm") then
  88.             floppysong=fs.find("/disk/*.dfpwm")
  89.             song=floppysong[1]
  90.             print("Floppy Song is ", song)
  91.         end
  92.     end
  93.    
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement