// This is a demo of the RBBB running as webserver with the Ether Card
// 2010-05-28 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = {
  0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = {
  192,168,1,200 };

byte Ethernet::buffer[500];
BufferFiller bfill;


void setup () {
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
  ether.staticSetup(myip);
}

static word homePage() {

  bfill = ether.tcpOffset();
  bfill.emit_p(
  "<html><body><h2>LIttleFe Reset/Power System</h2><p><table border=1><tr><th>Node</th><th>Reset</th><th>Power</th></tr><form name='input' action='index.html' method='get'><tr><td>1</td><td><input type='radio' name='r1'></td><td><input type='radio' name='p1'></td></tr><tr><td>2</td><td><input type='radio' name='r2'></td><td><input type='radio' name='p2'></td></tr><tr><td>3</td><td><input type='radio' name='r3'></td><td><input type='radio' name='p3'></td></tr><tr><td>4</td><td><input type='radio' name='r4'></td><td><input type='radio' name='p4'></td></tr><tr><td>5</td><td><input type='radio' name='r5'></td><td><input type='radio' name='p5'></td></tr><tr><td>6</td><td><input type='radio' name='r6'></td><td><input type='radio' name='p6'></td></tr></table><input type='submit' value='Submit'></form></body></html>");
  return bfill.position();
}

void loop () {
  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if (pos)  // check if valid tcp data is received
    ether.httpServerReply(homePage()); // send web page data
}