ハイパー・マイコン mbedでインターネット 電子工作 4章 リスト4-2 IP_Phoneのプログラム

Dependencies:   EthernetInterface TextLCD mbed-rtos mbed

Committer:
sunifu
Date:
Fri Jul 11 14:34:23 2014 +0000
Revision:
0:4e33b86642bd
2014.07.11

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sunifu 0:4e33b86642bd 1 #include "mbed.h"
sunifu 0:4e33b86642bd 2 #include "EthernetInterface.h"
sunifu 0:4e33b86642bd 3 #include "TextLCD.h"
sunifu 0:4e33b86642bd 4
sunifu 0:4e33b86642bd 5 TextLCD lcd(p24, p26, p27, p28, p29, p30);
sunifu 0:4e33b86642bd 6 DigitalIn sw(p21);
sunifu 0:4e33b86642bd 7 AnalogOut out(p18);
sunifu 0:4e33b86642bd 8 AnalogIn in(p20);
sunifu 0:4e33b86642bd 9
sunifu 0:4e33b86642bd 10 // DES_ADDRESS(Static IP)
sunifu 0:4e33b86642bd 11 const char* DES_ADDRESS = "192.168.0.21";
sunifu 0:4e33b86642bd 12 //const char* DES_ADDRESS = "192.168.0.26";
sunifu 0:4e33b86642bd 13
sunifu 0:4e33b86642bd 14 const int PORT = 50505;
sunifu 0:4e33b86642bd 15 const int DSIZE = 1024;
sunifu 0:4e33b86642bd 16
sunifu 0:4e33b86642bd 17 UDPSocket server;
sunifu 0:4e33b86642bd 18 Endpoint client;
sunifu 0:4e33b86642bd 19 unsigned short rtmp,wtmp;
sunifu 0:4e33b86642bd 20 unsigned char rbuf[DSIZE], wbuf[DSIZE];
sunifu 0:4e33b86642bd 21
sunifu 0:4e33b86642bd 22 void read(void) {
sunifu 0:4e33b86642bd 23 int i;
sunifu 0:4e33b86642bd 24 for ( i = 0 ; i < DSIZE ; i=i+2 ){
sunifu 0:4e33b86642bd 25 rtmp = in.read_u16();
sunifu 0:4e33b86642bd 26 rbuf[i] = (unsigned char)(rtmp & 0x00FF);
sunifu 0:4e33b86642bd 27 rbuf[i+1] = (unsigned char)((rtmp>>8)&0x00FF) ;
sunifu 0:4e33b86642bd 28 wait(0.0001);
sunifu 0:4e33b86642bd 29 }
sunifu 0:4e33b86642bd 30 server.sendTo(client, (char*)rbuf, sizeof(rbuf));
sunifu 0:4e33b86642bd 31 }
sunifu 0:4e33b86642bd 32
sunifu 0:4e33b86642bd 33 void write(void) {
sunifu 0:4e33b86642bd 34 int i;
sunifu 0:4e33b86642bd 35
sunifu 0:4e33b86642bd 36 server.receiveFrom(client, (char*)wbuf, sizeof(wbuf));
sunifu 0:4e33b86642bd 37 for ( i = 0 ; i < DSIZE ; i=i+2 ){
sunifu 0:4e33b86642bd 38 wtmp = (unsigned char)(wbuf[i+1]);
sunifu 0:4e33b86642bd 39 wtmp <<=8;
sunifu 0:4e33b86642bd 40 wtmp |= (unsigned char)wbuf[i] ;
sunifu 0:4e33b86642bd 41 out.write_u16(wtmp) ;
sunifu 0:4e33b86642bd 42 wait(0.0001);
sunifu 0:4e33b86642bd 43 }
sunifu 0:4e33b86642bd 44 }
sunifu 0:4e33b86642bd 45
sunifu 0:4e33b86642bd 46
sunifu 0:4e33b86642bd 47 int main (void) {
sunifu 0:4e33b86642bd 48 EthernetInterface eth;
sunifu 0:4e33b86642bd 49
sunifu 0:4e33b86642bd 50 eth.init("192.168.0.26","255.255.255.0","192.168.0.1");
sunifu 0:4e33b86642bd 51 //eth.init("192.168.0.21","255.255.255.0","192.168.0.1");
sunifu 0:4e33b86642bd 52
sunifu 0:4e33b86642bd 53 eth.connect();
sunifu 0:4e33b86642bd 54 lcd.printf("%s", eth.getIPAddress());
sunifu 0:4e33b86642bd 55
sunifu 0:4e33b86642bd 56 server.bind( PORT );
sunifu 0:4e33b86642bd 57 server.set_blocking(false) ;
sunifu 0:4e33b86642bd 58 client.set_address(DES_ADDRESS, PORT);
sunifu 0:4e33b86642bd 59
sunifu 0:4e33b86642bd 60 while(1) {
sunifu 0:4e33b86642bd 61 if ( sw == 0 ){
sunifu 0:4e33b86642bd 62 lcd.locate(0,1);
sunifu 0:4e33b86642bd 63 lcd.printf("Listen...");
sunifu 0:4e33b86642bd 64
sunifu 0:4e33b86642bd 65 write();
sunifu 0:4e33b86642bd 66 }else{
sunifu 0:4e33b86642bd 67 read();
sunifu 0:4e33b86642bd 68 lcd.locate(0,1);
sunifu 0:4e33b86642bd 69 lcd.printf("Talk... ");
sunifu 0:4e33b86642bd 70 }
sunifu 0:4e33b86642bd 71 }
sunifu 0:4e33b86642bd 72 }