Skip to content

Commit

Permalink
Add WIDGETS/Graphs/main.lua to support HORUS platforms
Browse files Browse the repository at this point in the history
- still lots of todos

Update GRAPHS/graphs.lua
- function init(id): clears `graphs` table, clears `graphs` item by id

Update GRAPHS/graphs.lua
- set table `graphs` global

Update Graph/main.lua

Update WIDGETS/Graph/main.lua
- put options and config tables in extra files

Update WIDGETS/Graph/main.lua
- replace param `style` with `lnStyle`
- add param `lnSize` for line thickness

Update WIDGETS/graph.lua
- replace param `style` with `lnStyle`
- add param `lnSize` for line thickness

Update memgph.lua
- add mem used kByte value

Update TELEMETRY screens
- replace param `style` with `lnStyle`
- add param `lnSize` for line thickness

Update GRAPHS/graphs.lua
- add param `lnSize` for line thickness

Update README.md

Update build files
  • Loading branch information
Matze-Jung committed Nov 12, 2019
1 parent 4e014b8 commit a41c844
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 24 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ Please go to the [releases page](https://github.com/Matze-Jung/opentx-lua-runnin
* [OpenTX Companion v2.3.1](https://www.open-tx.org/) (*FrSky platforms only*)

## File structure
- `GRAPHS/`
- `SCRIPTS/GRAPHS/`
* `graphs.lua` main script
* `trigger.lua` user inputs


- `MIXES/`
- `SCRIPTS/MIXES/`
* `graphs.lua` model script


- `TELEMETRY/` telemetry screen examples
- `SCRIPTS/TELEMETRY/` telemetry screen examples


- `WIDGETS/`
- `SCRIPTS/WIDGETS/`
* `graph.lua` widget for the [opentx-lua-widgets](https://github.com/Matze-Jung/opentx-lua-widgets) grid system

- `WIDGETS/Graph/` Horus integration example


## API
#### Functions
`createGraph(id, opts)`
Expand All @@ -49,7 +52,8 @@ Init and display the graph.
| opts.**max** | number | Largest possible value |
| opts.**min** | number | Smallest possible value |
| opts.**speed** | number *(optional, default `75`)* | Update speed in 100ths second intervals, smaller is faster |
| opts.**style** | number *(optional, default `SOLID` - `DOTTED` at 'min' and 'max' values)* | `SOLID` for a full solid line, `DOTTED` for a dotted line |
| opts.**lnStyle** | number *(optional, default `SOLID` - `DOTTED` at 'min' and 'max' values)* | `SOLID` for a full solid line, `DOTTED` for a dotted line |
| opts.**lnSize** | number *(optional, default 1)* | Line thickness in px |
| opts.**crit** | number *(optional)* | If set, the line style is `DOTTED` below and `SOLID` above this value. The Y-axis gets a mark at the values position |

##
Expand Down
27 changes: 17 additions & 10 deletions src/SCRIPTS/GRAPHS/graphs.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local MAXLEN = 64

local graphs = {}
graphs = {}
local trgrs = {}
local paused = false

Expand All @@ -24,14 +24,14 @@ function createGraph(id, opts)
end

-- Y-line
lcd.drawLine(g.x + 1, g.y, g.x + 1, g.y + g.h + 1, SOLID, FORCE)
lcd.drawLine(g.x + 1, g.y, g.x + 1, g.y + g.h + 1, SOLID, FORCE or 0)
lcd.drawPoint(g.x, g.y)
if g.crit ~= nil then
lcd.drawPoint(g.x, math.floor(g.y + (g.max - g.crit) * ((g.h-1) / (g.max - g.min))))
end

-- X-line
lcd.drawLine(g.x, g.y + g.h, g.x + g.w, g.y + g.h, SOLID, FORCE)
lcd.drawLine(g.x, g.y + g.h, g.x + g.w, g.y + g.h, SOLID, FORCE or 0)

if paused then
lcd.drawText(g.x + (g.w / 2) - 20, g.y + (g.h / 2) - 4, "paused", SMLSIZE+BLINK+INVERS)
Expand All @@ -52,17 +52,18 @@ function createGraph(id, opts)

local range = g.max - g.min
local maxY = g.y
local minY = g.h + g.y - 1
local curY = math.floor(maxY + (g.max - value) * ((g.h-1) / range))
local nxtY = math.floor(maxY + (g.max - nxtVal) * ((g.h-1) / range))
local minY = g.h + g.y - (g.lnSize or 1)
local curY = math.floor(maxY + (g.max - value) * ((minY-maxY) / range))
local nxtY = math.floor(maxY + (g.max - nxtVal) * ((minY-maxY) / range))
local lineForm = g.style or (((nxtY >= minY and curY >= minY) or (nxtY <= maxY and curY <= maxY)) and DOTTED or SOLID)


if g.crit ~= nil then
lineForm = (value <= g.crit and nxtVal <= g.crit) and DOTTED or SOLID
end

lcd.drawLine(g.x + (j*g.dp) - g.dp + 2, curY, g.x + ((j+1)*g.dp) - g.dp + 2, nxtY, lineForm, FORCE)
for i=0, g.lnSize and g.lnSize-1 or 0 do
lcd.drawLine(g.x + (j*g.dp) - g.dp + 2, curY + i, g.x + ((j+1)*g.dp) - g.dp + 2, nxtY + i, lineForm, FORCE or 0)
end
end
end

Expand All @@ -81,11 +82,18 @@ function getGraphAverage(id)
return 0
end

local function init()
local function init(id)
trgrs = assert(loadScript("/SCRIPTS/GRAPHS/trigger.lua"))()
for j, t in pairs(trgrs) do
trgrs[j].state = t.func()
end

if id and graphs[id] then
graphs[id] = {}
return
end

graphs = {}
end

local function update()
Expand All @@ -106,7 +114,6 @@ local function update()
if g.lastRun == 0 or g.lastRun + g.speed < getTime() then
local maxValues = math.ceil(g.w / g.dp)
local vals = g.values

if #vals >= maxValues then
local tmp = {}
for j=1,#(vals) do
Expand Down
5 changes: 3 additions & 2 deletions src/SCRIPTS/TELEMETRY/graph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ local function run(event)
speed=25,
min=-1000,
max=1000,
crit=350,
-- style=SOLID
-- crit=350,
-- lnStyle=SOLID,
lnSize=2,
})
lcd.drawText(2, 9, "max", SMLSIZE)
lcd.drawText(3, 55, "min", SMLSIZE)
Expand Down
4 changes: 2 additions & 2 deletions src/SCRIPTS/TELEMETRY/graphs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local function run(event)
speed=50,
min=-1000,
max=1000,
style=SOLID
lnStyle=SOLID
})
lcd.drawText(1, 8, "max", SMLSIZE)
lcd.drawText(1, 24, "min", SMLSIZE)
Expand All @@ -40,7 +40,7 @@ local function run(event)
speed=50,
min=-1000,
max=1000,
style=DOTTED
lnStyle=DOTTED
})
lcd.drawText(1, 40, "max", SMLSIZE)
lcd.drawText(1, 56, "min", SMLSIZE)
Expand Down
9 changes: 8 additions & 1 deletion src/SCRIPTS/TELEMETRY/memgph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ local function run(event)

