Strumenti Utente

Strumenti Sito


lua_scripts_for_neufbox4

A couple of scripts that will make the neufbox4 light from 1 to 6 white LEDs corresponding to the quality of the connection of an Internet key, when the front button is pressed.

LEDs library

This is a module with some simple functions to manage the LEDs. Save it as “led.lua” in a folder “lib”.

module(..., package.seeall)


white_names = {"NB4-SER-r2:white:adsl", 
               "NB4-SER-r2:white:traffic",
               "NB4-SER-r2:white:tel",
               "NB4-SER-r2:white:tv",
               "NB4-SER-r2:white:wifi",
               "NB4-SER-r2:white:alarm"}


col_names = {"NB4-SER-r2:blue:service",
             "NB4-SER-r2:green:service",
             "NB4-SER-r2:red:service"}


function get_led(name)
  local f = io.open("/sys/class/leds/" ..  name .. "/brightness")
  local val = f:read()
  return val
end

function set_led(name, value)
  local f = io.open("/sys/class/leds/" ..  name .. "/brightness", "w")
  f:write(value)
  f:close()
end

function switch_led(name)
  set_led(name, 1-get_led(name))
end

function switch_all()
  for key, name in pairs(white_names) do
    switch_led(name)
  end
end

function anim()
  for key, name in pairs(white_names) do
    switch_led(name)
    os.execute("sleep " .. 1)
  end
  
  for key, name in pairs(col_names) do    
    switch_led(name)                      
    os.execute("sleep " .. 1)
  end 
  
  for key, name in pairs(col_names) do    
    switch_led(name)                      
    os.execute("sleep " .. 1)             
  end
end

function set_n_leds(n, value)
  -- Cohercion:
  nn = 0 + n
  for key, name in pairs(white_names) do
    if nn >= key then
      set_led(name, value)
    else
      set_led(name, 1-value)
    end
  end
end

The signalometer

This script lights from 1 to 6 LEDs depending on the quality of the signal of the connection of an Internet key connected to the box. Save it as “show_signal.lua”.

module(..., package.seeall)

require "lib.led"

DEVICE = "/dev/ttyACM1"

function act()

  lib.led.set_n_leds(5, 0)

  local pop = io.popen("comgt -d " .. DEVICE)
  
  -- Line 1
  out = pop:read()
  -- Line 2
  out = pop:read()
  -- Line 3
  out = pop:read()
  
  for p in string.gmatch(out, '([^,]+)') do
    -- The last is the desired one
    connection = p
  end

  -- Line 4
  out = pop:read()
  
  for p in string.gmatch(out, "%S+") do
    -- The last is the desired one
    quality = p
  end
  
  quality = string.gsub(quality, ",", ".")
  
  -- The scale for connection should be
  -- (http://www.shapeshifter.se/2008/04/30/list-of-at-commands/):
  --
  --  0. GSM
  --  1. Compact GSM
  --  2. UTRAN
  --  3. GSM with EGPRS
  --  4. UTRAN with HSDPA
  --  5. UTRAN with HSUPA
  --  6. UTRAN with HSDPA and HSUPA
  --  
  --  while the scale for quality should be from 1 to 31, with
  --  
  --  0  -113 dBm or less
  --  1 -111 dBm
  --  2 to 30 -109 to -53 dBm
  --  31 -51 dBm or greater
  --  99  not known or not detectable
  
  -- however, gcom does not return the true connection type for my
  -- K3806-Z, so I will just use the signal strenght indicator:
  val = 6 * (quality / 31)

  lib.led.set_n_leds(val, 1)
  os.execute("sleep " .. 2)
  lib.led.set_n_leds(0, 1)
end

Dispatcher script

This script is called on any button event. It does nothing if the button was pressed but only if it was released - to avoid actions being executed twice. If the front button was released, it calls the above script.

#! /usr/bin/lua

-- Referred from "/etc/hotplug.d/button/etc/hotplug.d/button/buttons"
-- (http://wiki.openwrt.org/doc/howto/hardware.button)

print("button.lua running")

if os.getenv('ACTION') == "pressed" then os.exit() end

if os.getenv('BUTTON') == "BTN_1" then
  print("OK, go")
  require "show_signal"
  show_signal.act()
end

Hotplug configuration file

Save this as “/etc/hotplug.d/button/button” (and edit “/etc/hotplug2.rules” as suggested in this page, under “Configuration”):

#!/bin/sh
logger $BUTTON
logger $ACTION
cd /root/scripts
logger `./button.lua`

(replace “/root/scripts” with the dir where the previous files were saved)

lua_scripts_for_neufbox4.txt · Ultima modifica: 2014/04/18 18:42 da pietro