-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
-- Lua Gauge widget | ||
|
||
local translations = {en="Lua Gauge", fr="Jauge Lua"} | ||
|
||
local function name(widget) | ||
local locale = system.getLocale() | ||
return translations[locale] or translations["en"] | ||
end | ||
|
||
local function create() | ||
return {color=lcd.RGB(0xEA, 0x5E, 0x00), source=nil, min=-1024, max=1024, value=0} | ||
end | ||
|
||
local function paint(widget) | ||
local w, h = lcd.getWindowSize() | ||
|
||
if widget.source == nil then | ||
return | ||
end | ||
|
||
-- Define positions | ||
if h < 50 then | ||
lcd.font(FONT_XS) | ||
elseif h < 80 then | ||
lcd.font(FONT_S) | ||
elseif h > 170 then | ||
lcd.font(FONT_XL) | ||
else | ||
lcd.font(FONT_STD) | ||
end | ||
|
||
local text_w, text_h = lcd.getTextSize("") | ||
local box_top, box_height = text_h, h - text_h - 4 | ||
local box_left, box_width = 4, w - 8 | ||
|
||
-- Source name and value | ||
lcd.drawText(box_left, 0, widget.source:name()) | ||
lcd.drawText(box_left + box_width, 0, widget.source:stringValue(), RIGHT) | ||
|
||
-- Compute percentage | ||
local percent = (widget.value - widget.min) / (widget.max - widget.min) * 100 | ||
if percent > 100 then | ||
percent = 100 | ||
elseif percent < 0 then | ||
percent = 0 | ||
end | ||
|
||
-- Gauge background | ||
gauge_width = math.floor((((box_width - 2) / 100) * percent) + 2) | ||
lcd.color(lcd.RGB(200, 200, 200)) | ||
lcd.drawFilledRectangle(box_left, box_top, box_width, box_height) | ||
|
||
-- Gauge color | ||
lcd.color(widget.color) | ||
|
||
-- Gauge bar | ||
lcd.drawFilledRectangle(box_left, box_top, gauge_width, box_height) | ||
|
||
-- Gauge frame outline | ||
lcd.color(BLACK) | ||
lcd.drawRectangle(box_left, box_top, box_width, box_height) | ||
|
||
-- Gauge percentage | ||
lcd.drawText(box_left + box_width / 2, box_top + (box_height - text_h) / 2, math.floor(percent).."%", CENTERED) | ||
end | ||
|
||
local function wakeup(widget) | ||
if widget.source then | ||
local newValue = widget.source:value() | ||
if widget.value ~= newValue then | ||
widget.value = newValue | ||
lcd.invalidate() | ||
end | ||
end | ||
end | ||
|
||
local function configure(widget) | ||
-- Source choice | ||
line = form.addLine("Source") | ||
form.addSourceField(line, nil, function() return widget.source end, function(value) widget.source = value end) | ||
|
||
-- Color | ||
line = form.addLine("Color") | ||
form.addColorField(line, nil, function() return widget.color end, function(color) widget.color = color end) | ||
|
||
-- Min & Max | ||
line = form.addLine("Range") | ||
local slots = form.getFieldSlots(line, {0, "-", 0}) | ||
form.addNumberField(line, slots[1], -1024, 1024, function() return widget.min end, function(value) widget.min = value end); | ||
form.addStaticText(line, slots[2], "-") | ||
form.addNumberField(line, slots[3], -1024, 1024, function() return widget.max end, function(value) widget.max = value end); | ||
end | ||
|
||
local function read(widget) | ||
widget.source = storage.read("source") | ||
widget.min = storage.read("min") | ||
widget.max = storage.read("max") | ||
widget.color = storage.read("color") | ||
end | ||
|
||
local function write(widget) | ||
storage.write("source", widget.source) | ||
storage.write("min", widget.min) | ||
storage.write("max", widget.max) | ||
storage.write("color", widget.color) | ||
end | ||
|
||
local function init() | ||
system.registerWidget({key="gauge", name=name, create=create, paint=paint, wakeup=wakeup, configure=configure, read=read, write=write}) | ||
end | ||
|
||
return {init=init} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
-- Lua Servo Configure | ||
|
||
local translations = {en="Lua Servo"} | ||
|
||
local function name(widget) | ||
local locale = system.getLocale() | ||
return translations[locale] or translations["en"] | ||
end | ||
|
||
local requestInProgress = false | ||
local refreshIndex = 0 | ||
local modifications = {} | ||
local repeatTimes = 0 | ||
local fields = {} | ||
|
||
local function getValue(parameter) | ||
if parameter[4] == nil then | ||
return 0 | ||
else | ||
return parameter[4] | ||
end | ||
end | ||
|
||
local function setValue(parameter, value) | ||
parameter[4] = value | ||
modifications[#modifications+1] = {parameter[3], value} | ||
for index = 1, #fields do | ||
fields[index]:enable(false) | ||
end | ||
end | ||
|
||
local function createNumberField(line, parameter) | ||
local field = form.addNumberField(line, nil, parameter[5], parameter[6], function() return getValue(parameter) end, function(value) setValue(parameter, value) end) | ||
field:enableInstantChange(false) | ||
if #parameter == 7 then | ||
field:suffix(parameter[7]) | ||
end | ||
field:enable(false) | ||
return field | ||
end | ||
|
||
local function createChoiceField(line, parameter) | ||
local field = form.addChoiceField(line, nil, parameter[5], function() return getValue(parameter) end, function(value) setValue(parameter, value) end) | ||
field:enable(false) | ||
return field | ||
end | ||
|
||
local function createChannelField(line, parameter) | ||
local field = form.addNumberField(line, nil, parameter[5], parameter[6], function() return getValue(parameter) + 1 end, function(value) setValue(parameter, value - 1) end) | ||
field:prefix("CH") | ||
field:enableInstantChange(false) | ||
field:enable(false) | ||
return field | ||
end | ||
|
||
local function createTextButton(line, parameter) | ||
local field = form.addTextButton(line, nil, parameter[5], function() return setValue(parameter, 0) end) | ||
field:enable(false) | ||
return field | ||
end | ||
|
||
local parameters = { | ||
-- { name, type, default, value, min, max } | ||
{"Physical ID", createNumberField, 0x00, nil, 0, 26 }, | ||
{"Servo ID", createNumberField, 0x01, nil, 0, 15 }, | ||
{"Refresh timer", createNumberField, 0x02, nil, 0, 65535, "ms" }, | ||
{"Range", createChoiceField, 0x04, nil, {{"120°", 0}, {"90°", 1}, {"180°", 2}}}, | ||
{"Direction", createChoiceField, 0x05, nil, {{"CW", 0}, {"CCW", 1}}}, | ||
{"PWM pulse type", createChoiceField, 0x06, nil, {{"1500us", 0}, {"760us", 1}}}, | ||
{"S.Port channel", createChannelField, 0x07, nil, 0, 23 }, | ||
{"Center", createNumberField, 0x08, nil, -125, 125 }, | ||
{"", createTextButton, 0x30, nil, "Save to flash"}, | ||
} | ||
|
||
local function updateField(index) | ||
if parameters[index][4] ~= nil then | ||
fields[index]:enable(true) | ||
end | ||
end | ||
|
||
local function create() | ||
requestInProgress = false | ||
refreshIndex = 0 | ||
modifications = {} | ||
repeatTimes = 0 | ||
fields = {} | ||
|
||
local sensor = sport.getSensor({appIdStart=0x6800, appIdEnd=0x680F}); | ||
|
||
for index = 1, #parameters do | ||
local parameter = parameters[index] | ||
local line = form.addLine(parameter[1]) | ||
local field = parameter[2](line, parameter) | ||
fields[#fields + 1] = field | ||
end | ||
|
||
sensor:idle() | ||
|
||
return {sensor=sensor} | ||
end | ||
|
||
local function wakeup(widget) | ||
if widget.sensor:alive() then | ||
if requestInProgress then | ||
local value = widget.sensor:getParameter() | ||
if value then | ||
local fieldId = value % 256 | ||
local parameter = parameters[refreshIndex + 1] | ||
if fieldId == parameter[3] then | ||
value = math.floor(value / 256) | ||
if parameter[3] == 0x08 then | ||
value = value & 0xFF; | ||
if value > parameter[6] then | ||
value = value - 256 | ||
end | ||
end | ||
|
||
parameters[refreshIndex + 1][4] = value | ||
if value ~= nil then | ||
fields[refreshIndex + 1]:enable(true) | ||
if refreshIndex + 2 == #parameters then | ||
fields[#parameters]:enable(true) | ||
end | ||
end | ||
|
||
refreshIndex = refreshIndex + 1 | ||
requestInProgress = false | ||
end | ||
else | ||
requestInProgress = false | ||
end | ||
else | ||
if #modifications > 0 then | ||
if widget.sensor:writeParameter(modifications[1][1], modifications[1][2]) == true then | ||
if modifications[1][1] == 0x01 then -- appId changed | ||
widget.sensor:appId(0x6800 + modifications[1][2]) | ||
end | ||
refreshIndex = 0 | ||
requestInProgress = false | ||
modifications[1] = nil | ||
end | ||
elseif refreshIndex < (#parameters - 1) then | ||
local parameter = parameters[refreshIndex + 1] | ||
if widget.sensor:requestParameter(parameter[3]) then | ||
requestInProgress = true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
local function event(widget, category, value, x, y) | ||
print("Event received:", category, value, x, y, KEY_EXIT_BREAK) | ||
if category == EVT_KEY and value == KEY_EXIT_BREAK then | ||
widget.sensor:idle(false) | ||
end | ||
return false | ||
end | ||
|
||
local icon = lcd.loadMask("/scripts/servo.png") | ||
|
||
local function init() | ||
system.registerSystemTool({name=name, icon=icon, create=create, wakeup=wakeup, event=event}) | ||
end | ||
|
||
return {init=init} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-- Lua Sinus widget | ||
|
||
local translations = {en="Lua Sinus", fr="Sinus Lua"} | ||
|
||
local function name(widget) | ||
local locale = system.getLocale() | ||
return translations[locale] or translations["en"] | ||
end | ||
|
||
local function create() | ||
return {color=lcd.RGB(0, 0, 255)} | ||
end | ||
|
||
local function paint(widget) | ||
local w, h = lcd.getWindowSize() | ||
lcd.drawLine(0, h/2, w, h/2) | ||
lcd.color(widget.color) | ||
for i = 0,w do | ||
local val = math.sin(i*math.pi/(w/2)) | ||
lcd.drawPoint(i, val*h/2+h/2) | ||
end | ||
end | ||
|
||
local function init() | ||
system.registerWidget({key="sinus", name=name, create=create, paint=paint}) | ||
end | ||
|
||
return {init=init} |