ws2801.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. module(..., package.seeall)
  2. --- 模块功能:ws2801-LED驱动控制
  3. -- @author openLuat
  4. -- @module WS2801
  5. -- @license MIT
  6. -- @copyright openLuat
  7. -- @release 2021.8.31
  8. --注!!!
  9. --ws2801驱动电平为3.3V
  10. --724开发板引出的spi电平为1.8V,需要转到3.3V才可以正常使用;
  11. --820开发板引出的spi经过板上电平转换到3.3v,将VLCD电压域打开到15用于电平转换;
  12. pmd.ldoset(15, pmd.LDO_VMMC)
  13. pmd.ldoset(15, pmd.LDO_VLCD)
  14. require "pm"
  15. pm.wake("WS2801")
  16. local i, j, k = 1, 1, 1
  17. local ledNmb = 31 --灯的个数
  18. function setColor(r, g, b) return string.char(r, g, b) end
  19. local red = setColor(255, 0, 0)
  20. local green = setColor(0, 255, 0)
  21. local blue = setColor(0, 0, 255)
  22. local white = setColor(255, 255, 255)
  23. local orange = setColor(253, 128, 60)
  24. local pink = setColor(255, 87, 215)
  25. local lightBlue = setColor(0, 255, 255)
  26. local purple = setColor(128, 0, 255)
  27. local tmp = {red, green, blue, white, orange, pink, lightBlue, purple}
  28. local black = setColor(0, 0, 0)
  29. -- 每次打开下面其中一种分支进行测试!!!
  30. -- 单个灯随机,全部一起亮
  31. sys.taskInit(function()
  32. sys.wait(5000)
  33. log.info("test start")
  34. local result = spi.setup(spi.SPI_1, 0, 0, 8, 20000, 1)
  35. log.info("spi开启结果", result)
  36. while true do
  37. local data = ""
  38. for i = 1, ledNmb do
  39. local temp = setColor(math.random(0, 255), math.random(0, 255),
  40. math.random(0, 255))
  41. data = data .. temp
  42. end
  43. spi.send(spi.SPI_1, data)
  44. sys.wait(200)
  45. end
  46. end)
  47. -- 从头到尾,从尾到头
  48. --[[ sys.taskInit(function()
  49. sys.wait(5000)
  50. log.info("test start")
  51. local result = spi.setup(spi.SPI_1, 0, 0, 8, 20000, 1)
  52. log.info("spi开启结果", result)
  53. while true do
  54. local temp = i % 2
  55. if temp == 1 then
  56. for i = 1, ledNmb do
  57. spi.send(spi.SPI_1 , string.rep(tmp[k], i))
  58. sys.wait(10)
  59. end
  60. elseif temp == 0 then
  61. if k - 1 == 0 then
  62. for j = 1, ledNmb do
  63. spi.send(spi.SPI_1, string.rep((tmp[#tmp]), ledNmb - j) .. string.rep(tmp[k], j))
  64. sys.wait(10)
  65. end
  66. else
  67. for j = 1, ledNmb do
  68. spi.send(spi.SPI_1, string.rep((tmp[k - 1]), ledNmb - j) .. string.rep(tmp[k], j))
  69. sys.wait(10)
  70. end
  71. end
  72. end
  73. i = i + 1
  74. k = k + 1
  75. if k > #tmp then
  76. k = 1
  77. end
  78. end
  79. end) ]]
  80. -- 全部随机颜色
  81. --[[ sys.taskInit(function()
  82. sys.wait(5000)
  83. log.info("test start")
  84. local result = spi.setup(spi.SPI_1, 0, 0, 8, 20000, 1)
  85. log.info("spi开启结果", result)
  86. while true do
  87. local data = setColor(math.random(0, 255), math.random(0, 255), math.random(0, 255))
  88. spi.send(spi.SPI_1, string.rep(data, ledNmb))
  89. sys.wait(200)
  90. end
  91. end) ]]
  92. -- 从中间到两边,从两边到中间
  93. --[[ sys.taskInit(function()
  94. sys.wait(5000)
  95. log.info("test start")
  96. local result = spi.setup(spi.SPI_1, 0, 0, 8, 20000, 1)
  97. log.info("spi开启结果", result)
  98. while true do
  99. local data = ""
  100. if k > #tmp then k = 1 end
  101. -- data = string.rep(black, 15) .. white .. string.rep(black, 15)
  102. for i = 0, ledNmb / 2 do
  103. if k - 1 == 0 then
  104. data = string.rep(tmp[#tmp], i) ..
  105. string.rep(tmp[1], ledNmb - 2 * i) ..
  106. string.rep(tmp[#tmp], i)
  107. -- elseif k - #tmp == 0 then
  108. -- data = string.rep(tmp[1], i) .. string.rep(tmp[k], ledNmb - 2 * i) .. string.rep(tmp[1], i)
  109. else
  110. data = string.rep(tmp[k - 1], i) ..
  111. string.rep(tmp[k], ledNmb - 2 * i) ..
  112. string.rep(tmp[k - 1], i)
  113. end
  114. spi.send(spi.SPI_1, data)
  115. sys.wait(50)
  116. end
  117. for i = 0, ledNmb / 2 do
  118. if k - #tmp == 0 then
  119. data = string.rep(tmp[k - 1], ledNmb / 2 - i) ..
  120. string.rep(tmp[1], 1 + 2 * i) ..
  121. string.rep(k - 1, ledNmb / 2 - i)
  122. elseif k - 1 == 0 then
  123. data = string.rep(tmp[#tmp], ledNmb / 2 - i) ..
  124. string.rep(tmp[k + 1], 1 + 2 * i) ..
  125. string.rep(tmp[#tmp], ledNmb / 2 - i)
  126. else
  127. data = string.rep(tmp[k - 1], ledNmb / 2 - i) ..
  128. string.rep(tmp[k + 1], 1 + 2 * i) ..
  129. string.rep(tmp[k - 1], ledNmb / 2 - i)
  130. end
  131. spi.send(spi.SPI_1, data)
  132. sys.wait(50)
  133. end
  134. k = k + 1
  135. end
  136. end) ]]
  137. -- 声控灯
  138. --[[ require "record"
  139. local result = spi.setup(spi.SPI_1, 0, 0, 8, 20000, 1)
  140. local nm
  141. local function lightUp(num)
  142. if not num then
  143. return
  144. end
  145. local tmp = {}
  146. for i =1, ledNmb do
  147. if i <= num then
  148. table.insert(tmp, i, string.char(255,0,0))
  149. else
  150. table.insert(tmp, i, string.char(0, 0, 0))
  151. end
  152. end
  153. --log.info("这里是测试", table.concat(tmp):toHex())
  154. return table.concat(tmp)
  155. end
  156. record.start(0x7FFFFFFF,function (result,size,tag)
  157. if result and tag=="STREAM" then
  158. local s = audiocore.streamrecordread(size)
  159. --print("s的长度", #s)
  160. if #s < 100 then return end
  161. temp = {}
  162. for i=1,50 do
  163. local d = s:byte(i*2)--低八位数据用不上。。
  164. if d > 127 then d = - d + 256 end
  165. d = d * ledNmb / 127
  166. nm = d
  167. end
  168. end
  169. end,"STREAM",0,1,audiocore.PCM,200)
  170. sys.timerLoopStart(function() spi.send(spi.SPI_1, lightUp(nm)) end, 10) ]]
  171. -- 炫彩声控灯
  172. --[[ spi.setup(spi.SPI_1,0,0,8,20000,1,0)
  173. local function onLight(lights,r,g,b)
  174. local temp = {}
  175. for i=1,#lights do
  176. table.insert(temp,lights[i] and string.char(r,g,b) or string.char(0x00,0x00,0x00))
  177. end
  178. return table.concat(temp)
  179. end
  180. --前n个灯,点亮
  181. local function makeOn(n)
  182. local temp = {}
  183. for i=1,ledNmb do
  184. temp[i] = i<=n
  185. end
  186. return temp
  187. end
  188. --local max = 0
  189. local now = 0
  190. require"record"
  191. record.start(0x7FFFFFFF,function (result,size,tag)
  192. if result and tag=="STREAM" then
  193. local s = audiocore.streamrecordread(size)
  194. if #s < 100 then return end
  195. local tm = 0
  196. for i=1,50 do
  197. local d = s:byte(i*2)--低八位数据用不上。。
  198. if d > 127 then d = - d + 256 end
  199. d = d * ledNmb / 127
  200. if d > tm then tm = d end
  201. end
  202. now = tm
  203. end
  204. end,"STREAM",0,1,audiocore.PCM,100)
  205. sys.timerLoopStart(function()
  206. local nl = makeOn(now)
  207. -- if max > now then
  208. -- max = max - 1
  209. -- end
  210. -- if max < now then
  211. -- max = now
  212. -- end
  213. -- nl[max] = true
  214. --log.info("now,max",now,max)
  215. local r,g,b = 0,0,0
  216. if now <= 10 then
  217. r = 0xff - now * 0xff / 10
  218. b = now * 0xff / 10
  219. elseif now <= 20 then
  220. b = 0xff - (now - 10) * 0xff / 10
  221. g = (now - 10) * 0xff / 10
  222. else
  223. r = (now - 20) * 0xff / 10
  224. if r > 0xff then r = 0xff end
  225. g = 0xff - (now - 20) * 0xff / 10
  226. if g < 0 then g = 0 end
  227. end
  228. spi.send(spi.SPI_1,onLight(nl,r,g,b))
  229. end,20) ]]