lcd.drawFilledRectangle(0, 0, LCD_W, 8)
lcd.drawText(1, 1, "MEM USED "..(MEMORY or 0).."B", SMLSIZE+INVERS)
lcd.drawNumber(lcd.getLastPos() + 18, 1, (MEMORY or 0) / 1024 * 100, PREC2+SMLSIZE+INVERS)
lcd.drawText(lcd.getLastPos(), 1, "kB", SMLSIZE+INVERS)

createGraph(6, {
src=function() return MEMORY or 0 end,
x=17,
Expand All @@ -16,12 +19,16 @@ local function run(event)
min=25600,
max=76800,
crit=61440,
lnSize=1,
})
lcd.drawText(1, 10, "75K", SMLSIZE)
lcd.drawText(1, LCD_H-16, "25K", SMLSIZE)

lcd.drawFilledRectangle(0, LCD_H-8, LCD_W, 8)
lcd.drawText(1, LCD_H-7, "AVRG "..math.floor(getGraphAverage(6) + 0.5).."B MAX "..(getGraphRange(6).max or 0).."B", SMLSIZE+INVERS)
lcd.drawText(1, LCD_H-7,
"AVRG " ..math.floor(getGraphAverage(6) + 0.5).."B MAX "..(getGraphRange(6).max or 0).."B",
SMLSIZE+INVERS
)
end

