<?xml version="1.0"?>
<!--Copyright Brian Starkey 2011-->
<page title="Network Monitor" dir="projects/netmon" filename="netmon" type="windowpage" root="/xml_95">
<window title="USB Network Monitor">
	<menu>		
		<menu-item name="File">
			<!--<pdf-subitem name="Get PDF"/>-->
			<home-subitem name="Home" href="{/page/@root}"/>
			<menu-subitem name="XML" href="netmon.xml"/>
		</menu-item>
		<!--include common menu items (dynamically generated)-->
		<autogen type='category' category='all'/>
	</menu>
	<substance>
	<locations name="{/page/@title}">
		<folder name="Documentation">
			<!--<leaf class="pdfleaf" name="proj.pdf" href="proj.pdf" />-->
			<!-- img="/images/pdf_icon.gif" -->
			<leaf class="sectionleaf" name="Introduction" href="#Introduction"/>
			<leaf class="sectionleaf" name="Hardware &amp; Firmware" href="#Hardware &amp; Firmware"/>
			<leaf class="sectionleaf" name="Software" href="#Software"/>
			<leaf class="videoleaf" name="Video" href="#Video"/>
			<leaf class="imgleaf" name="Images" href="#Images"/>
		</folder>
		<folder name="Code">
			<leaf class="zipleaf" name="netmon.zip" href="code/netmon.zip"/>
		</folder>
	</locations>
	<window-contents>
		<row>
			<image src="images/gui.png">The GTK Interface</image>
			<section-title name="Introduction"/>
			<section-content>
				This is basically a USB controlled RGB LED. I had the hardware finished but had no real purpose for it, so I left it unused for almost a year. I had a GUI written, consisting of essentially just a GtkColorSelection,
				so I could set the LED colour to my heart's content, but that's as far as I got. Later in the year, I had some real issues with ISPs, and spent all of my time monitoring network downspeed with a gnome panel applet. 
				All well and good, but then it hit me to use my LED as a network throughput monitor.
			</section-content>
		</row>
		<row>
            <image src="images/DSC04142.JPG"/>
            <image src="images/board.JPG">The Board</image>
			<section-title name="Hardware &amp; Firmware"/>
			<section-content>
				The circuit is very simple - it consists of an Attiny45, a common anode RGB LED (TruOpto Piranha), current limiting resistors, 2 diodes to drop VBUS to 3.6V and a USB socket.
                I'm not sure exactly what type of socket it is, it was salvaged from a cheap generic mp3 player, not that it really matters. There's a momentary push button on the board but currently it isn't wired to anything<br/>
                The Attiny is running at 16.5 MHz off the internal oscillator. If anyone wants the schematic drop me a mail (contact details under Help - About)<br/>
                The board is put inside the lid off a spray-snow can, makes a rather nice light diffuser, though is a little bit too opaque.
                The firmware uses V-USB, and is heavily based on the hid-custom-rq example by Christian Starkjohann (which seems to have disappeared - found a copy <link href="http://gitorious.org/usbflattiny/usbflattiny/trees/24b6ad660c8a03f6f83c394a0dde3e59680df7e9/firmware/vusb/examples/hid-custom-rq">here</link>)<br/>
                It is also based on this project, which has a similar function: <link href="http://eeegadgets.blogspot.com/2009/07/usb-mail-notifier-final-revision.html">USB Mail Notifier</link><br/>
                I implemented my own PWM loop, simply doing the appropriate LED switching. There is then a 'fade to' function, which takes a colour and a speed. It adjusts the R, G and B values to move the current colour
                to the required one, at the required speed.<br/>
			</section-content>
		</row>
        <row>
			<image src="images/colours.png" style="max-width: 200px;">The data-rate colours</image>
            <image src="images/console_output.png">Output of the PC-side Software</image>
			<section-title name="Software"/>
			<section-content>
				The PC-side software is a bit of an ugly hack. It basically runs <i>ifconfig</i> every second, greps out the Tx and Rx byte figures, compares it to the last read values, and determines a data rate.<br/>
                This rate is put through a few formulae:
                <code language='cpp'>
                <![CDATA[ 

    if (kBps < 10) {
        if (inactive <= 85) inactive+=5;
        else if (inactive < 99) inactive++;
    }
    else {
        inactive = 0;
    }
    if (kBps < 60) {
        if (inactive) {
            data[0] = 100 - inactive;
            data[1] = 0;
            data[2] = 0;
        }
        else {
            data[0] = 150;
            data[1] = (unsigned char)(510*(1-cos(DEG2RAD(kBps))));
            data[2] = 0;
        }
    }
    else if (kBps < 120) {
        data[0] = 150-(unsigned char)(300*(1-cos(DEG2RAD(kBps-60))));
        data[1] = 255;
        data[2] = 0;
    }
    else if (kBps < 180) {
        data[0] = 0;
        data[1] = 255;
        data[2] = (unsigned char)(510*(1-cos(DEG2RAD(kBps-120))));
    }
    else if (kBps < 240) {
        data[0] = 0;
        data[1] = 255-(unsigned char)(510*(1-cos(DEG2RAD(kBps-180))));
        data[2] = 255;
    }
    else if (kBps < 300) {
        data[0] = (unsigned char)(300*(1-cos(DEG2RAD(kBps-240))));
        data[1] = 0;
        data[2] = 255;
    }
    else if (kBps < 360) {
        data[0] = 150;
        data[1] = (unsigned char)(510*(1-cos(DEG2RAD(kBps-300))));
        data[2] = 255;
    }
    else {
        data[0] = 255;
        data[1] = 255;
        data[2] = 255;
    }
        ]]>
                </code>
                Resulting in different colours for different data rates, as shown in the <link href="images/colours.png">picture</link>.<br/>
                All the source code is avaialble in this <link href="code/netmon.zip">zip</link>.
			</section-content>
		</row>
        <row>
			<video name="Video" src="http://www.youtube.com/embed/gLMZ2z2mQFY?version=3&amp;hl=en_US&amp;rel=0"/>
			<section-title name="Video"/>
			<section-content>
				The video shows the network monitor whilst loading webpages. The terminal window is the output of the PC-side software, the line graph in the bottom of the screen is gnome's network monitor.<br/>
				There is around 1 second of lag between network traffic and the displayed colour. This is due to a) The refresh rate being 1 Hz, b) Averaging in software to stop the colours bouncing all over the place.<br/>
				The light follows the gnome network monitor closely, which also has a 1 Hz refresh rate (and presumably software averaging.)
			</section-content>
		</row>
		<row>
			<section-title name="Images"/>
			<section-content>
			<image-gallery>
               <autogen type="gallery" directory="images"/>
            </image-gallery>
			</section-content>
		</row>
        
        <comments/>
        
	</window-contents>
	</substance>
</window>

</page>


