Threads + Network Demo
Dependencies: EthernetInterface mbed-rtos mbed-src
Fork of TestNetComm by
main.cpp@4:f2789e665f4e, 2014-05-04 (annotated)
- Committer:
- MalcolmNixon
- Date:
- Sun May 04 04:31:58 2014 +0000
- Revision:
- 4:f2789e665f4e
- Parent:
- 3:1e86c29d1eac
- Child:
- 5:18415c556f04
Got threads working, but it's fragile as hell
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MalcolmNixon | 0:9eba2bd74769 | 1 | #include "EthernetInterface.h" |
MalcolmNixon | 0:9eba2bd74769 | 2 | #include "Thread.h" |
MalcolmNixon | 0:9eba2bd74769 | 3 | #include "mbed.h" |
MalcolmNixon | 0:9eba2bd74769 | 4 | |
MalcolmNixon | 4:f2789e665f4e | 5 | // Configure the LEDs (off) |
MalcolmNixon | 4:f2789e665f4e | 6 | DigitalOut led1(LED1, 1); |
MalcolmNixon | 3:1e86c29d1eac | 7 | DigitalOut led2(LED2, 1); |
MalcolmNixon | 2:d62b28f02d45 | 8 | |
MalcolmNixon | 4:f2789e665f4e | 9 | |
MalcolmNixon | 4:f2789e665f4e | 10 | void ledThreadProc(const void *param) |
MalcolmNixon | 3:1e86c29d1eac | 11 | { |
MalcolmNixon | 4:f2789e665f4e | 12 | // Toggle the Green LED |
MalcolmNixon | 4:f2789e665f4e | 13 | for (;;) |
MalcolmNixon | 4:f2789e665f4e | 14 | { |
MalcolmNixon | 4:f2789e665f4e | 15 | led2 = !led2; |
MalcolmNixon | 4:f2789e665f4e | 16 | Thread::wait(1000); |
MalcolmNixon | 4:f2789e665f4e | 17 | } |
MalcolmNixon | 2:d62b28f02d45 | 18 | } |
MalcolmNixon | 0:9eba2bd74769 | 19 | |
MalcolmNixon | 1:429f7bca62aa | 20 | int main(void) |
MalcolmNixon | 0:9eba2bd74769 | 21 | { |
MalcolmNixon | 4:f2789e665f4e | 22 | Thread ledThread(ledThreadProc); |
MalcolmNixon | 3:1e86c29d1eac | 23 | |
MalcolmNixon | 4:f2789e665f4e | 24 | // Start the network |
MalcolmNixon | 1:429f7bca62aa | 25 | EthernetInterface eth; |
MalcolmNixon | 1:429f7bca62aa | 26 | eth.init("192.168.5.100", "255.255.255.0", "0.0.0.0"); |
MalcolmNixon | 1:429f7bca62aa | 27 | eth.connect(); |
MalcolmNixon | 4:f2789e665f4e | 28 | |
MalcolmNixon | 1:429f7bca62aa | 29 | // Construct the server |
MalcolmNixon | 4:f2789e665f4e | 30 | TCPSocketConnection conn; |
MalcolmNixon | 1:429f7bca62aa | 31 | TCPSocketServer svr; |
MalcolmNixon | 1:429f7bca62aa | 32 | svr.bind(1234); |
MalcolmNixon | 1:429f7bca62aa | 33 | svr.listen(); |
MalcolmNixon | 4:f2789e665f4e | 34 | |
MalcolmNixon | 0:9eba2bd74769 | 35 | for (;;) |
MalcolmNixon | 0:9eba2bd74769 | 36 | { |
MalcolmNixon | 1:429f7bca62aa | 37 | // Accept the next connection |
MalcolmNixon | 4:f2789e665f4e | 38 | svr.accept(conn); |
MalcolmNixon | 4:f2789e665f4e | 39 | |
MalcolmNixon | 4:f2789e665f4e | 40 | // Manage the connection |
MalcolmNixon | 4:f2789e665f4e | 41 | for (;;) |
MalcolmNixon | 1:429f7bca62aa | 42 | { |
MalcolmNixon | 4:f2789e665f4e | 43 | // Read the input |
MalcolmNixon | 4:f2789e665f4e | 44 | char buf[32]; |
MalcolmNixon | 4:f2789e665f4e | 45 | int nr = conn.receive(buf, 32); |
MalcolmNixon | 4:f2789e665f4e | 46 | if (nr <= 0) |
MalcolmNixon | 1:429f7bca62aa | 47 | { |
MalcolmNixon | 4:f2789e665f4e | 48 | break; |
MalcolmNixon | 1:429f7bca62aa | 49 | } |
MalcolmNixon | 4:f2789e665f4e | 50 | |
MalcolmNixon | 4:f2789e665f4e | 51 | // Toggle the output on led1 |
MalcolmNixon | 4:f2789e665f4e | 52 | led1 = !led1; |
MalcolmNixon | 4:f2789e665f4e | 53 | |
MalcolmNixon | 4:f2789e665f4e | 54 | // Write the response twice |
MalcolmNixon | 4:f2789e665f4e | 55 | conn.send(buf, nr); |
MalcolmNixon | 4:f2789e665f4e | 56 | conn.send(buf, nr); |
MalcolmNixon | 1:429f7bca62aa | 57 | } |
MalcolmNixon | 4:f2789e665f4e | 58 | } |
MalcolmNixon | 4:f2789e665f4e | 59 | } |