Browse Source

init project by WifiLightController

master
jkpete 7 months ago
commit
b62ee73eee
  1. 12
      .vscode/settings.json
  2. BIN
      DocRes/1_0.jpg
  3. BIN
      DocRes/1_1.jpg
  4. BIN
      DocRes/1_2.jpg
  5. BIN
      DocRes/1_3.jpg
  6. BIN
      DocRes/1_4.png
  7. BIN
      DocRes/InstallSoft.png
  8. BIN
      DocRes/NodeMcu_Pinout.webp
  9. BIN
      Firmware/nodemcu-release-13-modules-2023-09-16-02-20-47-float.bin
  10. BIN
      Firmware/nodemcu-release-13-modules-2023-09-16-02-20-47-integer.bin
  11. 9
      LICENSE
  12. 30
      OldCode/AutoWifi.lua
  13. 142
      README.md
  14. 6
      SaveData/Config.lua
  15. 1
      SaveData/eus_params.lua
  16. 41
      Src/EasySave.lua
  17. 89
      Src/GPIO.lua
  18. 107
      Src/OLED_SSD1306.lua
  19. 43
      Src/UDPService.lua
  20. 96
      Src/WifiMgr.lua
  21. 13
      Src/init.lua
  22. 23
      Third_Part_License/Json.lua.txt
  23. 364
      Third_Part_License/json.lua

12
.vscode/settings.json vendored

@ -0,0 +1,12 @@
{
"Lua.diagnostics.globals": [
"wifi",
"net",
"u8g2",
"i2c",
"gpio",
"enduser_setup",
"file",
"tmr"
]
}

BIN
DocRes/1_0.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

BIN
DocRes/1_1.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

BIN
DocRes/1_2.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
DocRes/1_3.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

BIN
DocRes/1_4.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

BIN
DocRes/InstallSoft.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

BIN
DocRes/NodeMcu_Pinout.webp

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

BIN
Firmware/nodemcu-release-13-modules-2023-09-16-02-20-47-float.bin

Binary file not shown.

BIN
Firmware/nodemcu-release-13-modules-2023-09-16-02-20-47-integer.bin

Binary file not shown.

9
LICENSE

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2023 jkpete
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

30
OldCode/AutoWifi.lua

@ -0,0 +1,30 @@
--tips to user link-AP
function PageAPNet()
Disp:clearBuffer()
--Disp:setFont(u8g2.font_unifont_t_symbols);
Disp:setFont(u8g2.font_unifont_t_symbols);
Disp:drawStr(0,10,"AP-LinkMode");
Disp:setFont(u8g2.font_6x10_tf);
Disp:drawStr(0,26,"Please use your");
Disp:drawStr(0,36,"mobile phone search");
Disp:drawStr(0,46,"and link ap-point");
Disp:drawStr(0,56,"DY-Light-Controller");
Disp:updateDisplay();
end
--mobile setting ap and passwd
function AutoLinkByMobile()
wifi.sta.clearconfig();
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="DY-Light-Controller", auth=wifi.OPEN});
enduser_setup.manual(true);
enduser_setup.start(
"DY-Light-Controller",
function()
print("Connected to WiFi!")
end,
function(err, str)
print("enduser_setup: Err #" .. err .. ": " .. str)
end
)
print("init endUserServer")
end

142
README.md

