Advertisement
cyber_Ahn

openFmRadio

Jul 12th, 2016
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.36 KB | None | 0 0
  1. --monitot size 5x4
  2.  
  3. shell.run("delete caAPI")
  4. shell.run("pastebin get EDLdR1nF caAPI")
  5. os.loadAPI("caAPI")
  6. local radio_number = caAPI.get_hardware("openfm_radio")
  7. if radio_number == "openfm_radio_off" then
  8. shell.run("clear")
  9. else
  10. fm = peripheral.wrap(radio_number)
  11. end
  12. local fileDataX = {}
  13.  
  14. --install database--
  15. local found = fs.exists("database/radio.db")
  16. if found == false then
  17. local file = fs.open("database/radio.db","w")
  18. file.writeLine("Music Trap#http://95.141.24.73:80")
  19. file.writeLine("cyber#http://caworks-sl.de:8000")
  20. file.close()
  21. end
  22.  
  23. --set monitor
  24. function set_monitor()
  25. local monitor_number = caAPI.get_hardware("monitor")
  26. local found = fs.exists("config/monitor.cfg")
  27. if found == true then
  28. file = fs.open("config/monitor.cfg","r")
  29. local fileData = {}
  30. local line = file.readLine()
  31. repeat
  32. table.insert(fileData,line)
  33. line = file.readLine()
  34. until line == nil
  35. file.close()
  36. monitor_number = fileData[1]
  37. end
  38. mon = peripheral.wrap(monitor_number)
  39. end
  40.  
  41. --draw screen
  42. function draw_screen()
  43. mon.setBackgroundColor(colors.white)
  44. mon.setTextColor(colors.white)
  45. mon.clear()
  46. mon.setBackgroundColor(colors.white)
  47. mon.setCursorPos(1,1)
  48. mon.setBackgroundColor(colors.blue)
  49. mon.write("openFM Radio 0.1                                 ")
  50. mon.setCursorPos(50,1)
  51. mon.setBackgroundColor(colors.red)
  52. mon.setTextColor(colors.black)
  53. mon.write("X")
  54. mon.setCursorPos(3,2)
  55. mon.setBackgroundColor(colors.gray)
  56. mon.setTextColor(colors.black)
  57. mon.write("Play")
  58. mon.setCursorPos(9,2)
  59. mon.setBackgroundColor(colors.gray)
  60. mon.setTextColor(colors.black)
  61. mon.write("Add a New Stream")
  62. mon.setCursorPos(26,2)
  63. mon.setBackgroundColor(colors.gray)
  64. mon.setTextColor(colors.black)
  65. mon.write("Delete Stream")
  66. mon.setCursorPos(41,2)
  67. mon.setBackgroundColor(colors.gray)
  68. mon.setTextColor(colors.black)
  69. mon.write("Stop")
  70. mon.setCursorPos(46,2)
  71. mon.setBackgroundColor(colors.gray)
  72. mon.setTextColor(colors.black)
  73. mon.write("V-")
  74. mon.setCursorPos(49,2)
  75. mon.setBackgroundColor(colors.gray)
  76. mon.setTextColor(colors.black)
  77. mon.write("V+")
  78. mon.setCursorPos(1,3)
  79. mon.setBackgroundColor(colors.black)
  80. mon.setTextColor(colors.black)
  81. mon.write("                                                  ")
  82. mon.setCursorPos(1,5)
  83. mon.setBackgroundColor(colors.black)
  84. mon.setTextColor(colors.black)
  85. mon.write("                                                  ")
  86. mon.setCursorPos(1,26)
  87. mon.setBackgroundColor(colors.black)
  88. mon.setTextColor(colors.black)
  89. mon.write("                                                  ")
  90. for yi = 4,25 do
  91. mon.setCursorPos(1,yi)
  92. mon.write(" ")
  93. mon.setCursorPos(50,yi)
  94. mon.write(" ")
  95. yi = yi + 1
  96. end
  97. mon.setCursorPos(2,4)
  98. mon.setBackgroundColor(colors.white)
  99. mon.setTextColor(colors.black)
  100. mon.write("Play Name              IP")
  101. end
  102.  
  103. --load DB
  104. function load_db()
  105. file = fs.open("database/radio.db","r")
  106. fileDataX = {}
  107. local line = file.readLine()
  108. repeat
  109. table.insert(fileDataX,line)
  110. line = file.readLine()
  111. until line == nil
  112. file.close()
  113. mon.setBackgroundColor(colors.white)
  114. mon.setTextColor(colors.black)
  115. local startcursor = 6
  116. for iu=1,#fileDataX do
  117. data = fileDataX[iu]
  118. index = string.find(data,"#")
  119. mon.setBackgroundColor(colors.gray)
  120. mon.setCursorPos(2,startcursor)
  121. mon.write("> "..iu..".")
  122. mon.setBackgroundColor(colors.white)
  123. mon.setCursorPos(7,startcursor)
  124. mon.write(string.sub(data,1,index-1))
  125. mon.setBackgroundColor(colors.white)
  126. mon.setCursorPos(25,startcursor)
  127. mon.write(string.sub(data,index+1))
  128. startcursor = startcursor + 1
  129. end
  130. end
  131.  
  132. --touch
  133. function touch()
  134. db_power = true
  135. while db_power == true do
  136. mon.setCursorPos(35,4)
  137. mon.setBackgroundColor(colors.white)
  138. mon.setTextColor(colors.black)
  139. vol = fm.getVol()
  140. indexB = string.find(vol,".")
  141. vol = string.sub(vol,indexB+2)
  142. mon.write("Volume:"..vol)
  143. event,side,x,y = os.pullEvent()
  144. if event == "monitor_touch" then
  145. if x == 50 and y == 1 then
  146. db_power = false
  147. shell.run("clear")
  148. end
  149. if x >= 3 and x <= 7 and y == 2 then
  150. fm.start()
  151. fm.setScreenText("Play")
  152. end
  153. if x >= 8 and x <= 24 and y == 2 then
  154. add()
  155. db_power = false
  156. end
  157. if x >= 26 and x <= 40 and y == 2 then
  158. delete()
  159. db_power = false
  160. end
  161. if x >= 41 and x <= 44 and y == 2 then
  162. fm.setScreenText("Stop")
  163. fm.stop()
  164. end
  165. if x >= 46 and x <= 47 and y == 2 then
  166. fm.volDown()
  167. end
  168. if x >= 49 and x <= 50 and y == 2 then
  169. fm.volUp()
  170. end
  171. if y == 6 then
  172. data = fileDataX[1]
  173. index = string.find(data,"#")
  174. name = string.sub(data,1,index-1)
  175. ip_ad = string.sub(data,index+1)
  176. mon.setTextColor(colors.white)
  177. mon.setBackgroundColor(colors.black)
  178. mon.setCursorPos(1,26)
  179. mon.write("                                                  ")
  180. mon.setCursorPos(1,26)
  181. mon.write("Play: "..name.." "..ip_ad)
  182. mon.setCursorPos(1,1)
  183. fm.setScreenText(name)
  184. --fm.setURL(ip_ad)
  185. fm.start()
  186. end
  187. if y == 7 then
  188. data = fileDataX[2]
  189. index = string.find(data,"#")
  190. name = string.sub(data,1,index-1)
  191. ip_ad = string.sub(data,index+1)
  192. mon.setTextColor(colors.white)
  193. mon.setBackgroundColor(colors.black)
  194. mon.setCursorPos(1,26)
  195. mon.write("                                                  ")
  196. mon.setCursorPos(1,26)
  197. mon.write("Play: "..name.." "..ip_ad)
  198. mon.setCursorPos(1,1)
  199. fm.setScreenText(name)
  200. --fm.setURL(ip_ad)
  201. fm.start()
  202. end
  203. if y == 8 then
  204. data = fileDataX[3]
  205. index = string.find(data,"#")
  206. name = string.sub(data,1,index-1)
  207. ip_ad = string.sub(data,index+1)
  208. mon.setTextColor(colors.white)
  209. mon.setBackgroundColor(colors.black)
  210. mon.setCursorPos(1,26)
  211. mon.write("                                                  ")
  212. mon.setCursorPos(1,26)
  213. mon.write("Play: "..name.." "..ip_ad)
  214. mon.setCursorPos(1,1)
  215. fm.setScreenText(name)
  216. --fm.setURL(ip_ad)
  217. fm.start()
  218. end
  219. if y == 9 then
  220. data = fileDataX[4]
  221. index = string.find(data,"#")
  222. name = string.sub(data,1,index-1)
  223. ip_ad = string.sub(data,index+1)
  224. mon.setTextColor(colors.white)
  225. mon.setBackgroundColor(colors.black)
  226. mon.setCursorPos(1,26)
  227. mon.write("                                                  ")
  228. mon.setCursorPos(1,26)
  229. mon.write("Play: "..name.." "..ip_ad)
  230. mon.setCursorPos(1,1)
  231. fm.setScreenText(name)
  232. --fm.setURL(ip_ad)
  233. fm.start()
  234. end
  235. if y == 10 then
  236. data = fileDataX[5]
  237. index = string.find(data,"#")
  238. name = string.sub(data,1,index-1)
  239. ip_ad = string.sub(data,index+1)
  240. mon.setTextColor(colors.white)
  241. mon.setBackgroundColor(colors.black)
  242. mon.setCursorPos(1,26)
  243. mon.write("                                                  ")
  244. mon.setCursorPos(1,26)
  245. mon.write("Play: "..name.." "..ip_ad)
  246. mon.setCursorPos(1,1)
  247. fm.setScreenText(name)
  248. --fm.setURL(ip_ad)
  249. fm.start()
  250. end
  251. if y == 11 then
  252. data = fileDataX[6]
  253. index = string.find(data,"#")
  254. name = string.sub(data,1,index-1)
  255. ip_ad = string.sub(data,index+1)
  256. mon.setTextColor(colors.white)
  257. mon.setBackgroundColor(colors.black)
  258. mon.setCursorPos(1,26)
  259. mon.write("                                                  ")
  260. mon.setCursorPos(1,26)
  261. mon.write("Play: "..name.." "..ip_ad)
  262. mon.setCursorPos(1,1)
  263. fm.setScreenText(name)
  264. --fm.setURL(ip_ad)
  265. fm.start()
  266. end
  267. if y == 12 then
  268. data = fileDataX[7]
  269. index = string.find(data,"#")
  270. name = string.sub(data,1,index-1)
  271. ip_ad = string.sub(data,index+1)
  272. mon.setTextColor(colors.white)
  273. mon.setBackgroundColor(colors.black)
  274. mon.setCursorPos(1,26)
  275. mon.write("                                                  ")
  276. mon.setCursorPos(1,26)
  277. mon.write("Play: "..name.." "..ip_ad)
  278. mon.setCursorPos(1,1)
  279. fm.setScreenText(name)
  280. --fm.setURL(ip_ad)
  281. fm.start()
  282. end
  283. if y == 13 then
  284. data = fileDataX[8]
  285. index = string.find(data,"#")
  286. name = string.sub(data,1,index-1)
  287. ip_ad = string.sub(data,index+1)
  288. mon.setTextColor(colors.white)
  289. mon.setBackgroundColor(colors.black)
  290. mon.setCursorPos(1,26)
  291. mon.write("                                                  ")
  292. mon.setCursorPos(1,26)
  293. mon.write("Play: "..name.." "..ip_ad)
  294. mon.setCursorPos(1,1)
  295. fm.setScreenText(name)
  296. --fm.setURL(ip_ad)
  297. fm.start()
  298. end
  299. if y == 14 then
  300. data = fileDataX[9]
  301. index = string.find(data,"#")
  302. name = string.sub(data,1,index-1)
  303. ip_ad = string.sub(data,index+1)
  304. mon.setTextColor(colors.white)
  305. mon.setBackgroundColor(colors.black)
  306. mon.setCursorPos(1,26)
  307. mon.write("                                                  ")
  308. mon.setCursorPos(1,26)
  309. mon.write("Play: "..name.." "..ip_ad)
  310. mon.setCursorPos(1,1)
  311. fm.setScreenText(name)
  312. --fm.setURL(ip_ad)
  313. fm.start()
  314. end
  315. if y == 15 then
  316. data = fileDataX[10]
  317. index = string.find(data,"#")
  318. name = string.sub(data,1,index-1)
  319. ip_ad = string.sub(data,index+1)
  320. mon.setTextColor(colors.white)
  321. mon.setBackgroundColor(colors.black)
  322. mon.setCursorPos(1,26)
  323. mon.write("                                                  ")
  324. mon.setCursorPos(1,26)
  325. mon.write("Play: "..name.." "..ip_ad)
  326. mon.setCursorPos(1,1)
  327. fm.setScreenText(name)
  328. --fm.setURL(ip_ad)
  329. fm.start()
  330. end
  331. if y == 16 then
  332. data = fileDataX[11]
  333. index = string.find(data,"#")
  334. name = string.sub(data,1,index-1)
  335. ip_ad = string.sub(data,index+1)
  336. mon.setTextColor(colors.white)
  337. mon.setBackgroundColor(colors.black)
  338. mon.setCursorPos(1,26)
  339. mon.write("                                                  ")
  340. mon.setCursorPos(1,26)
  341. mon.write("Play: "..name.." "..ip_ad)
  342. mon.setCursorPos(1,1)
  343. fm.setScreenText(name)
  344. --fm.setURL(ip_ad)
  345. fm.start()
  346. end
  347. if y == 17 then
  348. data = fileDataX[12]
  349. index = string.find(data,"#")
  350. name = string.sub(data,1,index-1)
  351. ip_ad = string.sub(data,index+1)
  352. mon.setTextColor(colors.white)
  353. mon.setBackgroundColor(colors.black)
  354. mon.setCursorPos(1,26)
  355. mon.write("                                                  ")
  356. mon.setCursorPos(1,26)
  357. mon.write("Play: "..name.." "..ip_ad)
  358. mon.setCursorPos(1,1)
  359. fm.setScreenText(name)
  360. --fm.setURL(ip_ad)
  361. fm.start()
  362. end
  363. if y == 18 then
  364. data = fileDataX[13]
  365. index = string.find(data,"#")
  366. name = string.sub(data,1,index-1)
  367. ip_ad = string.sub(data,index+1)
  368. mon.setTextColor(colors.white)
  369. mon.setBackgroundColor(colors.black)
  370. mon.setCursorPos(1,26)
  371. mon.write("                                                  ")
  372. mon.setCursorPos(1,26)
  373. mon.write("Play: "..name.." "..ip_ad)
  374. mon.setCursorPos(1,1)
  375. fm.setScreenText(name)
  376. --fm.setURL(ip_ad)
  377. fm.start()
  378. end
  379. if y == 19 then
  380. data = fileDataX[14]
  381. index = string.find(data,"#")
  382. name = string.sub(data,1,index-1)
  383. ip_ad = string.sub(data,index+1)
  384. mon.setTextColor(colors.white)
  385. mon.setBackgroundColor(colors.black)
  386. mon.setCursorPos(1,26)
  387. mon.write("                                                  ")
  388. mon.setCursorPos(1,26)
  389. mon.write("Play: "..name.." "..ip_ad)
  390. mon.setCursorPos(1,1)
  391. fm.setScreenText(name)
  392. --fm.setURL(ip_ad)
  393. fm.start()
  394. end
  395. if y == 20 then
  396. data = fileDataX[15]
  397. index = string.find(data,"#")
  398. name = string.sub(data,1,index-1)
  399. ip_ad = string.sub(data,index+1)
  400. mon.setTextColor(colors.white)
  401. mon.setBackgroundColor(colors.black)
  402. mon.setCursorPos(1,26)
  403. mon.write("                                                  ")
  404. mon.setCursorPos(1,26)
  405. mon.write("Play: "..name.." "..ip_ad)
  406. mon.setCursorPos(1,1)
  407. fm.setScreenText(name)
  408. --fm.setURL(ip_ad)
  409. fm.start()
  410. end
  411. if y == 21 then
  412. data = fileDataX[16]
  413. index = string.find(data,"#")
  414. name = string.sub(data,1,index-1)
  415. ip_ad = string.sub(data,index+1)
  416. mon.setTextColor(colors.white)
  417. mon.setBackgroundColor(colors.black)
  418. mon.setCursorPos(1,26)
  419. mon.write("                                                  ")
  420. mon.setCursorPos(1,26)
  421. mon.write("Play: "..name.." "..ip_ad)
  422. mon.setCursorPos(1,1)
  423. fm.setScreenText(name)
  424. --fm.setURL(ip_ad)
  425. fm.start()
  426. end
  427. if y == 22 then
  428. data = fileDataX[17]
  429. index = string.find(data,"#")
  430. name = string.sub(data,1,index-1)
  431. ip_ad = string.sub(data,index+1)
  432. mon.setTextColor(colors.white)
  433. mon.setBackgroundColor(colors.black)
  434. mon.setCursorPos(1,26)
  435. mon.write("                                                  ")
  436. mon.setCursorPos(1,26)
  437. mon.write("Play: "..name.." "..ip_ad)
  438. mon.setCursorPos(1,1)
  439. fm.setScreenText(name)
  440. --fm.setURL(ip_ad)
  441. fm.start()
  442. end
  443. if y == 23 then
  444. data = fileDataX[18]
  445. index = string.find(data,"#")
  446. name = string.sub(data,1,index-1)
  447. ip_ad = string.sub(data,index+1)
  448. mon.setTextColor(colors.white)
  449. mon.setBackgroundColor(colors.black)
  450. mon.setCursorPos(1,26)
  451. mon.write("                                                  ")
  452. mon.setCursorPos(1,26)
  453. mon.write("Play: "..name.." "..ip_ad)
  454. mon.setCursorPos(1,1)
  455. fm.setScreenText(name)
  456. --fm.setURL(ip_ad)
  457. fm.start()
  458. end
  459. if y == 24 then
  460. data = fileDataX[19]
  461. index = string.find(data,"#")
  462. name = string.sub(data,1,index-1)
  463. ip_ad = string.sub(data,index+1)
  464. mon.setTextColor(colors.white)
  465. mon.setBackgroundColor(colors.black)
  466. mon.setCursorPos(1,26)
  467. mon.write("                                                  ")
  468. mon.setCursorPos(1,26)
  469. mon.write("Play: "..name.." "..ip_ad)
  470. mon.setCursorPos(1,1)
  471. fm.setScreenText(name)
  472. --fm.setURL(ip_ad)
  473. fm.start()
  474. end
  475. end
  476. end
  477. end
  478.  
  479. --add stream
  480. function add()
  481. mon.setTextColor(colors.black)
  482. mon.setBackgroundColor(colors.white)
  483. mon.setCursorPos(1,26)
  484. mon.write("Name:")
  485. nam = read()
  486. mon.setCursorPos(25,26)
  487. mon.write("Address:")
  488. ad = read()
  489. save = nam.."#"..ad
  490. local file = fs.open("database/radio.db","a")
  491. file.writeLine(save)
  492. file.close()
  493. mon.setCursorPos(2,25)
  494. mon.write(save)
  495. sleep(3)
  496. draw_screen()
  497. load_db()
  498. touch()
  499. end
  500.  
  501. -- del stream
  502. function delete()
  503. mon.setTextColor(colors.black)
  504. mon.setBackgroundColor(colors.white)
  505. mon.setCursorPos(1,26)
  506. mon.write("Line Number:")
  507. num = read()
  508. table.remove(fileDataX,num)
  509. local file = fs.open("database/radio.db","w")
  510. for iux=1,#fileDataX do
  511. file.writeLine(fileDataX[iux])
  512. end
  513. file.close()
  514. draw_screen()
  515. load_db()
  516. touch()
  517. end
  518.  
  519. --prog
  520. set_monitor()
  521. draw_screen()
  522. load_db()
  523. touch()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement