From e15a73edccf8b3ff5abe63618d0a8714094fb00d Mon Sep 17 00:00:00 2001 From: Johan Kanflo Date: Wed, 20 Sep 2017 22:37:03 +0200 Subject: [PATCH] Fixed code rot, compatible with latest EOR --- Makefile | 2 +- arducam/arducam_arch_esp8266.c | 14 ++-- camdriver.c | 19 +++--- camdriver.h | 7 +- capture.c | 113 ++++++++++++++------------------- 5 files changed, 72 insertions(+), 83 deletions(-) diff --git a/Makefile b/Makefile index 920eb32..122cf0a 100644 --- a/Makefile +++ b/Makefile @@ -2,5 +2,5 @@ PROGRAM = capture PROGRAM_SRC_DIR = . arducam PROGRAM_INC_DIR = . arducam OTA=1 -EXTRA_COMPONENTS=extras/stdin_uart_interrupt extras/i2c extras/spi extras/spiflash extras/http-upload extras/rboot-ota +EXTRA_COMPONENTS=extras/stdin_uart_interrupt extras/i2c extras/spi extras/http-upload extras/rboot-ota include $(EOR_ROOT)/common.mk diff --git a/arducam/arducam_arch_esp8266.c b/arducam/arducam_arch_esp8266.c index ba67faa..c21663f 100644 --- a/arducam/arducam_arch_esp8266.c +++ b/arducam/arducam_arch_esp8266.c @@ -39,8 +39,9 @@ static uint8_t _sensor_addr; //#define CONFIG_VERIFY +#define I2C_BUS (0) #define I2C_SDA_PIN (2) -#define I2C_SCK_PIN (0) +#define I2C_SCL_PIN (0) static void spi_chip_select(uint8_t cs_pin); @@ -59,7 +60,7 @@ bool arducam_spi_init(void) bool arducam_i2c_init(uint8_t sensor_addr) { printf("arducam_i2c_init\n"); - i2c_init(I2C_SCK_PIN, I2C_SDA_PIN); + i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_80K); _sensor_addr = sensor_addr; return true; } @@ -103,13 +104,16 @@ uint8_t arducam_spi_read(uint8_t address) uint8_t arducam_i2c_write(uint8_t regID, uint8_t regDat) { - uint8_t data[] = {regID, regDat}; - return i2c_slave_write(_sensor_addr, data, sizeof(data)); + const uint8_t data[] = {regID, regDat}; + return i2c_slave_write(I2C_BUS, _sensor_addr, 0, data, sizeof(data)) == 0; } uint8_t arducam_i2c_read(uint8_t regID, uint8_t* regDat) { - return i2c_slave_read(_sensor_addr, regID, regDat, 1); + const uint8_t data[] = {regID}; + return i2c_slave_read(I2C_BUS, _sensor_addr, data, regDat, 1) == 0; + +int i2c_slave_read(uint8_t bus, uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len); } uint8_t arducam_i2c_write16(uint8_t regID, uint16_t regDat) diff --git a/camdriver.c b/camdriver.c index 0b435f1..ff7955f 100644 --- a/camdriver.c +++ b/camdriver.c @@ -25,7 +25,6 @@ #include #include #include -#include // write(...) will dump to uart if this include is missing :-/ #include #include "timeutils.h" #include "camdriver.h" @@ -97,7 +96,7 @@ bool arducam_capture(void) return true; } -void arudcam_fifo_to_socket(int client_sock) +void arudcam_fifo_to_netcon(struct netconn *client) { uint8_t temp, temp_last = 0, buf_idx = 0; uint32_t fifo_size, bytes_read, start_time = systime_ms(); @@ -114,11 +113,10 @@ void arudcam_fifo_to_socket(int client_sock) temp_last = temp; temp = arducam_read_fifo(); buffer[buf_idx++] = temp; - if (client_sock && buf_idx == BUF_SIZE) { - int res = write(client_sock, buffer, buf_idx); - if (res < 0) { - printf("\nERROR: write returned %d\n", res); - break; + if (client && buf_idx == BUF_SIZE) { + if (ERR_OK != netconn_write(client, buffer, buf_idx, NETCONN_COPY)) { + printf("netconn_write failed\n"); + return; } buf_idx = 0; } @@ -128,9 +126,10 @@ void arudcam_fifo_to_socket(int client_sock) break; } } - if (client_sock && buf_idx > 0) { - int res = write(client_sock, buffer, buf_idx); - (void) res; + if (client && buf_idx > 0) { + if (ERR_OK != netconn_write(client, buffer, buf_idx, NETCONN_COPY)) { + printf("netconn_write failed\n"); + } } printf("Done, read %u bytes in %ums\n", bytes_read, systime_ms()-start_time); } diff --git a/camdriver.h b/camdriver.h index 3966c96..7c07b5b 100644 --- a/camdriver.h +++ b/camdriver.h @@ -25,6 +25,9 @@ #ifndef _CAMDRIVER_H_ #define _CAMDRIVER_H_ +#include +#include + #define OV2640_CHIPID_HIGH 0x0A #define OV2640_CHIPID_LOW 0x0B @@ -34,8 +37,8 @@ bool arducam_setup(void); // Capture, return true if success bool arducam_capture(void); -// Read FIFO and write to client socket -void arudcam_fifo_to_socket(int client_sock); +// Read FIFO and write to lwip netconn +void arudcam_fifo_to_netcon(struct netconn *client); // Read FIFO and write to, well, nowhere void arudcam_fifo_to_devnull(void); diff --git a/capture.c b/capture.c index f70efd6..1cc3d86 100644 --- a/capture.c +++ b/capture.c @@ -105,53 +105,36 @@ void server_task(void *p) camera_ok = arducam_setup(); if (!camera_ok) { printf("Camera init failed!\n"); + return; } + struct netconn *client = NULL; + struct netconn *nc = netconn_new(NETCONN_TCP); + if (nc == NULL) { + printf("Failed to allocate socket\n"); + vTaskDelete(NULL); + } + netconn_bind(nc, IP_ADDR_ANY, 80); + netconn_listen(nc); while (1) { - struct sockaddr_in server_addr, client_addr; - int server_sock, client_sock; - socklen_t sin_size; - bzero(&server_addr, sizeof(struct sockaddr_in)); - server_addr.sin_family = AF_INET; - server_addr.sin_addr.s_addr = INADDR_ANY; - server_addr.sin_port = htons(80); - - int recbytes; - - do { - if (-1 == (server_sock = socket(AF_INET, SOCK_STREAM, 0))) { - printf("Socket error\n"); - break; - } - - if (-1 == bind(server_sock, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr))) { - printf("bind fail\n"); - break; - } - - printf(" bind port: %d\n", ntohs(server_addr.sin_port)); - - if (-1 == listen(server_sock, 5)) { - printf("listen fail\n"); - break; - } - - sin_size = sizeof(client_addr); - - for (;;) { - if ((client_sock = accept(server_sock, (struct sockaddr *) &client_addr, &sin_size)) < 0) { - printf("accept fail\n"); - continue; - } - - printf("Client from %s:%d\n", inet_ntoa(client_addr.sin_addr), htons(client_addr.sin_port)); - - int opt = 50; - if (lwip_setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(int)) < 0) { - printf("failed to set timeout on socket\n"); - } - while ((recbytes = read(client_sock, buffer, BUF_SIZE)) > 0) { - buffer[recbytes] = 0; + err_t err = netconn_accept(nc, &client); + if (err == ERR_OK) { + struct netbuf *nb; + + ip_addr_t client_addr; + uint16_t port_ignore; + netconn_peer(client, &client_addr, &port_ignore); + printf("---\n"); + printf("Client from %d.%d.%d.%d\n", ip4_addr1(&client_addr), ip4_addr2(&client_addr), ip4_addr3(&client_addr), ip4_addr4(&client_addr)); + + if ((err = netconn_recv(client, &nb)) == ERR_OK) { + char *data; + uint16_t len; + err = netbuf_data(nb, (void*) &data, &len); + if (err == ERR_OK) { + printf("Received %d bytes\n", len); + } else { + printf("netbuf_data returned error %d\n", err); } sprintf(buffer, "HTTP/1.1 200 OK\nContent-Type: image/jpeg; charset=utf-8\nConnection: close\n\n"); @@ -159,35 +142,35 @@ void server_task(void *p) #ifdef CONFIG_TARGET_ESPARDUCAM_MINI gpio_write(LED_nPWR, 0); #endif // CONFIG_TARGET_ESPARDUCAM_MINI - if (write(client_sock, buffer, strlen(buffer)) > 0) { - if (camera_ok) { - uint32_t retries = 4; - bool success; - while(retries--) { - success = arducam_capture(); - if (success) { - break; - } else { - printf("Capture retry\n"); - delay_ms(10); // Arbitrary value - } - } + + netconn_write(client, buffer, strlen(buffer), NETCONN_COPY); + + if (camera_ok) { + uint32_t retries = 4; + bool success; + while(retries--) { + success = arducam_capture(); if (success) { - printf("Reading fifo\n"); - arudcam_fifo_to_socket(client_sock); - printf("---\n"); + break; + } else { + printf("Capture retry\n"); + delay_ms(10); // Arbitrary value } } + if (success) { + printf("Reading fifo\n"); + arudcam_fifo_to_netcon(client); + } } #ifdef CONFIG_TARGET_ESPARDUCAM_MINI gpio_write(LED_nPWR, 1); #endif // CONFIG_TARGET_ESPARDUCAM_MINI - - if (recbytes <= 0) { - close(client_sock); - } } - } while (0); + netbuf_delete(nb); + } + printf("Closing connection\n"); + netconn_close(client); + netconn_delete(client); } }