Tuesday, November 23, 2010

Even More Modular

My last code post simplified things, but many things were hard coded in. These weekend I modularized things so you can choose which network adapters you needs as well as what devices are connected.
#include <ports.>h>
//#include <portslcd.h>
#include <rf12.>h>
#include <homenet.>h>
//#include <homenetdevices.h>
#include <homenetdevicelcd.>h>

//Start HomeNet Packet Stack
HomeNet stack(0x04);//0x01 is RF12 base station //0xFF is PC uplink

//Setup Network Adapters
//HomeNetPortSerial portSerial(PORT_SERIAL, stack);  //PORT_SERIAL tells the adpater which serial serial port to use (for future support of the arduino mega)
HomeNetPortRF12   portRF12(stack);

//Setup attached devices
HomeNetDeviceStatusLights statusLights(stack);
//HomeNetDeviceTMP37 tmp37(stack);
//HomeNetDeviceLight light(stack);
HomeNetDeviceLCD lcd(stack);

//package the setup info in a nice neat arrays
 HomeNetPort*  ports[] = {&portRF12};//,&portSerial}; 
 HomeNetDevice*  devices[] = {&statusLights,  &lcd};// &light , &tmp37};

MilliTimer sendTimer;

void setup() {
  //initize home net with the setup info
  stack.init(ports, 1, devices, 2); 
  stack.registerStatusLights(statusLights); //setup status lights
}

void loop() {
  //receive incoming packets
  stack.receive();
  
  if (sendTimer.poll(10000)){
   // PayloadBuffer buffer;
   // buffer.print("TEST NODE 3");
   // stack.addUdpPacket(0, 2, 0, CMD_STRING, buffer.payload());
    //stack.schedule(2,255,0); //type, toNode, toDevice
     
  }
  
  //process packets in the stack- send packets that ready
  stack.process();
  delay(1);

}

No comments:

Post a Comment