Matt Lesslaw / Mbed 2 deprecated TCPtest

Dependencies:   EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 
00005 PwmOut pump_tick(LED1);
00006 DigitalOut tick(LED2);
00007 Ticker pkt;
00008 InterruptIn *flow;
00009 PwmOut *pump;
00010 
00011 unsigned int flowcount = 0;
00012 unsigned int lastflow;
00013 
00014 float pi = 3.1415926535897932384626433832795;
00015 
00016 void flowtick() {
00017     tick.write(1);
00018     flowcount++;
00019     tick.write(0);
00020 }
00021 
00022 int main() {
00023  
00024     EthernetInterface eth;
00025     UDPSocket sock;
00026     Endpoint seven;
00027     eth.init("192.168.9.8", "255.255.255.0", "192.168.9.1");
00028     eth.connect();
00029     sock.init();
00030     seven.set_address("192.168.9.7", 37);
00031     char out_buffer[8]; // Does not matter
00032     
00033     flow = new InterruptIn(p5);
00034     flow->mode(PullUp);
00035     pump = new PwmOut(p21);  
00036     
00037     pump->period(1.0);
00038     pump_tick.period(1.0);
00039     
00040     lastflow = flowcount;
00041     
00042     flow->rise(&flowtick);
00043     
00044     float t = 0.0;
00045     while(1) {   
00046         if(t < pi) {
00047             t += 0.01;
00048             pump->write(1 - cos(t));
00049             pump_tick.write(1 - cos(t));
00050         }
00051         
00052         wait(0.1);
00053         if(flowcount > lastflow) {
00054             sprintf(out_buffer, "%07d", flowcount);
00055             sock.sendTo(seven, out_buffer, sizeof(out_buffer));
00056             lastflow = flowcount;
00057         }
00058     }
00059     sock.close();
00060     eth.disconnect();
00061 }