Skip to content

Commit

Permalink
Made the code in the same format as smart fan, add service to blink a…
Browse files Browse the repository at this point in the history
…ll leds
nikhildps committed May 18, 2015

Verified

This commit was signed with the committer’s verified signature.
antonbaliasnikov Anton Baliasnikov
1 parent 3a5d232 commit b720b04
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions demo/startershieldLEDlamp.lua
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
require "cord"
require "svcd"
sh = require("stormsh")
shield = require("starter")
shield.LED.start()

green = 0
sh = require "stormsh"

function buzz()
cord.new(function()
shield.Buzz.go()
cord.await(storm.os.invokeLater, 100*storm.os.MILLISECOND)
shield.Buzz.stop()
end)
end
shield.LED.start()

actFan = function(state)
if state == 1 then
green = 1
shield.LED.on("green")
else
green = 0
shield.LED.off("green")
end
buzz()
color = 0
function blinker(color, count)
cord.new(function()
local i = 0
while (i < count) do
shield.LED.on(color)
cord.await(storm.os.invokeLater, 1*storm.os.SECOND)
shield.LED.off(color)
cord.await(storm.os.invokeLater, 1*storm.os.SECOND)
i = i + 1
end
end)

end

sock = storm.net.udpsocket(1234, function(payload, from, port)
print (string.format("from %s port %d: %s",from,port,payload))
actFan(storm.mp.unpack(payload))
end)
-- start a coroutine that provides a REPL
sh.start()

-- cord.new(function()
-- storm.os.invokePeriodically(1 * storm.os.SECOND, function()
-- cord.new( function()
-- storm.net.sendto(sock, storm.mp.pack(green), "2001:470:66:3f9::2", 1234)
-- end)
-- end)
-- end)

SVCD.init("firestormSensor", function()
print "starting SVCD"
SVCD.add_service(0x300e)
SVCD.add_attribute(0x300e, 0x401a, function(pay, ip, port)
print (pay)
actFan(pay)
MyDeviceName = "StarterShield"

cord.new(function()
cord.await(SVCD.init, MyDeviceName)
-- or add an echo service that will appear on 15.4 and bluetooth
-- the service numbers are listed in the manifest:
-- https://github.com/UCB-IoET/svc/blob/master/manifest.json
-- Feel free to add new ones by sending pull requests
SVCD.add_service(0x3002)
-- Attributes are similarly listed in the manifest
SVCD.add_attribute(0x3002, 0x4002, function(value)
local ps = storm.array.fromstr(value)
local lednum = ps:get(1)
color = lednum
local flashcount = ps:get(2)
print ("got a request to flash led",lednum, " x ", flashcount)
if (lednum == 0) then
blinker("red", flashcount)
elseif (lednum == 1) then
blinker("red2", flashcount)
elseif (lednum == 2) then
blinker("green", flashcount)
elseif (lednum == 3) then
blinker("blue", flashcount)
end
end)
cord.new(function()
while true do
SVCD.notify(0x300e, 0x401a, green)
cord.await(storm.os.invokeLater, storm.os.SECOND)
end

storm.os.invokePeriodically(storm.os.SECOND, function()
SVCD.notify(0x3002, 0x4002, color)
end)
end)



sh.start()
-- enter the main event loop. This puts the processor to sleep
-- in between events
cord.enter_loop()

0 comments on commit b720b04

Please sign in to comment.