diff --git a/README.md b/README.md index 41eadc4..cdeb063 100644 --- a/README.md +++ b/README.md @@ -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)` @@ -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 | ## diff --git a/src/SCRIPTS/GRAPHS/graphs.lua b/src/SCRIPTS/GRAPHS/graphs.lua index bb919c6..dbffe67 100644 --- a/src/SCRIPTS/GRAPHS/graphs.lua +++ b/src/SCRIPTS/GRAPHS/graphs.lua @@ -1,6 +1,6 @@ local MAXLEN = 64 -local graphs = {} +graphs = {} local trgrs = {} local paused = false @@ -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) @@ -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 @@ -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() @@ -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 diff --git a/src/SCRIPTS/TELEMETRY/graph.lua b/src/SCRIPTS/TELEMETRY/graph.lua index 4fbcb3b..bc72af5 100644 --- a/src/SCRIPTS/TELEMETRY/graph.lua +++ b/src/SCRIPTS/TELEMETRY/graph.lua @@ -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) diff --git a/src/SCRIPTS/TELEMETRY/graphs.lua b/src/SCRIPTS/TELEMETRY/graphs.lua index 4fb95c5..3d1ca44 100644 --- a/src/SCRIPTS/TELEMETRY/graphs.lua +++ b/src/SCRIPTS/TELEMETRY/graphs.lua @@ -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) @@ -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) diff --git a/src/SCRIPTS/TELEMETRY/memgph.lua b/src/SCRIPTS/TELEMETRY/memgph.lua index 26c96c7..826d7d9 100644 --- a/src/SCRIPTS/TELEMETRY/memgph.lua +++ b/src/SCRIPTS/TELEMETRY/memgph.lua @@ -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, @@ -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 } diff --git a/src/SCRIPTS/WIDGETS/graph.lua b/src/SCRIPTS/WIDGETS/graph.lua index 2271bb5..678ef73 100644 --- a/src/SCRIPTS/WIDGETS/graph.lua +++ b/src/SCRIPTS/WIDGETS/graph.lua @@ -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 @@ -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 @@ -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) diff --git a/src/WIDGETS/Graph/config.lua b/src/WIDGETS/Graph/config.lua new file mode 100644 index 0000000..35f687b --- /dev/null +++ b/src/WIDGETS/Graph/config.lua @@ -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 } +} diff --git a/src/WIDGETS/Graph/main.lua b/src/WIDGETS/Graph/main.lua new file mode 100644 index 0000000..c8edf3e --- /dev/null +++ b/src/WIDGETS/Graph/main.lua @@ -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 } diff --git a/src/WIDGETS/Graph/options.lua b/src/WIDGETS/Graph/options.lua new file mode 100644 index 0000000..9d3fd16 --- /dev/null +++ b/src/WIDGETS/Graph/options.lua @@ -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 }, +}