led-text.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. return function (connection, req, args)
  2. dofile('httpserver-header.lc')(connection, 200, 'html')
  3. local w, h, dataWidth, offset = 10, 6, 36, 0
  4. -- timer id(0-6), interval in ms
  5. local tmrId, tmrMs = 4, 200
  6. if req.method == 'POST' then
  7. local rd = req.getRequestData()
  8. if (rd['data'] ~= nil) then
  9. file.open('led-text.dat', 'w+')
  10. file.write(rd['data'])
  11. file.close()
  12. end
  13. end
  14. collectgarbage()
  15. tmr.alarm(tmrId, tmrMs, tmr.ALARM_SEMI, function()
  16. if offset < dataWidth then
  17. local data = ''
  18. file.open('led-text.dat', 'r')
  19. local row = 0
  20. while row < h do
  21. file.seek("set", (row * dataWidth + offset) * 3)
  22. local size = w
  23. if (offset + w > dataWidth) then
  24. size = dataWidth - offset
  25. end
  26. data = data .. file.read(size * 3)
  27. if size < w then
  28. data = data .. string.char(0):rep((w - size) * 3)
  29. end
  30. row = row + 1
  31. end
  32. file.close()
  33. ws2812.write(data)
  34. offset = offset + 1
  35. tmr.start(tmrId)
  36. else
  37. ws2812.write(string.char(0):rep(w*h*3))
  38. tmr.unregister(tmrId)
  39. end
  40. end)
  41. end