lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Gavin Kistner wrote:
What do _you_ do when you need a simple, lightweight way to create some
unique values? Do you worry about cross-enumeration comparisons? Do you
use strings?
I begun using strings, then switched to tables to save typing the quotes. The following example is not really for enumerations, but the same idea applies. I have many similar functions. The victims^H^H^H^H^H^H^H users of my code can then type bias(reset), bias(max), bias(10), etc. In the future I may use the same but capitalize the table names, to avoid conflicts with Lua keywords.


-- ---------------------------------------------------------------------
--    Objects to be used as dummy function parameters
-- ---------------------------------------------------------------------

again    = {}
all      = {}
back     = {}
clear    = {}
continue = {}
default  = {}
first    = {}
get      = {}
last     = {}
max      = {}
min      = {}
new      = {}
next     = {}
none     = {}
off      = {}
offset   = {}
old      = {}
on       = {}
only     = {}
pause    = {}
prev     = {}
reset    = {}
resume   = {}
set      = {}
start    = {}
stop     = {}
write    = {}
zero     = {}

.........
.........

-- ---------------------------------------------------------------------
--    set/show sensor bias current
-- ---------------------------------------------------------------------

def_vals ["bias"] = BIAS_CURR_DEFAULT
curr_vals["bias"] = def_vals["bias"]
prev_vals["bias"] = def_vals["bias"]
min_vals ["bias"] = 0
max_vals ["bias"] = 31

function bias(new_bias)
  if (not new_bias) then
     -- do nothing

  -- show default value
  elseif (new_bias == default) then
     print("Default=", def_vals["bias"])

  -- reset to default value
  elseif (new_bias == reset) then
     prev_vals["bias"], curr_vals["bias"] =
           curr_vals["bias"], def_vals["bias"]

  -- restore previous value
  elseif (new_bias == back) then
     prev_vals["bias"], curr_vals["bias"] =
           curr_vals["bias"], prev_vals["bias"]

  -- set minimum value
  elseif (new_bias == min) then
     prev_vals["bias"], curr_vals["bias"] =
           curr_vals["bias"], min_vals["bias"]

  -- set maximum value
  elseif (new_bias == max) then
     prev_vals["bias"], curr_vals["bias"] =
           curr_vals["bias"], max_vals["bias"]

  -- set given value
  else
     prev_vals["bias"], curr_vals["bias"] =
           curr_vals["bias"], new_bias
  end

  -- set current level even when not changed.
  set_bias_current(curr_vals["bias"])
  print("Bias current set to: ", curr_vals["bias"])
end