return { run = run }
12 changes: 8 additions & 4 deletions src/SCRIPTS/WIDGETS/graph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
- Configurations
uid: int
- Systemwide unique ID of the graph
- Systemwide unique graph ID
src: function or sensor-ID string/number
- Data source
Expand All @@ -36,10 +36,13 @@
speed: int (optional, default 75)
- Running speed in 100ths second intervals, means smaller is faster
style: int (optional, default SOLID - but DOTTED at 'min' or 'max' values)
lnStyle: int (optional, default SOLID - but DOTTED at 'min' or 'max' values)
- Set to SOLID for a full solid line
- Set to DOTTED for a dotted line
lnSize: int (optional, default 1)
- Set the line thickness in px
crit: number (optional)
- If set, the line style is DOTTED below and SOLID above this value
- The X-axis gets a mark at the values position
Expand Down Expand Up @@ -102,8 +105,9 @@ local function graphWidget(zone, event, opts)
speed=opts.speed or 75,
min=opts.min,
max=opts.max,
style=opts.style or nil,
crit=opts.crit or nil
lnStyle=opts.lnStyle,
lnSize=opts.lnSize,
crit=opts.crit
})
lcd.drawText(z.x + 1, p.t + z.y, opts.lblmax or "", SMLSIZE)
lcd.drawText(z.x + 1, z.y + z.h - p.b - 7, opts.lblmin or "", SMLSIZE)
Expand Down
9 changes: 9 additions & 0 deletions src/WIDGETS/Graph/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
return {
lbl="",
lblstyle=SMLSIZE+INVERS,
unit="",
crit=nil,
-- lnStyle=DOTTED,
lnSize=2,
p={ t=0,r=0,b=0,l=0 }
}
100 changes: 100 additions & 0 deletions src/WIDGETS/Graph/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
--[[ ** TELEMETRY SCREEN WIDGET **
Draws a moving line graph from a given source.
PLATFORM
HORUS and relatives
SCALING
vert: scalable
horiz: scalable
OPTIONS [options.lua]
UID: int
- Systemwide unique graph ID
Speed: int (optional, default 30)
- Running speed in 100ths second intervals, means smaller is faster
Min/Max: number
- Largest/smallest possible value
Source: number
- Input sensor-ID
PARAMETER [config.lua]
lbl: string (optional)
- Widgets label text at the top
lblstyle: int (optional, default 0)
- Set text attributes for label
unit: string (optional)
- Unit sign drawn behind the value
- Set to "%", to output percent of 'src', calculated with 'min' and 'max'
crit: number (optional)
- If set, the line style is DOTTED below and SOLID above this value
- The X-axis gets a mark at the values position
lnStyle: int (optional, default SOLID - but DOTTED at 'min' or 'max' values)
- Set to SOLID for a full solid line
- Set to DOTTED for a dotted line
lnSize: int (optional, default 1)
- Set the line thickness in px
p: table (optional, default [t=0, r=0, b=0, l=0])
- Cell padding in px
(top, right, bottom, left)
--]]

local options = assert(loadScript("/WIDGETS/Graph/options.lua"))()

local function create(zone, options)
local cfg = assert(loadScript("/WIDGETS/Graph/config.lua"))()
local core = assert(loadScript("/SCRIPTS/GRAPHS/graphs.lua"))()
core.init()

return { zone=zone, options=options, cfg=cfg, core=core }
end

local function update(wgt, options)
wgt.options = options
wgt.core.init(options.UID)

return wgt
end

local function background(wgt)
return wgt.core.run()
end

function refresh(wgt)
local cfg = wgt.cfg

wgt.core.run()

if cfg.lbl then
local val = getValue(wgt.options.Source)
lcd.drawText(wgt.zone.x + 1, wgt.zone.y + 1, cfg.lbl .. " " .. val .. (cfg.unit or ""), cfg.lblstyle)
cfg.p.t = 21
end

createGraph(wgt.options.UID, {
src=wgt.options.Source,
x=wgt.zone.x + cfg.p.l,
y=wgt.zone.y + cfg.p.t,
w=wgt.zone.w - cfg.p.l - cfg.p.r,
h=wgt.zone.h - cfg.p.t - cfg.p.b,
speed=wgt.options.Speed,
min=wgt.options.Min,
max=wgt.options.Max,
lnStyle=cfg.lnStyle,
lnSize=cfg.lnSize,
crit=cfg.crit
})
end

return { name="THR Curve", options=options, create=create, update=update, background=background, refresh=refresh }
7 changes: 7 additions & 0 deletions src/WIDGETS/Graph/options.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
return {
{ "UID", VALUE, 1, 1, 99 },
{ "Speed", VALUE, 30, 1, 999 },
{ "Min", VALUE, -1024, -1024, 1024 },
{ "Max", VALUE, 1024, -1024, 1024 },
{ "Source", SOURCE, 1 },
}

0 comments on commit a41c844

Please sign in to comment.