From 6d3d95a2710106efb8be1f997e56934ff014fd63 Mon Sep 17 00:00:00 2001 From: Lukas Schauer Date: Fri, 6 Feb 2015 04:41:17 +0100 Subject: [PATCH 1/4] Only set ESPTOOL if not set before This allows setting the path to esptool.py as an option on make, allowing the use of alternative tools, or modified esptool.py versions. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6b4a545b..7de24a3b 100644 --- a/Makefile +++ b/Makefile @@ -75,7 +75,7 @@ else endif endif ############################################################# -ESPTOOL = ../tools/esptool.py +ESPTOOL ?= ../tools/esptool.py CSRCS ?= $(wildcard *.c) From 40874baa2a69b5ddf65108de3d3edbcadc941ce3 Mon Sep 17 00:00:00 2001 From: Lukas Schauer Date: Fri, 6 Feb 2015 04:44:47 +0100 Subject: [PATCH 2/4] Use python2 instead of python(2/3) On modern systems "python" refers to python3, and the script won't work, so I suggest changing it to python2 with this patch. This patch shouldn't break compatibility with older systems. --- tools/esptool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/esptool.py b/tools/esptool.py index 130e80f0..c6027eb7 100755 --- a/tools/esptool.py +++ b/tools/esptool.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 # # ESP8266 ROM Bootloader Utility # https://github.com/themadinventor/esptool From 165e91e60da97d60ef4c1fa0e55abac90b10b813 Mon Sep 17 00:00:00 2001 From: Vladimir Dronnikov Date: Fri, 6 Feb 2015 22:22:09 +0300 Subject: [PATCH 3/4] switch require() to dofile() to ease memory cleanup --- lua_examples/irsend.lua | 2 +- lua_examples/yet-another-bmp085.lua | 27 ++++++++++----------------- lua_examples/yet-another-dht22.lua | 4 ++-- lua_examples/yet-another-ds18b20.lua | 2 +- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lua_examples/irsend.lua b/lua_examples/irsend.lua index 4ccd2ebb..6469a4c0 100644 --- a/lua_examples/irsend.lua +++ b/lua_examples/irsend.lua @@ -5,7 +5,7 @@ -- Vladimir Dronnikov -- -- Example: --- require("irsend").nec(4, 0x00ff00ff) +-- dofile("irsend.lua").nec(4, 0x00ff00ff) ------------------------------------------------------------------------------ local M do diff --git a/lua_examples/yet-another-bmp085.lua b/lua_examples/yet-another-bmp085.lua index 5de2de09..d9b94202 100644 --- a/lua_examples/yet-another-bmp085.lua +++ b/lua_examples/yet-another-bmp085.lua @@ -6,7 +6,7 @@ -- Heavily based on work of Christee -- -- Example: --- require("bmp085").read(sda, scl) +-- dofile("bmp085.lua").read(sda, scl) ------------------------------------------------------------------------------ local M do @@ -59,20 +59,13 @@ do MD = r16(0xBE) end -- get raw P - local p - -- NB: optimize for oss = 0 if not oss then oss = 0 end - if oss == 0 then - oss = 0 - w8(0xF4, 0x34) - tmr.delay(5000) - p = r8(0xF6) * 256 + r8(0xF7) - else - w8(0xF4, 0x34 + 64 * oss) - tmr.delay(30000) - p = r8(0xF6) * 65536 + r8(0xF7) * 256 + r8(0xF8) - p = p / 2^(8 - oss) - end + if oss <= 0 then oss = 0 end + if oss > 3 then oss = 3 end + w8(0xF4, 0x34 + 64 * oss) + tmr.delay((4 + 3 ^ oss) * 1000) + local p = r8(0xF6) * 65536 + r8(0xF7) * 256 + r8(0xF8) + p = p / 2 ^ (8 - oss) -- get T w8(0xF4, 0x2E) tmr.delay(5000) @@ -86,14 +79,14 @@ do local X1 = B2 * (B6 * B6 / 4096) / 2048 local X2 = AC2 * B6 / 2048 local X3 = X1 + X2 - local B3 = ((AC1 * 4 + X3) * 2^oss + 2) / 4 + local B3 = ((AC1 * 4 + X3) * 2 ^ oss + 2) / 4 X1 = AC3 * B6 / 8192 X2 = (B1 * (B6 * B6 / 4096)) / 65536 X3 = (X1 + X2 + 2) / 4 local B4 = AC4 * (X3 + 32768) / 32768 - local B7 = (p - B3) * (50000 / 2^oss) + local B7 = (p - B3) * (50000 / 2 ^ oss) p = B7 / B4 * 2 - X1 = (p / 256)^2 + X1 = (p / 256) ^ 2 X1 = (X1 * 3038) / 65536 X2 = (-7357 * p) / 65536 p = p + (X1 + X2 + 3791) / 16 diff --git a/lua_examples/yet-another-dht22.lua b/lua_examples/yet-another-dht22.lua index 1bf65458..90ac8d51 100644 --- a/lua_examples/yet-another-dht22.lua +++ b/lua_examples/yet-another-dht22.lua @@ -5,8 +5,8 @@ -- Vladimir Dronnikov -- -- Example: --- print("DHT11", require("dht22").read(4)) --- print("DHT22", require("dht22").read(4, true)) +-- print("DHT11", dofile("dht22.lua").read(4)) +-- print("DHT22", dofile("dht22.lua").read(4, true)) -- NB: the very first read sometimes fails ------------------------------------------------------------------------------ local M diff --git a/lua_examples/yet-another-ds18b20.lua b/lua_examples/yet-another-ds18b20.lua index e02a2fe9..b6c225c5 100644 --- a/lua_examples/yet-another-ds18b20.lua +++ b/lua_examples/yet-another-ds18b20.lua @@ -5,7 +5,7 @@ -- Vladimir Dronnikov -- -- Example: --- require("ds18b20").read(4, function(r) for k, v in pairs(r) do print(k, v) end end) +-- dofile("ds18b20.lua").read(4, function(r) for k, v in pairs(r) do print(k, v) end end) ------------------------------------------------------------------------------ local M do From edc7eaf3ff14685c98d0123b54e2bcde8e00d705 Mon Sep 17 00:00:00 2001 From: Vladimir Dronnikov Date: Fri, 6 Feb 2015 22:22:37 +0300 Subject: [PATCH 4/4] redis client module added. pubsub so far --- lua_modules/redis/redis.lua | 83 +++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 lua_modules/redis/redis.lua diff --git a/lua_modules/redis/redis.lua b/lua_modules/redis/redis.lua new file mode 100644 index 00000000..cc4bf64d --- /dev/null +++ b/lua_modules/redis/redis.lua @@ -0,0 +1,83 @@ +------------------------------------------------------------------------------ +-- Redis client module +-- +-- LICENCE: http://opensource.org/licenses/MIT +-- Vladimir Dronnikov +-- +-- Example: +-- local redis = dofile("redis.lua").connect(host, port) +-- redis:publish("chan1", foo") +-- redis:subscribe("chan1", function(channel, msg) print(channel, msg) end) +------------------------------------------------------------------------------ +local M +do + -- const + local REDIS_PORT = 6379 + -- cache + local pairs, tonumber = pairs, tonumber + -- + local publish = function(self, chn, s) + self._fd:send(("*3\r\n$7\r\npublish\r\n$%d\r\n%s\r\n$%d\r\n%s\r\n"):format( + #chn, chn, #s, s + )) + -- TODO: confirmation? then queue of answers needed + end + local subscribe = function(self, chn, handler) + -- TODO: subscription to all channels, with single handler + self._fd:send(("*2\r\n$9\r\nsubscribe\r\n$%d\r\n%s\r\n"):format( + #chn, chn + )) + self._handlers[chn] = handler + -- TODO: confirmation? then queue of answers needed + end + local unsubscribe = function(self, chn) + self._handlers[chn] = false + end + -- NB: pity we can not just augment what net.createConnection returns + local close = function(self) + self._fd:close() + end + local connect = function(host, port) + local _fd = net.createConnection(net.TCP, 0) + local self = { + _fd = _fd, + _handlers = { }, + -- TODO: consider metatables? + close = close, + publish = publish, + subscribe = subscribe, + unsubscribe = unsubscribe, + } + _fd:on("connection", function() + --print("+FD") + end) + _fd:on("disconnection", function() + -- FIXME: this suddenly occurs. timeout? + --print("-FD") + end) + _fd:on("receive", function(fd, s) + --print("IN", s) + -- TODO: subscription to all channels + -- lookup message pattern to determine channel and payload + -- NB: pairs() iteration gives no fixed order! + for chn, handler in pairs(self._handlers) do + local p = ("*3\r\n$7\r\nmessage\r\n$%d\r\n%s\r\n$"):format(#chn, chn) + if s:find(p, 1, true) then + -- extract and check message length + -- NB: only the first TCP packet considered! + local _, start, len = s:find("(%d-)\r\n", #p) + if start and tonumber(len) == #s - start - 2 and handler then + handler(chn, s:sub(start + 1, -2)) -- ends with \r\n + end + end + end + end) + _fd:connect(port or REDIS_PORT, host) + return self + end + -- expose + M = { + connect = connect, + } +end +return M