Attached please find a program that can be run from the Terminal
activity or the text console to display signal level, noise level and
link quality at ten updates per second.

It is useful for performing simple measurements of RF coverage on an XO
associated with an access point.

The levels are expressed as circles of varying radii.  Press q to quit.

Tested on OLPC build 802 and debxo.

-- 
James Cameron
http://quozl.linux.org.au/
#!/usr/bin/python
"""
    Signal Strength Meter
    Copyright (C) 2009  James Cameron ([email protected])

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""
import sys, pygame

license = [
"Signal Strength Meter",
"Copyright (C) 2009 James Cameron <[email protected]>",
" ",
"This program comes with ABSOLUTELY NO WARRANTY;",
"for details see source.",
" ",
"This is free software, and you are welcome to ",
"redistribute it under certain conditions; see ",
"source for details.",
" "
]

pause = 100             # milliseconds between screen updates
license_show_time = 10  # number of seconds to show license for
thickness = 4           # thickness of circles in pixels

fonts = ('DejaVuSansMono.ttf', 'DejaVuLGCSansMono.ttf')
fontpaths = ('/usr/share/fonts/dejavu/', '/usr/share/fonts/truetype/ttf-dejavu/')

# colours
c_license = (0, 0, 0)
c_link = (0, 0, 192)
c_signal = (0, 192, 0)
c_noise = (192, 0, 0)

def get_rf():
    """ obtain wireless status from first network interface """
    fp = open('/proc/net/wireless', 'r')
    head = fp.readline()
    head = fp.readline()
    line = fp.readline()
    fp.close()
    (x, x, link, signal, noise, x, x, x, x, x, x) = line.split()
    return (int(link.replace('.', ' ')),
            int(signal.replace('.', ' ')),
            int(noise.replace('.', ' ')))

# what to do when user wants to leave
def op_quit():
    sys.exit()

# control keyboard table, relates keys to functions
kb_table_control = {
    pygame.K_d: op_quit,
    }

# normal keyboard table, relates keys to functions
kb_table_normal = {
    pygame.K_q: op_quit,
    }

def kb(event):
    """ handle keyboard events from user """

    # ignore the shift and control keys
    if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT: return
    if event.key == pygame.K_LCTRL or event.key == pygame.K_RCTRL: return

    # check for control key sequences pressed
    if (event.mod == pygame.KMOD_CTRL or
        event.mod == pygame.KMOD_LCTRL or
        event.mod == pygame.KMOD_RCTRL):
        if kb_table_control.has_key(event.key):
            handler = kb_table_control[event.key]
            handler()
        return

    # check for normal keys pressed
    if kb_table_normal.has_key(event.key):
        handler = kb_table_normal[event.key]
        handler()
        return

class FontCache:
    def __init__(self):
        self.cache = {}

    def read(self, names, size):
        if names == None:
            return pygame.font.Font(None, size)

        for name in names:
            for path in fontpaths:
                try:
                    return pygame.font.Font(path + name, size)
                except:
                    continue
        return pygame.font.Font(None, size)

    def get(self, names, size):
        key = (names, size)
        if key not in self.cache:
            self.cache[key] = self.read(names, size)
        return self.cache[key]

def draw_license():
    fn = fc.get(fonts, 35)
    x = 50
    y = 394
    for line in license:
        ts = fn.render(line, 1, c_license, bg)
        tr = ts.get_rect(left=x, top=y)
        y = tr.bottom
        dirty.append(screen.blit(ts, tr))

def draw_labels():
    fn = fc.get(fonts, 35)
    ts = fn.render('LINK QUALITY', 1, c_link, bg)
    tr = ts.get_rect(centerx=width/2, top=0)
    dirty.append(screen.blit(ts, tr))
    ts = fn.render('SIGNAL LEVEL', 1, c_signal, bg)
    tr = ts.get_rect(left=0, bottom=height)
    dirty.append(screen.blit(ts, tr))
    ts = fn.render('NOISE LEVEL', 1, c_noise, bg)
    tr = ts.get_rect(right=width, bottom=height)
    dirty.append(screen.blit(ts, tr))

def draw_item(show, colour, x, y, radius, v):
    if not show:
        colour = bg
    if radius < 0:
        radius = 0
    p = pygame.draw.circle(screen, colour, (x, y), radius+thickness, thickness)
    fn = fc.get(fonts, 50)
    ts = fn.render(v, 1, colour, bg)
    tr = ts.get_rect(centerx=x, centery=y)
    q = screen.blit(ts, tr)
    dirty.append(pygame.Rect.union(p, q))

def draw_link(link, show):
    draw_item(show, c_link, width/2, height/3, link*15/10, "%d %%" % link)

def draw_signal(signal, show):
    if signal != -256: # scanning?
        draw_item(show, c_signal, width/3, height*2/3, 156+signal, "%d dBm" % signal)

def draw_noise(noise, show):
    draw_item(show, c_noise, width*2/3, height*2/3, 156+noise, "%d dBm" % noise)


from optparse import OptionParser
parser = OptionParser(usage="usage: %prog [options] message",
                     version="%prog 0.2")
parser.add_option("--verbose",
                  action="store_true", dest="verbose", default=False,
                  help="generate verbose output")
parser.add_option("--no-license",
                  action="store_true", dest="no_license", default=False,
                  help="do not display license")
(opt, args) = parser.parse_args()

if not opt.no_license:
    for line in license:
        print line

pygame.init()
fc = FontCache()

width, height = 1200, 900
screen = pygame.display.set_mode((width, height), pygame.FULLSCREEN)
pygame.mouse.set_visible(False)

white = (255, 255, 255)
black = (0, 0, 0)

pygame.time.set_timer(pygame.USEREVENT, pause)

if opt.no_license: license_show_time = 0
license_show_count = (1000 / pause) * license_show_time

fg = black
bg = white

# prepare background
screen.fill(bg)
pygame.display.flip()

# initial drawing pass
(link, signal, noise) = get_rf()
dirty = []
draw_labels()
draw_link(link, True)
draw_signal(signal, True)
draw_noise(noise, True)
pygame.display.update(dirty)

# main event loop
while True:
    event = pygame.event.wait()
    if event.type == pygame.QUIT:
        sys.exit()
    elif event.type == pygame.KEYDOWN:
        kb(event)
    elif event.type != pygame.USEREVENT:
        continue

    dirty = []
    if license_show_count > 0:
        # slow screen redraw with license present
        # everything is redrawn
        license_show_count = license_show_count - 1
        draw_link(link, False)
        draw_signal(signal, False)
        draw_noise(noise, False)
        if license_show_count == 0:
            dirty.append(screen.fill(bg))
        else:
            draw_license()
        (link, signal, noise) = get_rf()
        draw_labels()
        draw_link(link, True)
        draw_signal(signal, True)
        draw_noise(noise, True)
    else:
        # fast screen redraw with license absent
        # only changes are redrawn
        (old_link, old_signal, old_noise) = (link, signal, noise)
        (link, signal, noise) = get_rf()

        if signal == -256: # scanning?
            signal = old_signal

        if link != old_link:
            draw_link(old_link, False)
            draw_link(link, True)
        if signal != old_signal:
            draw_signal(old_signal, False)
            draw_signal(signal, True)
        if noise != old_noise:
            draw_noise(old_noise, False)
            draw_noise(noise, True)

    # update only the portions of screen that were changed
    pygame.display.update(dirty)
_______________________________________________
Devel mailing list
[email protected]
http://lists.laptop.org/listinfo/devel

Reply via email to