@ -0,0 +1,142 @@
# CarController
---
基于NodeMcu开发的ESP8266智能小车控制器,接入SSD1306作为配网以及服务显示功能,后续会增加更多功能
# 1.准备工作🧐
---
## 1.1设备清单
`数据线等烧录用不在此列`
| 设备名称 | 数量 | 设备型号 |
| ----------- | ------ | --------------------- |
| ESP8266 | 1 | ESP-12F,其他ESP8266可用型号 |
| led灯珠 | 4-5个 | 3v3最低耐压 |
| 电线 | 12-16条 | 常用杜邦线或者其他相似参数 |
| I2C协议OLED屏幕 | 1 | SSD1306_12864_noname |
| USB供电(可选) | 1 | 3.3v引脚供电&5VUSB供电 |
## 1.2固件烧录
先从该连接,下载 Flash下载工具(flash_download_tool_3.9.5_0.zip),如果有新版本可能名称会有所不同。
[工具 | 乐鑫科技 ](https://www.espressif.com.cn/zh-hans/support/download/other-tools)
下载完之后,将工程下的flash_download_tool_3.9.5.exe打开
![](DocRes\1_1.jpg)
选择ESP8266
![](DocRes\1_2.jpg)
进入如下界面,点击第一项,选择工程下 Firmware里对应的固件文件,由于程序使用的固件版本为float,但未曾写过使用float类型的程序。int没有测试过,保险起见可以选择float版本,右边填0。选择如下所示,串口检测到后点击对应的串口进行烧录即可。
![](DocRes/1_0.jpg)
烧录的波特率默认为115200,运行以及开发用的波特率同理。
## 1.3程序烧录
首先去github上下载ESPlorer,右侧release可以下载最新的发布版本,可能需要翻墙。
[ESPlorer](https://github.com/4refr0nt/ESPlorer)
下载完成后运行ESPlorer.bat,即可打开该开发工具,然后打开工程的所有lua文件并保存到ESP8266中。目前还处于手动配置网络的阶段,后续会继续优化该配网功能。
拷贝 **Src** 文件夹下的所有脚本文件到ESP8266中。
![](DocRes/InstallSoft.png)
# 2.接线说明🔌
---
NodeMCU开发板引脚图
![](DocRes/NodeMcu_Pinout.webp)
## 2.1 GPIO-LED接线说明
GPIO控制D0-D2,一共三路GPIO供电开关,引出脚需要接入二极管以防止干扰其他控制器工作。
| 接线方式 | LED引脚 | ESP8266引脚 |
| ---- | ----- | --------- |
| 直连 | 负极 | GND |
| 直连 | 正极 | D0 |
| 直连 | 正极 | D1 |
| 直连 | 正极 | D2 |
## 2.2 SSD1306-12864-I2C接线说明
oled显示屏用于显示配网参数以及连接状态,指示灯状态等信息,方便更快找到设备故障原因。
| 接线方式 | SSD1306-12864-I2C引脚 | ESP8266引脚 |
| ---- | ------------------- | --------- |
| 直连 | GND | GND |
| 直连 | VCC | 3v3 |
| 直连 | SCL | D6 |
| 直连 | SDA | D5 |
`如有其他设备需要接线后续补充`
# 3.串口软件网络设置⚙
---
串口软件下载地址[点击下载](https://pan.baidu.com/s/15V7FfWHhwIFyYaKDJv0Rig)
提取码:Node
打开串口软件,NodeMCUSerialTools文件夹下面的NetTools.exe。
以下操作开始之前,请先按照第一步烧录完固件与软件。
ESP8266+数据线与PC进行链接。
或者使用ESP8266+CH340等串口连接设备与PC进行链接
(TX-RX,RX-TX)
![](DocRes/1_3.jpg)
## 3.1选择串口
如果是连接之前打开,可以先检查右下角COM串口列表,连接设备后点击刷新,可以看到新的串口设备在列表中新增。
点击右下角以刷新或者是切换串口设备。
## 3.2设置WiFi连接信息
选项说明
| 名称 | 含义 |
| ------ | --------------- |
| SSID名称 | 手机扫描热点显示的WiFi名称 |
| 密码 | 该热点的WiFi密码 |
写入配网信息
等待2-3秒后,软件会通过串口写入WiFi连接的信息。
如果是首次配网,会自动连接网络。
如果是第二次配网,需要重启并验证网络。
## 3.3设置设备的IP地址
![](DocRes/1_4.png)
选项说明
| 名称 | 含义 |
| ---- | --------------------------------------------------------------------------------------- |
| IP地址 | 跟据平板的网段ip地址,配置好对应网段的地址(如:平板IP地址:192.168.6.15,则该地址只能配置为192.168.6.x,x为2-255之间除了15的任意一个数字) |
| 子网掩码 | 255.255.255.0(默认值,一般不需要修改) |
| 默认网关 | 如果平板网段地址为192.168.6.15,一般情况下网关为192.168.6.1,可以用电脑的cmd->ipconfig来查看相应的网关地址。 |
# 4.通讯协议📡
该设备使用UDP作为通讯协议,UDP地址为上面配置的ip地址,端口号默认为6000。

6
SaveData/Config.lua

@ -0,0 +1,6 @@
Config_Table = {
["IpAddress"]="192.168.1.210",
["Gateway"]="192.168.1.1",
["Version"]="V0.1",
["Netmask"]="255.255.255.0"
}

1
SaveData/eus_params.lua

@ -0,0 +1 @@

41
Src/EasySave.lua

@ -0,0 +1,41 @@
Files = file.list();
if Files["Config.lua"] then
dofile("Config.lua");
end
--Get data from config files
function GetData(key)
if Config_Table[key] ~=nil then
return Config_Table[key];
end
return nil;
end
--Save data to config files
function SaveData(key,value)
file.open("Config.lua", "w+");
Config_Table[key] = value;
local tableStr = TableToStr(Config_Table);
file.write(tableStr);
file.close();
--print(key.."-"..value.."has been Saved");
end
function TableToStr(table)
local stringData = "{\n"
for key, value in pairs(table) do
local keyValue = "[\"" .. tostring(key) .. "\"]="
local valueString = "\"" .. tostring(value) .. "\""
stringData = stringData .. " " .. keyValue .. valueString .. ",\n"
end
stringData = stringData:sub(1, #stringData - 2) .. "\n}\n"
stringData = "Config_Table = "..stringData;
--print(stringData)
return stringData
end
if Files["Config.lua"] == nil then
if Config_Table == nil then
Config_Table = {}
end
SaveData("Version","V0.1");
end

89
Src/GPIO.lua

@ -0,0 +1,89 @@
Is_AutoClose = true;
LightArr = {[1]=false,[2]=false,[3]=false,[4]=false}
--Init GPIO params
function InitGPIO()
gpio.mode(0,gpio.OUTPUT);
gpio.mode(1,gpio.OUTPUT);
gpio.mode(2,gpio.OUTPUT);
gpio.mode(3,gpio.OUTPUT);
gpio.mode(4,gpio.OUTPUT);
gpio.write(0,gpio.LOW);
gpio.write(1,gpio.LOW);
gpio.write(2,gpio.LOW);
gpio.write(3,gpio.LOW);
gpio.write(4,gpio.LOW);
end
--Push gpio to high
function GPIOOn(gpioId)
if gpioId>=0 and gpioId<=4 then
gpio.write(gpioId,gpio.HIGH);
LightArr[gpioId] = true;
PageLight(LightArr);
if Is_AutoClose then
AutoClose(gpioId);
end
end
if gpioId == 99 then
gpio.write(0,gpio.HIGH);
gpio.write(1,gpio.HIGH);
gpio.write(2,gpio.HIGH);
gpio.write(3,gpio.HIGH);
gpio.write(4,gpio.HIGH);
LightArr = {[1]=true,[2]=true,[3]=true,[4]=true}
PageLight(LightArr);
if Is_AutoClose then
AutoCloseAll();
end
end
if gpioId == 10 then
Is_AutoClose = true;
PageLight(LightArr);
end
end
--Push gpio to low
function GPIOOff(gpioId)
if gpioId>=0 and gpioId<=4 then
gpio.write(gpioId,gpio.LOW);
LightArr[gpioId] = false;
PageLight(LightArr);
end
if gpioId == 99 then
gpio.write(0,gpio.LOW);
gpio.write(1,gpio.LOW);
gpio.write(2,gpio.LOW);
gpio.write(3,gpio.LOW);
gpio.write(4,gpio.LOW);
LightArr = {[1]=false,[2]=false,[3]=false,[4]=false}
PageLight(LightArr);
end
if gpioId == 10 then
Is_AutoClose = false;
PageLight(LightArr);
end
end
function AutoClose(gpioId)
local mytimer = tmr.create()
mytimer:register(1000, tmr.ALARM_SINGLE, function()
gpio.write(gpioId,gpio.LOW);
LightArr[gpioId] = false;
PageLight(LightArr);
end)
mytimer:start()
end
function AutoCloseAll()
local mytimer = tmr.create()
mytimer:register(1000, tmr.ALARM_SINGLE, function()
gpio.write(0,gpio.LOW);
gpio.write(1,gpio.LOW);
gpio.write(2,gpio.LOW);
gpio.write(3,gpio.LOW);
gpio.write(4,gpio.LOW);
LightArr = {[1]=false,[2]=false,[3]=false,[4]=false}
PageLight(LightArr);
end)
mytimer:start()
end
InitGPIO();
print("gpio1-5 init over,turn all gpio to output,low")

107
Src/OLED_SSD1306.lua

@ -0,0 +1,107 @@
Id = 0
Sda = 5 -- GPIO14
Scl = 6 -- GPIO12
Sla = 0x3c
i2c.setup(Id, Sda, Scl, i2c.SLOW)
Disp = u8g2.ssd1306_i2c_128x64_noname(Id, Sla)
print("Oled Init Success!");
Screen_WIFINAME = "";
Screen_IPADDRESS = "";
Screen_VERSION = "";
Screen_LightArr = {}
--tips user use pc tools to link-AP
function PageSerialNet()
Disp:clearBuffer()
Disp:setFont(u8g2.font_unifont_t_symbols);
Disp:drawStr(0,10,"Serial-LinkMode");
Disp:setFont(u8g2.font_6x10_tf);
Disp:drawStr(0,26,"Open NetTools.exe ");
Disp:drawStr(0,36,"input ssid and pwd.");
Disp:updateDisplay();
end
--progress:1-100,tips loading and update from server
function PageLoading(progress)
Disp:clearBuffer();
Disp:setFont(u8g2.font_unifont_t_symbols);
Disp:drawStr(40,10,"DYTech");
Disp:setFont(u8g2.font_6x10_tf);
Disp:drawStr(16,32,"Check for update");
Disp:drawFrame(12,42,104,10);
Disp:drawBox(14,44,progress,6);
Disp:updateDisplay();
end
function DrawMainScene(wifiName,ipAddress,version)
--Disp:setFont(u8g2.font_unifont_t_symbols);
Disp:setFont(u8g2.font_unifont_t_symbols);
Disp:drawStr(40,10,"DYTech");
Disp:setFont(u8g2.font_6x10_tf);
Disp:drawStr(0,26,"Wifi:"..wifiName);
Disp:drawStr(0,36,"Light Ctrl-UDP"..version);
Disp:drawStr(0,46,"S:"..ipAddress..":5000");
end
function DrawLight(lightArr)
Disp:drawCircle(16,56,4,u8g2.U8G2_DRAW_ALL);
Disp:drawCircle(32,56,4,u8g2.U8G2_DRAW_ALL);
Disp:drawCircle(48,56,4,u8g2.U8G2_DRAW_ALL);
Disp:drawCircle(64,56,4,u8g2.U8G2_DRAW_ALL);
Screen_LightArr = lightArr;
for index, value in ipairs(lightArr) do
if value then
Disp:drawDisc(16*index,56,4,u8g2.U8G2_DRAW_ALL);
end
end
if Is_AutoClose ~= nil then
if Is_AutoClose then
Disp:setFont(u8g2.font_6x10_tf);
Disp:drawStr(84,64,"Auto");
else
Disp:setFont(u8g2.font_6x10_tf);
Disp:drawStr(84,64,"Manual");
end
end
end
function PageMainScene(wifiName,ipAddress,version)
Screen_WIFINAME = wifiName;
Screen_IPADDRESS = ipAddress;
Screen_VERSION = version;
Disp:clearBuffer();
DrawMainScene(Screen_WIFINAME,Screen_IPADDRESS,Screen_VERSION)
Disp:updateDisplay();
--Disp:sendBuffer();
end
function PageLight(lightArr)
Disp:clearBuffer();
DrawMainScene(Screen_WIFINAME,Screen_IPADDRESS,Screen_VERSION);
DrawLight(lightArr);
Disp:sendBuffer();
end
function RefeshIP(ipAddress)
Screen_IPADDRESS = ipAddress;
Disp:clearBuffer();
DrawMainScene(Screen_WIFINAME,Screen_IPADDRESS,Screen_VERSION);
DrawLight(Screen_LightArr);
Disp:sendBuffer();
end
function PageConnecting()
Disp:clearBuffer()
Disp:setFont(u8g2.font_6x10_tf);
Disp:drawStr(0,10,"Connecting to internet");
Disp:drawStr(0,36,"Waiting...");
Disp:updateDisplay();
end
function PageConnectFaild(reason)
Disp:clearBuffer()
Disp:setFont(u8g2.font_unifont_t_symbols);
Disp:drawStr(0,10,"Connect Failed");
Disp:setFont(u8g2.font_6x10_tf);
Disp:drawStr(0,26,reason);
Disp:updateDisplay();
end

43
Src/UDPService.lua

@ -0,0 +1,43 @@
require("GPIO");
function StartUDPService()
if UDPSocket == nil then
UDPSocket = net.createUDPSocket();
UDPSocket:listen(5000);
UDPSocket:on("receive", UDPService);
UDPPort, UDPIP = UDPSocket:getaddr();
else
UDPSocket:listen(5000);
UDPSocket:on("receive", UDPService);
UDPPort, UDPIP = UDPSocket:getaddr();
end
--print(string.format("local UDP socket address / port: %s:%d", UDPIP, UDPPort))
end
function UDPService(s, data, port, ip)
local commandSet = {};
for word in string.gmatch(data, "[^-]+") do
table.insert(commandSet,word);
end
print("get data count" .. #commandSet);
if string.match(data,"^Light") then
if #commandSet==3 then
print(tonumber(commandSet[2]).."-"..commandSet[3])
if commandSet[3]=="On" and tonumber(commandSet[2])~=nil then
GPIOOn(tonumber(commandSet[2]));
s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]);
elseif commandSet[3]=="Off" and tonumber(commandSet[2])~=nil then
GPIOOff(tonumber(commandSet[2]));
s:send(port, ip, "Light "..commandSet[2].." "..commandSet[3]);
else
s:send(port, ip, "CommandFaild");
end
end
end
end
function StopUDPService()
if UDPSocket ~= nil then
UDPSocket:close();
end
end
--StartUDPService();

96
Src/WifiMgr.lua

@ -0,0 +1,96 @@
StationCfg = {}
IpCfg = {}
--Load Config.lua and get value from Config_Table
function AutoLink()
if GetData("SSID") ~= nil or GetData("Password") ~= nil then
--WifiSaved = dofile("eus_params.lua");
ManualLink(GetData("SSID"),GetData("Password"));
else
PageSerialNet();
return nil;
end
end
function ManualLink(ssid,passwd)
wifi.setmode(wifi.STATION)
StationCfg.ssid=ssid
StationCfg.pwd=passwd
StationCfg.save=false
wifi.sta.config(StationCfg)
wifi.sta.connect()
if wifi.sta.getip() == nil then
SetIPFromFile();
end
PageConnecting();
--OnLinkSucess();
print(wifi.sta.status());
--regist
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T)
OnLinkSucess();
end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
OnLinkFailed(T.reason);
end)
end
function SetIP(ipAddress,netmask,gateway)
IpCfg = {
ip = ipAddress,
netmask = netmask,
gateway = gateway
}
wifi.sta.setip(IpCfg)
end
--SetIP("192.168.1.115","255.255.255.0","192.168.1.1")
function SaveIP(ipAddress,netmask,gateway)
SaveData("IpAddress",ipAddress);
SaveData("Netmask",netmask);
SaveData("Gateway",gateway);
SetIP(ipAddress,netmask,gateway);
RefeshIP(ipAddress);
end
function SaveSoftVersion(version)
SaveData("Version",version);
end
function SaveWifiInfo(ssid,passwd)
if GetData("SSID") == nil or GetData("Password") == nil then
SaveData("SSID",ssid);
SaveData("Password",passwd);
AutoLink();
else
SaveData("SSID",ssid);
SaveData("Password",passwd);
end
end
function SetIPFromFile()
if Config_Table["IpAddress"]~=nil and Config_Table["Netmask"]~=nil and Config_Table["Gateway"]~=nil then
IpCfg = {
ip = Config_Table["IpAddress"],
netmask = Config_Table["Netmask"],
gateway = Config_Table["Gateway"]
}
wifi.sta.setip(IpCfg);
else
SetIP("192.168.1.210","255.255.255.0","192.168.1.1")
print("No Ip get,default to 192.168.1.210!");
end
end
function OnLinkSucess()
PageMainScene(StationCfg.ssid,wifi.sta.getip(),Config_Table["Version"]);
local lightArr = {[1]=false,[2]=false,[3]=false,[4]=false}
PageLight(lightArr)
--Service Open
StartUDPService();
end
function OnLinkFailed(reason)
PageConnectFaild(reason);
end

13
Src/init.lua

@ -0,0 +1,13 @@
--Start with this file
--这个文件初始化会执行
require("EasySave")
require("GPIO")
require("OLED_SSD1306")
require("WifiMgr")
require("UDPService")
function Init()
--SetIP("192.168.1.115","255.255.255.0","192.168.1.1");
AutoLink();
--StartUDPService();
end
Init();

23
Third_Part_License/Json.lua.txt

@ -0,0 +1,23 @@
--
-- json.lua
--
-- Copyright (c) 2020 rxi
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
--

364
Third_Part_License/json.lua

@ -0,0 +1,364 @@
local json = { _version = "0.1.2" }
-------------------------------------------------------------------------------
-- Encode
-------------------------------------------------------------------------------
local encode
local escape_char_map = {
[ "\\" ] = "\\",
[ "\"" ] = "\"",
[ "\b" ] = "b",
[ "\f" ] = "f",
[ "\n" ] = "n",
[ "\r" ] = "r",
[ "\t" ] = "t",
}
local escape_char_map_inv = { [ "/" ] = "/" }
for k, v in pairs(escape_char_map) do
escape_char_map_inv[v] = k
end
local function escape_char(c)
return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte()))
end
local function encode_nil(val)
return "null"
end
local function encode_table(val, stack)
local res = {}
stack = stack or {}
-- Circular reference?
if stack[val] then error("circular reference") end
stack[val] = true
if rawget(val, 1) ~= nil or next(val) == nil then
-- Treat as array -- check keys are valid and it is not sparse
local n = 0
for k in pairs(val) do
if type(k) ~= "number" then
error("invalid table: mixed or invalid key types")
end
n = n + 1
end
if n ~= #val then
error("invalid table: sparse array")
end
-- Encode
for i, v in ipairs(val) do
table.insert(res, encode(v, stack))
end
stack[val] = nil
return "[" .. table.concat(res, ",") .. "]"
else
-- Treat as an object
for k, v in pairs(val) do
if type(k) ~= "string" then
error("invalid table: mixed or invalid key types")
end
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
end
stack[val] = nil
return "{" .. table.concat(res, ",") .. "}"
end
end
local function encode_string(val)
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end
local function encode_number(val)
-- Check for NaN, -inf and inf
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
end
return string.format("%.14g", val)
end
local type_func_map = {
[ "nil" ] = encode_nil,
[ "table" ] = encode_table,
[ "string" ] = encode_string,
[ "number" ] = encode_number,
[ "boolean" ] = tostring,
}
encode = function(val, stack)
local t = type(val)
local f = type_func_map[t]
if f then
return f(val, stack)
end
error("unexpected type '" .. t .. "'")
end
function json.encode(val)
return ( encode(val) )
end
-------------------------------------------------------------------------------
-- Decode
-------------------------------------------------------------------------------
local parse
local function create_set(...)
local res = {}
for i = 1, select("#", ...) do
res[ select(i, ...) ] = true
end
return res
end
local space_chars = create_set(" ", "\t", "\r", "\n")
local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
local literals = create_set("true", "false", "null")
local literal_map = {
[ "true" ] = true,
[ "false" ] = false,
[ "null" ] = nil,
}
local function next_char(str, idx, set, negate)
for i = idx, #str do
if set[str:sub(i, i)] ~= negate then
return i
end
end
return #str + 1
end
local function decode_error(str, idx, msg)
local line_count = 1
local col_count = 1
for i = 1, idx - 1 do
col_count = col_count + 1
if str:sub(i, i) == "\n" then
line_count = line_count + 1
col_count = 1
end
end
error( string.format("%s at line %d col %d", msg, line_count, col_count) )
end
local function codepoint_to_utf8(n)
-- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
local f = math.floor
if n <= 0x7f then
return string.char(n)
elseif n <= 0x7ff then
return string.char(f(n / 64) + 192, n % 64 + 128)
elseif n <= 0xffff then
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
elseif n <= 0x10ffff then
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
f(n % 4096 / 64) + 128, n % 64 + 128)
end
error( string.format("invalid unicode codepoint '%x'", n) )
end
local function parse_unicode_escape(s)
local n1 = tonumber( s:sub(1, 4), 16 )
local n2 = tonumber( s:sub(7, 10), 16 )
-- Surrogate pair?
if n2 then
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
else
return codepoint_to_utf8(n1)
end
end
local function parse_string(str, i)
local res = ""
local j = i + 1
local k = j
while j <= #str do
local x = str:byte(j)
if x < 32 then
decode_error(str, j, "control character in string")
elseif x == 92 then -- `\`: Escape
res = res .. str:sub(k, j - 1)
j = j + 1
local c = str:sub(j, j)
if c == "u" then
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string")
res = res .. parse_unicode_escape(hex)
j = j + #hex
else
if not escape_chars[c] then
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
end
res = res .. escape_char_map_inv[c]
end
k = j + 1
elseif x == 34 then -- `"`: End of string
res = res .. str:sub(k, j - 1)
return res, j + 1
end
j = j + 1
end
decode_error(str, i, "expected closing quote for string")
end
local function parse_number(str, i)
local x = next_char(str, i, delim_chars)
local s = str:sub(i, x - 1)
local n = tonumber(s)
if not n then
decode_error(str, i, "invalid number '" .. s .. "'")
end
return n, x
end
local function parse_literal(str, i)
local x = next_char(str, i, delim_chars)
local word = str:sub(i, x - 1)
if not literals[word] then
decode_error(str, i, "invalid literal '" .. word .. "'")
end
return literal_map[word], x
end
local function parse_array(str, i)
local res = {}
local n = 1
i = i + 1
while 1 do
local x
i = next_char(str, i, space_chars, true)
-- Empty / end of array?
if str:sub(i, i) == "]" then
i = i + 1
break
end
-- Read token
x, i = parse(str, i)
res[n] = x
n = n + 1
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "]" then break end
if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
end
return res, i
end
local function parse_object(str, i)
local res = {}
i = i + 1
while 1 do
local key, val
i = next_char(str, i, space_chars, true)
-- Empty / end of object?
if str:sub(i, i) == "}" then
i = i + 1
break
end
-- Read key
if str:sub(i, i) ~= '"' then
decode_error(str, i, "expected string for key")
end
key, i = parse(str, i)
-- Read ':' delimiter
i = next_char(str, i, space_chars, true)
if str:sub(i, i) ~= ":" then
decode_error(str, i, "expected ':' after key")
end
i = next_char(str, i + 1, space_chars, true)
-- Read value
val, i = parse(str, i)
-- Set
res[key] = val
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "}" then break end
if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
end
return res, i
end
local char_func_map = {
[ '"' ] = parse_string,
[ "0" ] = parse_number,
[ "1" ] = parse_number,
[ "2" ] = parse_number,
[ "3" ] = parse_number,
[ "4" ] = parse_number,
[ "5" ] = parse_number,
[ "6" ] = parse_number,
[ "7" ] = parse_number,
[ "8" ] = parse_number,
[ "9" ] = parse_number,
[ "-" ] = parse_number,
[ "t" ] = parse_literal,
[ "f" ] = parse_literal,
[ "n" ] = parse_literal,
[ "[" ] = parse_array,
[ "{" ] = parse_object,
}
parse = function(str, idx)
local chr = str:sub(idx, idx)
local f = char_func_map[chr]
if f then
return f(str, idx)
end
decode_error(str, idx, "unexpected character '" .. chr .. "'")
end
function json.decode(str)
if type(str) ~= "string" then
error("expected argument of type string, got " .. type(str))
end
local res, idx = parse(str, next_char(str, 1, space_chars, true))
idx = next_char(str, idx, space_chars, true)
if idx <= #str then
decode_error(str, idx, "trailing garbage")
end
return res
end
return json
Loading…
Cancel
Save