fix: 解决netdrv-ch390h在780epm下死机的问题
[LuatOS.git] / script / turnkey / EinkBook / Scripts / main.lua
blob80cf1712527d6ccd37caa7040325eefd313b299a
1 PROJECT = "EinkBook-LuatOS"
2 VERSION = "1.0.0"
3 MOD_TYPE = rtos.bsp()
4 log.info("MOD_TYPE", MOD_TYPE)
5 sys = require("sys")
6 require("sysplus")
7 wifiLib = require("wifiLib")
10 -- 兼容V1001固件的
11 if http == nil and http2 then
12 http = http2
13 end
16 -- 兼容V1001固件的
17 if http == nil and http2 then
18 http = http2
19 end
21 tag = "EINKBOOK"
22 -- 是否启用配网功能(配合esptouch使用)
23 USE_SMARTCONFIG = false
24 -- 这是已有的电纸书服务端地址,如果自己搭建或下面这个服务失效了,请修改
25 serverAdress = "http://47.96.229.157:2333/"
26 -- 改为需要连接的WiFi名称和密码(使用2.4Ghz频段,ESP32C3无法使用5GHz WiFi)
27 SSID, PASSWD = "Xiaomi_AX6000", "Air123456"
29 waitHttpTask, waitDoubleClick = false, false
30 einkPrintTime = 0
31 PAGE, gpage = "LIST", 1
32 onlineBooksTable, onlineBooksShowTable, onlineBooksShowTableTmp, einkBooksIndex, onlineBooksTableLen = {}, {}, {}, 1, 0
33 gBTN, gPressTime, gShortCb, gLongCb, gDoubleCb, gBtnStatus = 0, 1000, nil, nil, nil, "IDLE"
35 function printTable(tbl, lv)
36 lv = lv and lv .. "\t" or ""
37 print(lv .. "{")
38 for k, v in pairs(tbl) do
39 if type(k) == "string" then
40 k = "\"" .. k .. "\""
41 end
42 if "string" == type(v) then
43 local qv = string.match(string.format("%q", v), ".(.*).")
44 v = qv == v and '"' .. v .. '"' or "'" .. v:toHex() .. "'"
45 end
46 if type(v) == "table" then
47 print(lv .. "\t" .. tostring(k) .. " = ")
48 printTable(v, lv)
49 else
51 print(lv .. "\t" .. tostring(k) .. " = " .. tostring(v) .. ",")
52 end
53 end
54 print(lv .. "},")
55 end
57 function getTableSlice(intable, startIndex, endIndex)
58 local outTable = {}
59 for i = startIndex, endIndex do
60 table.insert(outTable, intable[i])
61 end
62 return outTable
63 end
65 function getTableLen(t)
66 local count = 0
67 for _, _ in pairs(t) do
68 count = count + 1
69 end
70 return count
71 end
73 function formatOnlineBooksTable(inTable)
74 local outTable = {}
75 local i = 1
76 for k, v in pairs(inTable) do
77 v["index"] = i
78 table.insert(outTable, {
79 [k] = v
81 i = i + 1
82 end
83 return outTable
84 end
86 function longTimerCb()
87 gBtnStatus = "LONGPRESSED"
88 gLongCb()
89 end
91 function btnHandle(val)
92 if val == 0 then
93 if waitDoubleClick == true then
94 sys.timerStop(gShortCb)
95 gDoubleCb()
96 waitDoubleClick = false
97 return
98 end
99 sys.timerStart(longTimerCb, gPressTime)
100 gBtnStatus = "PRESSED"
101 else
102 sys.timerStop(longTimerCb)
103 if gBtnStatus == "PRESSED" then
104 sys.timerStart(gShortCb, 500)
105 waitDoubleClick = true
106 gBtnStatus = "IDLE"
107 elseif gBtnStatus == "LONGPRESSED" then
108 gBtnStatus = "IDLE"
113 function btnSetup(gpioNumber, pressTime, shortCb, longCb, doubleCb)
114 gpio.setup(gpioNumber, btnHandle, gpio.PULLUP)
115 gPressTime = pressTime
116 gShortCb = shortCb
117 gLongCb = longCb
118 gDoubleCb = doubleCb
121 function showBookList(index)
122 local firstIndex
123 for k, v in pairs(onlineBooksShowTableTmp[1]) do
124 firstIndex = v["index"]
126 if index > firstIndex + 10 then
127 onlineBooksShowTableTmp = getTableSlice(onlineBooksShowTable, index - 10, index)
129 if index < firstIndex then
130 onlineBooksShowTableTmp = getTableSlice(onlineBooksShowTable, index, index + 10)
132 einkShowStr(0, 16, "图书列表", 0, true)
133 local ifShow = false
134 local len = getTableLen(onlineBooksTable)
135 local showLen = getTableLen(onlineBooksShowTableTmp)
136 if len == 0 then
137 einkShowStr(0, 32, "暂无在线图书", 0, false, true)
138 return
140 local i = 1
141 for k, v in pairs(onlineBooksShowTableTmp) do
142 for name, info in pairs(v) do
143 local bookName = string.split(name, ".")[1]
144 local bookSize = tonumber(info["size"]) / 1024 / 1024
145 if i == showLen then
146 ifShow = true
148 if info["index"] == index then
149 eink.rect(0, 16 * i, 200, 16 * (i + 1), 0, 1, nil, ifShow)
150 einkShowStr(0, 16 * (i + 1), bookName .. " " .. string.format("%.2f", bookSize) .. "MB", 1, nil,
151 ifShow)
152 else
153 einkShowStr(0, 16 * (i + 1), bookName .. " " .. string.format("%.2f", bookSize) .. "MB", 0, nil,
154 ifShow)
156 i = i + 1
161 function showBook(bookName, bookUrl, page)
162 sys.taskInit(function()
163 waitHttpTask = true
164 for i = 1, 3 do
165 local code, headers, data = http.request("GET",bookUrl .. "/" .. page).wait()
166 log.info("SHOWBOOK", code)
167 if code ~= 200 then
168 log.error("SHOWBOOK", "获取图书内容失败 ", data)
169 else
170 local bookLines = json.decode(data)
171 for k, v in pairs(bookLines) do
172 if k == 1 then
173 einkShowStr(0, 16 * k, v, 0, true, false)
174 elseif k == #bookLines then
175 einkShowStr(0, 16 * k, v, 0, false, false)
176 else
177 einkShowStr(0, 16 * k, v, 0, false, false)
180 einkShowStr(60, 16 * 12 + 2, page .. "/" .. onlineBooksTable[bookName]["pages"], 0, false, true)
181 break
184 waitHttpTask = false
185 end)
188 function btnShortHandle()
189 if waitHttpTask == true then
190 waitDoubleClick = false
191 return
193 if PAGE == "LIST" then
194 if einkBooksIndex == onlineBooksTableLen then
195 einkBooksIndex = 1
196 else
197 einkBooksIndex = einkBooksIndex + 1
199 showBookList(einkBooksIndex)
200 else
201 local i = 1
202 local bookName = nil
203 for k, v in pairs(onlineBooksTable) do
204 if i == einkBooksIndex then
205 bookName = k
207 i = i + 1
209 local thisBookPages = tonumber(onlineBooksTable[bookName]["pages"])
210 if thisBookPages == gpage then
211 waitDoubleClick = false
212 return
214 gpage = gpage + 1
215 showBook(bookName, serverAdress .. string.urlEncode(bookName), gpage)
216 log.info(bookName, gpage)
217 fdb.kv_set(bookName, gpage)
219 waitDoubleClick = false
222 function btnLongHandle()
223 if waitHttpTask == true then
224 return
226 if PAGE == "LIST" then
227 PAGE = "BOOK"
228 local i = 1
229 local bookName = nil
230 for k, v in pairs(onlineBooksTable) do
231 if i == einkBooksIndex then
232 bookName = k
234 i = i + 1
236 local pageCache = fdb.kv_get(bookName)
237 log.info(bookName, pageCache)
238 if pageCache == nil then
239 gpage = 1
240 showBook(bookName, serverAdress .. string.urlEncode(bookName), gpage)
241 else
242 gpage = pageCache
243 showBook(bookName, serverAdress .. string.urlEncode(bookName), pageCache)
246 elseif PAGE == "BOOK" then
247 PAGE = "LIST"
248 showBookList(einkBooksIndex)
252 function btnDoublehandle()
253 if waitHttpTask == true then
254 return
256 if PAGE == "LIST" then
257 if einkBooksIndex == 1 then
258 einkBooksIndex = onlineBooksTableLen
259 else
260 einkBooksIndex = einkBooksIndex - 1
262 showBookList(einkBooksIndex)
263 else
264 if gpage == 1 then
265 return
267 gpage = gpage - 1
268 local i = 1
269 local bookName = nil
270 for k, v in pairs(onlineBooksTable) do
271 if i == einkBooksIndex then
272 bookName = k
274 i = i + 1
276 log.info(bookName, gpage)
277 fdb.kv_set(bookName, gpage)
278 showBook(bookName, serverAdress .. string.urlEncode(bookName), gpage)
282 function einkShowStr(x, y, str, colored, clear, show)
283 if einkPrintTime > 20 then
284 einkPrintTime = 0
285 eink.rect(0, 0, 200, 200, 0, 1)
286 eink.show(0, 0, true)
287 eink.rect(0, 0, 200, 200, 1, 1)
288 eink.show(0, 0, true)
290 if clear == true then
291 eink.clear()
293 eink.print(x, y, str, colored)
294 if show == true then
295 einkPrintTime = einkPrintTime + 1
296 eink.show(0, 0, true)
300 sys.taskInit(function()
301 assert(fdb.kvdb_init("env", "onchip_flash") == true, tag .. ".kvdb_init ERROR")
302 eink.model(eink.MODEL_1in54)
303 if MOD_TYPE == "AIR101" then
304 eink.setup(1, 0, 16, 19, 17, 20)
305 elseif MOD_TYPE == "ESP32C3" then
306 eink.setup(1, 2, 11, 10, 6, 7)
308 eink.setWin(200, 200, 0)
309 eink.clear(0, true)
310 eink.show(0, 0)
311 eink.clear(1, true)
312 eink.show(0, 0)
313 -- eink.setFont(fonts.get("sarasa_regular_12"))
314 eink.setFont(eink.font_opposansm12_chinese)
316 if USE_SMARTCONFIG == true then
317 einkShowStr(0, 16, "开机中 等待配网...", 0, false, true)
318 local connectRes = wifiLib.connect()
319 if connectRes == false then
320 einkShowStr(0, 16, "配网失败 重启中...", 0, true, true)
321 rtos.reboot()
323 else
324 einkShowStr(0, 16, "开机中...", 0, false, true)
325 local connectRes = wifiLib.connect(SSID, PASSWD)
326 if connectRes == false then
327 einkShowStr(0, 16, "联网失败 重启中...", 0, true, true)
328 rtos.reboot()
331 for i = 1, 5 do
332 local code, headers, data = http.request("GET", serverAdress .. "getBooks").wait()
333 log.info("SHOWBOOK", code)
334 if code ~= 200 then
335 log.error(tag, "获取图书列表失败 ", data)
336 if i == 5 then
337 einkShowStr(0, 16, "连接图书服务器失败 正在重启", 0, true, true)
338 rtos.reboot()
340 else
341 onlineBooksTable = json.decode(data)
342 printTable(onlineBooksTable)
343 onlineBooksTableLen = getTableLen(onlineBooksTable)
344 onlineBooksShowTable = formatOnlineBooksTable(onlineBooksTable)
345 onlineBooksShowTableTmp = getTableSlice(onlineBooksShowTable, 1, 11)
346 showBookList(1)
347 btnSetup(9, 1000, btnShortHandle, btnLongHandle, btnDoublehandle)
348 break
350 sys.wait(1000)
352 end)
354 sys.run()