Seongbin Lim
/
sb_first_project
This is a very simple guide, reviewing the steps required to get Blinky working on an Mbed OS platform.
Diff: main.cpp
- Revision:
- 137:81b5a1672c6a
- Parent:
- 136:53a83b91854c
- Child:
- 138:e829be898713
--- a/main.cpp Fri May 22 08:05:14 2020 +0000 +++ b/main.cpp Tue May 26 00:30:01 2020 +0000 @@ -1,65 +1,106 @@ #include "mbed.h" -#include "Adafruit_SSD1306.h" +#define SERVER_IP "192.168.0.10" +#define SERVER_PORT 50000 +Serial pc(USBTX, USBRX, 115200); +WiFiInterface *wifi; +TCPSocket socket; +Thread sock_thread; +char rx_buf_pc[100]; +int index = 0; +volatile int flag ; -#define ADT7420_TEMP_REG (0x00) -#define ADT7420_CONF_REG (0x03) -#define EVAL_ADT7420_ADDR (0x48) +void rx_cb(void) // ISR for receiving data from the PC keyboard { +{ + char ch; + ch = pc.getc(); + pc.putc(ch); + rx_buf_pc[index++] = ch; + if (ch == 0x0D) { //LF + pc.putc(0x0A); + rx_buf_pc[--index] = '\0'; + index = 0; + flag = 1; + } +} -I2C i2c(I2C_SDA, I2C_SCL); -RawSerial pc(PA_2, PA_3, 115200); - - -Adafruit_SSD1306_SPI goled(D11, D13, D10, D4, D7, 64, 128); +// rx_thread: a thread to receive data from the TCP server +void rx_thread() +{ + char* buf = (char*)malloc(1024); + while (true) { + nsapi_size_or_error_t size = socket.recv(buf, 1024); + if (size <= 0) { + if (size == NSAPI_ERROR_WOULD_BLOCK) continue; +// (-3001) no data for the case of Non-blocking mode + pc.printf("Error while receiving data from TCP socket (%d)\r\n", size); + return; + } + buf[size] = '\0'; // turn into valid C string + pc.printf("\r\nRX data: (%d) %s \r\n", size, buf); + } +} int main() { - - int status; - float temp; - char data_write[2]; - char data_read[2]; - - i2c.frequency(100000); - data_write[0] = ADT7420_CONF_REG; - data_write[1] = 0x40; - status = i2c.write((EVAL_ADT7420_ADDR<<1), data_write, 2); - if(status!=0) { - pc.printf("I2C configuration error!\r\n"); - while(1){ + SocketAddress sockAddr; + SocketAddress serverAddr(SERVER_IP, SERVER_PORT); + pc.printf("\r\n WiFi TCP Client example\r\n"); + pc.attach(&rx_cb); + wifi = WiFiInterface::get_default_instance(); + if (!wifi) + { + pc.printf("ERROR: No WiFiInterface found.\n"); + while(1); + } + pc.printf("Connecting to %s...\r\n", MBED_CONF_APP_WIFI_SSID); + int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, + MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); + if (ret != 0) + { + pc.printf("Connection error!!\r\n"); + while(1); + } + + pc.printf("Success!!\r\n"); + pc.printf("RSSI: %d\r\n", wifi->get_rssi()); + wifi->get_ip_address(); + pc.printf("IP: %s, ", sockAddr.get_ip_address()); +//wifi->get_netmask(&sockAddr); +//pc.printf("Netmask: %s, ", sockAddr.get_ip_address()); pc.printf("Netmask: %s, ", wifi->get_netmask()); + wifi->get_gateway(); + pc.printf("Gateway: %s\r\n", sockAddr.get_ip_address()); + +// Open a TCP socket on the network interface, and create a TCP connection to a Server + socket.open(wifi); + int response = socket.connect(serverAddr); + if(0 != response) + { + pc.printf("Error connecting: %d\r\n", response); + socket.close(); + wifi->disconnect(); + while(1); + } + sock_thread.start(&rx_thread); + while (true) + { + flag = 0; + pc.printf("Enter characters to send to a server: "); + while (flag != 1) { + } + if (!strcmp(rx_buf_pc, "q") || !strcmp(rx_buf_pc, "Q")) { + break; + } else { + int len = strlen(rx_buf_pc); + pc.printf("Sent: %s (%d)\r\n", rx_buf_pc, len); + rx_buf_pc[len++] = 0x0D; + rx_buf_pc[len++] = 0x0A; + socket.send((const char *)rx_buf_pc, len); + Thread::wait(500); // 0.5sec } } - goled.clearDisplay(); - goled.splash(); - ThisThread::sleep_for(3000); - goled.clearDisplay(); - - goled.setTextSize(2); - goled.printf("OLED Lab.\r\n"); - goled.setTextSize(1); - goled.printf("HGU in Pohang \r\n\r\n"); - - int16_t cursor_x = goled.cursor_x; - int16_t cursor_y = goled.cursor_y; - - while(1) { - - goled.setTextCursor(cursor_x,cursor_y); - - data_write[0] = ADT7420_TEMP_REG; - i2c.write((EVAL_ADT7420_ADDR<<1),data_write,1,0); - i2c.read((EVAL_ADT7420_ADDR<<1 | 0x01), data_read, 2, 0); - - int tempval = ((int) data_read[0]<<8 | data_read[1]); - tempval >>= 3; - if((tempval & 0x1000) > 0) { - temp = (tempval - 8192)/16.0; - } else { - temp = tempval/16.0; - } - - pc.printf("Temperature: %0.4f\r\n", temp); - goled.printf("Temperature: %0.4f\r\n", temp); - goled.display(); - - ThisThread::sleep_for(1000); - } + socket.close(); + wifi->disconnect(); + sock_thread.join(); + pc.printf("RX sock_thread joined!!\r\n"); + pc.printf("\nDone\r\n"); + Thread::wait(osWaitForever); } \ No newline at end of file