project_client_original
Dependencies: C12832 EthernetInterface mbed-rtos mbed
main.cpp@0:c7b709c6ec3c, 2017-09-07 (annotated)
- Committer:
- jw574
- Date:
- Thu Sep 07 14:03:23 2017 +0000
- Revision:
- 0:c7b709c6ec3c
project_client_original
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jw574 | 0:c7b709c6ec3c | 1 | //project_client_original |
jw574 | 0:c7b709c6ec3c | 2 | #include "mbed.h" |
jw574 | 0:c7b709c6ec3c | 3 | #include "EthernetInterface.h" |
jw574 | 0:c7b709c6ec3c | 4 | #include "C12832.h" |
jw574 | 0:c7b709c6ec3c | 5 | |
jw574 | 0:c7b709c6ec3c | 6 | C12832 lcd(D11, D13, D12, D7, D10); |
jw574 | 0:c7b709c6ec3c | 7 | DigitalOut red(LED_RED); |
jw574 | 0:c7b709c6ec3c | 8 | DigitalOut green(LED_GREEN); |
jw574 | 0:c7b709c6ec3c | 9 | DigitalOut blue(LED_BLUE); |
jw574 | 0:c7b709c6ec3c | 10 | |
jw574 | 0:c7b709c6ec3c | 11 | static const char* SERVER_IP = "192.168.1.1"; |
jw574 | 0:c7b709c6ec3c | 12 | static const char* CLIENT_IP = "192.168.1.2"; |
jw574 | 0:c7b709c6ec3c | 13 | static const char* MASK = "255.255.255.0"; |
jw574 | 0:c7b709c6ec3c | 14 | static const char* GATEWAY = "192.168.1.1"; |
jw574 | 0:c7b709c6ec3c | 15 | EthernetInterface eth; |
jw574 | 0:c7b709c6ec3c | 16 | //init 3 thread |
jw574 | 0:c7b709c6ec3c | 17 | Thread *transmitter; |
jw574 | 0:c7b709c6ec3c | 18 | Thread *receiver; |
jw574 | 0:c7b709c6ec3c | 19 | Thread *init; |
jw574 | 0:c7b709c6ec3c | 20 | void transmit(void const *args); |
jw574 | 0:c7b709c6ec3c | 21 | void receive(void const *args); |
jw574 | 0:c7b709c6ec3c | 22 | void init_eth(void const *args); |
jw574 | 0:c7b709c6ec3c | 23 | int n; |
jw574 | 0:c7b709c6ec3c | 24 | char counter[4]= {3,2,1,4}; |
jw574 | 0:c7b709c6ec3c | 25 | char counter1[4]= {0}; |
jw574 | 0:c7b709c6ec3c | 26 | //init eth point |
jw574 | 0:c7b709c6ec3c | 27 | void init_eth(void const *args) |
jw574 | 0:c7b709c6ec3c | 28 | { |
jw574 | 0:c7b709c6ec3c | 29 | eth.init(CLIENT_IP, MASK, GATEWAY); |
jw574 | 0:c7b709c6ec3c | 30 | int num=eth.connect(); |
jw574 | 0:c7b709c6ec3c | 31 | if(num!=-1) { |
jw574 | 0:c7b709c6ec3c | 32 | transmitter= new Thread(transmit); |
jw574 | 0:c7b709c6ec3c | 33 | receiver=new Thread(receive); |
jw574 | 0:c7b709c6ec3c | 34 | } |
jw574 | 0:c7b709c6ec3c | 35 | } |
jw574 | 0:c7b709c6ec3c | 36 | //transmit function send information |
jw574 | 0:c7b709c6ec3c | 37 | void transmit(void const *args) |
jw574 | 0:c7b709c6ec3c | 38 | { |
jw574 | 0:c7b709c6ec3c | 39 | Endpoint server; |
jw574 | 0:c7b709c6ec3c | 40 | UDPSocket sock; |
jw574 | 0:c7b709c6ec3c | 41 | sock.init(); |
jw574 | 0:c7b709c6ec3c | 42 | server.set_address(SERVER_IP, 6503); |
jw574 | 0:c7b709c6ec3c | 43 | while(1) { |
jw574 | 0:c7b709c6ec3c | 44 | sock.sendTo(server, counter, sizeof(counter)); |
jw574 | 0:c7b709c6ec3c | 45 | lcd.locate(0,3); |
jw574 | 0:c7b709c6ec3c | 46 | lcd.printf("send %d,%d,%d,%d",counter[0],counter[1],counter[2],counter[3]); |
jw574 | 0:c7b709c6ec3c | 47 | wait(3); |
jw574 | 0:c7b709c6ec3c | 48 | } |
jw574 | 0:c7b709c6ec3c | 49 | } |
jw574 | 0:c7b709c6ec3c | 50 | //receive function receive to array "counter1" |
jw574 | 0:c7b709c6ec3c | 51 | void receive(void const *args) |
jw574 | 0:c7b709c6ec3c | 52 | { |
jw574 | 0:c7b709c6ec3c | 53 | UDPSocket server; |
jw574 | 0:c7b709c6ec3c | 54 | Endpoint client; |
jw574 | 0:c7b709c6ec3c | 55 | server.bind(6500); |
jw574 | 0:c7b709c6ec3c | 56 | while(1) { |
jw574 | 0:c7b709c6ec3c | 57 | client.set_address(CLIENT_IP,6500); |
jw574 | 0:c7b709c6ec3c | 58 | n = server.receiveFrom(client, counter1, sizeof(counter1)); |
jw574 | 0:c7b709c6ec3c | 59 | lcd.locate(0,15); |
jw574 | 0:c7b709c6ec3c | 60 | lcd.printf("receive %d,%d,%d,%d",counter1[0],counter1[1],counter1[2],counter1[3]); |
jw574 | 0:c7b709c6ec3c | 61 | if(counter1[3]==0) { |
jw574 | 0:c7b709c6ec3c | 62 | if (counter1[0]==0&&counter1[1]==0&&counter1[2]==1) { |
jw574 | 0:c7b709c6ec3c | 63 | red =0; |
jw574 | 0:c7b709c6ec3c | 64 | green=1; |
jw574 | 0:c7b709c6ec3c | 65 | blue=1; |
jw574 | 0:c7b709c6ec3c | 66 | } else if (counter1[0]==0&&counter1[1]==1&&counter1[2]==0) { |
jw574 | 0:c7b709c6ec3c | 67 | green =0; |
jw574 | 0:c7b709c6ec3c | 68 | red =1; |
jw574 | 0:c7b709c6ec3c | 69 | blue =1; |
jw574 | 0:c7b709c6ec3c | 70 | } else if(counter1[0]==1&&counter1[1]==0&&counter1[2]==0) { |
jw574 | 0:c7b709c6ec3c | 71 | blue = 0; |
jw574 | 0:c7b709c6ec3c | 72 | red =1; |
jw574 | 0:c7b709c6ec3c | 73 | green =1; |
jw574 | 0:c7b709c6ec3c | 74 | } |
jw574 | 0:c7b709c6ec3c | 75 | } |
jw574 | 0:c7b709c6ec3c | 76 | } |
jw574 | 0:c7b709c6ec3c | 77 | } |
jw574 | 0:c7b709c6ec3c | 78 | int main() { |
jw574 | 0:c7b709c6ec3c | 79 | blue =1; |
jw574 | 0:c7b709c6ec3c | 80 | red =1; |
jw574 | 0:c7b709c6ec3c | 81 | green =1; |
jw574 | 0:c7b709c6ec3c | 82 | init=new Thread(init_eth); |
jw574 | 0:c7b709c6ec3c | 83 | while (true) { |
jw574 | 0:c7b709c6ec3c | 84 | } |
jw574 | 0:c7b709c6ec3c | 85 | } |