-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScratchLua.lua
128 lines (112 loc) · 3.69 KB
/
ScratchLua.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
print("====Wicon, a LUA console over wifi.==========")
print("Author: [email protected]. copyright&GPL V2.")
print("Last modified SW 30Sep15 V0.0.4")
print("Waiting for connection")
gpiolookup = {[0]=3,[1]=10,[2]=4,[3]=9,[4]=1,[5]=2,[10]=12,[12]=6,[13]=7,[14]=5,[15]=8,[16]=0};
stripPin = 2 -- Comment: GPIO5
wsg = 0
wsr = 0
wsb = 0
sparkleTable ={"ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC","ABC"}
function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
function connected(conn)
print("Scratch connected")
function s_output(str)
if (conn~=nil) then
conn:send(str)
end
end
--node.output(s_output,0)
conn:on("receive", function(conn, pl)
--node.input(pl)
msg = string.sub(pl,5,-1)
print ("msg received:" .. msg)
if string.sub(msg,1,9) == "broadcast" then
word=msg:match('%"(%a+)%"')
msg = string.gsub(msg, "%s+", "")
quote2 = string.find(msg, '"', 11 )
pkt = string.sub(msg,11,quote2 - 1)
print ("pkt:" .. pkt)
if string.sub(pkt,1,4) == "gpio" then
gpin = 15
state = gpio.LOW
if string.sub(pkt,-2,-1) == "on" then
state = gpio.HIGH
print ("gpin:" .. string.sub(pkt,5,-3))
gpin = tonumber(string.sub(pkt,5,-3))
end
if string.sub(pkt,-3,-1) == "off" then
state = gpio.LOW
print ("gpin:" .. string.sub(pkt,5,-4))
gpin = tonumber(string.sub(pkt,5,-4))
end
gpio.write(gpiolookup[gpin],state);
end
if string.sub(pkt,1,7) == "sparkle" then
--wsnum = tonumber(string.sub(pkt,8,9))
wsrgb = string.sub(pkt,8,-1)
print ("wsrgb" .. wsrgb)
myTable = split(wsrgb,",")
sparkleNum = tonumber(myTable[1])
r = tonumber(myTable[2])
g = tonumber(myTable[3])
b = tonumber(myTable[4])
print (myTable[1])
print (myTable[2])
print (myTable[3])
print (myTable[4])
sparkleTable[sparkleNum] = string.char(r, g, b)
--print (sparkleTable)
rgb = ""
for loop = 1,30 do
rgb = (rgb .. sparkleTable[loop])
end
ws2812.writergb(stripPin, rgb)
end
end
end)
conn:on("disconnection",function(conn)
print ("")
print ("DISCONNECTED")
print ("")
end)
end
function startServer()
sv=net.createServer(net.TCP, 3600)
sv:listen(42001, connected)
print("Server running at :42001")
print("")
end
print("Setting the device up as a STATION")
wifi.setmode(wifi.STATION)
print("Connecting to the AP")
wifi.sta.config("CYCY", "")
wifi.sta.connect()
print("Waiting a little bit to get an IP address...")
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()=="0.0.0.0" or wifi.sta.getip() == nil then
print("Waiting for IP ...")
else
print("Wifi AP connected. IP:")
print(wifi.sta.getip())
startServer()
tmr.stop(1)
end
end)