Debian Codegraph @

obvious/umts/init.lua

raw symbols SCIP
1 --
2 -- Track indicators from F3705 gadget.
3 --
4
5 ------------------------------------------------------------------------------
6 -- "THE BEER-WARE LICENSE" (Revision 42):
7 -- <[email protected]> wrote this file. As long as you retain this notice you
8 -- can do whatever you want with this stuff. If we meet some day, and you
9 -- think this stuff is worth it, you can buy me a beer in return.
10 -- - Philip Paeps
11 ------------------------------------------------------------------------------
12
13 local assert = assert
14 local setmetatable = setmetatable
15 local io = {
16 open = io.open
17 }
18 local capi = {
19 widget = widget,
20 mouse = mouse
21 }
22
23 local naughty = require("naughty")
24 local awful = require("awful")
25 local wibox = require("wibox")
26
27 local lib = {
28 hooks = require("obvious.lib.hooks"),
29 markup = require("obvious.lib.markup")
30 }
31
32 local widget = wibox.widget.textbox()
33
34 local fh = nil
35 local cops = {}
36 local cind = {}
37
38 local function wait_for_data(input)
39 fh:write(input)
40 local data = ""
41 local lastline
42 repeat
43 lastline = assert(fh:read())
44 data = data .. lastline .. "\n"
45 until lastline:match("OK")
46 return data
47 end
48
49 local function get_indicators()
50 local cind = wait_for_data("AT+CIND?\r\n")
51 local rv = {}
52 rv.signal = cind:match("+CIND: %d,(%d)")
53 rv.service = cind:match("+CIND: %d,%d,%d,%d,(%d)")
54 rv.roaming = cind:match("+CIND: %d,%d,%d,%d,%d,%d,%d,(%d)")
55 return rv
56 end
57
58 local function get_operator()
59 wait_for_data("AT+COPS=3,0\r\n")
60 local cops = wait_for_data("AT+COPS?\r\n")
61 local rv = {}
62 rv.mode = cops:match("+COPS: (%d)")
63 rv.format = cops:match("+COPS: %d,(%d)")
64 rv.oper = cops:match("+COPS: %d,%d,\"(%a*)\"")
65 rv.act = cops:match("+COPS: %d,%d,\"%a*\",(%d)")
66 return rv
67 end
68
69 local function update()
70 fh = io.open("/dev/ttyACM1", "r+")
71 if not fh then
72 cops = {}
73 cind = {}
74 widget.text = ""
75 return
76 end
77
78 cops = get_operator()
79 cind = get_indicators()
80 widget.text = " " .. cops.oper
81 fh:close()
82 end
83
84 local function detail()
85 if not cops.oper then return end
86
87 naughty.notify({
88 text = "Mobile operator: " .. cops.oper ..
89 "\nSignal strength: " .. cind.signal .. "/5" ..
90 "\nRoaming: " .. cind.roaming,
91 screen = capi.mouse.screen
92 })
93 end
94
95 widget:buttons(awful.util.table.join(
96 awful.button({ }, 1, detail)
97 ))
98
99 update()
100 lib.hooks.timer.register(60, 300, update)
101 lib.hooks.timer.start(update)
102
103 return setmetatable({}, { __call = function () return widget end })
104
105 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et