Code to run tests on the Ethernet library

Dependencies:   EthernetInterface mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
lawless
Date:
Mon Oct 28 23:05:43 2013 +0000
Parent:
2:483bc2e71bfe
Commit message:
It worked! yay

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Oct 28 21:14:44 2013 +0000
+++ b/main.cpp	Mon Oct 28 23:05:43 2013 +0000
@@ -1,56 +1,61 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 
-DigitalOut myled(LED2);
+
+PwmOut pump_tick(LED1);
+DigitalOut tick(LED2);
 Ticker pkt;
-EthernetInterface *eth;
-UDPSocket *sock;
 InterruptIn *flow;
 PwmOut *pump;
 
 unsigned int flowcount = 0;
+unsigned int lastflow;
+
 float pi = 3.1415926535897932384626433832795;
 
 void flowtick() {
+    tick.write(1);
     flowcount++;
+    tick.write(0);
 }
 
-void ping() {
-    Endpoint seven;
+int main() {
+ 
+    EthernetInterface eth;
     UDPSocket sock;
+    Endpoint seven;
+    eth.init("192.168.9.8", "255.255.255.0", "192.168.9.1");
+    eth.connect();
     sock.init();
-    myled.write(1);
-    
     seven.set_address("192.168.9.7", 37);
     char out_buffer[8]; // Does not matter
-    sprintf(out_buffer, "%07d", flowcount);
-    sock.sendTo(seven, out_buffer, sizeof(out_buffer));  
-    sock.close();
-    wait(0.5);
-    myled.write(0);
-}
- 
-int main() {
-    eth = new EthernetInterface();
+    
     flow = new InterruptIn(p5);
+    flow->mode(PullUp);
     pump = new PwmOut(p21);  
     
     pump->period(1.0);
-    eth->init("192.168.9.8", "255.255.255.0", "192.168.9.1");
-    eth->connect();   
+    pump_tick.period(1.0);
+    
+    lastflow = flowcount;
     
     flow->rise(&flowtick);
-    pkt.attach(&ping, 2.0);
     
     float t = 0.0;
     while(1) {   
         if(t < pi) {
             t += 0.01;
             pump->write(1 - cos(t));
+            pump_tick.write(1 - cos(t));
         }
         
         wait(0.1);
+        if(flowcount > lastflow) {
+            sprintf(out_buffer, "%07d", flowcount);
+            sock.sendTo(seven, out_buffer, sizeof(out_buffer));
+            lastflow = flowcount;
+        }
     }
-    
-    eth->disconnect();
+    sock.close();
+    eth.disconnect();
 }