1 PROJECT
= "EinkBook-LuatOS"
4 log.info("MOD_TYPE", MOD_TYPE
)
7 wifiLib
= require("wifiLib")
11 if http
== nil and http2
then
17 if http
== nil and http2
then
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
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 ""
38 for k
, v
in pairs(tbl
) do
39 if type(k
) == "string" then
42 if "string" == type(v
) then
43 local qv
= string.match(string.format("%q", v
), ".(.*).")
44 v
= qv
== v
and '"' .. v
.. '"' or "'" .. v
:toHex() .. "'"
46 if type(v
) == "table" then
47 print(lv
.. "\t" .. tostring(k
) .. " = ")
51 print(lv
.. "\t" .. tostring(k
) .. " = " .. tostring(v
) .. ",")
57 function getTableSlice(intable
, startIndex
, endIndex
)
59 for i
= startIndex
, endIndex
do
60 table.insert(outTable
, intable
[i
])
65 function getTableLen(t
)
67 for _
, _
in pairs(t
) do
73 function formatOnlineBooksTable(inTable
)
76 for k
, v
in pairs(inTable
) do
78 table.insert(outTable
, {
86 function longTimerCb()
87 gBtnStatus
= "LONGPRESSED"
91 function btnHandle(val
)
93 if waitDoubleClick
== true then
94 sys
.timerStop(gShortCb
)
96 waitDoubleClick
= false
99 sys
.timerStart(longTimerCb
, gPressTime
)
100 gBtnStatus
= "PRESSED"
102 sys
.timerStop(longTimerCb
)
103 if gBtnStatus
== "PRESSED" then
104 sys
.timerStart(gShortCb
, 500)
105 waitDoubleClick
= true
107 elseif gBtnStatus
== "LONGPRESSED" then
113 function btnSetup(gpioNumber
, pressTime
, shortCb
, longCb
, doubleCb
)
114 gpio
.setup(gpioNumber
, btnHandle
, gpio
.PULLUP
)
115 gPressTime
= pressTime
121 function showBookList(index
)
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)
134 local len
= getTableLen(onlineBooksTable
)
135 local showLen
= getTableLen(onlineBooksShowTableTmp
)
137 einkShowStr(0, 32, "暂无在线图书", 0, false, true)
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
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,
153 einkShowStr(0, 16 * (i
+ 1), bookName
.. " " .. string.format("%.2f", bookSize
) .. "MB", 0, nil,
161 function showBook(bookName
, bookUrl
, page
)
162 sys
.taskInit(function()
165 local code
, headers
, data
= http
.request("GET",bookUrl
.. "/" .. page
).wait()
166 log.info("SHOWBOOK", code
)
168 log.error("SHOWBOOK", "获取图书内容失败 ", data
)
170 local bookLines
= json
.decode(data
)
171 for k
, v
in pairs(bookLines
) do
173 einkShowStr(0, 16 * k
, v
, 0, true, false)
174 elseif k
== #bookLines
then
175 einkShowStr(0, 16 * k
, v
, 0, false, false)
177 einkShowStr(0, 16 * k
, v
, 0, false, false)
180 einkShowStr(60, 16 * 12 + 2, page
.. "/" .. onlineBooksTable
[bookName
]["pages"], 0, false, true)
188 function btnShortHandle()
189 if waitHttpTask
== true then
190 waitDoubleClick
= false
193 if PAGE
== "LIST" then
194 if einkBooksIndex
== onlineBooksTableLen
then
197 einkBooksIndex
= einkBooksIndex
+ 1
199 showBookList(einkBooksIndex
)
203 for k
, v
in pairs(onlineBooksTable
) do
204 if i
== einkBooksIndex
then
209 local thisBookPages
= tonumber(onlineBooksTable
[bookName
]["pages"])
210 if thisBookPages
== gpage
then
211 waitDoubleClick
= false
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
226 if PAGE
== "LIST" then
230 for k
, v
in pairs(onlineBooksTable
) do
231 if i
== einkBooksIndex
then
236 local pageCache
= fdb
.kv_get(bookName
)
237 log.info(bookName
, pageCache
)
238 if pageCache
== nil then
240 showBook(bookName
, serverAdress
.. string.urlEncode(bookName
), gpage
)
243 showBook(bookName
, serverAdress
.. string.urlEncode(bookName
), pageCache
)
246 elseif PAGE
== "BOOK" then
248 showBookList(einkBooksIndex
)
252 function btnDoublehandle()
253 if waitHttpTask
== true then
256 if PAGE
== "LIST" then
257 if einkBooksIndex
== 1 then
258 einkBooksIndex
= onlineBooksTableLen
260 einkBooksIndex
= einkBooksIndex
- 1
262 showBookList(einkBooksIndex
)
270 for k
, v
in pairs(onlineBooksTable
) do
271 if i
== einkBooksIndex
then
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
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
293 eink
.print(x
, y
, str
, colored
)
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)
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)
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)
332 local code
, headers
, data
= http
.request("GET", serverAdress
.. "getBooks").wait()
333 log.info("SHOWBOOK", code
)
335 log.error(tag, "获取图书列表失败 ", data
)
337 einkShowStr(0, 16, "连接图书服务器失败 正在重启", 0, true, true)
341 onlineBooksTable
= json
.decode(data
)
342 printTable(onlineBooksTable
)
343 onlineBooksTableLen
= getTableLen(onlineBooksTable
)
344 onlineBooksShowTable
= formatOnlineBooksTable(onlineBooksTable
)
345 onlineBooksShowTableTmp
= getTableSlice(onlineBooksShowTable
, 1, 11)
347 btnSetup(9, 1000, btnShortHandle
, btnLongHandle
, btnDoublehandle
)